def main(): pygame.init() screen = pygame.display.set_mode((630, 630)) pygame.display.set_caption("Tank War") background_image = pygame.image.load(r"../resources/img/background.png") while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() screen.blit(background_image, (0, 0)) #定义精灵组 myTankGroup = pygame.sprite.Group() #创建砖块 bgMap = wall.Map() for each in bgMap.brickGroup: screen.blit(each.image, each.rect) #创建坦克 myTank_T1 = myTank.MyTank() myTankGroup.add(myTank_T1) screen.blit(myTank_T1.tank_R0, (3, 3)) #屏幕刷新 pygame.display.flip()
def main(self): pygame.init() pygame.mixer.init() pygame.display.set_caption("坦克大战") # 背景 backgroundImage = pygame.image.load("image/background.png") self.startInterface(630, 630) # 游戏开始的时间 time1 = pygame.time.get_ticks() # 游戏主循环 while not self.isOver: self.stage += 1 if self.stage > self.totalStages: break if self.isDifficult: self.stage = 10 self.switchStage(630, 630) # 音效 gameMusic = music.Music() if self.closeMusic: gameMusic.closeMusic() gameMusic.start() #该关卡敌方坦克总数量 totalEnemyTanks = 19 + self.stage #场上存在的敌方坦克总数量 existEnemyTanks = 0 #场上可以存在的敌方坦克总数量 canExistEnemyTanks = min(max(self.stage * 2, 4), 8) #定义精灵组:坦克,我方坦克,敌方坦克,敌方子弹 self.allTankGroup = pygame.sprite.Group() self.mytankGroup = pygame.sprite.Group() self.allEnemyGroup = pygame.sprite.Group() self.mediumEnemyGroup = pygame.sprite.Group() #中型坦克 self.heavyEnemyGroup = pygame.sprite.Group() #重型坦克 self.lightEnemyGroup = pygame.sprite.Group() #轻型坦克 self.enemyBulletGroup = pygame.sprite.Group() #创建地图 gameMap = block.Map(self.stage) #创建我方坦克 myTank1 = myTank.MyTank(1) self.allTankGroup.add(myTank1) self.mytankGroup.add(myTank1) if self.playerNum != 1: myTank2 = myTank.MyTank(2) self.allTankGroup.add(myTank2) self.mytankGroup.add(myTank2) # 创建敌方坦克 for i in range(1, 4): if totalEnemyTanks > 0: totalEnemyTanks -= 1 existEnemyTanks += 1 enemy = enemyTank.EnemyTank(i) self.allTankGroup.add(enemy) self.allEnemyGroup.add(enemy) if enemy.kind == 2: self.mediumEnemyGroup.add(enemy) continue if enemy.kind == 3: self.heavyEnemyGroup.add(enemy) continue self.lightEnemyGroup.add(enemy) # 自定义事件 # 创建敌方坦克延迟200 DELAYEVENT = pygame.constants.USEREVENT pygame.time.set_timer(DELAYEVENT, 200) # 创建敌方子弹延迟1000 ENEMYBULLETNOTCOOLINGEVENT = pygame.constants.USEREVENT + 1 pygame.time.set_timer(ENEMYBULLETNOTCOOLINGEVENT, 1000) # 创建我方子弹延迟200 MYBULLETNOTCOOLINGEVENT = pygame.constants.USEREVENT + 2 pygame.time.set_timer(MYBULLETNOTCOOLINGEVENT, 200) clock = pygame.time.Clock() # 大本营 myhome = home.Home() # 关卡主循环 while True: if self.isOver: break if totalEnemyTanks < 1 and existEnemyTanks < 1: self.isOver = False break for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # 我方子弹冷却事件 if event.type == MYBULLETNOTCOOLINGEVENT: myTank1.bulletNotCooling = True if self.playerNum != 1: myTank2.bulletNotCooling = True # 敌方子弹冷却事件 if event.type == ENEMYBULLETNOTCOOLINGEVENT: for each in self.allEnemyGroup: each.bulletNotCooling = True # 创建敌方坦克 if event.type == DELAYEVENT: if totalEnemyTanks > 0: if existEnemyTanks < canExistEnemyTanks: enemy = enemyTank.EnemyTank() if pygame.sprite.spritecollide(enemy, self.allTankGroup, False, None): break self.allEnemyGroup.add(enemy) self.allTankGroup.add(enemy) existEnemyTanks += 1 if enemy.kind == 2: self.mediumEnemyGroup.add(enemy) elif enemy.kind == 3: self.heavyEnemyGroup.add(enemy) else: self.lightEnemyGroup.add(enemy) # 检查用户的键盘操作 key_pressed = pygame.key.get_pressed() # 玩家一的移动操作 myTank1.move1(self.allTankGroup, gameMap.brickGroup, gameMap.ironGroup) if key_pressed[pygame.K_SPACE]: if not myTank1.bullet.life and myTank1.bulletNotCooling: gameMusic.fire() myTank1.shoot() myTank1.bulletNotCooling = False # 玩家二的移动操作 if self.playerNum != 1: myTank2.move2(self.allTankGroup, gameMap.brickGroup, gameMap.ironGroup) if key_pressed[pygame.K_KP0]: if not myTank2.bullet.life and myTank2.bulletNotCooling: gameMusic.fire() myTank2.shoot() myTank2.bulletNotCooling = False # 实现各种场景 self.screen.blit(backgroundImage, (0, 0)) for each in gameMap.brickGroup: self.screen.blit(each.image, each.rect) for each in gameMap.ironGroup: self.screen.blit(each.image, each.rect) for each in gameMap.grassGroup: self.screen.blit(each.image, each.rect) self.screen.blit(myhome.home, myhome.rect) # 我方坦克1 print(myTank1.life) if myTank1.life > 0: self.screen.blit(myTank1.tank_R0, (myTank1.rect.left, myTank1.rect.top)) # 我方坦克2 if self.playerNum != 1: if myTank2.life > 0: self.screen.blit(myTank2.tank_R0, (myTank2.rect.left, myTank2.rect.top)) # 敌方坦克 enemy.cartoon() enemy.creatImage(self.allEnemyGroup, self.allTankGroup, self.screen, gameMap.brickGroup, gameMap.ironGroup) # 我方坦克1子弹 if myTank1.createBulletImage(self.screen, self.enemyBulletGroup, self.heavyEnemyGroup, self.mediumEnemyGroup, self.lightEnemyGroup, gameMap.brickGroup, gameMap.ironGroup, myhome, gameMusic) == -1: existEnemyTanks -= 1 totalEnemyTanks -= 1 # 我方坦克2子弹 if self.playerNum != 1: if myTank2.createBulletImage(self.screen, self.enemyBulletGroup, self.heavyEnemyGroup, self.mediumEnemyGroup, self.lightEnemyGroup, gameMap.brickGroup, gameMap.ironGroup, myhome, gameMusic) == -1: existEnemyTanks -= 1 totalEnemyTanks -= 1 # 绘制敌人子弹 if self.playerNum == 1: enemy.createBulletImage(self.screen, self.allEnemyGroup, self.enemyBulletGroup, myTank1, gameMusic, self.playerNum, gameMap, myhome) if myTank1.life == 0 or myhome.alive == False: self.isOver = True if self.playerNum != 1: enemy.createBulletImage(self.screen, self.allEnemyGroup, self.enemyBulletGroup, myTank1, gameMusic, self.playerNum, gameMap, myhome, myTank2) if myTank1.life == 0 and myTank2.life == 0: self.isOver = True if myhome.alive == False: self.isOver = True pygame.display.flip() clock.tick(60) time2 = pygame.time.get_ticks() self.time = time2 - time1 if not self.isOver: self.endInterface(630, 630, True, self.time) else: self.endInterface(630, 630, False, self.time)
def main(): pygame.init() pygame.mixer.init() resolution = 630, 630 screen = pygame.display.set_mode(resolution) pygame.display.set_caption("Tank War ") # 加载图片,音乐,音效. 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() # 定义精灵组:坦克,我方坦克,敌方坦克,敌方子弹 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() # 创建地图 bgMap = wall.Map() # 创建食物/道具 但不显示 prop = food.Food() # 创建我方坦克 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) # 创建敌方 坦克 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) # 敌军坦克出现动画 appearance_image = pygame.image.load(r"..\image\appear.png") 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))) # 自定义事件 # 创建敌方坦克延迟200 DELAYEVENT = pygame.constants.USEREVENT pygame.time.set_timer(DELAYEVENT, 200) # 创建 敌方 子弹延迟1000 ENEMYBULLETNOTCOOLINGEVENT = pygame.constants.USEREVENT + 1 pygame.time.set_timer(ENEMYBULLETNOTCOOLINGEVENT, 1000) # 创建 我方 子弹延迟200 MYBULLETNOTCOOLINGEVENT = pygame.constants.USEREVENT + 2 pygame.time.set_timer(MYBULLETNOTCOOLINGEVENT, 200) # 敌方坦克 静止8000 NOTMOVEEVENT = pygame.constants.USEREVENT + 3 pygame.time.set_timer(NOTMOVEEVENT, 8000) delay = 100 moving = 0 movdir = 0 moving2 = 0 movdir2 = 0 enemyNumber = 3 enemyCouldMove = True switch_R1_R2_image = True homeSurvive = True running_T1 = True running_T2 = True clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # 我方子弹冷却事件 if event.type == MYBULLETNOTCOOLINGEVENT: myTank_T1.bulletNotCooling = True # 敌方子弹冷却事件 if event.type == ENEMYBULLETNOTCOOLINGEVENT: for each in allEnemyGroup: each.bulletNotCooling = True # 敌方坦克静止事件 if event.type == NOTMOVEEVENT: enemyCouldMove = True # 创建敌方坦克延迟 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: #ctrl+c退出游戏的快捷键 if event.key == pygame.K_c and pygame.KMOD_CTRL: pygame.quit() sys.exit() #按下E键,坦克1升级 if event.key == pygame.K_e: myTank_T1.levelUp() #按下Q键,坦克1降级 if event.key == pygame.K_q: myTank_T1.levelDown() #按下3键,升至3级 if event.key == pygame.K_3: myTank_T1.levelUp() myTank_T1.levelUp() myTank_T1.level = 3 # print(myTank_T1.level) #按下2键,改变坦克1速度 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 <= 10: screen.blit(appearance[2], (3 + each.x * 12 * 24, 3)) elif each.times <= 20: screen.blit(appearance[1], (3 + each.x * 12 * 24, 3)) elif each.times <= 30: screen.blit(appearance[0], (3 + each.x * 12 * 24, 3)) elif each.times <= 40: screen.blit(appearance[2], (3 + each.x * 12 * 24, 3)) elif each.times <= 50: screen.blit(appearance[1], (3 + each.x * 12 * 24, 3)) elif each.times <= 60: screen.blit(appearance[0], (3 + each.x * 12 * 24, 3)) elif each.times <= 70: screen.blit(appearance[2], (3 + each.x * 12 * 24, 3)) elif each.times <= 80: screen.blit(appearance[1], (3 + each.x * 12 * 24, 3)) elif each.times <= 90: screen.blit(appearance[0], (3 + each.x * 12 * 24, 3)) if each.times == 0: each.flash = True # 绘制我方子弹1 if myTank_T1.bullet.life: myTank_T1.bullet.move() screen.blit(myTank_T1.bullet.bullet, myTank_T1.bullet.rect) # 子弹 碰撞 子弹 for each in enemyBulletGroup: if each.life: if pygame.sprite.collide_rect(myTank_T1.bullet, each): myTank_T1.bullet.life = False each.life = False pygame.sprite.spritecollide(myTank_T1.bullet, enemyBulletGroup, True, None) # 子弹 碰撞 敌方坦克 if pygame.sprite.spritecollide(myTank_T1.bullet, redEnemyGroup, True, None): prop.change() bang_sound.play() enemyNumber -= 1 myTank_T1.bullet.life = False elif pygame.sprite.spritecollide(myTank_T1.bullet,greenEnemyGroup, False, None): for each in greenEnemyGroup: if pygame.sprite.collide_rect(myTank_T1.bullet, each): if each.life == 1: pygame.sprite.spritecollide(myTank_T1.bullet,greenEnemyGroup, True, None) bang_sound.play() enemyNumber -= 1 elif each.life == 2: each.life -= 1 each.tank = each.enemy_3_0 elif each.life == 3: each.life -= 1 each.tank = each.enemy_3_2 myTank_T1.bullet.life = False elif pygame.sprite.spritecollide(myTank_T1.bullet, otherEnemyGroup, True, None): bang_sound.play() enemyNumber -= 1 myTank_T1.bullet.life = False #if pygame.sprite.spritecollide(myTank_T1.bullet, allEnemyGroup, True, None): # bang_sound.play() # enemyNumber -= 1 # myTank_T1.bullet.life = False # 子弹 碰撞 brickGroup if pygame.sprite.spritecollide(myTank_T1.bullet, bgMap.brickGroup, True, None): myTank_T1.bullet.life = False myTank_T1.bullet.rect.left, myTank_T1.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24 # 子弹 碰撞 brickGroup if myTank_T1.bullet.strong: if pygame.sprite.spritecollide(myTank_T1.bullet, bgMap.ironGroup, True, None): myTank_T1.bullet.life = False myTank_T1.bullet.rect.left, myTank_T1.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24 else: if pygame.sprite.spritecollide(myTank_T1.bullet, bgMap.ironGroup, False, None): myTank_T1.bullet.life = False myTank_T1.bullet.rect.left, myTank_T1.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24 # 绘制我方子弹2 if myTank_T2.bullet.life: myTank_T2.bullet.move() screen.blit(myTank_T2.bullet.bullet, myTank_T2.bullet.rect) # 子弹 碰撞 敌方坦克 if pygame.sprite.spritecollide(myTank_T2.bullet, allEnemyGroup, True, None): bang_sound.play() enemyNumber -= 1 myTank_T2.bullet.life = False # 子弹 碰撞 brickGroup if pygame.sprite.spritecollide(myTank_T2.bullet, bgMap.brickGroup, True, None): myTank_T2.bullet.life = False myTank_T2.bullet.rect.left, myTank_T2.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24 # 子弹 碰撞 brickGroup if myTank_T2.bullet.strong: if pygame.sprite.spritecollide(myTank_T2.bullet, bgMap.ironGroup, True, None): myTank_T2.bullet.life = False myTank_T2.bullet.rect.left, myTank_T2.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24 else: if pygame.sprite.spritecollide(myTank_T2.bullet, bgMap.ironGroup, False, None): myTank_T2.bullet.life = False myTank_T2.bullet.rect.left, myTank_T2.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24 # 绘制敌人子弹 for each in allEnemyGroup: # 如果子弹没有生命,则赋予子弹生命 if not each.bullet.life and each.bulletNotCooling and enemyCouldMove: enemyBulletGroup.remove(each.bullet) each.shoot() enemyBulletGroup.add(each.bullet) each.bulletNotCooling = False # 如果5毛钱特效播放完毕 并且 子弹存活 则绘制敌方子弹 if each.flash: if each.bullet.life: # 如果敌人可以移动 if enemyCouldMove: each.bullet.move() screen.blit(each.bullet.bullet, each.bullet.rect) # 子弹 碰撞 我方坦克 if pygame.sprite.collide_rect(each.bullet, myTank_T1): bang_sound.play() myTank_T1.rect.left, myTank_T1.rect.top = 3 + 8 * 24, 3 + 24 * 24 each.bullet.life = False moving = 0 # 重置移动控制参数 for i in range(myTank_T1.level+1): myTank_T1.levelDown() if pygame.sprite.collide_rect(each.bullet, myTank_T2): bang_sound.play() myTank_T2.rect.left, myTank_T2.rect.top = 3 + 16 * 24, 3 + 24 * 24 each.bullet.life = False # 子弹 碰撞 brickGroup if pygame.sprite.spritecollide(each.bullet, bgMap.brickGroup, True, None): each.bullet.life = False # 子弹 碰撞 ironGroup if each.bullet.strong: if pygame.sprite.spritecollide(each.bullet, bgMap.ironGroup, True, None): each.bullet.life = False else: if pygame.sprite.spritecollide(each.bullet, bgMap.ironGroup, False, None): each.bullet.life = False # 最后画食物/道具 if prop.life: screen.blit(prop.image, prop.rect) # 我方坦克碰撞 食物/道具 if pygame.sprite.collide_rect(myTank_T1, prop): if prop.kind == 1: # 敌人全毁 for each in allEnemyGroup: if pygame.sprite.spritecollide(each, allEnemyGroup, True, None): bang_sound.play() enemyNumber -= 1 prop.life = False if prop.kind == 2: # 敌人静止 enemyCouldMove = False prop.life = False if prop.kind == 3: # 子弹增强 myTank_T1.bullet.strong = True prop.life = False if prop.kind == 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) prop.life = False if prop.kind == 5: # 坦克无敌 prop.life = False pass if prop.kind == 6: # 坦克升级 myTank_T1.levelUp() prop.life = False if prop.kind == 7: # 坦克生命+1 myTank_T1.life += 1 prop.life = False # 延迟 delay -= 1 if not delay: delay = 100 pygame.display.flip() clock.tick(60)
def main(): # 初始化pygame pygame.init() # 初始化音频 pygame.mixer.init() # 设置窗口,定义窗口大小 resolution = 630, 630 screen = pygame.display.set_mode(resolution) # 设置窗口的名字 pygame.display.set_caption("Tank War ") # 加载图片,音乐,音效. 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") # 设置音量大小为1 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() # 定义精灵组:所有坦克,我方坦克,敌方坦克,敌方子弹 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() # 创建地图(也就是显示砖块和铁块),其实就是实例化wall.py中的Map方法 bgMap = wall.Map() # 创建食物/道具 但不显示 prop = food.Food() # 创建我方坦克,把坦克加入到相应组当中去 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) # 循环创建敌方 坦克,把坦克加入到相应组当中去 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) # 敌军坦克出现动画 appearance_image = pygame.image.load(r"..\image\appear.png") 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))) # 自定义事件 # 创建敌方坦克延迟200毫秒,创建一个然后技能冷却为false,当200毫秒触发这个延迟事件则设置为true # 比如pygame.constants.USEREVENT定义了一个用户事件,这事件是延时事件 # constants是一个常量,这里得到的是一个id,用户的id DELAYEVENT = pygame.constants.USEREVENT # 这个就像setInterval间隔函数,就是每200毫秒触发这个延迟事件,循环的200毫秒就为true,就等于设置了一个冷却时间 # pygame.time.set_timer在队列中循环创建一个事件 pygame.time.set_timer(DELAYEVENT, 200) # 创建 敌方 子弹延迟1000 ENEMYBULLETNOTCOOLINGEVENT = pygame.constants.USEREVENT + 1 pygame.time.set_timer(ENEMYBULLETNOTCOOLINGEVENT, 1000) # 创建 我方 子弹延迟200 MYBULLETNOTCOOLINGEVENT = pygame.constants.USEREVENT + 2 pygame.time.set_timer(MYBULLETNOTCOOLINGEVENT, 200) # 敌方坦克 静止8000 NOTMOVEEVENT = pygame.constants.USEREVENT + 3 pygame.time.set_timer(NOTMOVEEVENT, 8000) # 这都是我们需要定义的初始值 # # 这里定义delay用于下面计算判断真假是程序运行用的,初始值可修改 delay = 100 # 坦克1的移动的距离 moving = 0 # 坦克1移动的方向 movdir = 0 # 坦克2的移动的距离 moving2 = 0 # 坦克2移动的方向 movdir2 = 0 # 敌方坦克数量 enemyNumber = 3 # 敌军是否可以动 enemyCouldMove = True # 是否切换左右两张图 switch_R1_R2_image = True # 老家,核心区域是否还在 homeSurvive = True # 这两个坦克(t1,t2)是否能走动 running_T1 = True running_T2 = True # pygame.time.Clock()会控制每个循环多长时间运行一次 # 定义时间戳,方便我们计算时间 clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # 我方子弹冷却事件,这个就是上面的自定义事件 if event.type == MYBULLETNOTCOOLINGEVENT: myTank_T1.bulletNotCooling = True # 敌方子弹冷却事件 if event.type == ENEMYBULLETNOTCOOLINGEVENT: for each in allEnemyGroup: each.bulletNotCooling = True # 敌方坦克静止事件 if event.type == NOTMOVEEVENT: enemyCouldMove = True # 创建敌方坦克延迟,坦克生成冷却 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: #ctrl+c退出游戏的快捷键 if event.key == pygame.K_c and pygame.KMOD_CTRL: pygame.quit() sys.exit() #按下E键,坦克1升级 if event.key == pygame.K_e: myTank_T1.levelUp() #按下Q键,坦克1降级 if event.key == pygame.K_q: myTank_T1.levelDown() #按下3键,升至3级 if event.key == pygame.K_3: myTank_T1.levelUp() myTank_T1.levelUp() myTank_T1.level = 3 # print(myTank_T1.level) #按下2键,改变坦克1速度 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)) # 画砖块 # 也可以用pygame.sprite。Group中的update和draw方法实现这个渲染 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 <= 10: screen.blit(appearance[2], (3 + each.x * 12 * 24, 3)) elif each.times <= 20: screen.blit(appearance[1], (3 + each.x * 12 * 24, 3)) elif each.times <= 30: screen.blit(appearance[0], (3 + each.x * 12 * 24, 3)) elif each.times <= 40: screen.blit(appearance[2], (3 + each.x * 12 * 24, 3)) elif each.times <= 50: screen.blit(appearance[1], (3 + each.x * 12 * 24, 3)) elif each.times <= 60: screen.blit(appearance[0], (3 + each.x * 12 * 24, 3)) elif each.times <= 70: screen.blit(appearance[2], (3 + each.x * 12 * 24, 3)) elif each.times <= 80: screen.blit(appearance[1], (3 + each.x * 12 * 24, 3)) elif each.times <= 90: screen.blit(appearance[0], (3 + each.x * 12 * 24, 3)) if each.times == 0: each.flash = True # 绘制我方子弹1 if myTank_T1.bullet.life: myTank_T1.bullet.move() screen.blit(myTank_T1.bullet.bullet, myTank_T1.bullet.rect) # 子弹 碰撞 子弹 for each in enemyBulletGroup: if each.life: if pygame.sprite.collide_rect(myTank_T1.bullet, each): myTank_T1.bullet.life = False each.life = False pygame.sprite.spritecollide(myTank_T1.bullet, enemyBulletGroup, True, None) # 子弹 碰撞 敌方坦克 if pygame.sprite.spritecollide(myTank_T1.bullet, redEnemyGroup, True, None): prop.change() bang_sound.play() enemyNumber -= 1 myTank_T1.bullet.life = False elif pygame.sprite.spritecollide(myTank_T1.bullet, greenEnemyGroup, False, None): for each in greenEnemyGroup: if pygame.sprite.collide_rect(myTank_T1.bullet, each): if each.life == 1: pygame.sprite.spritecollide( myTank_T1.bullet, greenEnemyGroup, True, None) bang_sound.play() enemyNumber -= 1 elif each.life == 2: each.life -= 1 each.tank = each.enemy_3_0 elif each.life == 3: each.life -= 1 each.tank = each.enemy_3_2 myTank_T1.bullet.life = False elif pygame.sprite.spritecollide(myTank_T1.bullet, otherEnemyGroup, True, None): bang_sound.play() enemyNumber -= 1 myTank_T1.bullet.life = False #if pygame.sprite.spritecollide(myTank_T1.bullet, allEnemyGroup, True, None): # bang_sound.play() # enemyNumber -= 1 # myTank_T1.bullet.life = False # 子弹 碰撞 brickGroup if pygame.sprite.spritecollide(myTank_T1.bullet, bgMap.brickGroup, True, None): myTank_T1.bullet.life = False myTank_T1.bullet.rect.left, myTank_T1.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24 # 子弹 碰撞 brickGroup if myTank_T1.bullet.strong: if pygame.sprite.spritecollide(myTank_T1.bullet, bgMap.ironGroup, True, None): myTank_T1.bullet.life = False myTank_T1.bullet.rect.left, myTank_T1.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24 else: if pygame.sprite.spritecollide(myTank_T1.bullet, bgMap.ironGroup, False, None): myTank_T1.bullet.life = False myTank_T1.bullet.rect.left, myTank_T1.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24 # 绘制我方子弹2 if myTank_T2.bullet.life: myTank_T2.bullet.move() screen.blit(myTank_T2.bullet.bullet, myTank_T2.bullet.rect) # 子弹 碰撞 敌方坦克 if pygame.sprite.spritecollide(myTank_T2.bullet, allEnemyGroup, True, None): bang_sound.play() enemyNumber -= 1 myTank_T2.bullet.life = False # 子弹 碰撞 brickGroup if pygame.sprite.spritecollide(myTank_T2.bullet, bgMap.brickGroup, True, None): myTank_T2.bullet.life = False myTank_T2.bullet.rect.left, myTank_T2.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24 # 子弹 碰撞 brickGroup if myTank_T2.bullet.strong: if pygame.sprite.spritecollide(myTank_T2.bullet, bgMap.ironGroup, True, None): myTank_T2.bullet.life = False myTank_T2.bullet.rect.left, myTank_T2.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24 else: if pygame.sprite.spritecollide(myTank_T2.bullet, bgMap.ironGroup, False, None): myTank_T2.bullet.life = False myTank_T2.bullet.rect.left, myTank_T2.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24 # 绘制敌人子弹 for each in allEnemyGroup: # 如果子弹没有生命,则赋予子弹生命 if not each.bullet.life and each.bulletNotCooling and enemyCouldMove: enemyBulletGroup.remove(each.bullet) each.shoot() enemyBulletGroup.add(each.bullet) each.bulletNotCooling = False # 如果5毛钱特效播放完毕 并且 子弹存活 则绘制敌方子弹 if each.flash: if each.bullet.life: # 如果敌人可以移动 if enemyCouldMove: each.bullet.move() screen.blit(each.bullet.bullet, each.bullet.rect) # 子弹 碰撞 我方坦克 if pygame.sprite.collide_rect(each.bullet, myTank_T1): bang_sound.play() myTank_T1.rect.left, myTank_T1.rect.top = 3 + 8 * 24, 3 + 24 * 24 each.bullet.life = False moving = 0 # 重置移动控制参数 for i in range(myTank_T1.level + 1): myTank_T1.levelDown() if pygame.sprite.collide_rect(each.bullet, myTank_T2): bang_sound.play() myTank_T2.rect.left, myTank_T2.rect.top = 3 + 16 * 24, 3 + 24 * 24 each.bullet.life = False # 子弹 碰撞 brickGroup if pygame.sprite.spritecollide(each.bullet, bgMap.brickGroup, True, None): each.bullet.life = False # 子弹 碰撞 ironGroup if each.bullet.strong: if pygame.sprite.spritecollide(each.bullet, bgMap.ironGroup, True, None): each.bullet.life = False else: if pygame.sprite.spritecollide(each.bullet, bgMap.ironGroup, False, None): each.bullet.life = False # 最后画食物/道具 if prop.life: screen.blit(prop.image, prop.rect) # 我方坦克碰撞 食物/道具 if pygame.sprite.collide_rect(myTank_T1, prop): if prop.kind == 1: # 敌人全毁 for each in allEnemyGroup: if pygame.sprite.spritecollide(each, allEnemyGroup, True, None): bang_sound.play() enemyNumber -= 1 prop.life = False if prop.kind == 2: # 敌人静止 enemyCouldMove = False prop.life = False if prop.kind == 3: # 子弹增强 myTank_T1.bullet.strong = True prop.life = False if prop.kind == 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) prop.life = False if prop.kind == 5: # 坦克无敌 prop.life = False pass if prop.kind == 6: # 坦克升级 myTank_T1.levelUp() prop.life = False if prop.kind == 7: # 坦克生命+1 myTank_T1.life += 1 prop.life = False # 延迟 delay -= 1 if not delay: delay = 100 pygame.display.flip() clock.tick(60)