以下是一个简单的Python实现坦克大战的代码示例:
import pygame
pygame.init()
# 设置窗口大小
width, height = 600, 400
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Tank Battle")
# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
# 加载图像
tank_img = pygame.image.load("tank.png")
tank_rect = tank_img.get_rect()
# 设置坦克的初始位置
tank_x, tank_y = width // 2, height // 2
tank_speed = 5
# 设置游戏循环
clock = pygame.time.Clock()
game_running = True
while game_running:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_running = False
# 处理键盘输入
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
tank_x -= tank_speed
if keys[pygame.K_RIGHT]:
tank_x += tank_speed
if keys[pygame.K_UP]:
tank_y -= tank_speed
if keys[pygame.K_DOWN]:
tank_y += tank_sp