Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. import random
  2. import sys
  3. import pygame
  4. import math
  5. from pygame.locals import *
  6.  
  7. FPS = 60
  8. SCREEN_SIZE = (1280, 720)
  9.  
  10.  
  11. class Colors:
  12. BLACK = (0, 0, 0)
  13. BLUE = (0, 191, 255)
  14.  
  15.  
  16. class Astronaut:
  17. def __init__(self):
  18. image = pygame.image.load('astronaut.png')
  19. astronaut_scale = (1 / 4)
  20. scale_tuple = (int(image.get_width() * astronaut_scale), int(image.get_height() * astronaut_scale))
  21. self.image = pygame.transform.scale(image, scale_tuple)
  22.  
  23. self.position = (100, 350)
  24. self.velocity = (0, 0)
  25.  
  26. def move(self, right, down):
  27.  
  28. speed = 10
  29.  
  30. vx = self.velocity[0]
  31. vy = self.velocity[1]
  32.  
  33. vx += right * speed
  34. vy += down * speed
  35.  
  36. self.velocity = (vx, vy)
  37.  
  38. def update(self):
  39.  
  40. vx = self.velocity[0]
  41. vy = self.velocity[1]
  42.  
  43. # Inertia
  44.  
  45. vx *= 0.99
  46. vy *= 0.99
  47.  
  48. # Movement
  49.  
  50. x = self.position[0] + (vx / FPS)
  51. y = self.position[1] + (vy / FPS)
  52.  
  53. # Prevent escaping up or down
  54.  
  55. if y < (self.image.get_height() / 2):
  56. y = (self.image.get_height() / 2)
  57. if vy < 0:
  58. vy = 0
  59. elif y > SCREEN_SIZE[1] - (self.image.get_height() / 2):
  60. y = SCREEN_SIZE[1] - (self.image.get_height() / 2)
  61. if vy > 0:
  62. vy = 0
  63.  
  64. self.velocity = (vx, vy)
  65. self.position = (x, y)
  66.  
  67. def draw(self, screen):
  68. rotated_astronaut = pygame.transform.rotate(self.image, 0)
  69. rect = rotated_astronaut.get_rect()
  70. rect.center = self.position
  71. screen.blit(rotated_astronaut, rect)
  72.  
  73.  
  74. class Game:
  75. def __init__(self):
  76.  
  77. pygame.init()
  78.  
  79. self.clock = pygame.time.Clock()
  80. self.screen = pygame.display.set_mode(SCREEN_SIZE)
  81.  
  82. # Astronaut
  83. self.astronaut = Astronaut()
  84.  
  85. # Background
  86. background_image = pygame.image.load("space.jpeg").convert()
  87. self.background_image = pygame.transform.scale(background_image, SCREEN_SIZE)
  88.  
  89. # Music
  90. pygame.mixer.music.load('spacesong.mp3')
  91. pygame.mixer.music.set_volume(0.02)
  92. pygame.mixer.music.play(-1)
  93.  
  94. # Display
  95. icon = pygame.image.load("astronaut.png")
  96. pygame.display.set_icon(icon)
  97. pygame.display.set_caption("Astronaut Invaders")
  98. pygame.display.update()
  99.  
  100. def game_intro(self):
  101. intro = True
  102.  
  103. large_text = pygame.font.Font('freesansbold.ttf', 115)
  104. text_surf = large_text.render("Astronaut Invaders", False, Colors.BLACK)
  105.  
  106. noob_text = pygame.font.Font('freesansbold.ttf', 48)
  107. intro_text = noob_text.render("Press Any key to continue", False, Colors.BLACK)
  108.  
  109. intro_image = pygame.image.load("moon.jpg").convert()
  110.  
  111. astro_x = 200
  112. astro_y = 200
  113.  
  114. while intro:
  115. for event in pygame.event.get():
  116. if event.type == pygame.QUIT:
  117. pygame.quit()
  118. quit()
  119. if event.type == pygame.KEYDOWN:
  120. intro = False
  121.  
  122. astro_x += random.randint(-10, 10)
  123. astro_y += random.randint(-10, 10)
  124.  
  125. self.screen.blit(intro_image, (0, 0))
  126. self.screen.blit(self.astronaut.image, (astro_x, astro_y))
  127. self.screen.blit(text_surf, (100, 300))
  128. self.screen.blit(intro_text, (100, 400))
  129.  
  130. pygame.display.update()
  131. pygame.display.flip()
  132.  
  133. self.clock.tick(60)
  134.  
  135. def start(self):
  136.  
  137. while True:
  138. self.loop()
  139.  
  140. def loop(self):
  141. self.clock.tick(FPS)
  142.  
  143. self.update()
  144. self.draw()
  145.  
  146. def update(self):
  147.  
  148. keys = pygame.key.get_pressed()
  149.  
  150. down = 0
  151.  
  152. if keys[pygame.K_UP]:
  153. down -= 1
  154. if keys[pygame.K_DOWN]:
  155. down += 1
  156. if keys[pygame.K_ESCAPE]:
  157. pygame.quit()
  158. sys.exit(0)
  159.  
  160. pygame.event.pump()
  161.  
  162. self.astronaut.move(0, down)
  163.  
  164. self.astronaut.update()
  165.  
  166. def draw(self):
  167.  
  168. self.screen.fill(Colors.BLACK)
  169. self.screen.blit(self.background_image, [0, 0])
  170.  
  171. self.astronaut.draw(self.screen)
  172. pygame.display.flip()
  173.  
  174.  
  175. game = Game()
  176. game.game_intro()
  177. game.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement