写一个坦克大战的小游戏的代码
时间: 2023-12-01 20:32:47 浏览: 118
这是一个简单的Python坦克大战游戏,使用了Pygame库。游戏规则是控制一辆坦克,射击其他敌方坦克,同时避免被敌方坦克击中。以下是代码:
```python
import pygame
import random
# 初始化游戏
pygame.init()
# 设置游戏窗口大小
width, height = 640, 480
screen = pygame.display.set_mode((width, height))
# 加载背景图片
background = pygame.image.load("background.png")
# 加载坦克图片
player_tank_image = pygame.image.load("player_tank.png")
enemy_tank_image = pygame.image.load("enemy_tank.png")
# 设置坦克大小和速度
tank_width, tank_height = 40, 40
tank_speed = 5
# 定义坦克类
class Tank:
def __init__(self, x, y, image):
self.x = x
self.y = y
self.image = image
self.direction = "up"
self.speed = tank_speed
self.rect = pygame.Rect(self.x, self.y, tank_width, tank_height)
# 移动坦克
def move(self):
if self.direction == "up":
self.y -= self.speed
elif self.direction == "down":
self.y += self.speed
elif self.direction == "left":
self.x -= self.speed
elif self.direction == "right":
self.x += self.speed
# 碰撞检测
if self.x < 0:
self.x = 0
elif self.x > width - tank_width:
self.x = width - tank_width
elif self.y < 0:
self.y = 0
elif self.y > height - tank_height:
self.y = height - tank_height
self.rect = pygame.Rect(self.x, self.y, tank_width, tank_height)
# 绘制坦克
def draw(self):
screen.blit(self.image, (self.x, self.y))
# 定义子弹类
class Bullet:
def __init__(self, x, y, direction):
self.x = x
self.y = y
self.direction = direction
self.speed = 10
self.rect = pygame.Rect(self.x, self.y, 5, 5)
# 移动子弹
def move(self):
if self.direction == "up":
self.y -= self.speed
elif self.direction == "down":
self.y += self.speed
elif self.direction == "left":
self.x -= self.speed
elif self.direction == "right":
self.x += self.speed
self.rect = pygame.Rect(self.x, self.y, 5, 5)
# 绘制子弹
def draw(self):
pygame.draw.rect(screen, (255, 255, 255), (self.x, self.y, 5, 5))
# 定义敌方坦克类
class EnemyTank(Tank):
def __init__(self, x, y, image):
super().__init__(x, y, image)
self.direction = random.choice(["up", "down", "left", "right"])
self.bullet_list = []
# 随机移动
def random_move(self):
self.direction = random.choice(["up", "down", "left", "right"])
self.move()
# 发射子弹
def fire(self):
bullet = Bullet(self.x + tank_width // 2, self.y + tank_height // 2, self.direction)
self.bullet_list.append(bullet)
# 绘制子弹
def draw_bullets(self):
for bullet in self.bullet_list:
bullet.move()
bullet.draw()
if bullet.rect.colliderect(player_tank.rect):
return True
if bullet.x < 0 or bullet.x > width or bullet.y < 0 or bullet.y > height:
self.bullet_list.remove(bullet)
# 创建玩家坦克和敌方坦克
player_tank = Tank(300, 400, player_tank_image)
enemy_tanks = []
for i in range(5):
x, y = random.randint(0, 600), random.randint(0, 200)
enemy_tank = EnemyTank(x, y, enemy_tank_image)
enemy_tanks.append(enemy_tank)
# 游戏循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# 绘制背景
screen.blit(background, (0, 0))
# 移动玩家坦克
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
player_tank.direction = "up"
player_tank.move()
elif keys[pygame.K_DOWN]:
player_tank.direction = "down"
player_tank.move()
elif keys[pygame.K_LEFT]:
player_tank.direction = "left"
player_tank.move()
elif keys[pygame.K_RIGHT]:
player_tank.direction = "right"
player_tank.move()
# 绘制玩家坦克
player_tank.draw()
# 移动敌方坦克并发射子弹
for enemy_tank in enemy_tanks:
enemy_tank.random_move()
if random.randint(0, 50) == 0:
enemy_tank.fire()
enemy_tank.draw()
enemy_tank.draw_bullets()
# 检测玩家坦克是否被敌方坦克击中
for enemy_tank in enemy_tanks:
if enemy_tank.rect.colliderect(player_tank.rect):
pygame.quit()
exit()
# 刷新窗口
pygame.display.update()
```
这个游戏还可以进一步优化,比如增加声音效果、加入游戏菜单等。
阅读全文