Coding
Appearance
import pygame import random import time
- Initialize Pygame
pygame.init()
- Set up display
WIDTH, HEIGHT = 800, 600 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Survival Game")
- Set up colors
WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0)
- Set up player
player_width, player_height = 50, 50 player_x = WIDTH // 2 player_y = HEIGHT - player_height - 10 player_speed = 5
- Set up enemies
enemy_width, enemy_height = 50, 50 enemy_speed = 3 enemy_frequency = 30 # Lower means more enemies
- Set up font for score
font = pygame.font.SysFont("Arial", 30)
- Set up game variables
score = 0 game_over = False
- 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)
- Run the game loop
game_loop()
- Quit pygame
pygame.quit()
Computer science
- Computer programming, the process of creating and maintaining the source code of computer programs
- Line coding, in data storage
- Source coding, compression used in data transmission
- Coding theory
- Channel coding
Other uses
- Coding (social sciences), an analytical process in which data are categorized for analysis
- Coding strand of DNA in molecular biology
- Legal coding, the process of creating summary or keyword data from a document in the legal profession
- Medical coding, representation of medical diagnoses and procedures in standard code numbers
- Number coding in Metro Manila, a road space rationing policy implemented in Metro Manila, Philippines, commonly referred to as "coding"
- Coding (therapy), alternative therapeutic methods used to treat addictions in the post-Soviet countries
- Queer coding