前言:创作背景-《坦克大战》是一款经典的平面射击游戏,最初由日本游戏公司南梦宫于1985年在任天堂FC平台上推出。游戏的主题围绕坦克战斗,玩家的任务是保卫自己的基地,同时摧毁所有敌人的坦克。游戏中有多种地形和敌人类型,玩家可以通过获取道具来强化坦克和基地。此外,游戏还支持玩家自创关卡,增加了游戏的趣味性。游戏中,玩家通过键盘控制坦克的移动和射击,需要灵活应对敌人的攻击并操作自身坦克摧毁敌方坦克,以获得胜利。那么话不多说,本次编程我们就一起来重温这部童年经典游戏。
编程思路:本次编程我们将会用到pygame,random等库。
第一步:准备第三方库
pygame是Python的一个第三方库,它需要我们自行下载。
下载方法:在PyCharm终端中输入"pip install pygame",回车等待一段时间。
pip install pygame
第二步:准备游戏相关图片(包括敌我双方的坦克主体和炮管)
敌方坦克主体图片
敌方坦克炮管图片
我方坦克主体图片
我方坦克炮管图片
(需要的也可以私信我发哦)
接下来我们将图片放在python项目下。(如下红色圆圈标注所示)
第三步:完整游戏代码
#导入库
import pygame
import random
#初始化Pygame
pygame.init()
#设置窗口及相关设置
WIDTH = 800
HEIGHT = 600
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLACK = (0, 0, 0)
TANK_SIZE = 30
BULLET_SPEED = 10
#图片加载函数
def load_image(path):
return pygame.image.load(path).convert_alpha()
#坦克类
class Tank:
def __init__(self, x, y, tank_image, barrel_image, color):
self.x = x
self.y = y
self.tank_image = tank_image
self.barrel_image = barrel_image
self.color = color
self.direction = 'UP'
self.speed = 4
self.bullets = []
def draw(self, screen):
screen.blit(self.tank_image, (self.x, self.y))
if self.direction == 'UP':
rotated_barrel = pygame.transform.rotate(self.barrel_image, -90)
screen.blit(rotated_barrel, (self.x + TANK_SIZE / 2 - rotated_barrel.get_width() / 2+20, self.y - rotated_barrel.get_height()))
elif self.direction == 'DOWN':
rotated_barrel = pygame.transform.rotate(self.barrel_image, 90)
screen.blit(rotated_barrel, (self.x + TANK_SIZE / 2 - rotated_barrel.get_width() / 2+20, self.y + TANK_SIZE+20))
elif self.direction == 'LEFT':
rotated_barrel = pygame.transform.rotate(self.barrel_image, 180)
screen.blit(rotated_barrel, (self.x -rotated_barrel.get_height()-30, self.y + TANK_SIZE / 2 - rotated_barrel.get_width() / 2+30))
else:
rotated_barrel = pygame.transform.rotate(self.barrel_image, 0)
screen.blit(rotated_barrel, (self.x + TANK_SIZE+30, self.y + TANK_SIZE / 2 - rotated_barrel.get_height() / 2+10))
def move(self):
if self.direction == 'UP':
self.y -= self.speed
if self.y < 0:
self.y = 0
elif self.direction == 'DOWN':
self.y += self.speed
if self.y > HEIGHT - TANK_SIZE:
self.y = HEIGHT - TANK_SIZE
elif self.direction == 'LEFT':
self.x -= self.speed
if self.x < 0:
self.x = 0
elif self.direction == 'RIGHT':
self.x += self.speed
if self.x > WIDTH - TANK_SIZE:
self.x = WIDTH - TANK_SIZE
def fire(self):
if self.direction == 'UP':
bullet = Bullet(self.x + TANK_SIZE / 2+20, self.y, 0, -BULLET_SPEED, self.color)
elif self.direction == 'DOWN':
bullet = Bullet(self.x + TANK_SIZE / 2+20, self.y + TANK_SIZE, 0, BULLET_SPEED, self.color)
elif self.direction == 'LEFT':
bullet = Bullet(self.x+30, self.y + TANK_SIZE / 2+10, -BULLET_SPEED, 0, self.color)
else:
bullet = Bullet(self.x + TANK_SIZE+30, self.y + TANK_SIZE / 2+10, BULLET_SPEED, 0, self.color)
self.bullets.append(bullet)
#炮弹类
class Bullet:
def __init__(self, x, y, dx, dy, color):
self.x = x
self.y = y
self.dx = dx
self.dy = dy
self.color = color
def move(self):
self.x += self.dx
self.y += self.dy
def draw(self, screen):
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), 3)
#坦克加载类
class TankWar:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('坦克大战-1.0(世界)')
# 加载玩家坦克图片
self.player_tank_image = load_image('tank_1.png')
self.player_barrel_image = load_image('tank_barrel.png')
self.player_tank = Tank(100, 100, self.player_tank_image, self.player_barrel_image, GREEN)
self.enemy_tanks = []
for _ in range(3):
enemy_tank_image = load_image('enemy_1.png')
enemy_barrel_image = load_image('enemy_barrel.png')
x = random.randint(0, WIDTH - TANK_SIZE)
y = random.randint(0, HEIGHT - TANK_SIZE)
enemy_tank = Tank(x, y, enemy_tank_image, enemy_barrel_image, RED)
self.enemy_tanks.append(enemy_tank)
self.clock = pygame.time.Clock()
def run(self):
running = True
while running:
self.clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
self.player_tank.direction = 'UP'
self.player_tank.move()
elif event.key == pygame.K_DOWN:
self.player_tank.direction = 'DOWN'
self.player_tank.move()
elif event.key == pygame.K_LEFT:
self.player_tank.direction = 'LEFT'
self.player_tank.move()
elif event.key == pygame.K_RIGHT:
self.player_tank.direction = 'RIGHT'
self.player_tank.move()
elif event.key == pygame.K_SPACE:
self.player_tank.fire()
for enemy in self.enemy_tanks:
# 随机改变敌方坦克方向
if random.randint(0, 100) < 20:
directions = ['UP', 'DOWN', 'LEFT', 'RIGHT']
enemy.direction = random.choice(directions)
enemy.move()
if random.randint(0, 100) < 10:
enemy.fire()
for bullet in self.player_tank.bullets:
bullet.move()
if bullet.x < 0 or bullet.x > WIDTH or bullet.y < 0 or bullet.y > HEIGHT:
self.player_tank.bullets.remove(bullet)
else:
for enemy in self.enemy_tanks:
if enemy.x < bullet.x < enemy.x + TANK_SIZE and enemy.y < bullet.y < enemy.y + TANK_SIZE:
self.enemy_tanks.remove(enemy)
for enemy in self.enemy_tanks:
for bullet in enemy.bullets:
bullet.move()
if bullet.x < 0 or bullet.x > WIDTH or bullet.y < 0 or bullet.y > HEIGHT:
enemy.bullets.remove(bullet)
else:
if self.player_tank.x < bullet.x < self.player_tank.x + TANK_SIZE and self.player_tank.y < bullet.y < self.player_tank.y + TANK_SIZE:
running = False
self.screen.fill(WHITE)
self.player_tank.draw(self.screen)
for enemy in self.enemy_tanks:
enemy.draw(self.screen)
for bullet in self.player_tank.bullets:
bullet.draw(self.screen)
for enemy in self.enemy_tanks:
for bullet in enemy.bullets:
bullet.draw(self.screen)
pygame.display.flip()
pygame.quit()
#游戏主函数
if __name__ == '__main__':
game = TankWar()
game.run()
第四步:运行效果展示
(后面我还会持续更新哦)
标签:enemy,1.0,bullet,Python,self,小游戏,pygame,tank,barrel From: https://blog.csdn.net/2401_83954530/article/details/144521489