用python写一个坦克大战小游戏
时间: 2023-12-07 17:27:38 浏览: 38
好的,下面是一个基于Python的简单坦克大战游戏,使用pygame库实现:
首先,安装pygame库:
```
pip install pygame
```
然后,创建一个名为tank_game.py的Python文件,写入以下代码:
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 游戏窗口大小
screen_width = 640
screen_height = 480
# 创建游戏窗口
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('坦克大战')
# 加载图片
tank_img = pygame.image.load('tank.png')
bullet_img = pygame.image.load('bullet.png')
enemy_img = pygame.image.load('enemy.png')
# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
# 定义游戏对象类
class GameObject:
def __init__(self, x, y, img):
self.x = x
self.y = y
self.img = img
self.width = img.get_width()
self.height = img.get_height()
# 绘制游戏对象
def draw(self, surface):
surface.blit(self.img, (self.x, self.y))
# 定义坦克类
class Tank(GameObject):
def __init__(self, x, y, img):
super().__init__(x, y, img)
self.speed = 5
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 fire(self):
bullet = Bullet(self.x + self.width / 2 - bullet_img.get_width() / 2, self.y + self.height / 2 - bullet_img.get_height() / 2)
return bullet
# 定义子弹类
class Bullet(GameObject):
def __init__(self, x, y):
super().__init__(x, y, bullet_img)
self.speed = 10
# 移动
def move(self):
self.y -= self.speed
# 定义敌方坦克类
class EnemyTank(GameObject):
def __init__(self, x, y, img):
super().__init__(x, y, img)
self.speed = 3
self.direction = 'down'
# 移动
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 random.randint(1, 100) == 1:
directions = ['up', 'down', 'left', 'right']
self.direction = random.choice(directions)
# 创建玩家坦克
player_tank = Tank(screen_width / 2 - tank_img.get_width() / 2, screen_height - tank_img.get_height(), tank_img)
# 创建敌方坦克
enemy_tank = EnemyTank(random.randint(0, screen_width - enemy_img.get_width()), 0, enemy_img)
# 创建子弹列表
bullets = []
# 游戏循环
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_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:
bullets.append(player_tank.fire())
# 移动玩家坦克
player_tank.move()
# 移动敌方坦克
enemy_tank.move()
# 移动子弹
for bullet in bullets:
bullet.move()
# 绘制背景
screen.fill(white)
# 绘制玩家坦克
player_tank.draw(screen)
# 绘制敌方坦克
enemy_tank.draw(screen)
# 绘制子弹
for bullet in bullets:
bullet.draw(screen)
# 判断子弹是否击中敌方坦克
for bullet in bullets:
if bullet.x > enemy_tank.x and bullet.x < enemy_tank.x + enemy_tank.width and bullet.y > enemy_tank.y and bullet.y < enemy_tank.y + enemy_tank.height:
bullets.remove(bullet)
enemy_tank = EnemyTank(random.randint(0, screen_width - enemy_img.get_width()), 0, enemy_img)
# 刷新屏幕
pygame.display.flip()
# 退出pygame
pygame.quit()
```
接下来,将tank.png、bullet.png和enemy.png三张图片放在同一个文件夹下,并运行tank_game.py即可开始游戏。玩家可以通过方向键控制坦克的移动方向,按下空格键发射子弹,当子弹击中敌方坦克时,会重新生成一个新的敌方坦克。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)