[Python] 纯文本查看 复制代码
import pygame
import random
import math
# 初始化pygame
pygame.init()
# 设置游戏窗口
WIDTH, HEIGHT = 800, 600
window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("人类打僵尸")
# 定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# 加载角色和僵尸图片
player_image = pygame.Surface((50, 50)) # 替换为你的角色图像
player_image.fill(GREEN)
zombie_image = pygame.Surface((50, 50)) # 替换为你的僵尸图像
zombie_image.fill(RED)
# 玩家类
class Player:
def __init__(self):
self.rect = player_image.get_rect(center=(WIDTH // 2, HEIGHT - 60))
self.shoot_delay = 500
self.last_shot = pygame.time.get_ticks()
self.health = 5 # 初始血量
def move(self, dx):
self.rect.x += dx
# 限制移动范围
if self.rect.x < 0:
self.rect.x = 0
elif self.rect.x > WIDTH - self.rect.width:
self.rect.x = WIDTH - self.rect.width
def shoot(self):
current_time = pygame.time.get_ticks()
if current_time - self.last_shot > self.shoot_delay:
self.last_shot = current_time
return Bullet(self.rect.centerx, self.rect.top)
return None
# 弹药类
class Bullet:
def __init__(self, x, y):
self.rect = pygame.Rect(x, y, 5, 10)
def move(self):
self.rect.y -= 10
# 僵尸类
class Zombie:
def __init__(self):
self.rect = zombie_image.get_rect(center=(random.randint(0, WIDTH), 0))
def move(self):
self.rect.y += 3
# 游戏主循环
def main():
player = Player()
zombies = []
bullets = []
clock = pygame.time.Clock()
score = 0
# 游戏循环
running = True
while running:
window.fill(WHITE)
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 控制玩家
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player.move(-5)
if keys[pygame.K_RIGHT]:
player.move(5)
if keys[pygame.K_SPACE]:
bullet = player.shoot()
if bullet:
bullets.append(bullet)
# 创建僵尸
if random.randint(1, 50) == 1:
zombies.append(Zombie())
# 更新并绘制子弹
for bullet in bullets[:]:
bullet.move()
if bullet.rect.y < 0:
bullets.remove(bullet)
# 更新并绘制僵尸
for zombie in zombies[:]:
zombie.move()
if zombie.rect.y > HEIGHT:
zombies.remove(zombie)
for bullet in bullets[:]:
if bullet.rect.colliderect(zombie.rect):
bullets.remove(bullet)
zombies.remove(zombie)
score += 1
break
# 检测玩家碰到僵尸
if player.rect.colliderect(zombie.rect):
zombies.remove(zombie)
player.health -= 1
if player.health <= 0:
print("游戏结束!得分:", score)
running = False # 游戏结束
# 获取鼠标位置
mouse_x, mouse_y = pygame.mouse.get_pos()
# 计算角度和旋转
angle = math.degrees(math.atan2(mouse_y - player.rect.centery, mouse_x - player.rect.centerx)) - 90
rotated_player_image = pygame.transform.rotate(player_image, angle)
# 绘制玩家(旋转后的角色)
new_rect = rotated_player_image.get_rect(center=player.rect.center) # 调整图片中心
window.blit(rotated_player_image, new_rect.topleft)
# 绘制僵尸
for zombie in zombies:
window.blit(zombie_image, zombie.rect)
# 绘制子弹
for bullet in bullets:
pygame.draw.rect(window, BLACK, bullet.rect)
# 显示分数和血量
font = pygame.font.Font(None, 36)
score_text = font.render(f"Score: {score}", True, BLACK)
health_text = font.render(f"Health: {player.health}", True, BLACK)
window.blit(score_text, (10, 10))
window.blit(health_text, (10, 50))
pygame.display.flip()
clock.tick(60)
pygame.quit()
if __name__ == "__main__":
main()