Python-基于Pygame的小游戏(坦克大战-1.0(世界))(一)

前言:创作背景-《坦克大战》是一款经典的平面射击游戏,最初由日本游戏公司南梦宫于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()

第四步:运行效果展示

第五步:玩法介绍

上,下,左,右键控制移动方向,空格键控制发射。

(后面我还会持续更新哦)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闪云-微星

感谢大家的支持与鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值