请用Python帮我编写一个坦克大战窗体小游戏
时间: 2023-11-05 08:37:45 浏览: 128
坦克大战.zip_Python游戏_castvm4_python_坦克大战_坦克大战gif
好的,以下是一个简单的坦克大战窗体小游戏的代码,采用 Pygame 库实现:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 窗口大小
WINDOW_WIDTH = 600
WINDOW_HEIGHT = 600
# 坦克大小
TANK_WIDTH = 50
TANK_HEIGHT = 50
# 子弹大小
BULLET_WIDTH = 10
BULLET_HEIGHT = 10
# 颜色定义
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# 创建窗口
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("坦克大战")
# 加载图片
tank_img = pygame.image.load("tank.png")
bullet_img = pygame.image.load("bullet.png")
# 创建坦克类
class Tank:
def __init__(self, x, y, speed):
self.x = x
self.y = y
self.speed = speed
self.direction = "up"
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
def draw(self):
if self.direction == "up":
tank_img_rotate = pygame.transform.rotate(tank_img, 0)
elif self.direction == "down":
tank_img_rotate = pygame.transform.rotate(tank_img, 180)
elif self.direction == "left":
tank_img_rotate = pygame.transform.rotate(tank_img, 90)
elif self.direction == "right":
tank_img_rotate = pygame.transform.rotate(tank_img, 270)
window.blit(tank_img_rotate, (self.x, self.y))
def fire(self):
if self.direction == "up":
bullet = Bullet(self.x + TANK_WIDTH / 2 - BULLET_WIDTH / 2, self.y, self.direction)
elif self.direction == "down":
bullet = Bullet(self.x + TANK_WIDTH / 2 - BULLET_WIDTH / 2, self.y + TANK_HEIGHT - BULLET_HEIGHT, self.direction)
elif self.direction == "left":
bullet = Bullet(self.x, self.y + TANK_HEIGHT / 2 - BULLET_HEIGHT / 2, self.direction)
elif self.direction == "right":
bullet = Bullet(self.x + TANK_WIDTH - BULLET_WIDTH, self.y + TANK_HEIGHT / 2 - BULLET_HEIGHT / 2, self.direction)
bullets.append(bullet)
# 创建子弹类
class Bullet:
def __init__(self, x, y, direction):
self.x = x
self.y = y
self.speed = 10
self.direction = direction
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
def draw(self):
window.blit(bullet_img, (self.x, self.y))
# 创建敌方坦克
enemy_tank = Tank(random.randint(0, WINDOW_WIDTH - TANK_WIDTH), 0, 5)
# 创建玩家坦克
player_tank = Tank(WINDOW_WIDTH / 2 - TANK_WIDTH / 2, WINDOW_HEIGHT - TANK_HEIGHT, 5)
# 子弹列表
bullets = []
# 游戏循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
player_tank.direction = "up"
elif event.key == pygame.K_DOWN:
player_tank.direction = "down"
elif event.key == pygame.K_LEFT:
player_tank.direction = "left"
elif event.key == pygame.K_RIGHT:
player_tank.direction = "right"
elif event.key == pygame.K_SPACE:
player_tank.fire()
# 移动坦克
player_tank.move()
enemy_tank.move()
# 移动子弹
for bullet in bullets:
bullet.move()
# 绘制背景
window.fill(WHITE)
# 绘制坦克和子弹
player_tank.draw()
enemy_tank.draw()
for bullet in bullets:
bullet.draw()
# 判断是否击中敌方坦克
for bullet in bullets:
if bullet.x >= enemy_tank.x and bullet.x <= enemy_tank.x + TANK_WIDTH \
and bullet.y >= enemy_tank.y and bullet.y <= enemy_tank.y + TANK_HEIGHT:
enemy_tank = Tank(random.randint(0, WINDOW_WIDTH - TANK_WIDTH), 0, 5)
bullets.remove(bullet)
# 刷新窗口
pygame.display.update()
```
在代码中,我们创建了一个 `Tank` 类和一个 `Bullet` 类,分别表示坦克和子弹。我们创建了一个敌方坦克和一个玩家坦克,并且可以用键盘控制玩家坦克的移动和发射子弹。子弹会一直向前移动,直到撞到墙壁或敌方坦克。如果撞到敌方坦克,敌方坦克会重新生成在窗口的顶部。祝您玩得愉快!
阅读全文