基于Python3-Pygame的坦克大战小游戏
源码文件,注释齐全。仅限学习交流。
效果图:
文件结构:
源码:
main.py
1 # -*- coding: utf-8 -*- 2 3 import pygame 4 import sys 5 import traceback 6 import wall 7 import myTank 8 import enemyTank 9 import food 10 11 12 def main(): 13 pygame.init() 14 pygame.mixer.init() 15 16 resolution = 630, 630 17 screen = pygame.display.set_mode(resolution) 18 pygame.display.set_caption("Tank War ") 19 20 # 加载图片,音乐,音效. 21 background_image = pygame.image.load(r"..\image\background.png") 22 home_image = pygame.image.load(r"..\image\home.png") 23 home_destroyed_image = pygame.image.load(r"..\image\home_destroyed.png") 24 25 bang_sound = pygame.mixer.Sound(r"..\music\bang.wav") 26 bang_sound.set_volume(1) 27 fire_sound = pygame.mixer.Sound(r"..\music\Gunfire.wav") 28 start_sound = pygame.mixer.Sound(r"..\music\start.wav") 29 start_sound.play() 30 31 # 定义精灵组:坦克,我方坦克,敌方坦克,敌方子弹 32 allTankGroup = pygame.sprite.Group() 33 mytankGroup = pygame.sprite.Group() 34 allEnemyGroup = pygame.sprite.Group() 35 redEnemyGroup = pygame.sprite.Group() 36 greenEnemyGroup = pygame.sprite.Group() 37 otherEnemyGroup = pygame.sprite.Group() 38 enemyBulletGroup = pygame.sprite.Group() 39 # 创建地图 40 bgMap = wall.Map() 41 # 创建食物/道具 但不显示 42 prop = food.Food() 43 # 创建我方坦克 44 myTank_T1 = myTank.MyTank(1) 45 allTankGroup.add(myTank_T1) 46 mytankGroup.add(myTank_T1) 47 myTank_T2 = myTank.MyTank(2) 48 allTankGroup.add(myTank_T2) 49 mytankGroup.add(myTank_T2) 50 # 创建敌方 坦克 51 for i in range(1, 4): 52 enemy = enemyTank.EnemyTank(i) 53 allTankGroup.add(enemy) 54 allEnemyGroup.add(enemy) 55 if enemy.isred == True: 56 redEnemyGroup.add(enemy) 57 continue 58 if enemy.kind == 3: 59 greenEnemyGroup.add(enemy) 60 continue 61 otherEnemyGroup.add(enemy) 62 # 敌军坦克出现动画 63 appearance_image = pygame.image.load(r"..\image\appear.png").convert_alpha() 64 appearance = [] 65 appearance.append(appearance_image.subsurface(( 0, 0), (48, 48))) 66 appearance.append(appearance_image.subsurface((48, 0), (48, 48))) 67 appearance.append(appearance_image.subsurface((96, 0), (48, 48))) 68 69 70 71 72 # 自定义事件 73 # 创建敌方坦克延迟200 74 DELAYEVENT = pygame.constants.USEREVENT 75 pygame.time.set_timer(DELAYEVENT, 200) 76 # 创建 敌方 子弹延迟1000 77 ENEMYBULLETNOTCOOLINGEVENT = pygame.constants.USEREVENT + 1 78 pygame.time.set_timer(ENEMYBULLETNOTCOOLINGEVENT, 1000) 79 # 创建 我方 子弹延迟200 80 MYBULLETNOTCOOLINGEVENT = pygame.constants.USEREVENT + 2 81 pygame.time.set_timer(MYBULLETNOTCOOLINGEVENT, 200) 82 # 敌方坦克 静止8000 83 NOTMOVEEVENT = pygame.constants.USEREVENT + 3 84 pygame.time.set_timer(NOTMOVEEVENT, 8000) 85 86 87 delay = 100 88 moving = 0 89 movdir = 0 90 moving2 = 0 91 movdir2 = 0 92 enemyNumber = 3 93 enemyCouldMove = True 94 switch_R1_R2_image = True 95 homeSurvive = True 96 running_T1 = True 97 running_T2 = True 98 clock = pygame.time.Clock() 99 while True: 100 for event in pygame.event.get(): 101 if event.type == pygame.QUIT: 102 pygame.quit() 103 sys.exit() 104 105 # 我方子弹冷却事件 106 if event.type == MYBULLETNOTCOOLINGEVENT: 107 myTank_T1.bulletNotCooling = True 108 109 # 敌方子弹冷却事件 110 if event.type == ENEMYBULLETNOTCOOLINGEVENT: 111 for each in allEnemyGroup: 112 each.bulletNotCooling = True 113 114 # 敌方坦克静止事件 115 if event.type == NOTMOVEEVENT: 116 enemyCouldMove = True 117 118 # 创建敌方坦克延迟 119 if event.type == DELAYEVENT: 120 if enemyNumber < 4: 121 enemy = enemyTank.EnemyTank() 122 if pygame.sprite.spritecollide(enemy, allTankGroup, False, None): 123 break 124 allEnemyGroup.add(enemy) 125 allTankGroup.add(enemy) 126 enemyNumber += 1 127 if enemy.isred == True: 128 redEnemyGroup.add(enemy) 129 elif enemy.kind == 3: 130 greenEnemyGroup.add(enemy) 131 else: 132 otherEnemyGroup.add(enemy) 133 134 if event.type == pygame.KEYDOWN: 135 if event.key == pygame.K_c and pygame.KMOD_CTRL: 136 pygame.quit() 137 sys.exit() 138 139 if event.key == pygame.K_e: 140 myTank_T1.levelUp() 141 if event.key == pygame.K_q: 142 myTank_T1.levelDown() 143 if event.key == pygame.K_3: 144 myTank_T1.levelUp() 145 myTank_T1.levelUp() 146 myTank_T1.level = 3 147 if event.key == pygame.K_2: 148 if myTank_T1.speed == 3: 149 myTank_T1.speed = 24 150 else: 151 myTank_T1.speed = 3 152 if event.key == pygame.K_1: 153 for x, y in [(11,23),(12,23),(13,23),(14,23),(11,24),(14,24),(11,25),(14,25)]: 154 bgMap.brick = wall.Brick() 155 bgMap.brick.rect.left, bgMap.brick.rect.top = 3 + x * 24, 3 + y * 24 156 bgMap.brickGroup.add(bgMap.brick) 157 if event.key == pygame.K_4: 158 for x, y in [(11,23),(12,23),(13,23),(14,23),(11,24),(14,24),(11,25),(14,25)]: 159 bgMap.iron = wall.Iron() 160 bgMap.iron.rect.left, bgMap.iron.rect.top = 3 + x * 24, 3 + y * 24 161 bgMap.ironGroup.add(bgMap.iron) 162 163 164 165 # 检查用户的键盘操作 166 key_pressed = pygame.key.get_pressed() 167 # 玩家一的移动操作 168 if moving: 169 moving -= 1 170 if movdir == 0: 171 allTankGroup.remove(myTank_T1) 172 if myTank_T1.moveUp(allTankGroup, bgMap.brickGroup, bgMap.ironGroup): 173 moving += 1 174 allTankGroup.add(myTank_T1) 175 running_T1 = True 176 if movdir == 1: 177 allTankGroup.remove(myTank_T1) 178 if myTank_T1.moveDown(allTankGroup, bgMap.brickGroup, bgMap.ironGroup): 179 moving += 1 180 allTankGroup.add(myTank_T1) 181 running_T1 = True 182 if movdir == 2: 183 allTankGroup.remove(myTank_T1) 184 if myTank_T1.moveLeft(allTankGroup, bgMap.brickGroup, bgMap.ironGroup): 185 moving += 1 186 allTankGroup.add(myTank_T1) 187 running_T1 = True 188 if movdir == 3: 189 allTankGroup.remove(myTank_T1) 190 if myTank_T1.moveRight(allTankGroup, bgMap.brickGroup, bgMap.ironGroup): 191 moving += 1 192 allTankGroup.add(myTank_T1) 193 running_T1 = True 194 195 if not moving: 196 if key_pressed[pygame.K_w]: 197 moving = 7 198 movdir = 0 199 running_T1 = True 200 allTankGroup.remove(myTank_T1) 201 if myTank_T1.moveUp(allTankGroup, bgMap.brickGroup, bgMap.ironGroup): 202 moving = 0 203 allTankGroup.add(myTank_T1) 204 elif key_pressed[pygame.K_s]: 205 moving = 7 206 movdir = 1 207 running_T1 = True 208 allTankGroup.remove(myTank_T1) 209 if myTank_T1.moveDown(allTankGroup, bgMap.brickGroup, bgMap.ironGroup): 210 moving = 0 211 allTankGroup.add(myTank_T1) 212 elif key_pressed[pygame.K_a]: 213 moving = 7 214 movdir = 2 215 running_T1 = True 216 allTankGroup.remove(myTank_T1) 217 if myTank_T1.moveLeft(allTankGroup, bgMap.brickGroup, bgMap.ironGroup): 218 moving = 0 219 allTankGroup.add(myTank_T1) 220 elif key_pressed[pygame.K_d]: 221 moving = 7 222 movdir = 3 223 running_T1 = True 224 allTankGroup.remove(myTank_T1) 225 if myTank_T1.moveRight(allTankGroup, bgMap.brickGroup, bgMap.ironGroup): 226 moving = 0 227 allTankGroup.add(myTank_T1) 228 if key_pressed[pygame.K_j]: 229 if not myTank_T1.bullet.life and myTank_T1.bulletNotCooling: 230 fire_sound.play() 231 myTank_T1.shoot() 232 myTank_T1.bulletNotCooling = False 233 234 # 玩家二的移动操作 235 if moving2: 236 moving2 -= 1 237 if movdir2 == 0: 238 allTankGroup.remove(myTank_T2) 239 myTank_T2.moveUp(allTankGroup, bgMap.brickGroup, bgMap.ironGroup) 240 allTankGroup.add(myTank_T2) 241 running_T2 = True 242 if movdir2 == 1: 243 allTankGroup.remove(myTank_T2) 244 myTank_T2.moveDown(allTankGroup, bgMap.brickGroup, bgMap.ironGroup) 245 allTankGroup.add(myTank_T2) 246 running_T2 = True 247 if movdir2 == 2: 248 allTankGroup.remove(myTank_T2) 249 myTank_T2.moveLeft(allTankGroup, bgMap.brickGroup, bgMap.ironGroup) 250 allTankGroup.add(myTank_T2) 251 running_T2 = True 252 if movdir2 == 3: 253 allTankGroup.remove(myTank_T2) 254 myTank_T2.moveRight(allTankGroup, bgMap.brickGroup, bgMap.ironGroup) 255 allTankGroup.add(myTank_T2) 256 running_T2 = True 257 258 if not moving2: 259 if key_pressed[pygame.K_UP]: 260 allTankGroup.remove(myTank_T2) 261 myTank_T2.moveUp(allTankGroup, bgMap.brickGroup, bgMap.ironGroup) 262 allTankGroup.add(myTank_T2) 263 moving2 = 7 264 movdir2 = 0 265 running_T2 = True 266 elif key_pressed[pygame.K_DOWN]: 267 allTankGroup.remove(myTank_T2) 268 myTank_T2.moveDown(allTankGroup, bgMap.brickGroup, bgMap.ironGroup) 269 allTankGroup.add(myTank_T2) 270 moving2 = 7 271 movdir2 = 1 272 running_T2 = True 273 elif key_pressed[pygame.K_LEFT]: 274 allTankGroup.remove(myTank_T2) 275 myTank_T2.moveLeft(allTankGroup, bgMap.brickGroup, bgMap.ironGroup) 276 allTankGroup.add(myTank_T2) 277 moving2 = 7 278 movdir2 = 2 279 running_T2 = True 280 elif key_pressed[pygame.K_RIGHT]: 281 allTankGroup.remove(myTank_T2) 282 myTank_T2.moveRight(allTankGroup, bgMap.brickGroup, bgMap.ironGroup) 283 allTankGroup.add(myTank_T2) 284 moving2 = 7 285 movdir2 = 3 286 running_T2 = True 287 if key_pressed[pygame.K_KP0]: 288 if not myTank_T2.bullet.life: 289 # fire_sound.play() 290 myTank_T2.shoot() 291 292 293 294 295 # 画背景 296 screen.blit(background_image, (0, 0)) 297 # 画砖块 298 for each in bgMap.brickGroup: 299 screen.blit(each.image, each.rect) 300 # 花石头 301 for each in bgMap.ironGroup: 302 screen.blit(each.image, each.rect) 303 # 画home 304 if homeSurvive: 305 screen.blit(home_image, (3 + 12 * 24, 3 + 24 * 24)) 306 else: 307 screen.blit(home_destroyed_image, (3 + 12 * 24, 3 + 24 * 24)) 308 # 画我方坦克1 309 if not (delay % 5): 310 switch_R1_R2_image = not switch_R1_R2_image 311 if switch_R1_R2_image and running_T1: 312 screen.blit(myTank_T1.tank_R0, (myTank_T1.rect.left, myTank_T1.rect.top)) 313 running_T1 = False 314 else: 315 screen.blit(myTank_T1.tank_R1, (myTank_T1.rect.left, myTank_T1.rect.top)) 316 # 画我方坦克2 317 if switch_R1_R2_image and running_T2: 318 screen.blit(myTank_T2.tank_R0, (myTank_T2.rect.left, myTank_T2.rect.top)) 319 running_T2 = False 320 else: 321 screen.blit(myTank_T2.tank_R1, (myTank_T2.rect.left, myTank_T2.rect.top)) 322 # 画敌方坦克 323 for each in allEnemyGroup: 324 # 判断5毛钱特效是否播放 325 if each.flash: 326 # 判断画左动作还是右动作 327 if switch_R1_R2_image: 328 screen.blit(each.tank_R0, (each.rect.left, each.rect.top)) 329 if enemyCouldMove: 330 allTankGroup.remove(each) 331 each.move(allTankGroup, bgMap.brickGroup, bgMap.ironGroup) 332 allTankGroup.add(each) 333 else: 334 screen.blit(each.tank_R1, (each.rect.left, each.rect.top)) 335 if enemyCouldMove: 336 allTankGroup.remove(each) 337 each.move(allTankGroup, bgMap.brickGroup, bgMap.ironGroup) 338 allTankGroup.add(each) 339 else: 340 # 播放5毛钱特效 341 if each.times > 0: 342 each.times -= 1 343 if each.times <= 10: 344 screen.blit(appearance[2], (3 + each.x * 12 * 24, 3)) 345 elif each.times <= 20: 346 screen.blit(appearance[1], (3 + each.x * 12 * 24, 3)) 347 elif each.times <= 30: 348 screen.blit(appearance[0], (3 + each.x * 12 * 24, 3)) 349 elif each.times <= 40: 350 screen.blit(appearance[2], (3 + each.x * 12 * 24, 3)) 351 elif each.times <= 50: 352 screen.blit(appearance[1], (3 + each.x * 12 * 24, 3)) 353 elif each.times <= 60: 354 screen.blit(appearance[0], (3 + each.x * 12 * 24, 3)) 355 elif each.times <= 70: 356 screen.blit(appearance[2], (3 + each.x * 12 * 24, 3)) 357 elif each.times <= 80: 358 screen.blit(appearance[1], (3 + each.x * 12 * 24, 3)) 359 elif each.times <= 90: 360 screen.blit(appearance[0], (3 + each.x * 12 * 24, 3)) 361 if each.times == 0: 362 each.flash = True 363 364 365 # 绘制我方子弹1 366 if myTank_T1.bullet.life: 367 myTank_T1.bullet.move() 368 screen.blit(myTank_T1.bullet.bullet, myTank_T1.bullet.rect) 369 # 子弹 碰撞 子弹 370 for each in enemyBulletGroup: 371 if each.life: 372 if pygame.sprite.collide_rect(myTank_T1.bullet, each): 373 myTank_T1.bullet.life = False 374 each.life = False 375 pygame.sprite.spritecollide(myTank_T1.bullet, enemyBulletGroup, True, None) 376 # 子弹 碰撞 敌方坦克 377 if pygame.sprite.spritecollide(myTank_T1.bullet, redEnemyGroup, True, None): 378 prop.change() 379 bang_sound.play() 380 enemyNumber -= 1 381 myTank_T1.bullet.life = False 382 elif pygame.sprite.spritecollide(myTank_T1.bullet,greenEnemyGroup, False, None): 383 for each in greenEnemyGroup: 384 if pygame.sprite.collide_rect(myTank_T1.bullet, each): 385 if each.life == 1: 386 pygame.sprite.spritecollide(myTank_T1.bullet,greenEnemyGroup, True, None) 387 bang_sound.play() 388 enemyNumber -= 1 389 elif each.life == 2: 390 each.life -= 1 391 each.tank = each.enemy_3_0 392 elif each.life == 3: 393 each.life -= 1 394 each.tank = each.enemy_3_2 395 myTank_T1.bullet.life = False 396 elif pygame.sprite.spritecollide(myTank_T1.bullet, otherEnemyGroup, True, None): 397 bang_sound.play() 398 enemyNumber -= 1 399 myTank_T1.bullet.life = False 400 #if pygame.sprite.spritecollide(myTank_T1.bullet, allEnemyGroup, True, None): 401 # bang_sound.play() 402 # enemyNumber -= 1 403 # myTank_T1.bullet.life = False 404 # 子弹 碰撞 brickGroup 405 if pygame.sprite.spritecollide(myTank_T1.bullet, bgMap.brickGroup, True, None): 406 myTank_T1.bullet.life = False 407 myTank_T1.bullet.rect.left, myTank_T1.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24 408 # 子弹 碰撞 brickGroup 409 if myTank_T1.bullet.strong: 410 if pygame.sprite.spritecollide(myTank_T1.bullet, bgMap.ironGroup, True, None): 411 myTank_T1.bullet.life = False 412 myTank_T1.bullet.rect.left, myTank_T1.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24 413 else: 414 if pygame.sprite.spritecollide(myTank_T1.bullet, bgMap.ironGroup, False, None): 415 myTank_T1.bullet.life = False 416 myTank_T1.bullet.rect.left, myTank_T1.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24 417 418 # 绘制我方子弹2 419 if myTank_T2.bullet.life: 420 myTank_T2.bullet.move() 421 screen.blit(myTank_T2.bullet.bullet, myTank_T2.bullet.rect) 422 # 子弹 碰撞 敌方坦克 423 if pygame.sprite.spritecollide(myTank_T2.bullet, allEnemyGroup, True, None): 424 bang_sound.play() 425 enemyNumber -= 1 426 myTank_T2.bullet.life = False 427 # 子弹 碰撞 brickGroup 428 if pygame.sprite.spritecollide(myTank_T2.bullet, bgMap.brickGroup, True, None): 429 myTank_T2.bullet.life = False 430 myTank_T2.bullet.rect.left, myTank_T2.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24 431 # 子弹 碰撞 brickGroup 432 if myTank_T2.bullet.strong: 433 if pygame.sprite.spritecollide(myTank_T2.bullet, bgMap.ironGroup, True, None): 434 myTank_T2.bullet.life = False 435 myTank_T2.bullet.rect.left, myTank_T2.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24 436 else: 437 if pygame.sprite.spritecollide(myTank_T2.bullet, bgMap.ironGroup, False, None): 438 myTank_T2.bullet.life = False 439 myTank_T2.bullet.rect.left, myTank_T2.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24 440 441 442 # 绘制敌人子弹 443 for each in allEnemyGroup: 444 # 如果子弹没有生命,则赋予子弹生命 445 if not each.bullet.life and each.bulletNotCooling and enemyCouldMove: 446 enemyBulletGroup.remove(each.bullet) 447 each.shoot() 448 enemyBulletGroup.add(each.bullet) 449 each.bulletNotCooling = False 450 # 如果5毛钱特效播放完毕 并且 子弹存活 则绘制敌方子弹 451 if each.flash: 452 if each.bullet.life: 453 # 如果敌人可以移动 454 if enemyCouldMove: 455 each.bullet.move() 456 screen.blit(each.bullet.bullet, each.bullet.rect) 457 # 子弹 碰撞 我方坦克 458 if pygame.sprite.collide_rect(each.bullet, myTank_T1): 459 bang_sound.play() 460 myTank_T1.rect.left, myTank_T1.rect.top = 3 + 8 * 24, 3 + 24 * 24 461 each.bullet.life = False 462 moving = 0 # 重置移动控制参数 463 for i in range(myTank_T1.level+1): 464 myTank_T1.levelDown() 465 if pygame.sprite.collide_rect(each.bullet, myTank_T2): 466 bang_sound.play() 467 myTank_T2.rect.left, myTank_T2.rect.top = 3 + 16 * 24, 3 + 24 * 24 468 each.bullet.life = False 469 # 子弹 碰撞 brickGroup 470 if pygame.sprite.spritecollide(each.bullet, bgMap.brickGroup, True, None): 471 each.bullet.life = False 472 # 子弹 碰撞 ironGroup 473 if each.bullet.strong: 474 if pygame.sprite.spritecollide(each.bullet, bgMap.ironGroup, True, None): 475 each.bullet.life = False 476 else: 477 if pygame.sprite.spritecollide(each.bullet, bgMap.ironGroup, False, None): 478 each.bullet.life = False 479 480 # 最后画食物/道具 481 if prop.life: 482 screen.blit(prop.image, prop.rect) 483 # 我方坦克碰撞 食物/道具 484 if pygame.sprite.collide_rect(myTank_T1, prop): 485 if prop.kind == 1: # 敌人全毁 486 for each in allEnemyGroup: 487 if pygame.sprite.spritecollide(each, allEnemyGroup, True, None): 488 bang_sound.play() 489 enemyNumber -= 1 490 prop.life = False 491 if prop.kind == 2: # 敌人静止 492 enemyCouldMove = False 493 prop.life = False 494 if prop.kind == 3: # 子弹增强 495 myTank_T1.bullet.strong = True 496 prop.life = False 497 if prop.kind == 4: # 家得到保护 498 for x, y in [(11,23),(12,23),(13,23),(14,23),(11,24),(14,24),(11,25),(14,25)]: 499 bgMap.iron = wall.Iron() 500 bgMap.iron.rect.left, bgMap.iron.rect.top = 3 + x * 24, 3 + y * 24 501 bgMap.ironGroup.add(bgMap.iron) 502 prop.life = False 503 if prop.kind == 5: # 坦克无敌 504 prop.life = False 505 pass 506 if prop.kind == 6: # 坦克升级 507 myTank_T1.levelUp() 508 prop.life = False 509 if prop.kind == 7: # 坦克生命+1 510 myTank_T1.life += 1 511 prop.life = False 512 513 514 515 516 # 延迟 517 delay -= 1 518 if not delay: 519 delay = 100 520 521 pygame.display.flip() 522 clock.tick(60) 523 524 525 if __name__ == "__main__": 526 try: 527 main() 528 except SystemExit: 529 pass 530 except: 531 traceback.print_exc() 532 pygame.quit() 533 input()
myTank.py
1 import pygame 2 import bulletClass 3 4 5 tank_T1_0 = r"..\image\tank_T1_0.png" 6 tank_T1_1 = r"..\image\tank_T1_1.png" 7 tank_T1_2 = r"..\image\tank_T1_2.png" 8 tank_T2_0 = r"..\image\tank_T2_0.png" 9 tank_T2_1 = r"..\image\tank_T2_1.png" 10 tank_T2_2 = r"..\image\tank_T2_2.png" 11 12 13 14 class MyTank(pygame.sprite.Sprite): 15 def __init__(self, playerNumber): 16 pygame.sprite.Sprite.__init__(self) 17 18 # 玩家生命 19 self.life = True 20 21 # 第几个玩家 坦克的三个等级 22 if playerNumber == 1: 23 self.tank_L0_image = pygame.image.load(tank_T1_0).convert_alpha() 24 self.tank_L1_image = pygame.image.load(tank_T1_1).convert_alpha() 25 self.tank_L2_image = pygame.image.load(tank_T1_2).convert_alpha() 26 if playerNumber == 2: 27 self.tank_L0_image = pygame.image.load(tank_T2_0).convert_alpha() 28 self.tank_L1_image = pygame.image.load(tank_T2_1).convert_alpha() 29 self.tank_L2_image = pygame.image.load(tank_T2_2).convert_alpha() 30 self.level = 0 31 32 # 初始坦克为0级 33 self.tank = self.tank_L0_image 34 35 # 运动中的两种图片 36 self.tank_R0 = self.tank.subsurface((0, 0),(48, 48)) 37 self.tank_R1 = self.tank.subsurface((48, 0),(48, 48)) 38 self.rect = self.tank_R0.get_rect() 39 if playerNumber == 1: 40 self.rect.left, self.rect.top = 3 + 24 * 8, 3 + 24 * 24 41 if playerNumber == 2: 42 self.rect.left, self.rect.top = 3 + 24 * 16, 3 + 24 * 24 43 44 # 坦克速度 坦克方向 坦克生命 子弹冷却 45 self.speed = 3 46 self.dir_x, self.dir_y = 0, -1 47 self.life = 3 48 self.bulletNotCooling = True 49 self.bullet = bulletClass.Bullet() 50 #self.bullet.rect.left, self.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24 51 52 def shoot(self): 53 # 子弹 54 self.bullet.life = True 55 self.bullet.changeImage(self.dir_x, self.dir_y) 56 57 if self.dir_x == 0 and self.dir_y == -1: 58 self.bullet.rect.left = self.rect.left + 20 59 self.bullet.rect.bottom = self.rect.top + 1 60 elif self.dir_x == 0 and self.dir_y == 1: 61 self.bullet.rect.left = self.rect.left + 20 62 self.bullet.rect.top = self.rect.bottom - 1 63 elif self.dir_x == -1 and self.dir_y == 0: 64 self.bullet.rect.right = self.rect.left - 1 65 self.bullet.rect.top = self.rect.top + 20 66 elif self.dir_x == 1 and self.dir_y == 0: 67 self.bullet.rect.left = self.rect.right + 1 68 self.bullet.rect.top = self.rect.top + 20 69 70 if self.level == 1: 71 self.bullet.speed = 16 72 self.bullet.strong = False 73 if self.level == 2: 74 self.bullet.speed = 16 75 self.bullet.strong = True 76 if self.level == 3: 77 self.bullet.speed = 48 78 self.bullet.strong = True 79 80 81 def levelUp(self): 82 if self.level < 2: 83 self.level += 1 84 if self.level == 0: 85 self.tank = self.tank_L0_image 86 if self.level == 1: 87 self.tank = self.tank_L1_image 88 if self.level == 2: 89 self.tank = self.tank_L2_image 90 if self.level == 3: 91 self.tank = self.tank_L2_image 92 93 def levelDown(self): 94 if self.level > 0: 95 self.level -= 1 96 if self.level == 0: 97 self.tank = self.tank_L0_image 98 self.bullet.speed = 6 99 self.bullet.strong = False 100 if self.level == 1: 101 self.tank = self.tank_L1_image 102 if self.level == 2: 103 self.tank = self.tank_L2_image 104 105 106 # 返回True 代表发生碰撞 107 def moveUp(self, tankGroup, brickGroup, ironGroup): 108 self.rect = self.rect.move(self.speed * 0, self.speed * -1) 109 self.tank_R0 = self.tank.subsurface((0, 0),(48, 48)) 110 self.tank_R1 = self.tank.subsurface((48, 0),(48, 48)) 111 self.dir_x, self.dir_y = 0, -1 112 if self.rect.top < 3: 113 self.rect = self.rect.move(self.speed * 0, self.speed * 1) 114 return True 115 if pygame.sprite.spritecollide(self, brickGroup, False, None) \ 116 or pygame.sprite.spritecollide(self, ironGroup, False, None): 117 self.rect = self.rect.move(self.speed * 0, self.speed * 1) 118 return True 119 if pygame.sprite.spritecollide(self, tankGroup, False, None): 120 self.rect = self.rect.move(self.speed * 0, self.speed * 1) 121 return True 122 return False 123 def moveDown(self, tankGroup, brickGroup, ironGroup): 124 self.rect = self.rect.move(self.speed * 0, self.speed * 1) 125 self.tank_R0 = self.tank.subsurface((0, 48),(48, 48)) 126 self.tank_R1 = self.tank.subsurface((48, 48),(48, 48)) 127 self.dir_x, self.dir_y = 0, 1 128 if self.rect.bottom > 630 - 3: 129 self.rect = self.rect.move(self.speed * 0, self.speed * -1) 130 return True 131 if pygame.sprite.spritecollide(self, brickGroup, False, None) \ 132 or pygame.sprite.spritecollide(self, ironGroup, False, None): 133 self.rect = self.rect.move(self.speed * 0, self.speed * -1) 134 return True 135 if pygame.sprite.spritecollide(self, tankGroup, False, None): 136 self.rect = self.rect.move(self.speed * 0, self.speed * -1) 137 return True 138 return False 139 def moveLeft(self, tankGroup, brickGroup, ironGroup): 140 self.rect = self.rect.move(self.speed * -1, self.speed * 0) 141 self.tank_R0 = self.tank.subsurface((0, 96),(48, 48)) 142 self.tank_R1 = self.tank.subsurface((48, 96),(48, 48)) 143 self.dir_x, self.dir_y = -1, 0 144 if self.rect.left < 3: 145 self.rect = self.rect.move(self.speed * 1, self.speed * 0) 146 return True 147 if pygame.sprite.spritecollide(self, brickGroup, False, None) \ 148 or pygame.sprite.spritecollide(self, ironGroup, False, None): 149 self.rect = self.rect.move(self.speed * 1, self.speed * 0) 150 return True 151 if pygame.sprite.spritecollide(self, tankGroup, False, None): 152 self.rect = self.rect.move(self.speed * 1, self.speed * 0) 153 return True 154 return False 155 def moveRight(self, tankGroup, brickGroup, ironGroup): 156 self.rect = self.rect.move(self.speed * 1, self.speed * 0) 157 self.tank_R0 = self.tank.subsurface((0, 144),(48, 48)) 158 self.tank_R1 = self.tank.subsurface((48, 144),(48, 48)) 159 self.dir_x, self.dir_y = 1, 0 160 if self.rect.right > 630 - 3: 161 self.rect = self.rect.move(self.speed * -1, self.speed * 0) 162 return True 163 if pygame.sprite.spritecollide(self, brickGroup, False, None) \ 164 or pygame.sprite.spritecollide(self, ironGroup, False, None): 165 self.rect = self.rect.move(self.speed * -1, self.speed * 0) 166 return True 167 if pygame.sprite.spritecollide(self, tankGroup, False, None): 168 self.rect = self.rect.move(self.speed * -1, self.speed * 0) 169 return True 170 return False
wall.py
1 import pygame 2 3 brickImage = r"..\image\brick.png" 4 ironImage = r"..\image\iron.png" 5 6 class Brick(pygame.sprite.Sprite): 7 def __init__(self): 8 pygame.sprite.Sprite.__init__(self) 9 10 self.image = pygame.image.load(brickImage) 11 self.rect = self.image.get_rect() 12 13 class Iron(pygame.sprite.Sprite): 14 def __init__(self): 15 pygame.sprite.Sprite.__init__(self) 16 17 self.image = pygame.image.load(ironImage) 18 self.rect = self.image.get_rect() 19 20 class Map(): 21 def __init__(self): 22 self.brickGroup = pygame.sprite.Group() 23 self.ironGroup = pygame.sprite.Group() 24 25 # 数字代表地图中的位置 26 # 画砖块 27 X1379 = [2, 3, 6, 7, 18, 19, 22, 23] 28 Y1379 = [2, 3, 4, 5, 6, 7, 8, 9, 10, 17, 18, 19, 20, 21, 22, 23] 29 X28 = [10, 11, 14, 15] 30 Y28 = [2, 3, 4, 5, 6, 7, 8, 11, 12, 15, 16, 17, 18, 19, 20] 31 X46 = [4, 5, 6, 7, 18, 19, 20, 21] 32 Y46 = [13, 14] 33 X5 = [12, 13] 34 Y5 = [16, 17] 35 X0Y0 = [(11,23),(12,23),(13,23),(14,23),(11,24),(14,24),(11,25),(14,25)] 36 for x in X1379: 37 for y in Y1379: 38 self.brick = Brick() 39 self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24 40 self.brickGroup.add(self.brick) 41 for x in X28: 42 for y in Y28: 43 self.brick = Brick() 44 self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24 45 self.brickGroup.add(self.brick) 46 for x in X46: 47 for y in Y46: 48 self.brick = Brick() 49 self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24 50 self.brickGroup.add(self.brick) 51 for x in X5: 52 for y in Y5: 53 self.brick = Brick() 54 self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24 55 self.brickGroup.add(self.brick) 56 for x, y in X0Y0: 57 self.brick = Brick() 58 self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24 59 self.brickGroup.add(self.brick) 60 61 # 画石头 62 for x, y in [(0,14),(1,14),(12,6),(13,6),(12,7),(13,7),(24,14),(25,14)]: 63 self.iron = Iron() 64 self.iron.rect.left, self.iron.rect.top = 3 + x * 24, 3 + y * 24 65 self.ironGroup.add(self.iron)
food.py
1 import pygame 2 import random 3 4 class Food(pygame.sprite.Sprite): 5 def __init__(self): 6 7 self.food_boom = pygame.image.load(r"..\image\food_boom.png").convert_alpha() 8 self.food_clock = pygame.image.load(r"..\image\food_clock.png").convert_alpha() 9 self.food_gun = pygame.image.load(r"..\image\food_gun.png").convert_alpha() 10 self.food_iron = pygame.image.load(r"..\image\food_iron.png").convert_alpha() 11 self.food_protect = pygame.image.load(r"..\image\food_protect.png").convert_alpha() 12 self.food_star = pygame.image.load(r"..\image\food_star.png").convert_alpha() 13 self.food_tank = pygame.image.load(r"..\image\food_tank.png").convert_alpha() 14 self.kind = random.choice([1, 2, 3, 4, 5, 6, 7]) 15 if self.kind == 1: 16 self.image = self.food_boom 17 elif self.kind == 2: 18 self.image = self.food_clock 19 elif self.kind == 3: 20 self.image = self.food_gun 21 elif self.kind == 4: 22 self.image = self.food_iron 23 elif self.kind == 5: 24 self.image = self.food_protect 25 elif self.kind == 6: 26 self.image = self.food_star 27 elif self.kind == 7: 28 self.image = self.food_tank 29 30 self.rect = self.image.get_rect() 31 self.rect.left = self.rect.top = random.randint(100, 500) 32 33 self.life = False 34 35 def change(self): 36 self.kind = random.choice([1, 2, 3, 4, 5, 6, 7]) 37 if self.kind == 1: 38 self.image = self.food_boom 39 elif self.kind == 2: 40 self.image = self.food_clock 41 elif self.kind == 3: 42 self.image = self.food_gun 43 elif self.kind == 4: 44 self.image = self.food_iron 45 elif self.kind == 5: 46 self.image = self.food_protect 47 elif self.kind == 6: 48 self.image = self.food_star 49 elif self.kind == 7: 50 self.image = self.food_tank 51 52 self.rect.left = self.rect.top = random.randint(100, 500) 53 self.life = True
enemyTank.py
1 import pygame 2 import random 3 import bulletClass 4 5 6 7 class EnemyTank(pygame.sprite.Sprite): 8 def __init__(self, x = None, kind = None, isred = None): 9 pygame.sprite.Sprite.__init__(self) 10 11 # 坦克出现前动画是否播放 12 self.flash = False 13 self.times = 90 14 15 # 参数:坦克种类 16 self.kind = kind 17 if not kind: 18 self.kind = random.choice([1, 2, 3, 4]) 19 20 # 选择敌军坦克种类 21 if self.kind == 1: 22 self.enemy_x_0 = pygame.image.load(r"..\image\enemy_1_0.png").convert_alpha() 23 self.enemy_x_3 = pygame.image.load(r"..\image\enemy_1_3.png").convert_alpha() 24 if self.kind == 2: 25 self.enemy_x_0 = pygame.image.load(r"..\image\enemy_2_0.png").convert_alpha() 26 self.enemy_x_3 = pygame.image.load(r"..\image\enemy_2_3.png").convert_alpha() 27 if self.kind == 3: 28 self.enemy_x_0 = pygame.image.load(r"..\image\enemy_3_1.png").convert_alpha() 29 self.enemy_x_3 = pygame.image.load(r"..\image\enemy_3_0.png").convert_alpha() 30 if self.kind == 4: 31 self.enemy_x_0 = pygame.image.load(r"..\image\enemy_4_0.png").convert_alpha() 32 self.enemy_x_3 = pygame.image.load(r"..\image\enemy_4_3.png").convert_alpha() 33 self.enemy_3_0 = pygame.image.load(r"..\image\enemy_3_0.png").convert_alpha() 34 self.enemy_3_2 = pygame.image.load(r"..\image\enemy_3_2.png").convert_alpha() 35 36 37 # 参数:是否携带食物 38 self.isred = isred 39 if not None: 40 self.isred = random.choice((True, False, False, False, False)) 41 if self.isred: 42 self.tank = self.enemy_x_3 43 else: 44 self.tank = self.enemy_x_0 45 # 参数:坦克位置 46 self.x = x 47 if not self.x: 48 self.x = random.choice([1, 2, 3]) 49 self.x -= 1 50 51 # 运动中的两种图片 52 self.tank_R0 = self.tank.subsurface(( 0, 48), (48, 48)) 53 self.tank_R1 = self.tank.subsurface((48, 48), (48, 48)) 54 self.rect = self.tank_R0.get_rect() 55 self.rect.left, self.rect.top = 3 + self.x * 12 * 24, 3 + 0 * 24 56 57 # 坦克速度 方向 生命 子弹生命 子弹延迟 58 self.speed = 1 59 self.dir_x, self.dir_y = 0, 1 60 self.life = 1 61 self.bulletNotCooling = True 62 self.bullet = bulletClass.Bullet() 63 # 是否撞墙,撞墙则改变方向 64 self.dirChange = False 65 66 # 每种坦克不同的属性 67 if self.kind == 2: 68 self.speed = 3 69 if self.kind == 3: 70 self.life = 3 71 72 def shoot(self): 73 # 赋予子弹生命 74 self.bullet.life = True 75 self.bullet.changeImage(self.dir_x, self.dir_y) 76 77 if self.dir_x == 0 and self.dir_y == -1: 78 self.bullet.rect.left = self.rect.left + 20 79 self.bullet.rect.bottom = self.rect.top + 1 80 elif self.dir_x == 0 and self.dir_y == 1: 81 self.bullet.rect.left = self.rect.left + 20 82 self.bullet.rect.top = self.rect.bottom - 1 83 elif self.dir_x == -1 and self.dir_y == 0: 84 self.bullet.rect.right = self.rect.left - 1 85 self.bullet.rect.top = self.rect.top + 20 86 elif self.dir_x == 1 and self.dir_y == 0: 87 self.bullet.rect.left = self.rect.right + 1 88 self.bullet.rect.top = self.rect.top + 20 89 90 def move(self, tankGroup, brickGroup, ironGroup): 91 self.rect = self.rect.move(self.speed * self.dir_x, self.speed * self.dir_y) 92 93 if self.dir_x == 0 and self.dir_y == -1: 94 self.tank_R0 = self.tank.subsurface(( 0, 0),(48, 48)) 95 self.tank_R1 = self.tank.subsurface((48, 0),(48, 48)) 96 elif self.dir_x == 0 and self.dir_y == 1: 97 self.tank_R0 = self.tank.subsurface(( 0, 48),(48, 48)) 98 self.tank_R1 = self.tank.subsurface((48, 48),(48, 48)) 99 elif self.dir_x == -1 and self.dir_y == 0: 100 self.tank_R0 = self.tank.subsurface(( 0, 96),(48, 48)) 101 self.tank_R1 = self.tank.subsurface((48, 96),(48, 48)) 102 elif self.dir_x == 1 and self.dir_y == 0: 103 self.tank_R0 = self.tank.subsurface(( 0, 144),(48, 48)) 104 self.tank_R1 = self.tank.subsurface((48, 144),(48, 48)) 105 106 107 # 碰撞地图边缘 108 if self.rect.top < 3: 109 self.rect = self.rect.move(self.speed * 0, self.speed * 1) 110 self.dir_x, self.dir_y = random.choice(([0,1],[0,-1],[1,0],[-1,0])) 111 elif self.rect.bottom > 630 - 3: 112 self.rect = self.rect.move(self.speed * 0, self.speed * -1) 113 self.dir_x, self.dir_y = random.choice(([0,1],[0,-1],[1,0],[-1,0])) 114 elif self.rect.left < 3: 115 self.rect = self.rect.move(self.speed * 1, self.speed * 0) 116 self.dir_x, self.dir_y = random.choice(([0,1],[0,-1],[1,0],[-1,0])) 117 elif self.rect.right > 630 - 3: 118 self.rect = self.rect.move(self.speed * -1, self.speed * 0) 119 self.dir_x, self.dir_y = random.choice(([0,1],[0,-1],[1,0],[-1,0])) 120 # 碰撞墙体 和坦克 121 if pygame.sprite.spritecollide(self, brickGroup, False, None) \ 122 or pygame.sprite.spritecollide(self, ironGroup, False, None) \ 123 or pygame.sprite.spritecollide(self, tankGroup, False, None): 124 self.rect = self.rect.move(self.speed * -self.dir_x, self.speed * -self.dir_y) 125 self.dir_x, self.dir_y = random.choice(([0,1],[0,-1],[1,0],[-1,0]))
bulletClass.py
1 import pygame 2 3 4 class Bullet(pygame.sprite.Sprite): 5 def __init__(self): 6 pygame.sprite.Sprite.__init__(self) 7 8 self.bullet_up = pygame.image.load(r"..\image\bullet_up.png") 9 self.bullet_down = pygame.image.load(r"..\image\bullet_down.png") 10 self.bullet_left = pygame.image.load(r"..\image\bullet_left.png") 11 self.bullet_right = pygame.image.load(r"..\image\bullet_right.png") 12 13 # 子弹方向 速度 生命 碎石 14 self.dir_x, self.dir_y = 0, 0 15 self.speed = 6 16 self.life = False 17 self.strong = False 18 19 self.bullet = self.bullet_up 20 self.rect = self.bullet.get_rect() 21 self.rect.left, self.rect.right = 3 + 12 * 24, 3 + 24 * 24 22 23 def changeImage(self, dir_x, dir_y): 24 self.dir_x, self.dir_y = dir_x, dir_y 25 if self.dir_x == 0 and self.dir_y == -1: 26 self.bullet = self.bullet_up 27 elif self.dir_x == 0 and self.dir_y == 1: 28 self.bullet = self.bullet_down 29 elif self.dir_x == -1 and self.dir_y == 0: 30 self.bullet = self.bullet_left 31 elif self.dir_x == 1 and self.dir_y == 0: 32 self.bullet = self.bullet_right 33 34 35 36 def move(self): 37 self.rect = self.rect.move(self.speed * self.dir_x, 38 self.speed * self.dir_y) 39 40 # 碰撞地图边缘 41 if self.rect.top < 3: 42 self.life = False 43 # self.rect.left, self.rect.right = 3 + 12 * 24, 3 + 24 * 24 44 if self.rect.bottom > 630 - 3: 45 self.life = False 46 # self.rect.left, self.rect.right = 3 + 12 * 24, 3 + 24 * 24 47 if self.rect.left < 3: 48 self.life = False 49 # self.rect.left, self.rect.right = 3 + 12 * 24, 3 + 24 * 24 50 if self.rect.right > 630 - 3: 51 self.life = False 52 # self.rect.left, self.rect.right = 3 + 12 * 24, 3 + 24 * 24 53 54 # 碰撞 brickGroup 55 #if pygame.sprite.spritecollide(self, brickGroup, True, None): 56 # self.life = False 57 # moving = 0 58 # 碰撞 ironGroup 59 #if self.strong: 60 # if pygame.sprite.spritecollide(self, ironGroup, True, None): 61 # self.life = False 62 #else: 63 # if pygame.sprite.spritecollide(self, ironGroup, False, None): 64 # self.life = False 65 # moving = 0 66 #return moving
食用指南:
坦克1:WASD移动,J发射;
坦克2:方向键移动,0发射。
源码以及文件下载:
链接:https://pan.baidu.com/s/1a0UJkET1V9Kfg0FpqZldAA
提取码:tytm
若链接失效请私信。