Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the Tank Battle Game of two-player version by python

Shulou Source: shulou.com Published: 2022-06-02 07:04:20 09月25日 Update

This article mainly explains "python how to achieve a two-player version of the tank war game", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "python how to achieve a two-player version of the tank war game" it!

Game introduction:

The basic rule of the two-player version of "Tank Battle" is that players destroy enemy tanks that appear to defend our base.

In the middle will also randomly appear a lot of special props absorption can obtain the corresponding function, eliminate play can enter the next level.

Key: move up and down and around. Another key is: WSAD.

Environment configuration:

Python3 、 Pycharm 、 Pygame .

Installation of third-party libraries: pip install pygame

Effect display:

Start the interface one by one

You can start playing games with background music! The game is more enjoyable to play!

Game interface--

Code demonstration:

1) Game main program

Import pygameimport sysimport tracebackimport wallimport myTankimport enemyTankimport food def main (): pygame.init () pygame.mixer.init () resolution = 630,630 screen = pygame.display.set_mode (resolution) pygame.display.set_caption ("Tank War") # load pictures, music, sound effects. Background_image = pygame.image.load (r "..\ image\ background.png") home_image = pygame.image.load (r "..\ image\ home.png") home_destroyed_image = pygame.image.load (r "..\ image\ home_destroyed.png") bang_sound = pygame.mixer.Sound (r "..\ music\ bang.wav") bang_sound.set_volume (1) fire_sound = pygame.mixer.Sound (r "..\ music\ Gunfire.wav") start_sound = pygame.mixer.Sound (r "..\ music\ start.wav") start_sound.play () # define the spirit group: tank Our tanks, enemy tanks. Enemy bullet allTankGroup = pygame.sprite.Group () mytankGroup = pygame.sprite.Group () allEnemyGroup = pygame.sprite.Group () redEnemyGroup = pygame.sprite.Group () greenEnemyGroup = pygame.sprite.Group () otherEnemyGroup = pygame.sprite.Group () enemyBulletGroup = pygame.sprite.Group () # create map bgMap = wall.Map () # create food / props but not show Show prop = food.Food () # create our tank myTank_T1 = myTank.MyTank (1) allTankGroup.add (myTank_T1) mytankGroup.add (myTank_T1) myTank_T2 = myTank.MyTank (2) allTankGroup.add (myTank_T2) mytankGroup.add (myTank_T2) # create enemy tank for i in range (1 4): enemy = enemyTank.EnemyTank (I) allTankGroup.add (enemy) allEnemyGroup.add (enemy) if enemy.isred = = True: redEnemyGroup.add (enemy) continue if enemy.kind = = 3: greenEnemyGroup.add (enemy) continue otherEnemyGroup.add (enemy) # Animation of enemy tanks appearance_image = pygame.image.load (r "..\ image\ appear.png") .convert_alpha () appearance = [] appearance.append (appearance_image.subsurface ((0)) 0), (48,48)) appearance.append (appearance_image.subsurface ((48,0), (48,48) appearance.append (appearance_image.subsurface ((96,0), (48,48) # Custom event # creation delay of enemy tanks 200DELAYEVENT = pygame.constants.USEREVENT pygame.time.set_timer (DELAYEVENT ) # create enemy bullet delay 1000 ENEMYBULLETNOTCOOLINGEVENT = pygame.constants.USEREVENT + 1 pygame.time.set_timer (ENEMYBULLETNOTCOOLINGEVENT, 1000) # create our bullet delay 200 MYBULLETNOTCOOLINGEVENT = pygame.constants.USEREVENT + 2 pygame.time.set_timer (MYBULLETNOTCOOLINGEVENT, 200) # enemy tank stationary 8000 NOTMOVEEVENT = pygame.constants.USEREVENT + 3 pygame.time.set_timer (NOTMOVEEVENT 8000) delay = 100moving = 0 movdir = 0 moving2 = 0 movdir2 = 0 enemyNumber = 3 enemyCouldMove = True switch_R1_R2_image = True homeSurvive = True running_T1 = True clock = pygame.time.Clock () while True: for event in pygame.event.get (): if event.type = = pygame.QUIT: Pygame.quit () sys.exit () # our bullet cooling event if event.type = = MYBULLETNOTCOOLINGEVENT: myTank_T1.bulletNotCooling = True # enemy bullet cooling event if event.type = = ENEMYBULLETNOTCOOLINGEVENT: For each in allEnemyGroup: each.bulletNotCooling = True # enemy tank static event if event.type = = NOTMOVEEVENT: enemyCouldMove = True # create enemy tank delay if event.type = = DELAYEVENT: if enemyNumber

< 4: enemy = enemyTank.EnemyTank() if pygame.sprite.spritecollide(enemy, allTankGroup, False, None): break allEnemyGroup.add(enemy) allTankGroup.add(enemy) enemyNumber += 1 if enemy.isred == True: redEnemyGroup.add(enemy) elif enemy.kind == 3: greenEnemyGroup.add(enemy) else: otherEnemyGroup.add(enemy) if event.type == pygame.KEYDOWN: if event.key == pygame.K_c and pygame.KMOD_CTRL: pygame.quit() sys.exit() if event.key == pygame.K_e: myTank_T1.levelUp() if event.key == pygame.K_q: myTank_T1.levelDown() if event.key == pygame.K_3: myTank_T1.levelUp() myTank_T1.levelUp() myTank_T1.level = 3 if event.key == pygame.K_2: if myTank_T1.speed == 3: myTank_T1.speed = 24 else: myTank_T1.speed = 3 if event.key == pygame.K_1: for x, y in [(11,23),(12,23),(13,23),(14,23),(11,24),(14,24),(11,25),(14,25)]: bgMap.brick = wall.Brick() bgMap.brick.rect.left, bgMap.brick.rect.top = 3 + x * 24, 3 + y * 24 bgMap.brickGroup.add(bgMap.brick) if event.key == pygame.K_4: for x, y in [(11,23),(12,23),(13,23),(14,23),(11,24),(14,24),(11,25),(14,25)]: bgMap.iron = wall.Iron() bgMap.iron.rect.left, bgMap.iron.rect.top = 3 + x * 24, 3 + y * 24 bgMap.ironGroup.add(bgMap.iron) # 检查用户的键盘操作 key_pressed = pygame.key.get_pressed() # 玩家一的移动操作 if moving: moving -= 1 if movdir == 0: allTankGroup.remove(myTank_T1) if myTank_T1.moveUp(allTankGroup, bgMap.brickGroup, bgMap.ironGroup): moving += 1 allTankGroup.add(myTank_T1) running_T1 = True if movdir == 1: allTankGroup.remove(myTank_T1) if myTank_T1.moveDown(allTankGroup, bgMap.brickGroup, bgMap.ironGroup): moving += 1 allTankGroup.add(myTank_T1) running_T1 = True if movdir == 2: allTankGroup.remove(myTank_T1) if myTank_T1.moveLeft(allTankGroup, bgMap.brickGroup, bgMap.ironGroup): moving += 1 allTankGroup.add(myTank_T1) running_T1 = True if movdir == 3: allTankGroup.remove(myTank_T1) if myTank_T1.moveRight(allTankGroup, bgMap.brickGroup, bgMap.ironGroup): moving += 1 allTankGroup.add(myTank_T1) running_T1 = True if not moving: if key_pressed[pygame.K_w]: moving = 7 movdir = 0 running_T1 = True allTankGroup.remove(myTank_T1) if myTank_T1.moveUp(allTankGroup, bgMap.brickGroup, bgMap.ironGroup): moving = 0 allTankGroup.add(myTank_T1) elif key_pressed[pygame.K_s]: moving = 7 movdir = 1 running_T1 = True allTankGroup.remove(myTank_T1) if myTank_T1.moveDown(allTankGroup, bgMap.brickGroup, bgMap.ironGroup): moving = 0 allTankGroup.add(myTank_T1) elif key_pressed[pygame.K_a]: moving = 7 movdir = 2 running_T1 = True allTankGroup.remove(myTank_T1) if myTank_T1.moveLeft(allTankGroup, bgMap.brickGroup, bgMap.ironGroup): moving = 0 allTankGroup.add(myTank_T1) elif key_pressed[pygame.K_d]: moving = 7 movdir = 3 running_T1 = True allTankGroup.remove(myTank_T1) if myTank_T1.moveRight(allTankGroup, bgMap.brickGroup, bgMap.ironGroup): moving = 0 allTankGroup.add(myTank_T1) if key_pressed[pygame.K_j]: if not myTank_T1.bullet.life and myTank_T1.bulletNotCooling: fire_sound.play() myTank_T1.shoot() myTank_T1.bulletNotCooling = False # 玩家二的移动操作 if moving2: moving2 -= 1 if movdir2 == 0: allTankGroup.remove(myTank_T2) myTank_T2.moveUp(allTankGroup, bgMap.brickGroup, bgMap.ironGroup) allTankGroup.add(myTank_T2) running_T2 = True if movdir2 == 1: allTankGroup.remove(myTank_T2) myTank_T2.moveDown(allTankGroup, bgMap.brickGroup, bgMap.ironGroup) allTankGroup.add(myTank_T2) running_T2 = True if movdir2 == 2: allTankGroup.remove(myTank_T2) myTank_T2.moveLeft(allTankGroup, bgMap.brickGroup, bgMap.ironGroup) allTankGroup.add(myTank_T2) running_T2 = True if movdir2 == 3: allTankGroup.remove(myTank_T2) myTank_T2.moveRight(allTankGroup, bgMap.brickGroup, bgMap.ironGroup) allTankGroup.add(myTank_T2) running_T2 = True if not moving2: if key_pressed[pygame.K_UP]: allTankGroup.remove(myTank_T2) myTank_T2.moveUp(allTankGroup, bgMap.brickGroup, bgMap.ironGroup) allTankGroup.add(myTank_T2) moving2 = 7 movdir2 = 0 running_T2 = True elif key_pressed[pygame.K_DOWN]: allTankGroup.remove(myTank_T2) myTank_T2.moveDown(allTankGroup, bgMap.brickGroup, bgMap.ironGroup) allTankGroup.add(myTank_T2) moving2 = 7 movdir2 = 1 running_T2 = True elif key_pressed[pygame.K_LEFT]: allTankGroup.remove(myTank_T2) myTank_T2.moveLeft(allTankGroup, bgMap.brickGroup, bgMap.ironGroup) allTankGroup.add(myTank_T2) moving2 = 7 movdir2 = 2 running_T2 = True elif key_pressed[pygame.K_RIGHT]: allTankGroup.remove(myTank_T2) myTank_T2.moveRight(allTankGroup, bgMap.brickGroup, bgMap.ironGroup) allTankGroup.add(myTank_T2) moving2 = 7 movdir2 = 3 running_T2 = True if key_pressed[pygame.K_KP0]: if not myTank_T2.bullet.life: # fire_sound.play() myTank_T2.shoot() # 画背景 screen.blit(background_image, (0, 0)) # 画砖块 for each in bgMap.brickGroup: screen.blit(each.image, each.rect) # 花石头 for each in bgMap.ironGroup: screen.blit(each.image, each.rect) # 画home if homeSurvive: screen.blit(home_image, (3 + 12 * 24, 3 + 24 * 24)) else: screen.blit(home_destroyed_image, (3 + 12 * 24, 3 + 24 * 24)) # 画我方坦克1 if not (delay % 5): switch_R1_R2_image = not switch_R1_R2_image if switch_R1_R2_image and running_T1: screen.blit(myTank_T1.tank_R0, (myTank_T1.rect.left, myTank_T1.rect.top)) running_T1 = False else: screen.blit(myTank_T1.tank_R1, (myTank_T1.rect.left, myTank_T1.rect.top)) # 画我方坦克2 if switch_R1_R2_image and running_T2: screen.blit(myTank_T2.tank_R0, (myTank_T2.rect.left, myTank_T2.rect.top)) running_T2 = False else: screen.blit(myTank_T2.tank_R1, (myTank_T2.rect.left, myTank_T2.rect.top)) # 画敌方坦克 for each in allEnemyGroup: # 判断5毛钱特效是否播放 if each.flash: # 判断画左动作还是右动作 if switch_R1_R2_image: screen.blit(each.tank_R0, (each.rect.left, each.rect.top)) if enemyCouldMove: allTankGroup.remove(each) each.move(allTankGroup, bgMap.brickGroup, bgMap.ironGroup) allTankGroup.add(each) else: screen.blit(each.tank_R1, (each.rect.left, each.rect.top)) if enemyCouldMove: allTankGroup.remove(each) each.move(allTankGroup, bgMap.brickGroup, bgMap.ironGroup) allTankGroup.add(each) else: # 播放5毛钱特效 if each.times >

0: each.times-= 1 if each.times

Tags: Bullets tanks enemies collisions props delays wars events enemies maps life interfaces food movement stillness special content direction music cooling Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information MySQL Shulou Technology macOS Shulou Tech Info