【Python实现坦克大战,带你找回童年的快乐】附源码

2024-02-23 11:40

本文主要是介绍【Python实现坦克大战,带你找回童年的快乐】附源码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • 前言:
    • 第一步:安装Pygame库
    • 第二步:实现思路:
      • 场景实现:
      • 石头墙:
      • 钢墙:
      • 地面类(Grass)
      • 地图:
    • 第三步:坦克类的详细实现:
      • 坦克类(Tank)
      • 子弹类(Bullet)
      • 爆炸类(Explosion)
    • 坦克类详细代码以及实现:
    • 完整代码压缩包:
    • 最终成功展示:

在这里插入图片描述

前言:

坦克大战90版本是一款2D射击游戏,玩家扮演一辆坦克,通过操纵坦克进行战斗。游戏场景通常是由迷宫状的地图构成,在地图中有各种不同类型的障碍物,如墙壁、水域、草地等,这些障碍物会对玩家的行动和战斗产生影响。

玩家的目标是摧毁敌方坦克并保护自己的基地。游戏中会有敌方坦克和敌方基地,玩家需要躲避敌方坦克的攻击并利用自己的技巧和策略来摧毁敌方坦克和基地。同时,玩家还可以尝试收集道具,如增强装甲、提高射速等,以增加自己的战斗能力。

这款游戏具有简单直观的操作方式,通过方向键控制坦克移动,空格键发射子弹。游戏节奏紧凑,战斗刺激,让玩家体验到了紧张的战争氛围。此外,游戏还支持多人模式,玩家可以与朋友一起组队或对战,增加了游戏的趣味性和竞争性。

坦克大战90版本不仅在当时取得了巨大成功,在后来的几十年里也不断被翻新和改进,衍生出了更多精彩的版本和新的玩法。无论是当年的经典版还是现代的重制版,坦克大战都是一款具有吸引力和娱乐性的经典游戏,让玩家流连忘返。

第一步:安装Pygame库

确保你已经安装了Python:在安装Pygame之前,确保你已经安装了Python。你可以从官方网站下载Python的最新版本并进行安装。Pygame兼容Python 2.x和Python 3.x版本。

打开终端:在Windows操作系统中,你可以按下Win+R键,在运行窗口中输入"cmd"并按下回车键打开命令提示符。在macOS和Linux系统中,你可以打开终端应用程序。

使用pip安装Pygame:在终端中输入以下命令来使用pip包管理器安装Pygame库:

pip install pygame

如果你使用的是Python 3,则使用以下命令:

pip3 install pygame

等待安装完成:一旦执行了安装命令,pip将会自动下载和安装Pygame库及其依赖项。这个过程可能需要一些时间,取决于你的网络速度和系统环境。

验证安装:安装完成后,你可以通过在终端中输入以下命令来验证Pygame是否成功安装:

python -m pygame.examples.aliens

或者

python3 -m pygame.examples.aliens

如果一切顺利,你将会看到一个外星人射击游戏的窗口弹出。

在这里插入图片描述

第二步:实现思路:

场景实现:

游戏场景,场景的组成主要包括:石头墙、钢墙、地面、坦克、子弹地图。

石头墙:

石头墙是一种阻挡玩家和敌人行进路径的障碍物。可以使用矩形或图片来表示石头墙,并将其绘制在游戏界面的指定位置上。

# 石头墙
class Brick(pygame.sprite.Sprite):def __init__(self, position):pygame.sprite.Sprite.__init__(self)self.image = pygame.image.load('images/scene/brick.png').convert_alpha()self.rect = self.image.get_rect()self.rect.topleft = positionself.is_destroyed = Falsedef destroy(self):self.is_destroyed = Truedef draw(self, surface):if not self.is_destroyed:surface.blit(self.image, self.rect)position 参数:用于指定石头墙在游戏界面上的位置。
is_destroyed 属性:用于标记石头墙是否被摧毁。
destroy() 方法:用于将石头墙标记为被摧毁。
draw() 方法:用于将石头墙绘制在游戏界面上。

