Advertisement
skip420

shooter

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