Advertisement
thenewboston

[source code] Pygame (Python Game Development) Tutorial 84

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