Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- from elements import *
- pygame.init()
- BLUE = (135, 206, 235)
- ORANGE = (255,128, 0)
- GRAY = (10, 10, 10)
- screen = pygame.display.set_mode((800, 400))
- pygame.display.set_caption("fighterplane")
- screen.fill(BLUE)
- pygame.display.update()
- run = True
- dx, dy = 0, 0
- font = pygame.font.Font('mygamefont.ttf',40)
- fontsmall = pygame.font.Font('mygamefont.ttf',15)
- death_sound = pygame.mixer.Sound('death.wav')
- updown_sound = pygame.mixer.Sound('killenemy.wav')
- hscore=0
- #context manager
- with open('highscore.txt', 'r') as high_file:
- tempscore = high_file.read()
- for line in tempscore:
- for i in line:
- if i.isdigit()==True:
- hscore+=int(i)
- score = 0
- timer = 0
- alive = True
- new_hscore = False
- bullet_count = 50
- down = False
- player = Ship()
- bullets = []
- clouds = []
- enemies = []
- stars = []
- def gameover():
- death_sound.play()
- text = font.render('Game over!', True, ORANGE)
- screen.blit(text, (300, 150))
- pygame.display.update()
- pygame.time.delay(2000)
- with open('highscore.txt', 'w') as high_file:
- high_file.write(str(hscore))
- while run and alive:
- screen.fill(BLUE)
- pygame.time.delay(50)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- run = False
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_DOWN:
- dx, dy = 0, 10
- elif event.key == pygame.K_UP:
- dx, dy = 0, -10
- elif event.key == pygame.K_LEFT:
- dx, dy = -10, 0
- elif event.key == pygame.K_RIGHT:
- dx, dy = 10, 0
- elif event.key == pygame.K_SPACE:
- down = True
- if event.type == pygame.KEYUP:
- dx, dy = 0, 0
- down = False
- player.move(dx, dy)
- dead_e = []
- dead_b = []
- for e in enemies:
- for b in bullets:
- if e.e_rect.colliderect(b.b_rect):
- updown_sound.play()
- if e not in dead_e:
- dead_e.append(e)
- score += 1
- if score>hscore:
- hscore = score
- new_hscore = True
- t = 0
- if b not in dead_b:
- dead_b.append(b)
- for e in dead_e:
- enemies.remove(e)
- for b in dead_b:
- bullets.remove(b)
- for e in enemies:
- if e.e_rect.colliderect(player.s_rect):
- gameover()
- alive = False
- for s in stars:
- if s.s_rect.colliderect(player.s_rect):
- bullet_count += 10
- stars.remove(s)
- break
- text = fontsmall.render("Score: " + str(score), True, GRAY)
- screen.blit(text, (5, 5))
- text = fontsmall.render("High score: " + str(hscore), True, GRAY)
- screen.blit(text, (80, 5))
- text = fontsmall.render("Bullets: " + str(bullet_count), True, GRAY)
- screen.blit(text, (200, 5))
- if timer % 50 == 0:
- cloud = Cloud()
- clouds.append(cloud)
- if timer % 20 == 0:
- enemy = Enemy()
- enemies.append(enemy)
- time_gap = 150
- if timer % time_gap == 0:
- star = Star()
- stars.append(star)
- if down:
- if bullet_count > 0:
- bullet = Bullet(player.s_rect.right, player.s_rect.centery)
- bullets.append(bullet)
- bullet_count -= 1
- for c in clouds:
- if c.c_rect.right < 0:
- clouds.remove(c)
- for e in enemies:
- if e.e_rect.right < 0:
- enemies.remove(e)
- for b in bullets:
- if b.b_rect.left > 800:
- bullets.remove(b)
- for s in stars:
- if s.s_rect.top > 450:
- stars.remove(s)
- if new_hscore and t < 50:
- text = fontsmall.render('Congratulations, you beat the highscore!', True, ORANGE)
- screen.blit(text, (350, 5))
- t += 1
- timer += 1
- player.draw(screen)
- for e in enemies:
- e.move()
- e.draw(screen)
- for b in bullets:
- b.move()
- b.draw(screen)
- for c in clouds:
- c.move()
- c.draw(screen)
- for s in stars:
- s.move()
- s.draw(screen)
- pygame.display.update()
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement