├── README.md ├── ic.jpg ├── ssss3.png └── tank.py /README.md: -------------------------------------------------------------------------------- 1 | # Tank-Game-Python. 2 | 3 | Tank-Game-Python-(Pygame). 4 | 5 | Game between user and computer. 6 | 7 | make sure to have the module pygame installed. 8 | -------------------------------------------------------------------------------- /ic.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amark23/Tank-Game-Python-/6c3c9b91e97e711426e7414db46a086874f1c4f0/ic.jpg -------------------------------------------------------------------------------- /ssss3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amark23/Tank-Game-Python-/6c3c9b91e97e711426e7414db46a086874f1c4f0/ssss3.png -------------------------------------------------------------------------------- /tank.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | import time 3 | import random 4 | 5 | pygame.init() 6 | 7 | display_width = 800 8 | display_height = 600 9 | 10 | gameDisplay = pygame.display.set_mode((display_width, display_height)) 11 | 12 | #fire_sound = pygame.mixer.Sound("1.mp3") 13 | #explosion_sound = pygame.mixer.Sound("1.mp3") 14 | 15 | #pygame.mixer.music.load("1.mp3") 16 | #pygame.mixer.music.play(-1) 17 | 18 | 19 | pygame.display.set_caption('Tanks - Amar') 20 | 21 | icon = pygame.image.load("ic.jpg") 22 | pygame.display.set_icon(icon) 23 | #----------------------------------------colors---------------------------------------------- 24 | wheat=(245,222,179) 25 | 26 | white = (255, 255, 255) 27 | black = (0, 0, 0) 28 | blue = (0,0,255) 29 | 30 | red = (200, 0, 0) 31 | light_red = (255, 0, 0) 32 | 33 | yellow = (200, 200, 0) 34 | light_yellow = (255, 255, 0) 35 | 36 | green = (34, 177, 76) 37 | light_green = (0, 255, 0) 38 | 39 | #--------------------------------for picking current time for the frames per second---------------------- 40 | clock = pygame.time.Clock() 41 | #--------------------------------geometry of tank and its turret------------------------------------------ 42 | tankWidth = 40 43 | tankHeight = 20 44 | 45 | turretWidth = 5 46 | wheelWidth = 5 47 | 48 | ground_height = 35 49 | #--------------------------------------------fonts with size, for text_object function---------------- 50 | smallfont = pygame.font.SysFont("comicsansms", 25) 51 | medfont = pygame.font.SysFont("comicsansms", 50) 52 | largefont = pygame.font.SysFont("Yu Mincho Demibold", 85) 53 | vsmallfont = pygame.font.SysFont("Yu Mincho Demibold", 25) 54 | 55 | #--------------------------------------------defining score function---------------------------------- 56 | def score(score): 57 | text = smallfont.render("Score: " + str(score), True, white) 58 | gameDisplay.blit(text, [0, 0]) 59 | 60 | #---defining function to get the fonts and sizes assigned with them by size names by default size="small"-- 61 | def text_objects(text, color, size="small"): 62 | if size == "small": 63 | textSurface = smallfont.render(text, True, color) 64 | if size == "medium": 65 | textSurface = medfont.render(text, True, color) 66 | if size == "large": 67 | textSurface = largefont.render(text, True, color) 68 | if size == "vsmall": 69 | textSurface = vsmallfont.render(text, True, color) 70 | 71 | return textSurface, textSurface.get_rect() 72 | 73 | #---------------------fuction for texts that has to appear over button---------------------------------------- 74 | def text_to_button(msg, color, buttonx, buttony, buttonwidth, buttonheight, size="vsmall"): 75 | textSurf, textRect = text_objects(msg, color, size) 76 | textRect.center = ((buttonx + (buttonwidth / 2)), buttony + (buttonheight / 2)) 77 | gameDisplay.blit(textSurf, textRect) 78 | 79 | #--------------------fuction for texts that has to appear over screen---------------------------------------- 80 | def message_to_screen(msg, color, y_displace=0, size="small"): 81 | textSurf, textRect = text_objects(msg, color, size) 82 | textRect.center = (int(display_width / 2), int(display_height / 2) + y_displace) 83 | gameDisplay.blit(textSurf, textRect) 84 | 85 | #----------------------fuction for players tank , defining turrets positins and wheels dimensions------------ 86 | def tank(x, y, turPos): 87 | x = int(x) 88 | y = int(y) 89 | 90 | possibleTurrets = [(x - 27, y - 2), 91 | (x - 26, y - 5), 92 | (x - 25, y - 8), 93 | (x - 23, y - 12), 94 | (x - 20, y - 14), 95 | (x - 18, y - 15), 96 | (x - 15, y - 17), 97 | (x - 13, y - 19), 98 | (x - 11, y - 21) 99 | ] 100 | 101 | pygame.draw.circle(gameDisplay, blue, (x, y), int(tankHeight / 2)) 102 | pygame.draw.rect(gameDisplay, blue, (x - tankHeight, y, tankWidth, tankHeight)) 103 | 104 | pygame.draw.line(gameDisplay, blue, (x, y), possibleTurrets[turPos], turretWidth) 105 | 106 | pygame.draw.circle(gameDisplay, blue, (x - 15, y + 20), wheelWidth) 107 | pygame.draw.circle(gameDisplay, blue, (x - 10, y + 20), wheelWidth) 108 | 109 | pygame.draw.circle(gameDisplay, blue, (x - 15, y + 20), wheelWidth) 110 | pygame.draw.circle(gameDisplay, blue, (x - 10, y + 20), wheelWidth) 111 | pygame.draw.circle(gameDisplay, blue, (x - 5, y + 20), wheelWidth) 112 | pygame.draw.circle(gameDisplay, blue, (x, y + 20), wheelWidth) 113 | pygame.draw.circle(gameDisplay, blue, (x + 5, y + 20), wheelWidth) 114 | pygame.draw.circle(gameDisplay, blue, (x + 10, y + 20), wheelWidth) 115 | pygame.draw.circle(gameDisplay, blue, (x + 15, y + 20), wheelWidth) 116 | 117 | return possibleTurrets[turPos] 118 | 119 | #----------------------fuction for computers tank , defining turrets positins and wheels dimensions------------ 120 | def enemy_tank(x, y, turPos): 121 | x = int(x) 122 | y = int(y) 123 | 124 | possibleTurrets = [(x + 27, y - 2), 125 | (x + 26, y - 5), 126 | (x + 25, y - 8), 127 | (x + 23, y - 12), 128 | (x + 20, y - 14), 129 | (x + 18, y - 15), 130 | (x + 15, y - 17), 131 | (x + 13, y - 19), 132 | (x + 11, y - 21) 133 | ] 134 | 135 | pygame.draw.circle(gameDisplay, blue, (x, y), int(tankHeight / 2)) 136 | pygame.draw.rect(gameDisplay, blue, (x - tankHeight, y, tankWidth, tankHeight)) 137 | 138 | pygame.draw.line(gameDisplay, blue, (x, y), possibleTurrets[turPos], turretWidth) 139 | 140 | pygame.draw.circle(gameDisplay, blue, (x - 15, y + 20), wheelWidth) 141 | pygame.draw.circle(gameDisplay, blue, (x - 10, y + 20), wheelWidth) 142 | 143 | pygame.draw.circle(gameDisplay, blue, (x - 15, y + 20), wheelWidth) 144 | pygame.draw.circle(gameDisplay, blue, (x - 10, y + 20), wheelWidth) 145 | pygame.draw.circle(gameDisplay, blue, (x - 5, y + 20), wheelWidth) 146 | pygame.draw.circle(gameDisplay, blue, (x, y + 20), wheelWidth) 147 | pygame.draw.circle(gameDisplay, blue, (x + 5, y + 20), wheelWidth) 148 | pygame.draw.circle(gameDisplay, blue, (x + 10, y + 20), wheelWidth) 149 | pygame.draw.circle(gameDisplay, blue, (x + 15, y + 20), wheelWidth) 150 | 151 | return possibleTurrets[turPos] 152 | 153 | #-----------------------------------------Game control Screen------------------------------------------------ 154 | def game_controls(): 155 | gcont = True 156 | 157 | while gcont: 158 | for event in pygame.event.get(): 159 | # print(event) 160 | if event.type == pygame.QUIT: 161 | pygame.quit() 162 | quit() 163 | 164 | gameDisplay.fill(black) 165 | message_to_screen("Controls", white, -100, size="large") 166 | message_to_screen("Fire: Spacebar", wheat, -30) 167 | message_to_screen("Move Turret: Up and Down arrows", wheat, 10) 168 | message_to_screen("Move Tank: Left and Right arrows", wheat, 50) 169 | message_to_screen("Press D to raise Power % AND Press A to lower Power % ", wheat, 140) 170 | message_to_screen("Pause: P", wheat, 90) 171 | 172 | button("Play", 150, 500, 100, 50, green, light_green, action="play") 173 | button("Main", 350, 500, 100, 50, yellow, light_yellow, action="main") 174 | button("Quit", 550, 500, 100, 50, red, light_red, action="quit") 175 | 176 | pygame.display.update() 177 | 178 | clock.tick(15) 179 | 180 | #--------------function for buttons having action calls and text on it callings--------------------------- 181 | def button(text, x, y, width, height, inactive_color, active_color, action=None,size=" "): 182 | cur = pygame.mouse.get_pos() 183 | click = pygame.mouse.get_pressed() 184 | # print(click) 185 | if x + width > cur[0] > x and y + height > cur[1] > y: 186 | pygame.draw.rect(gameDisplay, active_color, (x, y, width, height)) 187 | if click[0] == 1 and action != None: 188 | if action == "quit": 189 | pygame.quit() 190 | quit() 191 | 192 | if action == "controls": 193 | game_controls() 194 | 195 | if action == "play": 196 | gameLoop() 197 | 198 | if action == "main": 199 | game_intro() 200 | 201 | else: 202 | pygame.draw.rect(gameDisplay, inactive_color, (x, y, width, height)) 203 | 204 | text_to_button(text, black, x, y, width, height) 205 | 206 | #---function for pause having transparent background, uncommenting fill statement will make it black----- 207 | def pause(): 208 | paused = True 209 | message_to_screen("Paused", white, -100, size="large") 210 | message_to_screen("Press C to continue playing or Q to quit", wheat, 25) 211 | pygame.display.update() 212 | while paused: 213 | #gameDisplay.fill(black) 214 | for event in pygame.event.get(): 215 | 216 | if event.type == pygame.QUIT: 217 | pygame.quit() 218 | quit() 219 | if event.type == pygame.KEYDOWN: 220 | if event.key == pygame.K_c: 221 | paused = False 222 | elif event.key == pygame.K_q: 223 | pygame.quit() 224 | quit() 225 | 226 | clock.tick(5) 227 | 228 | #----------------------------function for barrior------------------------------------------------------- 229 | def barrier(xlocation, randomHeight, barrier_width): 230 | pygame.draw.rect(gameDisplay, green, [xlocation, display_height - randomHeight, barrier_width, randomHeight]) 231 | 232 | #---------------------------function for explosion for both tanks--------------------------------------- 233 | def explosion(x, y, size=50): 234 | #pygame.mixer.Sound.play(explosion_sound) 235 | explode = True 236 | 237 | while explode: 238 | for event in pygame.event.get(): 239 | if event.type == pygame.QUIT: 240 | pygame.quit() 241 | quit() 242 | 243 | startPoint = x, y 244 | 245 | colorChoices = [red, light_red, yellow, light_yellow] 246 | 247 | magnitude = 1 248 | 249 | while magnitude < size: 250 | exploding_bit_x = x + random.randrange(-1 * magnitude, magnitude) 251 | exploding_bit_y = y + random.randrange(-1 * magnitude, magnitude) 252 | 253 | pygame.draw.circle(gameDisplay, colorChoices[random.randrange(0, 4)], (exploding_bit_x, exploding_bit_y), 254 | random.randrange(1, 5)) 255 | magnitude += 1 256 | 257 | pygame.display.update() 258 | clock.tick(100) 259 | 260 | explode = False 261 | 262 | #--------------------------------firing function for players tank------------------------------------------- 263 | def fireShell(xy, tankx, tanky, turPos, gun_power, xlocation, barrier_width, randomHeight, enemyTankX, enemyTankY): 264 | #pygame.mixer.Sound.play(fire_sound) 265 | fire = True 266 | damage = 0 267 | 268 | startingShell = list(xy) 269 | 270 | print("FIRE!", xy) 271 | 272 | while fire: 273 | for event in pygame.event.get(): 274 | if event.type == pygame.QUIT: 275 | pygame.quit() 276 | quit() 277 | 278 | # print(startingShell[0],startingShell[1]) 279 | pygame.draw.circle(gameDisplay, red, (startingShell[0], startingShell[1]), 5) 280 | 281 | startingShell[0] -= (12 - turPos) * 2 282 | 283 | # y = x**2 284 | startingShell[1] += int( 285 | (((startingShell[0] - xy[0]) * 0.015 / (gun_power / 50)) ** 2) - (turPos + turPos / (12 - turPos))) 286 | 287 | if startingShell[1] > display_height - ground_height: 288 | print("Last shell:", startingShell[0], startingShell[1]) 289 | hit_x = int((startingShell[0] * display_height - ground_height) / startingShell[1]) 290 | hit_y = int(display_height - ground_height) 291 | print("Impact:", hit_x, hit_y) 292 | 293 | if enemyTankX + 10 > hit_x > enemyTankX - 10: 294 | print("Critical Hit!") 295 | damage = 25 296 | elif enemyTankX + 15 > hit_x > enemyTankX - 15: 297 | print("Hard Hit!") 298 | damage = 18 299 | elif enemyTankX + 25 > hit_x > enemyTankX - 25: 300 | print("Medium Hit") 301 | damage = 10 302 | elif enemyTankX + 35 > hit_x > enemyTankX - 35: 303 | print("Light Hit") 304 | damage = 5 305 | 306 | explosion(hit_x, hit_y) 307 | fire = False 308 | 309 | check_x_1 = startingShell[0] <= xlocation + barrier_width 310 | check_x_2 = startingShell[0] >= xlocation 311 | 312 | check_y_1 = startingShell[1] <= display_height 313 | check_y_2 = startingShell[1] >= display_height - randomHeight 314 | 315 | if check_x_1 and check_x_2 and check_y_1 and check_y_2: 316 | print("Last shell:", startingShell[0], startingShell[1]) 317 | hit_x = int((startingShell[0])) 318 | hit_y = int(startingShell[1]) 319 | print("Impact:", hit_x, hit_y) 320 | explosion(hit_x, hit_y) 321 | fire = False 322 | 323 | pygame.display.update() 324 | clock.tick(60) 325 | return damage 326 | 327 | #--------------------------------firing function for computer's tank------------------------------------------- 328 | def e_fireShell(xy, tankx, tanky, turPos, gun_power, xlocation, barrier_width, randomHeight, ptankx, ptanky): 329 | #pygame.mixer.Sound.play(fire_sound) 330 | damage = 0 331 | currentPower = 1 332 | power_found = False 333 | 334 | while not power_found: 335 | currentPower += 1 336 | if currentPower > 100: 337 | power_found = True 338 | # print(currentPower) 339 | 340 | fire = True 341 | startingShell = list(xy) 342 | 343 | while fire: 344 | for event in pygame.event.get(): 345 | if event.type == pygame.QUIT: 346 | pygame.quit() 347 | quit() 348 | 349 | # pygame.draw.circle(gameDisplay, red, (startingShell[0],startingShell[1]),5) 350 | 351 | startingShell[0] += (12 - turPos) * 2 352 | startingShell[1] += int( 353 | (((startingShell[0] - xy[0]) * 0.015 / (currentPower / 50)) ** 2) - (turPos + turPos / (12 - turPos))) 354 | 355 | if startingShell[1] > display_height - ground_height: 356 | hit_x = int((startingShell[0] * display_height - ground_height) / startingShell[1]) 357 | hit_y = int(display_height - ground_height) 358 | # explosion(hit_x,hit_y) 359 | if ptankx + 15 > hit_x > ptankx - 15: 360 | print("target acquired!") 361 | power_found = True 362 | fire = False 363 | 364 | check_x_1 = startingShell[0] <= xlocation + barrier_width 365 | check_x_2 = startingShell[0] >= xlocation 366 | 367 | check_y_1 = startingShell[1] <= display_height 368 | check_y_2 = startingShell[1] >= display_height - randomHeight 369 | 370 | if check_x_1 and check_x_2 and check_y_1 and check_y_2: 371 | hit_x = int((startingShell[0])) 372 | hit_y = int(startingShell[1]) 373 | # explosion(hit_x,hit_y) 374 | fire = False 375 | 376 | fire = True 377 | startingShell = list(xy) 378 | print("FIRE!", xy) 379 | 380 | while fire: 381 | for event in pygame.event.get(): 382 | if event.type == pygame.QUIT: 383 | pygame.quit() 384 | quit() 385 | 386 | # print(startingShell[0],startingShell[1]) 387 | pygame.draw.circle(gameDisplay, red, (startingShell[0], startingShell[1]), 5) 388 | 389 | startingShell[0] += (12 - turPos) * 2 390 | 391 | # y = x**2 392 | 393 | gun_power = random.randrange(int(currentPower * 0.90), int(currentPower * 1.10)) 394 | 395 | startingShell[1] += int( 396 | (((startingShell[0] - xy[0]) * 0.015 / (gun_power / 50)) ** 2) - (turPos + turPos / (12 - turPos))) 397 | 398 | if startingShell[1] > display_height - ground_height: 399 | print("last shell:", startingShell[0], startingShell[1]) 400 | hit_x = int((startingShell[0] * display_height - ground_height) / startingShell[1]) 401 | hit_y = int(display_height - ground_height) 402 | print("Impact:", hit_x, hit_y) 403 | 404 | if ptankx + 10 > hit_x > ptankx - 10: 405 | print("Critical Hit!") 406 | damage = 25 407 | elif ptankx + 15 > hit_x > ptankx - 15: 408 | print("Hard Hit!") 409 | damage = 18 410 | elif ptankx + 25 > hit_x > ptankx - 25: 411 | print("Medium Hit") 412 | damage = 10 413 | elif ptankx + 35 > hit_x > ptankx - 35: 414 | print("Light Hit") 415 | damage = 5 416 | 417 | explosion(hit_x, hit_y) 418 | fire = False 419 | 420 | check_x_1 = startingShell[0] <= xlocation + barrier_width 421 | check_x_2 = startingShell[0] >= xlocation 422 | 423 | check_y_1 = startingShell[1] <= display_height 424 | check_y_2 = startingShell[1] >= display_height - randomHeight 425 | 426 | if check_x_1 and check_x_2 and check_y_1 and check_y_2: 427 | print("Last shell:", startingShell[0], startingShell[1]) 428 | hit_x = int((startingShell[0])) 429 | hit_y = int(startingShell[1]) 430 | print("Impact:", hit_x, hit_y) 431 | explosion(hit_x, hit_y) 432 | fire = False 433 | 434 | pygame.display.update() 435 | clock.tick(60) 436 | return damage 437 | 438 | #--------------------------- function for power level of players tank------------------------------------- 439 | def power(level): 440 | text = smallfont.render("Power: " + str(level) + "%", True, wheat) 441 | gameDisplay.blit(text, [display_width / 2, 0]) 442 | 443 | #---------------------------function for intro screen------------------------------------------------------ 444 | def game_intro(): 445 | intro = True 446 | 447 | while intro: 448 | for event in pygame.event.get(): 449 | # print(event) 450 | if event.type == pygame.QUIT: 451 | pygame.quit() 452 | quit() 453 | 454 | if event.type == pygame.KEYDOWN: 455 | if event.key == pygame.K_c: 456 | intro = False 457 | elif event.key == pygame.K_q: 458 | 459 | pygame.quit() 460 | quit() 461 | 462 | gameDisplay.fill(black) 463 | message_to_screen("Welcome to Tanks!", white, -100, size="large") 464 | message_to_screen("The objective is to shoot and destroy", wheat, 15) 465 | message_to_screen("the enemy tank before they destroy you.", wheat, 60) 466 | message_to_screen("The more enemies you destroy, the harder they get.", wheat, 110) 467 | message_to_screen("Created by :- Amar Kumar", wheat, 280) 468 | # message_to_screen("Press C to play, P to pause or Q to quit",black,180) 469 | 470 | 471 | button("Play", 150, 500, 100, 50, wheat, light_green, action="play",size="vsmall") 472 | button("Controls", 350, 500, 100, 50, wheat, light_yellow, action="controls",size="vsmall") 473 | button("Quit", 550, 500, 100, 50, wheat, light_red, action="quit",size="vsmall") 474 | 475 | pygame.display.update() 476 | 477 | clock.tick(15) 478 | 479 | #---------------------------function for game Over screen------------------------------------------------------ 480 | def game_over(): 481 | game_over = True 482 | 483 | while game_over: 484 | for event in pygame.event.get(): 485 | # print(event) 486 | if event.type == pygame.QUIT: 487 | pygame.quit() 488 | quit() 489 | 490 | gameDisplay.fill(black) 491 | message_to_screen("Game Over", white, -100, size="large") 492 | message_to_screen("You died.", wheat, -30) 493 | 494 | button("Play Again", 150, 500, 150, 50, wheat, light_green, action="play") 495 | button("Controls", 350, 500, 100, 50, wheat, light_yellow, action="controls") 496 | button("Quit", 550, 500, 100, 50, wheat, light_red, action="quit") 497 | 498 | pygame.display.update() 499 | 500 | clock.tick(15) 501 | 502 | #---------------------------function for players win screen-------------------------------------------------- 503 | def you_win(): 504 | win = True 505 | 506 | while win: 507 | for event in pygame.event.get(): 508 | # print(event) 509 | if event.type == pygame.QUIT: 510 | pygame.quit() 511 | quit() 512 | 513 | gameDisplay.fill(black) 514 | message_to_screen("You won!", white, -100, size="large") 515 | message_to_screen("Congratulations!", wheat, -30) 516 | 517 | button("play Again", 150, 500, 150, 50, wheat, light_green, action="play") 518 | button("controls", 350, 500, 100, 50, wheat, light_yellow, action="controls") 519 | button("quit", 550, 500, 100, 50, wheat, light_red, action="quit") 520 | 521 | pygame.display.update() 522 | 523 | clock.tick(15) 524 | 525 | #---------------------------function for health bars of both tanks--------------------------------------- 526 | def health_bars(player_health, enemy_health): 527 | if player_health > 75: 528 | player_health_color = green 529 | elif player_health > 50: 530 | player_health_color = yellow 531 | else: 532 | player_health_color = red 533 | 534 | if enemy_health > 75: 535 | enemy_health_color = green 536 | elif enemy_health > 50: 537 | enemy_health_color = yellow 538 | else: 539 | enemy_health_color = red 540 | 541 | pygame.draw.rect(gameDisplay, player_health_color, (680, 25, player_health, 25)) 542 | pygame.draw.rect(gameDisplay, enemy_health_color, (20, 25, enemy_health, 25)) 543 | 544 | #---------------------------function for main gameloop---------------------------------------------------- 545 | def gameLoop(): 546 | gameExit = False 547 | gameOver = False 548 | FPS = 15 549 | 550 | player_health = 100 551 | enemy_health = 100 552 | 553 | barrier_width = 50 554 | 555 | mainTankX = display_width * 0.9 556 | mainTankY = display_height * 0.9 557 | tankMove = 0 558 | currentTurPos = 0 559 | changeTur = 0 560 | 561 | enemyTankX = display_width * 0.1 562 | enemyTankY = display_height * 0.9 563 | 564 | fire_power = 50 565 | power_change = 0 566 | 567 | xlocation = (display_width / 2) + random.randint(-0.1 * display_width, 0.1 * display_width) 568 | randomHeight = random.randrange(display_height * 0.1, display_height * 0.6) 569 | 570 | while not gameExit: 571 | 572 | if gameOver == True: 573 | # gameDisplay.fill(white) 574 | message_to_screen("Game Over", red, -50, size="large") 575 | message_to_screen("Press C to play again or Q to exit", black, 50) 576 | pygame.display.update() 577 | while gameOver == True: 578 | for event in pygame.event.get(): 579 | if event.type == pygame.QUIT: 580 | gameExit = True 581 | gameOver = False 582 | 583 | if event.type == pygame.KEYDOWN: 584 | if event.key == pygame.K_c: 585 | gameLoop() 586 | elif event.key == pygame.K_q: 587 | 588 | gameExit = True 589 | gameOver = False 590 | 591 | for event in pygame.event.get(): 592 | 593 | if event.type == pygame.QUIT: 594 | gameExit = True 595 | 596 | if event.type == pygame.KEYDOWN: 597 | if event.key == pygame.K_LEFT: 598 | tankMove = -5 599 | 600 | elif event.key == pygame.K_RIGHT: 601 | tankMove = 5 602 | 603 | elif event.key == pygame.K_UP: 604 | changeTur = 1 605 | 606 | elif event.key == pygame.K_DOWN: 607 | changeTur = -1 608 | 609 | elif event.key == pygame.K_p: 610 | pause() 611 | 612 | elif event.key == pygame.K_SPACE: 613 | 614 | damage = fireShell(gun, mainTankX, mainTankY, currentTurPos, fire_power, xlocation, barrier_width, 615 | randomHeight, enemyTankX, enemyTankY) 616 | enemy_health -= damage 617 | 618 | possibleMovement = ['f', 'r'] 619 | moveIndex = random.randrange(0, 2) 620 | 621 | for x in range(random.randrange(0, 10)): 622 | 623 | if display_width * 0.3 > enemyTankX > display_width * 0.03: 624 | if possibleMovement[moveIndex] == "f": 625 | enemyTankX += 5 626 | elif possibleMovement[moveIndex] == "r": 627 | enemyTankX -= 5 628 | 629 | gameDisplay.fill(black) 630 | health_bars(player_health, enemy_health) 631 | gun = tank(mainTankX, mainTankY, currentTurPos) 632 | enemy_gun = enemy_tank(enemyTankX, enemyTankY, 8) 633 | fire_power += power_change 634 | 635 | power(fire_power) 636 | 637 | barrier(xlocation, randomHeight, barrier_width) 638 | gameDisplay.fill(green, 639 | rect=[0, display_height - ground_height, display_width, ground_height]) 640 | pygame.display.update() 641 | 642 | clock.tick(FPS) 643 | 644 | damage = e_fireShell(enemy_gun, enemyTankX, enemyTankY, 8, 50, xlocation, barrier_width, 645 | randomHeight, mainTankX, mainTankY) 646 | player_health -= damage 647 | 648 | elif event.key == pygame.K_a: 649 | power_change = -1 650 | elif event.key == pygame.K_d: 651 | power_change = 1 652 | 653 | elif event.type == pygame.KEYUP: 654 | if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: 655 | tankMove = 0 656 | 657 | if event.key == pygame.K_UP or event.key == pygame.K_DOWN: 658 | changeTur = 0 659 | 660 | if event.key == pygame.K_a or event.key == pygame.K_d: 661 | power_change = 0 662 | 663 | mainTankX += tankMove 664 | 665 | currentTurPos += changeTur 666 | 667 | if currentTurPos > 8: 668 | currentTurPos = 8 669 | elif currentTurPos < 0: 670 | currentTurPos = 0 671 | 672 | if mainTankX - (tankWidth / 2) < xlocation + barrier_width: 673 | mainTankX += 5 674 | 675 | gameDisplay.fill(black) 676 | health_bars(player_health, enemy_health) 677 | gun = tank(mainTankX, mainTankY, currentTurPos) 678 | enemy_gun = enemy_tank(enemyTankX, enemyTankY, 8) 679 | 680 | fire_power += power_change 681 | 682 | if fire_power > 100: 683 | fire_power = 100 684 | elif fire_power < 1: 685 | fire_power = 1 686 | 687 | power(fire_power) 688 | 689 | barrier(xlocation, randomHeight, barrier_width) 690 | gameDisplay.fill(green, rect=[0, display_height - ground_height, display_width, ground_height]) 691 | pygame.display.update() 692 | 693 | if player_health < 1: 694 | game_over() 695 | elif enemy_health < 1: 696 | you_win() 697 | clock.tick(FPS) 698 | 699 | pygame.quit() 700 | quit() 701 | 702 | game_intro() 703 | gameLoop() --------------------------------------------------------------------------------