写一个可以用pycharm运行的python坦克大战小游戏
时间: 2023-06-12 16:04:30 浏览: 398
好的,以下是一个简单的坦克大战小游戏代码,可以在PyCharm中运行:
```python
import pygame
import random
# 初始化 pygame
pygame.init()
# 游戏窗口大小
WINDOW_WIDTH = 600
WINDOW_HEIGHT = 600
# 创建游戏窗口
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("坦克大战")
# 加载图片
tank1 = pygame.image.load("tank1.png")
tank2 = pygame.image.load("tank2.png")
bullet = pygame.image.load("bullet.png")
wall = pygame.image.load("wall.png")
# 设置字体
font = pygame.font.SysFont("arial", 30)
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# 定义坦克类
class Tank(pygame.sprite.Sprite):
def __init__(self, x, y, image):
pygame.sprite.Sprite.__init__(self)
self.image = image
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.speed = 5
self.direction = "up"
def move(self):
if self.direction == "up":
self.rect.y -= self.speed
elif self.direction == "down":
self.rect.y += self.speed
elif self.direction == "left":
self.rect.x -= self.speed
elif self.direction == "right":
self.rect.x += self.speed
def fire(self):
bullet1 = Bullet(self.rect.centerx, self.rect.centery, self.direction)
bullets.add(bullet1)
def update(self):
self.move()
# 定义子弹类
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y, direction):
pygame.sprite.Sprite.__init__(self)
self.image = bullet
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.speed = 10
self.direction = direction
def move(self):
if self.direction == "up":
self.rect.y -= self.speed
elif self.direction == "down":
self.rect.y += self.speed
elif self.direction == "left":
self.rect.x -= self.speed
elif self.direction == "right":
self.rect.x += self.speed
def update(self):
self.move()
# 定义墙类
class Wall(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = wall
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
# 创建精灵组
tanks = pygame.sprite.Group()
bullets = pygame.sprite.Group()
walls = pygame.sprite.Group()
# 创建玩家坦克
player_tank = Tank(200, 200, tank1)
tanks.add(player_tank)
# 创建敌方坦克
for i in range(3):
enemy_tank = Tank(random.randint(0, WINDOW_WIDTH), random.randint(0, WINDOW_HEIGHT), tank2)
tanks.add(enemy_tank)
# 创建墙
for i in range(10):
wall1 = Wall(random.randint(0, WINDOW_WIDTH), random.randint(0, WINDOW_HEIGHT))
walls.add(wall1)
# 游戏主循环
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:
player_tank.fire()
# 更新精灵组
tanks.update()
bullets.update()
# 碰撞检测
for bullet in bullets:
# 子弹和墙的碰撞检测
wall_hit = pygame.sprite.spritecollide(bullet, walls, False)
if wall_hit:
bullet.kill()
for wall in wall_hit:
wall.kill()
# 子弹和坦克的碰撞检测
tank_hit = pygame.sprite.spritecollide(bullet, tanks, False)
if tank_hit:
bullet.kill()
for tank in tank_hit:
tank.kill()
# 绘制背景
screen.fill(BLACK)
# 绘制精灵组
walls.draw(screen)
bullets.draw(screen)
tanks.draw(screen)
# 绘制分数
score_text = font.render("Score: 0", True, WHITE)
screen.blit(score_text, (10, 10))
# 更新屏幕
pygame.display.update()
# 退出 pygame
pygame.quit()
```
这个小游戏包括了玩家坦克、敌方坦克、子弹和墙,玩家可以通过方向键控制自己的坦克移动和发射子弹,敌方坦克和墙可以阻挡玩家坦克和子弹的前进。希望对你有帮助!
阅读全文