Advertisement
Arcot

fighterplane main

Sep 1st, 2022
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.40 KB | None | 0 0
  1. import pygame
  2. import random
  3. from elements import *
  4.  
  5. pygame.init()
  6. BLUE = (135, 206, 235)
  7. ORANGE = (255,128, 0)
  8. GRAY = (10, 10, 10)
  9. screen = pygame.display.set_mode((800, 400))
  10. pygame.display.set_caption("fighterplane")
  11. screen.fill(BLUE)
  12. pygame.display.update()
  13.  
  14. run = True
  15. dx, dy = 0, 0
  16. font = pygame.font.Font('mygamefont.ttf',40)
  17. fontsmall = pygame.font.Font('mygamefont.ttf',15)
  18. death_sound = pygame.mixer.Sound('death.wav')
  19. updown_sound = pygame.mixer.Sound('killenemy.wav')
  20.  
  21. hscore=0
  22. #context manager
  23. with open('highscore.txt', 'r') as high_file:
  24.     tempscore = high_file.read()
  25. for line in tempscore:
  26.     for i in line:
  27.         if i.isdigit()==True:
  28.             hscore+=int(i)
  29.  
  30.  
  31. score = 0
  32. timer = 0
  33. alive = True
  34. new_hscore = False
  35. bullet_count = 50
  36. down = False
  37.  
  38. player = Ship()
  39. bullets = []
  40. clouds = []
  41. enemies = []
  42. stars = []
  43.  
  44. def gameover():
  45.     death_sound.play()
  46.     text = font.render('Game over!', True, ORANGE)
  47.     screen.blit(text, (300, 150))
  48.     pygame.display.update()
  49.     pygame.time.delay(2000)
  50.     with open('highscore.txt', 'w') as high_file:
  51.         high_file.write(str(hscore))
  52.  
  53. while run and alive:
  54.     screen.fill(BLUE)
  55.     pygame.time.delay(50)
  56.     for event in pygame.event.get():
  57.         if event.type == pygame.QUIT:
  58.             run = False
  59.         if event.type == pygame.KEYDOWN:
  60.             if event.key == pygame.K_DOWN:
  61.                 dx, dy = 0, 10
  62.             elif event.key == pygame.K_UP:
  63.                 dx, dy = 0, -10
  64.             elif event.key == pygame.K_LEFT:
  65.                 dx, dy = -10, 0
  66.             elif event.key == pygame.K_RIGHT:
  67.                 dx, dy = 10, 0
  68.             elif event.key == pygame.K_SPACE:
  69.                 down = True    
  70.         if event.type == pygame.KEYUP:
  71.             dx, dy = 0, 0
  72.             down = False
  73.  
  74.     player.move(dx, dy)
  75.     dead_e = []
  76.     dead_b = []
  77.  
  78.  
  79.     for e in enemies:
  80.         for b in bullets:
  81.             if e.e_rect.colliderect(b.b_rect):
  82.                 updown_sound.play()
  83.                 if e not in dead_e:
  84.                     dead_e.append(e)
  85.                     score += 1
  86.                     if score>hscore:
  87.                         hscore = score
  88.                         new_hscore = True
  89.                         t = 0
  90.                 if b not in dead_b:
  91.                     dead_b.append(b)
  92.  
  93.     for e in dead_e:
  94.         enemies.remove(e)
  95.  
  96.     for b in dead_b:
  97.         bullets.remove(b)
  98.  
  99.     for e in enemies:
  100.         if e.e_rect.colliderect(player.s_rect):
  101.             gameover()
  102.             alive = False
  103.  
  104.     for s in stars:
  105.         if s.s_rect.colliderect(player.s_rect):
  106.             bullet_count += 10
  107.             stars.remove(s)
  108.             break
  109.            
  110.  
  111.  
  112.     text = fontsmall.render("Score: " + str(score), True, GRAY)
  113.     screen.blit(text, (5, 5))    
  114.     text = fontsmall.render("High score: " + str(hscore), True, GRAY)
  115.     screen.blit(text, (80, 5))
  116.     text = fontsmall.render("Bullets: " + str(bullet_count), True, GRAY)
  117.     screen.blit(text, (200, 5))  
  118.  
  119.     if timer % 50 == 0:
  120.         cloud = Cloud()
  121.         clouds.append(cloud)
  122.  
  123.     if timer % 20 == 0:
  124.         enemy = Enemy()
  125.         enemies.append(enemy)
  126.    
  127.     time_gap = 150
  128.     if timer % time_gap == 0:
  129.         star = Star()
  130.         stars.append(star)    
  131.  
  132.     if down:
  133.         if bullet_count > 0:
  134.             bullet = Bullet(player.s_rect.right, player.s_rect.centery)
  135.             bullets.append(bullet)
  136.             bullet_count -= 1
  137.  
  138.     for c in clouds:
  139.         if c.c_rect.right < 0:
  140.             clouds.remove(c)
  141.  
  142.     for e in enemies:
  143.         if e.e_rect.right < 0:
  144.             enemies.remove(e)
  145.  
  146.     for b in bullets:
  147.         if b.b_rect.left > 800:
  148.             bullets.remove(b)
  149.  
  150.     for s in stars:
  151.         if s.s_rect.top > 450:
  152.             stars.remove(s)  
  153.  
  154.     if new_hscore and t < 50:
  155.         text = fontsmall.render('Congratulations, you beat the highscore!', True, ORANGE)
  156.         screen.blit(text, (350, 5))
  157.         t += 1                
  158.    
  159.     timer += 1
  160.     player.draw(screen)  
  161.  
  162.     for e in enemies:
  163.         e.move()
  164.         e.draw(screen)
  165.  
  166.     for b in bullets:
  167.         b.move()
  168.         b.draw(screen)    
  169.  
  170.     for c in clouds:
  171.         c.move()
  172.         c.draw(screen)
  173.  
  174.     for s in stars:
  175.         s.move()
  176.         s.draw(screen)        
  177.  
  178.     pygame.display.update()
  179.  
  180. pygame.quit()                
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement