Jump to content

Coding

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 2402:8100:31d9:a858:0:44:cd63:1701 (talk) at 06:49, 11 March 2025. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

import pygame import random import time

  1. Initialize Pygame

pygame.init()

  1. Set up display

WIDTH, HEIGHT = 800, 600 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Survival Game")

  1. Set up colors

WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0)

  1. Set up player

player_width, player_height = 50, 50 player_x = WIDTH // 2 player_y = HEIGHT - player_height - 10 player_speed = 5

  1. Set up enemies

enemy_width, enemy_height = 50, 50 enemy_speed = 3 enemy_frequency = 30 # Lower means more enemies

  1. Set up font for score

font = pygame.font.SysFont("Arial", 30)

  1. Set up game variables

score = 0 game_over = False

  1. Set up clock

clock = pygame.time.Clock()

def draw_player(x, y):

   pygame.draw.rect(screen, WHITE, (x, y, player_width, player_height))

def draw_enemy(x, y):

   pygame.draw.rect(screen, RED, (x, y, enemy_width, enemy_height))

def show_score(score):

   score_text = font.render(f"Score: {score}", True, WHITE)
   screen.blit(score_text, (10, 10))

def game_loop():

   global player_x, player_y, score, game_over
   
   # Initialize enemy positions
   enemies = []
   
   # Game loop
   while not game_over:
       screen.fill(BLACK)
       
       # Check for events (such as closing the game)
       for event in pygame.event.get():
           if event.type == pygame.QUIT:
               game_over = True
       
       # Get pressed keys
       keys = pygame.key.get_pressed()
       
       # Control player movement
       if keys[pygame.K_LEFT] and player_x > 0:
           player_x -= player_speed
       if keys[pygame.K_RIGHT] and player_x < WIDTH - player_width:
           player_x += player_speed
       if keys[pygame.K_UP] and player_y > 0:
           player_y -= player_speed
       if keys[pygame.K_DOWN] and player_y < HEIGHT - player_height:
           player_y += player_speed
       
       # Spawn enemies
       if random.randint(1, enemy_frequency) == 1:
           enemy_x = random.randint(0, WIDTH - enemy_width)
           enemy_y = -enemy_height
           enemies.append([enemy_x, enemy_y])
       
       # Move enemies down
       for enemy in enemies:
           enemy[1] += enemy_speed
           if enemy[1] > HEIGHT:
               enemies.remove(enemy)
               score += 1
           
           # Check for collision
           if player_x < enemy[0] + enemy_width and player_x + player_width > enemy[0]:
               if player_y < enemy[1] + enemy_height and player_y + player_height > enemy[1]:
                   game_over = True
       
       # Draw player and enemies
       draw_player(player_x, player_y)
       for enemy in enemies:
           draw_enemy(enemy[0], enemy[1])
       
       # Show score
       show_score(score)
       
       # Update the screen
       pygame.display.flip()
       
       # Set the frame rate
       clock.tick(60)
   # Game over message
   game_over_text = font.render(f"Game Over! Final Score: {score}", True, RED)
   screen.blit(game_over_text, (WIDTH // 2 - 150, HEIGHT // 2))
   pygame.display.flip()
   time.sleep(3)
  1. Run the game loop

game_loop()

  1. Quit pygame

pygame.quit()

Computer science

Other uses

See also