Python坦克大战源代码
生活随笔
收集整理的這篇文章主要介紹了
Python坦克大战源代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Python坦克大戰小游戲源程序,可實現單人及雙人模式,程序入口:Game5.py,源代碼程序較大,完整程序:Python坦克大戰源代碼。先上程序截圖:
''' 更多Python源代碼,請微信關注:Python代碼大全 ''' import sys import pygame import scene import bullet import food import tanks import home from pygame.locals import *# 開始界面顯示 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, 0, 255))content2 = cfont.render(u'按2鍵進入雙人人游戲', True, (0, 0, 255))trect = title.get_rect()trect.midtop = (width/2, height/4)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 1if event.key == pygame.K_2:return 2# 結束界面顯示 def show_end_interface(screen, width, height, is_win):bg_img = pygame.image.load("./圖片/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("./圖片/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("./圖片/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 = pygame.constants.USEREVENTpygame.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()screen = pygame.display.set_mode((630, 630))pygame.display.set_caption("坦克大戰")# 加載圖片bg_img = pygame.image.load("./圖片/others/background.png")# 開始界面num_player = show_start_interface(screen, 630, 630)# 關卡stage = 0num_stage = 2# 游戲是否結束is_gameover = False# 時鐘clock = pygame.time.Clock()# 主循環while not is_gameover:# 關卡stage += 1if stage > num_stage:breakshow_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 = pygame.constants.USEREVENT + 0pygame.time.set_timer(genEnemyEvent, 100)# -敵方坦克靜止恢復事件recoverEnemyEvent = pygame.constants.USEREVENT + 1pygame.time.set_timer(recoverEnemyEvent, 8000)# -我方坦克無敵恢復事件noprotectMytankEvent = pygame.constants.USEREVENT + 2pygame.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 = Trueplayer1_moving = Falseplayer2_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 += 1enemytanks_total -= 1# 大本營myhome = home.Home()# 出場特效appearance_img = pygame.image.load("./圖片/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:breakif enemytanks_total < 1 and enemytanks_now < 1:is_gameover = Falsebreakfor 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 += 1enemytanks_total -= 1if event.type == recoverEnemyEvent:for each in enemytanksGroup:each.can_move = Trueif 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 = Trueelif 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 = Trueelif 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 = Trueelif 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 = Trueelif key_pressed[pygame.K_SPACE]:if not tank_player1.bullet.being: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 = Trueelif 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 = Trueelif 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 = Trueelif 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 = Trueelif key_pressed[pygame.K_KP0]:if not tank_player2.bullet.being: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 += 1if time == 5:time = 0is_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 = Falseelse: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 = Falseelse: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 -= 1if 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 = Falseelse: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 = Falseeach.being = FalseenemybulletsGroup.remove(each)breakelse: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 = Falseeach.blood -= 1each.color -= 1if each.blood < 0:each.being = FalseenemytanksGroup.remove(each)enemytanks_now -= 1tanksGroup.remove(each)else:each.reload()tank_player.bullet.being = Falsebreakelse:enemytanksGroup.remove(each)tanksGroup.remove(each)# 子彈碰撞石頭墻if pygame.sprite.spritecollide(tank_player.bullet, map_stage.brickGroup, True, None):tank_player.bullet.being = False'''# 等價方案(更科學點)for each in map_stage.brickGroup:if pygame.sprite.collide_rect(tank_player.bullet, each):tank_player.bullet.being = Falseeach.being = Falsemap_stage.brickGroup.remove(each)break'''# 子彈碰鋼墻if tank_player.bullet.stronger:if pygame.sprite.spritecollide(tank_player.bullet, map_stage.ironGroup, True, None):tank_player.bullet.being = Falseelse:if pygame.sprite.spritecollide(tank_player.bullet, map_stage.ironGroup, False, None):tank_player.bullet.being = False'''# 等價方案(更科學點)for each in map_stage.ironGroup:if pygame.sprite.collide_rect(tank_player.bullet, each):tank_player.bullet.being = Falseif tank_player.bullet.stronger:each.being = Falsemap_stage.ironGroup.remove(each)break'''# 子彈碰大本營if pygame.sprite.collide_rect(tank_player.bullet, myhome):tank_player.bullet.being = Falsemyhome.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:tank_player.life -= 1if tank_player.life < 0:mytanksGroup.remove(tank_player)tanksGroup.remove(tank_player)if len(mytanksGroup) < 1:is_gameover = Trueelse:tank_player.reset()each.bullet.being = FalseenemybulletsGroup.remove(each.bullet)break# 子彈碰撞石頭墻if pygame.sprite.spritecollide(each.bullet, map_stage.brickGroup, True, None):each.bullet.being = FalseenemybulletsGroup.remove(each.bullet)'''# 等價方案(更科學點)for one in map_stage.brickGroup:if pygame.sprite.collide_rect(each.bullet, one):each.bullet.being = Falseone.being = FalseenemybulletsGroup.remove(one)break'''# 子彈碰鋼墻if each.bullet.stronger:if pygame.sprite.spritecollide(each.bullet, map_stage.ironGroup, True, None):each.bullet.being = Falseelse:if pygame.sprite.spritecollide(each.bullet, map_stage.ironGroup, False, None):each.bullet.being = False'''# 等價方案(更科學點)for one in map_stage.ironGroup:if pygame.sprite.collide_rect(each.bullet, one):each.bullet.being = Falseif each.bullet.stronger:one.being = Falsemap_stage.ironGroup.remove(one)break'''# 子彈碰大本營if pygame.sprite.collide_rect(each.bullet, myhome):each.bullet.being = Falsemyhome.set_dead()is_gameover = Trueelse: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 -= 1for tank_player in mytanksGroup:if pygame.sprite.collide_rect(tank_player, myfood):# 消滅當前所有敵人if myfood.kind == 0:enemytanksGroup = pygame.sprite.Group()enemytanks_total -= enemytanks_nowenemytanks_now = 0# 敵人靜止if myfood.kind == 1:for each in enemytanksGroup:each.can_move = False# 子彈增強if myfood.kind == 2:tank_player.bullet.stronger = True# 使得大本營的墻變為鋼板if myfood.kind == 3:map_stage.protect_home()# 坦克獲得一段時間的保護罩if myfood.kind == 4:for tank_player in mytanksGroup:tank_player.protected = True# 坦克升級if myfood.kind == 5:tank_player.up_level()# 坦克生命+1if myfood.kind == 6:tank_player.life += 1myfood.being = FalsemyfoodsGroup.remove(myfood)breakelse:myfood.being = FalsemyfoodsGroup.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()更多Python程序源代碼,請微信關注:Python代碼大全
總結
以上是生活随笔為你收集整理的Python坦克大战源代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codeforces - 65D - H
- 下一篇: 教你配置支付宝应用网关和授权回调地址