Untitled
unknown
plain_text
2 years ago
1.7 kB
11
Indexable
import pygame
import random
# Initialize Pygame
pygame.init()
# Create game window
window = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Zombie Chase Game")
# Colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# Player
player_x = 400
player_y = 300
player_speed = 5
# Zombie
zombie_x = random.randint(0, 800)
zombie_y = random.randint(0, 600)
zombie_speed = 3
# Gun
gun_power = 1
coins = 0
# Game Loop
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player_x -= player_speed
if keys[pygame.K_RIGHT]:
player_x += player_speed
if keys[pygame.K_UP]:
player_y -= player_speed
if keys[pygame.K_DOWN]:
player_y += player_speed
# Update zombie position
if zombie_x < player_x:
zombie_x += zombie_speed
else:
zombie_x -= zombie_speed
if zombie_y < player_y:
zombie_y += zombie_speed
else:
zombie_y -= zombie_speed
# Check collision
if abs(player_x - zombie_x) < 20 and abs(player_y - zombie_y) < 20:
coins += 1
zombie_x = random.randint(0, 800)
zombie_y = random.randint(0, 600)
window.fill(WHITE)
pygame.draw.circle(window, GREEN, (player_x, player_y), 10) # Draw player
pygame.draw.circle(window, RED, (zombie_x, zombie_y), 10) # Draw zombie
font = pygame.font.Font(None, 36)
text = font.render(f'Coins: {coins}', True, (0, 0, 0))
window.blit(text, (10, 10))
pygame.display.update()
pygame.quit()Editor is loading...
Leave a Comment