钢墙:

钢墙是一种更坚固的墙体,通常比石头墙更难被破坏。与石头墙类似,可以使用矩形或图片来表示钢墙,并将其绘制在游戏界面上。

# 钢墙
class Iron(pygame.sprite.Sprite):def __init__(self):pygame.sprite.Sprite.__init__(self)self.iron = pygame.image.load('images/scene/iron.png')self.rect = self.iron.get_rect()self.being = False

地面类(Grass)

草地是游戏中的一种地形,玩家可以在草地上自由行动。以下是一个简单的草地类定义:

import pygameclass Grass(pygame.sprite.Sprite):def __init__(self, x, y, width=32, height=32):pygame.sprite.Sprite.__init__(self)self.image = pygame.image.load('images/grass.gif').convert_alpha()self.rect = self.image.get_rect()self.rect.left, self.rect.top = x, yself.width, self.height = width, heightdef draw(self, surface):surface.blit(self.image, self.rect)

在这个示例中,我们定义了一个 Grass 类,用于表示游戏中的草地。x 和 y 参数用于指定草地的位置,width 和 height 参数用于指定草地的宽度和高度。

draw() 方法用于将草地绘制在游戏界面上。

地图:

地图:地图是一个整体的场景,它包含了所有的游戏元素,并定义了玩家和敌人的移动范围。地图可以通过一个矩阵或类似的数据结构来表示,其中不同的值代表不同的场景元素(如石头墙、钢墙、地板等)。根据地图的定义,可以将相应的场景元素绘制在游戏界面上。

# 地图
class Map():def __init__(self, stage):self.brickGroup = pygame.sprite.Group()self.ironGroup  = pygame.sprite.Group()self.iceGroup = pygame.sprite.Group()self.riverGroup = pygame.sprite.Group()self.treeGroup = pygame.sprite.Group()if stage == 1:self.stage1()elif stage == 2:self.stage2()# 关卡def stage1(self):for x in [2, 3, 6, 7, 18, 19, 22, 23]:for y in [2, 3, 4, 5, 6, 7, 8, 9, 10, 17, 18, 19, 20, 21, 22, 23]:self.brick = Brick()self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24self.brick.being = Trueself.brickGroup.add(self.brick)for x in [10, 11, 14, 15]:for y in [2, 3, 4, 5, 6, 7, 8, 11, 12, 15, 16, 17, 18, 19, 20]:self.brick = Brick()self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24self.brick.being = Trueself.brickGroup.add(self.brick)for x in [4, 5, 6, 7, 18, 19, 20, 21]:for y in [13, 14]:self.brick = Brick()self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24self.brick.being = Trueself.brickGroup.add(self.brick)for x in [12, 13]:for y in [16, 17]:self.brick = Brick()self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24self.brick.being = Trueself.brickGroup.add(self.brick)for x, y in [(11, 23), (12, 23), (13, 23), (14, 23), (11, 24), (14, 24), (11, 25), (14, 25)]:self.brick = Brick()self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24self.brick.being = Trueself.brickGroup.add(self.brick)for x, y in [(0, 14), (1, 14), (12, 6), (13, 6), (12, 7), (13, 7), (24, 14), (25, 14)]:self.iron = Iron()self.iron.rect.left, self.iron.rect.top = 3 + x * 24, 3 + y * 24self.iron.being = Trueself.ironGroup.add(self.iron)

第三步:坦克类的详细实现:

坦克类(Tank)

坦克是游戏中最主要的角色之一,玩家可以操控它来击败敌人。以下是一个简单的坦克类定义:

import pygameclass Tank(pygame.sprite.Sprite):def __init__(self, x, y, speed=5):pygame.sprite.Sprite.__init__(self)self.image = pygame.image.load('images/tank/player1.png').convert_alpha()self.rect = self.image.get_rect()self.rect.left, self.rect.top = x, yself.speed = speedself.direction = 'up'self.is_alive = Truedef move(self):if self.direction == 'up':self.rect.top -= self.speedelif self.direction == 'down':self.rect.top += self.speedelif self.direction == 'left':self.rect.left -= self.speedelif self.direction == 'right':self.rect.left += self.speeddef draw(self, surface):surface.blit(self.image, self.rect)

在这个示例中,我们定义了一个 Tank 类,用于表示游戏中的坦克。x 和 y 参数用于指定坦克的初始位置,speed 参数用于指定坦克的移动速度。direction 属性表示坦克当前的朝向,is_alive 属性表示坦克是否存活。

move() 方法用于根据坦克的当前朝向,改变坦克的位置。draw() 方法用于将坦克绘制在游戏界面上。

子弹类(Bullet)

子弹是坦克和敌人所使用的武器。
子弹的主要属性包括:方向、速度、是否存活、是否为加强版等,代码实现如下:

# 子弹类
class Bullet(pygame.sprite.Sprite):def __init__(self):pygame.sprite.Sprite.__init__(self)# 子弹四个方向(上下左右)self.bullets = ['images/bullet/bullet_up.png', 'images/bullet/bullet_down.png', 'images/bullet/bullet_left.png', 'images/bullet/bullet_right.png']# 子弹方向(默认向上)self.direction_x, self.direction_y = 0, -1self.bullet = pygame.image.load(self.bullets[0])self.rect = self.bullet.get_rect()# 在坦克类中再赋实际值self.rect.left, self.rect.right = 0, 0# 速度self.speed = 6# 是否存活self.being = False# 是否为加强版子弹(可碎钢板)self.stronger = False# 改变子弹方向def turn(self, direction_x, direction_y):self.direction_x, self.direction_y = direction_x, direction_yif self.direction_x == 0 and self.direction_y == -1:self.bullet = pygame.image.load(self.bullets[0])elif self.direction_x == 0 and self.direction_y == 1:self.bullet = pygame.image.load(self.bullets[1])elif self.direction_x == -1 and self.direction_y == 0:self.bullet = pygame.image.load(self.bullets[2])elif self.direction_x == 1 and self.direction_y == 0:self.bullet = pygame.image.load(self.bullets[3])else:raise ValueError('Bullet class -> direction value error.')# 移动def move(self):self.rect = self.rect.move(self.speed*self.direction_x, self.speed*self.direction_y)# 到地图边缘后消失if (self.rect.top < 3) or (self.rect.bottom > 630 - 3) or (self.rect.left < 3) or (self.rect.right > 630 - 3):self.being = False

爆炸类(Explosion)

爆炸是游戏中的一种特效,当坦克被击中或者墙壁被摧毁时会产生爆炸效果。以下是一个简单的爆炸类定义:

import pygameclass Explosion(pygame.sprite.Sprite):def __init__(self, x, y):pygame.sprite.Sprite.__init__(self)self.image = pygame.image.load('images/explosion.png').convert_alpha()self.rect = self.image.get_rect()self.rect.centerx, self.rect.centery = x, yself.frame = 0self.last_update = pygame.time.get_ticks()self.frame_rate = 50def update(self):now = pygame.time.get_ticks()if now - self.last_update > self.frame_rate:self.frame += 1if self.frame == 6:self.kill()else:self.last_update = nowdef draw(self, surface):surface.blit(self.image, self.rect, pygame.Rect(self.frame * 64, 0, 64, 64))

在这个示例中,我们定义了一个 Explosion 类,用于表示游戏中的爆炸效果。x 和 y 参数用于指定爆炸的位置。

update() 方法用于更新爆炸动画的帧数。draw() 方法用于将当前帧的爆炸效果绘制在游戏界面上。

坦克类详细代码以及实现:

坦克包括我方坦克和敌方坦克,我方坦克由玩家自己控制移动、射击等操作,敌方坦克实现自动移动、射击等操作,代码实现如下:

# 我方坦克类
class myTank(pygame.sprite.Sprite):def __init__(self, player):pygame.sprite.Sprite.__init__(self)# 玩家编号(1/2)self.player = player# 不同玩家用不同的坦克(不同等级对应不同的图)if player == 1:self.tanks = ['images/myTank/tank_T1_0.png', 'images/myTank/tank_T1_1.png', 'images/myTank/tank_T1_2.png']elif player == 2:self.tanks = ['images/myTank/tank_T2_0.png', 'images/myTank/tank_T2_1.png', 'images/myTank/tank_T2_2.png']else:raise ValueError('myTank class -> player value error.')# 坦克等级(初始0)self.level = 0# 载入(两个tank是为了轮子特效)self.tank = pygame.image.load(self.tanks[self.level]).convert_alpha()self.tank_0 = self.tank.subsurface((0, 0), (48, 48))self.tank_1 = self.tank.subsurface((48, 0), (48, 48))self.rect = self.tank_0.get_rect()# 保护罩self.protected_mask = pygame.image.load('images/others/protect.png').convert_alpha()self.protected_mask1 = self.protected_mask.subsurface((0, 0), (48, 48))self.protected_mask2 = self.protected_mask.subsurface((48, 0), (48, 48))# 坦克方向self.direction_x, self.direction_y = 0, -1# 不同玩家的出生位置不同if player == 1:self.rect.left, self.rect.top = 3 + 24 * 8, 3 + 24 * 24elif player == 2:self.rect.left, self.rect.top = 3 + 24 * 16, 3 + 24 * 24else:raise ValueError('myTank class -> player value error.')# 坦克速度self.speed = 3# 是否存活self.being = True# 有几条命self.life = 3# 是否处于保护状态self.protected = False# 子弹self.bullet = Bullet()# 射击def shoot(self):self.bullet.being = Trueself.bullet.turn(self.direction_x, self.direction_y)if self.direction_x == 0 and self.direction_y == -1:self.bullet.rect.left = self.rect.left + 20self.bullet.rect.bottom = self.rect.top - 1elif self.direction_x == 0 and self.direction_y == 1:self.bullet.rect.left = self.rect.left + 20self.bullet.rect.top = self.rect.bottom + 1elif self.direction_x == -1 and self.direction_y == 0:self.bullet.rect.right = self.rect.left - 1self.bullet.rect.top = self.rect.top + 20elif self.direction_x == 1 and self.direction_y == 0:self.bullet.rect.left = self.rect.right + 1self.bullet.rect.top = self.rect.top + 20else:raise ValueError('myTank class -> direction value error.')if self.level == 0:self.bullet.speed = 8self.bullet.stronger = Falseelif self.level == 1:self.bullet.speed = 12self.bullet.stronger = Falseelif self.level == 2:self.bullet.speed = 12self.bullet.stronger = Trueelif self.level == 3:self.bullet.speed = 16self.bullet.stronger = Trueelse:raise ValueError('myTank class -> level value error.')# 等级提升def up_level(self):if self.level < 3:self.level += 1try:self.tank = pygame.image.load(self.tanks[self.level]).convert_alpha()except:self.tank = pygame.image.load(self.tanks[-1]).convert_alpha()# 等级降低def down_level(self):if self.level > 0:self.level -= 1self.tank = pygame.image.load(self.tanks[self.level]).convert_alpha()# 向上def move_up(self, tankGroup, brickGroup, ironGroup, myhome):self.direction_x, self.direction_y = 0, -1# 先移动后判断self.rect = self.rect.move(self.speed*self.direction_x, self.speed*self.direction_y)self.tank_0 = self.tank.subsurface((0, 0), (48, 48))self.tank_1 = self.tank.subsurface((48, 0), (48, 48))# 是否可以移动is_move = True# 地图顶端if self.rect.top < 3:self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)is_move = False# 撞石头/钢墙if pygame.sprite.spritecollide(self, brickGroup, False, None) or \pygame.sprite.spritecollide(self, ironGroup, False, None):self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)is_move = False# 撞其他坦克if pygame.sprite.spritecollide(self, tankGroup, False, None):self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)is_move = False# 大本营if pygame.sprite.collide_rect(self, myhome):self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)is_move = Falsereturn is_move# 向下def move_down(self, tankGroup, brickGroup, ironGroup, myhome):self.direction_x, self.direction_y = 0, 1# 先移动后判断self.rect = self.rect.move(self.speed*self.direction_x, self.speed*self.direction_y)self.tank_0 = self.tank.subsurface((0, 48), (48, 48))self.tank_1 = self.tank.subsurface((48, 48), (48, 48))# 是否可以移动is_move = True# 地图底端if self.rect.bottom > 630 - 3:self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)is_move = False# 撞石头/钢墙if pygame.sprite.spritecollide(self, brickGroup, False, None) or \pygame.sprite.spritecollide(self, ironGroup, False, None):self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)is_move = False# 撞其他坦克if pygame.sprite.spritecollide(self, tankGroup, False, None):self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)is_move = False# 大本营if pygame.sprite.collide_rect(self, myhome):self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)is_move = Falsereturn is_move# 向左def move_left(self, tankGroup, brickGroup, ironGroup, myhome):self.direction_x, self.direction_y = -1, 0# 先移动后判断self.rect = self.rect.move(self.speed*self.direction_x, self.speed*self.direction_y)self.tank_0 = self.tank.subsurface((0, 96), (48, 48))self.tank_1 = self.tank.subsurface((48, 96), (48, 48))# 是否可以移动is_move = True# 地图左端if self.rect.left < 3:self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)is_move = False# 撞石头/钢墙if pygame.sprite.spritecollide(self, brickGroup, False, None) or \pygame.sprite.spritecollide(self, ironGroup, False, None):self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)is_move = False# 撞其他坦克if pygame.sprite.spritecollide(self, tankGroup, False, None):self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)is_move = False  # 大本营if pygame.sprite.collide_rect(self, myhome):self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)is_move = Falsereturn is_move# 向右def move_right(self, tankGroup, brickGroup, ironGroup, myhome):self.direction_x, self.direction_y = 1, 0# 先移动后判断self.rect = self.rect.move(self.speed*self.direction_x, self.speed*self.direction_y)self.tank_0 = self.tank.subsurface((0, 144), (48, 48))self.tank_1 = self.tank.subsurface((48, 144), (48, 48))# 是否可以移动is_move = True# 地图右端if self.rect.right > 630 - 3:self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)is_move = False# 撞石头/钢墙if pygame.sprite.spritecollide(self, brickGroup, False, None) or \pygame.sprite.spritecollide(self, ironGroup, False, None):self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)is_move = False# 撞其他坦克if pygame.sprite.spritecollide(self, tankGroup, False, None):self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)is_move = False# 大本营if pygame.sprite.collide_rect(self, myhome):self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)is_move = Falsereturn is_move# 死后重置def reset(self):self.level = 0self.protected = Falseself.tank = pygame.image.load(self.tanks[self.level]).convert_alpha()self.tank_0 = self.tank.subsurface((0, 0), (48, 48))self.tank_1 = self.tank.subsurface((48, 0), (48, 48))self.rect = self.tank_0.get_rect()self.direction_x, self.direction_y = 0, -1if self.player == 1:self.rect.left, self.rect.top = 3 + 24 * 8, 3 + 24 * 24elif self.player == 2:self.rect.left, self.rect.top = 3 + 24 * 16, 3 + 24 * 24else:raise ValueError('myTank class -> player value error.')self.speed = 3# 敌方坦克类
class enemyTank(pygame.sprite.Sprite):def __init__(self, x=None, kind=None, is_red=None):pygame.sprite.Sprite.__init__(self)# 用于给刚生成的坦克播放出生特效self.born = Trueself.times = 90# 坦克的种类编号if kind is None:self.kind = random.randint(0, 3)else:self.kind = kind# 所有坦克self.tanks1 = ['images/enemyTank/enemy_1_0.png', 'images/enemyTank/enemy_1_1.png', 'images/enemyTank/enemy_1_2.png', 'images/enemyTank/enemy_1_3.png']self.tanks2 = ['images/enemyTank/enemy_2_0.png', 'images/enemyTank/enemy_2_1.png', 'images/enemyTank/enemy_2_2.png', 'images/enemyTank/enemy_2_3.png']self.tanks3 = ['images/enemyTank/enemy_3_0.png', 'images/enemyTank/enemy_3_1.png', 'images/enemyTank/enemy_3_2.png', 'images/enemyTank/enemy_3_3.png']self.tanks4 = ['images/enemyTank/enemy_4_0.png', 'images/enemyTank/enemy_4_1.png', 'images/enemyTank/enemy_4_2.png', 'images/enemyTank/enemy_4_3.png']self.tanks = [self.tanks1, self.tanks2, self.tanks3, self.tanks4]# 是否携带食物(红色的坦克携带食物)if is_red is None:self.is_red = random.choice((True, False, False, False, False))else:self.is_red = is_red# 同一种类的坦克具有不同的颜色, 红色的坦克比同类坦克多一点血量if self.is_red:self.color = 3else:self.color = random.randint(0, 2)# 血量self.blood = self.color# 载入(两个tank是为了轮子特效)self.tank = pygame.image.load(self.tanks[self.kind][self.color]).convert_alpha()self.tank_0 = self.tank.subsurface((0, 48), (48, 48))self.tank_1 = self.tank.subsurface((48, 48), (48, 48))self.rect = self.tank_0.get_rect()# 坦克位置if x is None:self.x = random.randint(0, 2)else:self.x = xself.rect.left, self.rect.top = 3 + self.x * 12 * 24, 3# 坦克是否可以行动self.can_move = True# 坦克速度self.speed = max(3 - self.kind, 1)# 方向self.direction_x, self.direction_y = 0, 1# 是否存活self.being = True# 子弹self.bullet = Bullet()# 射击def shoot(self):self.bullet.being = Trueself.bullet.turn(self.direction_x, self.direction_y)if self.direction_x == 0 and self.direction_y == -1:self.bullet.rect.left = self.rect.left + 20self.bullet.rect.bottom = self.rect.top - 1elif self.direction_x == 0 and self.direction_y == 1:self.bullet.rect.left = self.rect.left + 20self.bullet.rect.top = self.rect.bottom + 1elif self.direction_x == -1 and self.direction_y == 0:self.bullet.rect.right = self.rect.left - 1self.bullet.rect.top = self.rect.top + 20elif self.direction_x == 1 and self.direction_y == 0:self.bullet.rect.left = self.rect.right + 1self.bullet.rect.top = self.rect.top + 20else:raise ValueError('enemyTank class -> direction value error.')# 随机移动def move(self, tankGroup, brickGroup, ironGroup, myhome):self.rect = self.rect.move(self.speed*self.direction_x, self.speed*self.direction_y)is_move = Trueif self.direction_x == 0 and self.direction_y == -1:self.tank_0 = self.tank.subsurface((0, 0), (48, 48))self.tank_1 = self.tank.subsurface((48, 0), (48, 48))if self.rect.top < 3:self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)self.direction_x, self.direction_y = random.choice(([0, 1], [0, -1], [1, 0], [-1, 0]))is_move = Falseelif self.direction_x == 0 and self.direction_y == 1:self.tank_0 = self.tank.subsurface((0, 48), (48, 48))self.tank_1 = self.tank.subsurface((48, 48), (48, 48))if self.rect.bottom > 630 - 3:self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)self.direction_x, self.direction_y = random.choice(([0, 1], [0, -1], [1, 0], [-1, 0]))is_move = Falseelif self.direction_x == -1 and self.direction_y == 0:self.tank_0 = self.tank.subsurface((0, 96), (48, 48))self.tank_1 = self.tank.subsurface((48, 96), (48, 48))if self.rect.left < 3:self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)self.direction_x, self.direction_y = random.choice(([0, 1], [0, -1], [1, 0], [-1, 0]))is_move = Falseelif self.direction_x == 1 and self.direction_y == 0:self.tank_0 = self.tank.subsurface((0, 144), (48, 48))self.tank_1 = self.tank.subsurface((48, 144), (48, 48))if self.rect.right > 630 - 3:self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)self.direction_x, self.direction_y = random.choice(([0, 1], [0, -1], [1, 0], [-1, 0]))is_move = Falseelse:raise ValueError('enemyTank class -> direction value error.')if pygame.sprite.spritecollide(self, brickGroup, False, None) \or pygame.sprite.spritecollide(self, ironGroup, False, None) \or pygame.sprite.spritecollide(self, tankGroup, False, None):self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)self.direction_x, self.direction_y = random.choice(([0, 1], [0, -1], [1, 0], [-1, 0]))is_move = Falseif pygame.sprite.collide_rect(self, myhome):self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)self.direction_x, self.direction_y = random.choice(([0, 1], [0, -1], [1, 0], [-1, 0]))is_move = Falsereturn is_move# 重新载入坦克def reload(self):self.tank = pygame.image.load(self.tanks[self.kind][self.color]).convert_alpha()self.tank_0 = self.tank.subsurface((0, 48), (48, 48))self.tank_1 = self.tank.subsurface((48, 48), (48, 48))

完整代码压缩包:

由于代码内容过多就不一一展示放在文章当中了,如需完整项目请私信…

最终成功展示:

在这里插入图片描述

在这里插入图片描述

这篇关于【Python实现坦克大战,带你找回童年的快乐】附源码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/738510

相关文章

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

一文深入详解Python的secrets模块

《一文深入详解Python的secrets模块》在构建涉及用户身份认证、权限管理、加密通信等系统时,开发者最不能忽视的一个问题就是“安全性”,Python在3.6版本中引入了专门面向安全用途的secr... 目录引言一、背景与动机:为什么需要 secrets 模块?二、secrets 模块的核心功能1. 基

PostgreSQL中MVCC 机制的实现

《PostgreSQL中MVCC机制的实现》本文主要介绍了PostgreSQL中MVCC机制的实现,通过多版本数据存储、快照隔离和事务ID管理实现高并发读写,具有一定的参考价值,感兴趣的可以了解一下... 目录一 MVCC 基本原理python1.1 MVCC 核心概念1.2 与传统锁机制对比二 Postg

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

python常见环境管理工具超全解析

《python常见环境管理工具超全解析》在Python开发中,管理多个项目及其依赖项通常是一个挑战,下面:本文主要介绍python常见环境管理工具的相关资料,文中通过代码介绍的非常详细,需要的朋友... 目录1. conda2. pip3. uvuv 工具自动创建和管理环境的特点4. setup.py5.

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

C++高效内存池实现减少动态分配开销的解决方案

《C++高效内存池实现减少动态分配开销的解决方案》C++动态内存分配存在系统调用开销、碎片化和锁竞争等性能问题,内存池通过预分配、分块管理和缓存复用解决这些问题,下面就来了解一下... 目录一、C++内存分配的性能挑战二、内存池技术的核心原理三、主流内存池实现:TCMalloc与Jemalloc1. TCM

OpenCV实现实时颜色检测的示例

《OpenCV实现实时颜色检测的示例》本文主要介绍了OpenCV实现实时颜色检测的示例,通过HSV色彩空间转换和色调范围判断实现红黄绿蓝颜色检测,包含视频捕捉、区域标记、颜色分析等功能,具有一定的参考... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间

Python UV安装、升级、卸载详细步骤记录

《PythonUV安装、升级、卸载详细步骤记录》:本文主要介绍PythonUV安装、升级、卸载的详细步骤,uv是Astral推出的下一代Python包与项目管理器,主打单一可执行文件、极致性能... 目录安装检查升级设置自动补全卸载UV 命令总结 官方文档详见:https://docs.astral.sh/