[Python] 纯文本查看 复制代码
import pygame
import random
pygame.init()
screen_width, screen_height = 800, 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('打丧尸小游戏')
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
YELLOW = (255, 255, 0)
BLUE = (0, 0, 255)
pygame.font.init()
font = pygame.font.Font(None, 36)
player_x = screen_width // 2
player_y = screen_height - 50
player_radius = 20
player_speed = 5
player_health = 200
zombie_radius = 15
zombie_speed = 3
zombies = []
zombie_health = 30
bullets = []
bullet_speed = 10
bullet_damage = 10
base_x = screen_width // 2 - 50
base_y = screen_height - 100
base_width = 100
base_height = 50
base_health = 200
def draw_player():
pygame.draw.circle(screen, RED, (player_x, player_y), player_radius)
pygame.draw.rect(screen, YELLOW, (10, 10, player_health, 10))
health_text = font.render(f'Player: {player_health}', True, WHITE)
screen.blit(health_text, (10, 30))
def draw_zombies():
for zombie in zombies:
pygame.draw.circle(screen, GREEN, (zombie[0], zombie[1]), zombie_radius)
zombie_rect = pygame.Rect(zombie[0] - zombie_radius, zombie[1] - zombie_radius - 15, zombie[2], 5)
pygame.draw.rect(screen, YELLOW, zombie_rect)
zombie_health_text = font.render(f'{zombie[2]}', True, WHITE)
screen.blit(zombie_health_text, (zombie[0] - zombie_radius, zombie[1] - zombie_radius - 20))
def draw_bullets():
for bullet in bullets:
if 0 <= bullet.x <= screen_width and 0 <= bullet.y <= screen_height:
pygame.draw.rect(screen, WHITE, bullet)
def draw_base():
pygame.draw.rect(screen, BLUE, (base_x, base_y, base_width, base_height))
pygame.draw.rect(screen, YELLOW, (base_x, base_y - 15, base_health, 10))
base_health_text = font.render(f'Base: {base_health}', True, WHITE)
screen.blit(base_health_text, (base_x, base_y - 30))
def move_player():
global player_x
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player_x > player_radius:
player_x -= player_speed
elif keys[pygame.K_RIGHT] and player_x < screen_width - player_radius:
player_x += player_speed
def create_zombie():
if random.randint(1, 100) < 3:
zombie_x = random.randint(zombie_radius, screen_width - zombie_radius)
zombie_y = 0
zombies.append([zombie_x, zombie_y, zombie_health])
def move_zombies():
for zombie in zombies:
zombie[1] += zombie_speed
if zombie[1] > screen_height:
zombies.remove(zombie)
elif base_x <= zombie[0] <= base_x + base_width and base_y <= zombie[1] <= base_y + base_height:
base_health -= 10
if base_health <= 0:
game_over('Base Destroyed!')
zombies.remove(zombie)
def fire_bullet():
bullet = pygame.Rect(player_x - 2, player_y - 10, 4, 10)
bullets.append(bullet)
def move_bullets():
for bullet in bullets:
bullet.y -= bullet_speed
if bullet.y < 0:
bullets.remove(bullet)
def check_collisions():
global player_health
for bullet in bullets[:]:
for zombie in zombies[:]:
dx = bullet.x - zombie[0]
dy = bullet.y - zombie[1]
distance = (dx ** 2 + dy ** 2) ** 0.5
if distance <= zombie_radius:
bullets.remove(bullet)
zombie[2] -= bullet_damage
if zombie[2] <= 0:
zombies.remove(zombie)
for zombie in zombies[:]:
dx = player_x - zombie[0]
dy = player_y - zombie[1]
distance = (dx ** 2 + dy ** 2) ** 0.5
if distance <= player_radius + zombie_radius:
player_health -= 10
if player_health <= 0:
game_over('Player Defeated!')
def game_over(reason):
global running
running = False
game_over_text = font.render(reason, True, RED)
screen.blit(game_over_text, (screen_width // 2 - 100, screen_height // 2))
pygame.display.flip()
pygame.time.wait(3000)
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
fire_bullet()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player_x > player_radius:
player_x -= player_speed
elif keys[pygame.K_RIGHT] and player_x < screen_width - player_radius:
player_x += player_speed
if keys[pygame.K_SPACE]:
fire_bullet()
create_zombie()
move_zombies()
move_bullets()
check_collisions()
screen.fill(BLACK)
draw_player()
draw_zombies()
draw_bullets()
draw_base()
pygame.display.flip()
clock.tick(60)
pygame.quit()