Advertisement
ithinkrok

Untitled

Apr 30th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.33 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 = 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. self.image = game.alien_image
  130.  
  131. self.position = pos
  132. self.size = (self.image.get_width(), self.image.get_height())
  133. self.health = 100
  134.  
  135. def update(self) -> bool:
  136. self.position = (self.position[0] - 100 / FPS, self.position[1])
  137.  
  138. return self.health > 0 and self.position[0] > -100
  139.  
  140. def draw(self, screen):
  141. screen.blit(self.image, self.position)
  142.  
  143.  
  144. class Game:
  145. def __init__(self):
  146.  
  147. pygame.init()
  148.  
  149. self.clock = pygame.time.Clock()
  150. self.screen = pygame.display.set_mode(SCREEN_SIZE)
  151.  
  152. # Astronaut
  153. self.astronaut = Astronaut()
  154.  
  155. # Background
  156. background_image = pygame.image.load("space.jpeg").convert()
  157. self.background_image = pygame.transform.scale(background_image, SCREEN_SIZE)
  158. self.background_move = 0
  159.  
  160. # Lasers
  161. self.lasers = []
  162. self.laser_image = pygame.image.load("space_bullet.png")
  163. self.laser_image = pygame.transform.scale(self.laser_image,
  164. (int(self.laser_image.get_width() / 2),
  165. int(self.laser_image.get_height() / 2)))
  166. self.laser_sound = pygame.mixer.Sound("blaster.wav")
  167. self.laser_sound.set_volume(0.05)
  168. self.laser_reload = 0
  169.  
  170. # Aliens
  171. self.aliens = []
  172. self.alien_image = pygame.image.load("ufo.png")
  173. self.last_alien_spawn_time = 0.0
  174.  
  175. # Music
  176. pygame.mixer.music.load('spacesong.mp3')
  177. pygame.mixer.music.set_volume(0.02)
  178. pygame.mixer.music.play(-1)
  179.  
  180. # Display
  181. icon = pygame.image.load("astronaut.png")
  182. pygame.display.set_icon(icon)
  183. pygame.display.set_caption("Astronaut Invaders")
  184. pygame.display.update()
  185.  
  186. self.timer = 0.0
  187.  
  188. def game_intro(self):
  189. intro = True
  190.  
  191. text_surf = pygame.image.load("astronaut_invaderstext.png")
  192.  
  193. intro_image = pygame.image.load("moon.jpg").convert()
  194.  
  195. astro_x = 200
  196. astro_y = 200
  197.  
  198. astro_vx = 0
  199. astro_vy = 0
  200.  
  201. t = 0
  202.  
  203. while intro:
  204. for event in pygame.event.get():
  205. if event.type == pygame.QUIT:
  206. pygame.quit()
  207. quit()
  208. if event.type == pygame.KEYDOWN:
  209. intro = False
  210.  
  211. t += 1
  212.  
  213. astro_vx += random.uniform(-0.2, 0.2)
  214. astro_vy += random.uniform(-0.2, 0.2)
  215.  
  216. astro_vx += math.cos(t * 0.01) * 0.01
  217. astro_vy += math.sin(t * 0.01) * 0.01
  218.  
  219. astro_x += astro_vx
  220. astro_y += astro_vy
  221.  
  222. astro_width = self.astronaut.image.get_width()
  223. astro_height = self.astronaut.image.get_height()
  224.  
  225. if astro_x < 0:
  226. astro_x = 0
  227. astro_vx = 0
  228. if astro_x > SCREEN_SIZE[0] - astro_width:
  229. astro_x = SCREEN_SIZE[0] - astro_width
  230. astro_vx = 0
  231. if astro_y < 0:
  232. astro_y = 0
  233. astro_vy = 0
  234. if astro_y > SCREEN_SIZE[1] - astro_height:
  235. astro_y = SCREEN_SIZE[1] - astro_height
  236. astro_vy = 0
  237.  
  238. self.screen.blit(intro_image, (0, 0))
  239. self.screen.blit(self.astronaut.image, (astro_x, astro_y))
  240. self.screen.blit(text_surf, (80, 300))
  241.  
  242. pygame.display.update()
  243. pygame.display.flip()
  244.  
  245. self.clock.tick(60)
  246.  
  247. intro_voice = pygame.mixer.Sound('Intro_voice.wav')
  248. intro_voice.set_volume(0.5)
  249. # Loop 1/100 times
  250. intro_voice.play(math.ceil(random.uniform(0.01, 1.01) - 1))
  251.  
  252. def start(self):
  253.  
  254. while True:
  255. self.loop()
  256.  
  257. def loop(self):
  258. self.clock.tick(FPS)
  259.  
  260. self.update()
  261. self.draw()
  262.  
  263. def update(self):
  264.  
  265. self.timer += (1 / FPS)
  266. self.background_move += (10 / FPS)
  267.  
  268. if self.laser_reload > 0:
  269. self.laser_reload -= (1 / FPS)
  270.  
  271. for event in pygame.event.get():
  272. if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE and self.laser_reload <= 0:
  273. self.fire_laser()
  274. self.laser_reload = 0.3
  275.  
  276. keys = pygame.key.get_pressed()
  277.  
  278. down = 0
  279.  
  280. if keys[pygame.K_UP]:
  281. down -= 1
  282. if keys[pygame.K_DOWN]:
  283. down += 1
  284. if keys[pygame.K_ESCAPE]:
  285. pygame.quit()
  286. sys.exit(0)
  287.  
  288. pygame.event.pump()
  289.  
  290. self.astronaut.move(0, down)
  291.  
  292. self.astronaut.update()
  293.  
  294. new_lasers = []
  295.  
  296. for laser in self.lasers:
  297. if laser.update():
  298. new_lasers.append(laser)
  299.  
  300. self.lasers = new_lasers
  301.  
  302. new_aliens = []
  303.  
  304. for alien in self.aliens:
  305. if alien.update():
  306. new_aliens.append(alien)
  307.  
  308. self.aliens = new_aliens
  309.  
  310. time_between = 2 - 0.02 * self.timer
  311.  
  312. if time_between < 0.3:
  313. time_between = 0.3
  314.  
  315. if self.timer - self.last_alien_spawn_time > time_between:
  316. self.last_alien_spawn_time = self.timer
  317.  
  318. self.aliens.append(Alien((SCREEN_SIZE[0], random.randint(50, SCREEN_SIZE[1] - 50))))
  319.  
  320. def fire_laser(self):
  321.  
  322. pos = (self.astronaut.position[0], self.astronaut.position[1])
  323.  
  324. self.lasers.append(Laser(self.laser_image, pos))
  325.  
  326. self.laser_sound.play()
  327.  
  328. def draw(self):
  329.  
  330. self.screen.fill(Colors.BLACK)
  331.  
  332. if self.background_move > SCREEN_SIZE[0]:
  333. self.background_move -= SCREEN_SIZE[0]
  334.  
  335. self.screen.blit(self.background_image, [-self.background_move, 0])
  336. self.screen.blit(self.background_image, [SCREEN_SIZE[0] - self.background_move, 0])
  337.  
  338. for alien in self.aliens:
  339. alien.draw(self.screen)
  340.  
  341. for laser in self.lasers:
  342. laser.draw(self.screen)
  343.  
  344. self.astronaut.draw(self.screen)
  345. pygame.display.flip()
  346.  
  347.  
  348. game = Game()
  349. game.game_intro()
  350. game.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement