Pygame坦克大战游戏开发实战详解代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Pygame坦克⼤战游戏开发实战详解代码
导语
哈喽!哈喽——我是⽊⽊⼦
今天来升级下之前写的坦克⼤战游戏嘛,哈哈哈其实也不算是修改,就是稍微的调试⼀下!
因为之前写的界⾯都是英⽂的,有的⼩伙伴⼉英⽂⼀点⼉都不会的可能看着别扭,今天来⼀款中
⽂版的给⼤家嘛!
俗话说的好:“⾬露均沾”。
哈哈哈.jpg
⼩简介:
《坦克⼤战》,1985年由⽇本开发商南梦宫(Namco)开发,是第⼀款可以双打的红⽩机游戏。
当时使⽤的还是⼩霸王。
很多⼩朋友以学习的名义买了以后偷偷打的打游戏还被家长发现了有没得!
《坦克⼤战》红⽩机原版共有35关,每⼀关的地形和障碍都不同。
图为原版坦克⼤战最后⼀关,你
有没有打通关过?(⼩声bb,我这个游戏⽔平可能达不到!)
正⽂
1)游戏规则:
游戏过程是这样的,玩家操作坦克消灭电脑控制的坦克,并保护⾃⼰基地。
基地图标是⼀只傲娇的张着翅膀的⽼鹰。
⼩时候⾃⼰失⼿把飞鹰轰成烧鸡的惨案经常发⽣。
双打的时候,为了看谁刷得分⾼,都争着打坦克,⼤本营的⽼鹰被烤熟了都不管。
坦克⼤战中的宝贝有战车、星星、时钟等,⼩编当时最喜欢的是时钟,敌不能动我能动的感觉妙极了。
图中的坦克图标吃了是可以加⼀条命的,当时为了抢宝贝都抢先把队友的坦克打晕。
2)环境安装
Python3、Pycharm、Pygame、以及⾃带或⾃定义的模块。
pip install +模块名或pip install -i https:///simple/ +模块名
3)代码演⽰
(之前不是写过的嘛,今天的话就是修改下的,这种⼩游戏代码肯定都很多的,所以这⾥直接贴主程序了。
需要完整的打包好的代码跟素材哪些的话直接滴滴我即可或者看我主页左侧哪⾥有源码基地的哈!)
主程序:
import pygame
from pygame.locals import *
import sys
import scene
import bullet
import food
import tanks
import home
# 开始界⾯显⽰
def show_start_interface(screen, width, height):
tfont = pygame.font.Font('./font/simkai.ttf', width // 4)
cfont = pygame.font.Font('./font/simkai.ttf', width // 20)
title = tfont.render(u'坦克⼤战', True, (255, 0, 0))
content1 = cfont.render(u'按1键进⼊单⼈游戏', True, (0, 244, 222))
content2 = cfont.render(u'按2键进⼊双⼈⼈游戏', True, (0, 0, 255))
#显⽰字体pygame.font.Font.render(text⽂本, antialias是否抗锯齿, color颜⾊, background=None)
trect = title.get_rect()
# 默认title左上⾓的坐标是 (0, 0)
trect.midtop = (width / 2, height / 5)
crect1 = content1.get_rect()
crect1.midtop = (width / 2, height / 1.8)
crect2 = content2.get_rect()
crect2.midtop = (width / 2, height / 1.6)
screen.blit(title, trect)
screen.blit(content1, crect1)
screen.blit(content2, crect2)
# 在指定位置绘制指定⽂字对象
pygame.display.update()
# 更新界⾯
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
return 1
if event.key == pygame.K_2:
return 2
# 结束界⾯显⽰
def show_end_interface(screen, width, height, is_win):
bg_img = pygame.image.load("./images/others/background.png") screen.blit(bg_img, (0, 0))
if is_win:
font = pygame.font.Font('./font/simkai.ttf', width // 10)
content = font.render(u'恭喜通关!', True, (255, 0, 0))
rect = content.get_rect()
rect.midtop = (width / 2, height / 2)
screen.blit(content, rect)
else:
fail_img = pygame.image.load("./images/others/gameover.png") rect = fail_img.get_rect()
rect.midtop = (width / 2, height / 2)
screen.blit(fail_img, rect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
# 关卡切换
def show_switch_stage(screen, width, height, stage):
bg_img = pygame.image.load("./images/others/background.png") screen.blit(bg_img, (0, 0))
font = pygame.font.Font('./font/simkai.ttf', width // 10)
content = font.render(u'第%d关' % stage, True, (0, 255, 0))
rect = content.get_rect()
rect.midtop = (width / 2, height / 2)
screen.blit(content, rect)
pygame.display.update()
delay_event = EREVENT
pygame.time.set_timer(delay_event, 1000)
#定时器延时
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
if event.type == delay_event:
return
def main():
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((630, 630))
pygame.display.set_caption('坦克⼤战')
bg_img = pygame.image.load('./images/others/background.png') # 加载⾳效
add_sound = pygame.mixer.Sound("./audios/add.wav")
add_sound.set_volume(1)
bang_sound = pygame.mixer.Sound("./audios/bang.wav")
bang_sound.set_volume(1)
blast_sound = pygame.mixer.Sound("./audios/blast.wav")
blast_sound.set_volume(1)
fire_sound = pygame.mixer.Sound("./audios/fire.wav")
fire_sound.set_volume(1)
Gunfire_sound = pygame.mixer.Sound("./audios/Gunfire.wav")
Gunfire_sound.set_volume(1)
hit_sound = pygame.mixer.Sound("./audios/hit.wav")
hit_sound.set_volume(1)
start_sound = pygame.mixer.Sound("./audios/start.wav")
start_sound.set_volume(1)
# 开始界⾯
num_player = show_start_interface(screen, 630, 630)
# 播放游戏开始的⾳乐
start_sound.play()
# 关卡
stage = 0
num_stage = 2
# 游戏是否结束
is_gameover = False
# 时钟
clock = pygame.time.Clock()
# 主循环
while not is_gameover:
# 关卡
stage += 1
if stage > num_stage:
break
show_switch_stage(screen, 630, 630, stage)
# 该关卡坦克总数量
enemytanks_total = min(stage * 18, 80)
# 场上存在的敌⽅坦克总数量
enemytanks_now = 0
# 场上可以存在的敌⽅坦克总数量
enemytanks_now_max = min(max(stage * 2, 4), 8)
# 精灵组,独⽴运⾏的动画组
tanksGroup = pygame.sprite.Group()
mytanksGroup = pygame.sprite.Group()
enemytanksGroup = pygame.sprite.Group()
bulletsGroup = pygame.sprite.Group()
mybulletsGroup = pygame.sprite.Group()
enemybulletsGroup = pygame.sprite.Group()
myfoodsGroup = pygame.sprite.Group()
# ⾃定义事件
# -⽣成敌⽅坦克事件
genEnemyEvent = EREVENT
pygame.time.set_timer(genEnemyEvent, 100)
# -敌⽅坦克静⽌恢复事件
recoverEnemyEvent = EREVENT
pygame.time.set_timer(recoverEnemyEvent, 8000)
# -我⽅坦克⽆敌恢复事件
noprotectMytankEvent = EREVENT
pygame.time.set_timer(noprotectMytankEvent, 8000)
# 关卡地图
map_stage = scene.Map(stage)
# 我⽅坦克
tank_player1 = tanks.myTank(1)
tanksGroup.add(tank_player1)
mytanksGroup.add(tank_player1)
if num_player > 1:
tank_player2 = tanks.myTank(2)
tanksGroup.add(tank_player2)
mytanksGroup.add(tank_player2)
is_switch_tank = True
player1_moving = False
player2_moving = False
# 为了轮胎的动画效果
time = 0
# 敌⽅坦克
for i in range(0, 3):
if enemytanks_total > 0:
enemytank = tanks.enemyTank(i)
tanksGroup.add(enemytank)
enemytanksGroup.add(enemytank)
enemytanks_now += 1
enemytanks_total -= 1
# ⼤本营
myhome = home.Home()
# 出场特效
appearance_img = pygame.image.load("./images/others/appear.png").convert_alpha() appearances = []
appearances.append(appearance_img.subsurface((0, 0), (48, 48)))
appearances.append(appearance_img.subsurface((48, 0), (48, 48)))
appearances.append(appearance_img.subsurface((96, 0), (48, 48)))
# 关卡主循环
while True:
if is_gameover is True:
break
if enemytanks_total < 1 and enemytanks_now < 1:
is_gameover = False
break
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == genEnemyEvent:
if enemytanks_total > 0:
if enemytanks_now < enemytanks_now_max:
enemytank = tanks.enemyTank()
if not pygame.sprite.spritecollide(enemytank, tanksGroup, False, None):
tanksGroup.add(enemytank)
enemytanksGroup.add(enemytank)
enemytanks_now += 1
enemytanks_total -= 1
if event.type == recoverEnemyEvent:
for each in enemytanksGroup:
each.can_move = True
if event.type == noprotectMytankEvent:
for each in mytanksGroup:
mytanksGroup.protected = False
# 检查⽤户键盘操作
key_pressed = pygame.key.get_pressed()
# 玩家⼀
# WSAD -> 上下左右
# 空格键射击
if key_pressed[pygame.K_w]:
tanksGroup.remove(tank_player1)
tank_player1.move_up(tanksGroup, map_stage.brickGroup, map_stage.ironGroup, myhome)
tanksGroup.add(tank_player1)
player1_moving = True
elif key_pressed[pygame.K_s]:
tanksGroup.remove(tank_player1)
tank_player1.move_down(tanksGroup, map_stage.brickGroup, map_stage.ironGroup, myhome) tanksGroup.add(tank_player1)
player1_moving = True
elif key_pressed[pygame.K_a]:
tanksGroup.remove(tank_player1)
tank_player1.move_left(tanksGroup, map_stage.brickGroup, map_stage.ironGroup, myhome)
tanksGroup.add(tank_player1)
player1_moving = True
elif key_pressed[pygame.K_d]:
tanksGroup.remove(tank_player1)
tank_player1.move_right(tanksGroup, map_stage.brickGroup, map_stage.ironGroup, myhome)
tanksGroup.add(tank_player1)
player1_moving = True
elif key_pressed[pygame.K_SPACE]:
if not tank_player1.bullet.being:
fire_sound.play()
tank_player1.shoot()
# 玩家⼆
# ↑↓←→ -> 上下左右
# ⼩键盘0键射击
if num_player > 1:
if key_pressed[pygame.K_UP]:
tanksGroup.remove(tank_player2)
tank_player2.move_up(tanksGroup, map_stage.brickGroup, map_stage.ironGroup, myhome) tanksGroup.add(tank_player2)
player2_moving = True
elif key_pressed[pygame.K_DOWN]:
tanksGroup.remove(tank_player2)
tank_player2.move_down(tanksGroup, map_stage.brickGroup, map_stage.ironGroup, myhome) tanksGroup.add(tank_player2)
player2_moving = True
elif key_pressed[pygame.K_LEFT]:
tanksGroup.remove(tank_player2)
tank_player2.move_left(tanksGroup, map_stage.brickGroup, map_stage.ironGroup, myhome) tanksGroup.add(tank_player2)
player2_moving = True
elif key_pressed[pygame.K_RIGHT]:
tanksGroup.remove(tank_player2)
tank_player2.move_right(tanksGroup, map_stage.brickGroup, map_stage.ironGroup, myhome) tanksGroup.add(tank_player2)
player2_moving = True
elif key_pressed[pygame.K_KP0]:
if not tank_player2.bullet.being:
fire_sound.play()
tank_player2.shoot()
# 背景
screen.blit(bg_img, (0, 0))
# ⽯头墙
for each in map_stage.brickGroup:
screen.blit(each.brick, each.rect)
# 钢墙
for each in map_stage.ironGroup:
screen.blit(each.iron, each.rect)
# 冰
for each in map_stage.iceGroup:
screen.blit(each.ice, each.rect)
# 河流
for each in map_stage.riverGroup:
screen.blit(each.river, each.rect)
# 树
for each in map_stage.treeGroup:
screen.blit(each.tree, each.rect)
time += 1
if time == 5:
time = 0
is_switch_tank = not is_switch_tank
# 我⽅坦克
if tank_player1 in mytanksGroup:
if is_switch_tank and player1_moving:
screen.blit(tank_player1.tank_0, (tank_player1.rect.left, tank_player1.rect.top))
player1_moving = False
else:
screen.blit(tank_player1.tank_1, (tank_player1.rect.left, tank_player1.rect.top))
if tank_player1.protected:
screen.blit(tank_player1.protected_mask1, (tank_player1.rect.left, tank_player1.rect.top))
if num_player > 1:
if tank_player2 in mytanksGroup:
if is_switch_tank and player2_moving:
screen.blit(tank_player2.tank_0, (tank_player2.rect.left, tank_player2.rect.top))
player1_moving = False
else:
screen.blit(tank_player2.tank_1, (tank_player2.rect.left, tank_player2.rect.top))
if tank_player2.protected:
screen.blit(tank_player1.protected_mask1, (tank_player2.rect.left, tank_player2.rect.top)) # 敌⽅坦克
for each in enemytanksGroup:
# 出⽣特效
if each.born:
if each.times > 0:
each.times -= 1
if each.times <= 10:
screen.blit(appearances[2], (3 + each.x * 12 * 24, 3))
elif each.times <= 20:
screen.blit(appearances[1], (3 + each.x * 12 * 24, 3))
elif each.times <= 30:
screen.blit(appearances[0], (3 + each.x * 12 * 24, 3))
elif each.times <= 40:
screen.blit(appearances[2], (3 + each.x * 12 * 24, 3))
elif each.times <= 50:
screen.blit(appearances[1], (3 + each.x * 12 * 24, 3))
elif each.times <= 60:
screen.blit(appearances[0], (3 + each.x * 12 * 24, 3))
elif each.times <= 70:
screen.blit(appearances[2], (3 + each.x * 12 * 24, 3))
elif each.times <= 80:
screen.blit(appearances[1], (3 + each.x * 12 * 24, 3))
elif each.times <= 90:
screen.blit(appearances[0], (3 + each.x * 12 * 24, 3))
else:
each.born = False
else:
if is_switch_tank:
screen.blit(each.tank_0, (each.rect.left, each.rect.top))
else:
screen.blit(each.tank_1, (each.rect.left, each.rect.top))
if each.can_move:
tanksGroup.remove(each)
each.move(tanksGroup, map_stage.brickGroup, map_stage.ironGroup, myhome)
tanksGroup.add(each)
# 我⽅⼦弹
for tank_player in mytanksGroup:
if tank_player.bullet.being:
tank_player.bullet.move()
screen.blit(tank_player.bullet.bullet, tank_player.bullet.rect)
# ⼦弹碰撞敌⽅⼦弹
for each in enemybulletsGroup:
if each.being:
if pygame.sprite.collide_rect(tank_player.bullet, each):
tank_player.bullet.being = False
each.being = False
enemybulletsGroup.remove(each)
break
else:
enemybulletsGroup.remove(each)
# ⼦弹碰撞敌⽅坦克
for each in enemytanksGroup:
if each.being:
if pygame.sprite.collide_rect(tank_player.bullet, each):
if each.is_red == True:
myfood = food.Food()
myfood.generate()
myfoodsGroup.add(myfood)
each.is_red = False
each.blood -= 1
each.color -= 1
if each.blood < 0:
bang_sound.play()
each.being = False
enemytanksGroup.remove(each)
enemytanks_now -= 1
tanksGroup.remove(each)
else:
each.reload()
tank_player.bullet.being = False
break
else:
enemytanksGroup.remove(each)
tanksGroup.remove(each)
# ⼦弹碰撞⽯头墙
if pygame.sprite.spritecollide(tank_player.bullet, map_stage.brickGroup, True, None): tank_player.bullet.being = False
# ⼦弹碰钢墙
if tank_player.bullet.stronger:
if pygame.sprite.spritecollide(tank_player.bullet, map_stage.ironGroup, True, None): tank_player.bullet.being = False
else:
if pygame.sprite.spritecollide(tank_player.bullet, map_stage.ironGroup, False, None): tank_player.bullet.being = False
# ⼦弹碰⼤本营
if pygame.sprite.collide_rect(tank_player.bullet, myhome):
tank_player.bullet.being = False
myhome.set_dead()
is_gameover = True
# 敌⽅⼦弹
for each in enemytanksGroup:
if each.being:
if each.can_move and not each.bullet.being:
enemybulletsGroup.remove(each.bullet)
each.shoot()
enemybulletsGroup.add(each.bullet)
if not each.born:
if each.bullet.being:
each.bullet.move()
screen.blit(each.bullet.bullet, each.bullet.rect)
# ⼦弹碰撞我⽅坦克
for tank_player in mytanksGroup:
if pygame.sprite.collide_rect(each.bullet, tank_player):
if not tank_player.protected:
bang_sound.play()
tank_player.life -= 1
if tank_player.life < 0:
mytanksGroup.remove(tank_player)
tanksGroup.remove(tank_player)
if len(mytanksGroup) < 1:
is_gameover = True
else:
tank_player.reset()
each.bullet.being = False
enemybulletsGroup.remove(each.bullet)
break
# ⼦弹碰撞⽯头墙
if pygame.sprite.spritecollide(each.bullet, map_stage.brickGroup, True, None): each.bullet.being = False
enemybulletsGroup.remove(each.bullet)
# ⼦弹碰钢墙
if each.bullet.stronger:
if pygame.sprite.spritecollide(each.bullet, map_stage.ironGroup, True, None): each.bullet.being = False
else:
if pygame.sprite.spritecollide(each.bullet, map_stage.ironGroup, False, None): each.bullet.being = False
# ⼦弹碰⼤本营
if pygame.sprite.collide_rect(each.bullet, myhome):
each.bullet.being = False
myhome.set_dead()
is_gameover = True
else:
enemytanksGroup.remove(each)
tanksGroup.remove(each)
# 家
screen.blit(myhome.home, myhome.rect)
# ⾷物
for myfood in myfoodsGroup:
if myfood.being and myfood.time > 0:
screen.blit(myfood.food, myfood.rect)
myfood.time -= 1
for tank_player in mytanksGroup:
if pygame.sprite.collide_rect(tank_player, myfood):
# 消灭当前所有敌⼈
if myfood.kind == 0:
for _ in enemytanksGroup:
bang_sound.play()
enemytanksGroup = pygame.sprite.Group()
enemytanks_total -= enemytanks_now
enemytanks_now = 0
# 敌⼈静⽌
if myfood.kind == 1:
for each in enemytanksGroup:
each.can_move = False
# ⼦弹增强
if myfood.kind == 2:
add_sound.play()
tank_player.bullet.stronger = True
# 使得⼤本营的墙变为钢板
if myfood.kind == 3:
map_stage.protect_home()
# 坦克获得⼀段时间的保护罩
if myfood.kind == 4:
add_sound.play()
for tank_player in mytanksGroup:
tank_player.protected = True
# 坦克升级
if myfood.kind == 5:
add_sound.play()
tank_player.up_level()
# 坦克⽣命+1
if myfood.kind == 6:
add_sound.play()
tank_player.life += 1
myfood.being = False
myfoodsGroup.remove(myfood)
break
else:
myfood.being = False
myfoodsGroup.remove(myfood)
pygame.display.flip()
clock.tick(60)
if not is_gameover:
show_end_interface(screen, 630, 630, True)
else:
show_end_interface(screen, 630, 630, False)
if __name__ == '__main__':
main()
4)效果展⽰
视频展⽰效果——
【Pygame实战】经典的坦克⼤战游戏,勾起童年⽆限回忆!静态截图效果——
游戏界⾯:
第⼀关单⼈游戏:
双⼈第⼀关游戏:
总结
好啦!中⽂版的坦克⼤战都看的懂了哈,想咋玩⼉咋玩⼉。
这是程序员的浪漫,这也是我们的浪漫。
到此这篇关于Pygame坦克⼤战游戏开发实战详解代码的⽂章就介绍到这了,更多相关Pygame 坦克⼤战内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!。