Advertisement
ithinkrok

Untitled

Apr 30th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.22 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.0
  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. k = 0.45
  46. t = 1 / FPS
  47.  
  48. vx *= math.exp(- k * t)
  49. vy *= math.exp(- k * t)
  50.  
  51. # Movement
  52.  
  53. x = self.position[0] + (vx / FPS)
  54. y = self.position[1] + (vy / FPS)
  55.  
  56. # Prevent escaping up or down
  57.  
  58. if y < (self.image.get_height() / 2):
  59. y = (self.image.get_height() / 2)
  60. if vy < 0:
  61. vy = 0
  62. elif y > SCREEN_SIZE[1] - (self.image.get_height() / 2):
  63. y = SCREEN_SIZE[1] - (self.image.get_height() / 2)
  64. if vy > 0:
  65. vy = 0
  66.  
  67. self.velocity = (vx, vy)
  68. self.position = (x, y)
  69.  
  70. def draw(self, screen):
  71. rotated_astronaut = pygame.transform.rotate(self.image, 0)
  72. rect = rotated_astronaut.get_rect()
  73. rect.center = self.position
  74. screen.blit(rotated_astronaut, rect)
  75.  
  76.  
  77. class Laser:
  78. def __init__(self, image, pos):
  79. self.image = image
  80.  
  81. self.damage = 100
  82.  
  83. self.position = pos
  84. self.size = (self.image.get_width(), self.image.get_height())
  85.  
  86. def update(self) -> bool:
  87. closest_alien: Alien = None
  88.  
  89. fulldist = 1000 / FPS
  90.  
  91. while fulldist > 0 and self.damage > 0:
  92.  
  93. dist = fulldist
  94. for alien in game.aliens:
  95.  
  96. if alien.health <= 0:
  97. continue
  98.  
  99. if math.fabs(alien.position[1] - self.position[1]) > (self.size[1] + alien.size[1]) / 2:
  100. continue
  101.  
  102. alien_dist = alien.position[0] - self.position[0] - (self.size[0] + alien.size[1]) / 2
  103.  
  104. if alien_dist < dist:
  105. dist = alien_dist
  106. closest_alien = alien
  107.  
  108. self.position = (self.position[0] + dist, self.position[1])
  109. fulldist -= dist
  110.  
  111. if closest_alien is not None:
  112.  
  113. if closest_alien.health >= self.damage:
  114. closest_alien.health -= self.damage
  115. self.damage = 0
  116.  
  117. else:
  118. self.damage -= closest_alien.health
  119. closest_alien.health = 0
  120.  
  121. return self.damage > 0 and self.position[0] < SCREEN_SIZE[0] * 2
  122.  
  123. def draw(self, screen):
  124. screen.blit(self.image, self.position)
  125.  
  126.  
  127. class Alien:
  128. def __init__(self, pos):
  129.  
  130. self.image = game.alien_image
  131.  
  132. self.position = pos
  133. self.size = (self.image.get_width(), self.image.get_height())
  134. self.health = 100
  135.  
  136. def update(self) -> bool:
  137.  
  138. self.position = (self.position[0] - 100 / FPS, self.position[1])
  139.  
  140. return self.health > 0 and self.position[0] > -100
  141.  
  142. def draw(self, screen):
  143.  
  144. screen.blit(self.image, self.position)
  145.  
  146.  
  147. class Game:
  148. def __init__(self):
  149.  
  150. pygame.init()
  151.  
  152. self.clock = pygame.time.Clock()
  153. self.screen = pygame.display.set_mode(SCREEN_SIZE)
  154.  
  155. # Astronaut
  156. self.astronaut = Astronaut()
  157.  
  158. # Background
  159. background_image = pygame.image.load("space.jpeg").convert()
  160. self.background_image = pygame.transform.scale(background_image, SCREEN_SIZE)
  161. self.background_move = 0
  162.  
  163. # Lasers
  164. self.lasers = []
  165. self.laser_image = pygame.image.load("space_bullet.png")
  166. self.laser_image = pygame.transform.scale(self.laser_image,
  167. (int(self.laser_image.get_width() / 2),
  168. int(self.laser_image.get_height() / 2)))
  169. self.laser_sound = pygame.mixer.Sound("blaster.wav")
  170. self.laser_sound.set_volume(0.05)
  171. self.laser_reload = 0
  172.  
  173. # Aliens
  174. self.aliens = []
  175. self.alien_image = pygame.image.load("ufo.png")
  176. self.last_alien_spawn_time = 0.0
  177.  
  178. # Music
  179. pygame.mixer.music.load('spacesong.mp3')
  180. pygame.mixer.music.set_volume(0.02)
  181. pygame.mixer.music.play(-1)
  182.  
  183. # Display
  184. icon = pygame.image.load("astronaut.png")
  185. pygame.display.set_icon(icon)
  186. pygame.display.set_caption("Astronaut Invaders")
  187. pygame.display.update()
  188.  
  189. self.timer = 0.0
  190.  
  191. def game_intro(self):
  192. intro = True
  193.  
  194. text_surf = pygame.image.load("astronaut_invaderstext.png")
  195.  
  196. intro_image = pygame.image.load("moon.jpg").convert()
  197.  
  198. astro_x = 200
  199. astro_y = 200
  200.  
  201. astro_vx = 0
  202. astro_vy = 0
  203.  
  204. t = 0
  205.  
  206. while intro:
  207. for event in pygame.event.get():
  208. if event.type == pygame.QUIT:
  209. pygame.quit()
  210. quit()
  211. if event.type == pygame.KEYDOWN:
  212. intro = False
  213.  
  214. t += 1
  215.  
  216. astro_vx += random.uniform(-0.2, 0.2)
  217. astro_vy += random.uniform(-0.2, 0.2)
  218.  
  219. astro_vx += math.cos(t * 0.01) * 0.01
  220. astro_vy += math.sin(t * 0.01) * 0.01
  221.  
  222. astro_x += astro_vx
  223. astro_y += astro_vy
  224.  
  225. astro_width = self.astronaut.image.get_width()
  226. astro_height = self.astronaut.image.get_height()
  227.  
  228. if astro_x < 0:
  229. astro_x = 0
  230. astro_vx = 0
  231. if astro_x > SCREEN_SIZE[0] - astro_width:
  232. astro_x = SCREEN_SIZE[0] - astro_width
  233. astro_vx = 0
  234. if astro_y < 0:
  235. astro_y = 0
  236. astro_vy = 0
  237. if astro_y > SCREEN_SIZE[1] - astro_height:
  238. astro_y = SCREEN_SIZE[1] - astro_height
  239. astro_vy = 0
  240.  
  241. self.screen.blit(intro_image, (0, 0))
  242. self.screen.blit(self.astronaut.image, (astro_x, astro_y))
  243. self.screen.blit(text_surf, (80, 300))
  244.  
  245. pygame.display.update()
  246. pygame.display.flip()
  247.  
  248. self.clock.tick(60)
  249.  
  250. intro_voice = pygame.mixer.Sound('Intro_voice.wav')
  251. intro_voice.set_volume(0.5)
  252. # Loop 1/100 times
  253. intro_voice.play(math.ceil(random.uniform(0.01, 1.01) - 1))
  254.  
  255. def start(self):
  256.  
  257. while True:
  258. self.loop()
  259.  
  260. def loop(self):
  261. self.clock.tick(FPS)
  262.  
  263. self.update()
  264. self.draw()
  265.  
  266. def update(self):
  267.  
  268. self.timer += (1/FPS)
  269. self.background_move += (10 / FPS)
  270.  
  271. if self.laser_reload > 0:
  272. self.laser_reload -= (1 / FPS)
  273.  
  274. for event in pygame.event.get():
  275. if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE and self.laser_reload <= 0:
  276. self.fire_laser()
  277. self.laser_reload = 0.3
  278.  
  279. keys = pygame.key.get_pressed()
  280.  
  281. down = 0
  282.  
  283. if keys[pygame.K_UP]:
  284. down -= 1
  285. if keys[pygame.K_DOWN]:
  286. down += 1
  287. if keys[pygame.K_ESCAPE]:
  288. pygame.quit()
  289. sys.exit(0)
  290.  
  291. pygame.event.pump()
  292.  
  293. self.astronaut.move(0, down)
  294.  
  295. self.astronaut.update()
  296.  
  297. new_lasers = []
  298.  
  299. for laser in self.lasers:
  300. if laser.update():
  301. new_lasers.append(laser)
  302.  
  303. self.lasers = new_lasers
  304.  
  305. new_aliens = []
  306.  
  307. for alien in self.aliens:
  308. if alien.update():
  309. new_aliens.append(alien)
  310.  
  311. self.aliens = new_aliens
  312.  
  313. if self.timer - self.last_alien_spawn_time > 0.5:
  314. self.last_alien_spawn_time = self.timer
  315.  
  316. self.aliens.append(Alien((SCREEN_SIZE[0], random.randint(0, SCREEN_SIZE[1] - 50))))
  317.  
  318.  
  319. def fire_laser(self):
  320.  
  321. pos = (self.astronaut.position[0], self.astronaut.position[1])
  322.  
  323. self.lasers.append(Laser(self.laser_image, pos))
  324.  
  325. self.laser_sound.play()
  326.  
  327. def draw(self):
  328.  
  329. self.screen.fill(Colors.BLACK)
  330.  
  331. if self.background_move > SCREEN_SIZE[0]:
  332. self.background_move -= SCREEN_SIZE[0]
  333.  
  334. self.screen.blit(self.background_image, [-self.background_move, 0])
  335. self.screen.blit(self.background_image, [SCREEN_SIZE[0] - self.background_move, 0])
  336.  
  337. for alien in self.aliens:
  338. alien.draw(self.screen)
  339.  
  340. for laser in self.lasers:
  341. laser.draw(self.screen)
  342.  
  343. self.astronaut.draw(self.screen)
  344. pygame.display.flip()
  345.  
  346.  
  347. game = Game()
  348. game.game_intro()
  349. game.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement