Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.06 KB | None | 0 0
  1. import pygame, time, random, math
  2. print("Z and C control enemy firerate. A and S control enemy horizontal speed. \n"
  3. "WARNING:THESE FEATURES ARE EXPERIMENTAL!")
  4. pygame.mixer.pre_init()
  5. pygame.init()
  6. # -------------------------------------------------------------------------------
  7. w_width = 800
  8. w_height = 600
  9. surface = pygame.display.set_mode((w_width, w_height))
  10. bullet_num = -1
  11. left = False
  12. right = False
  13. spaceship_size = 50
  14. spaceship = pygame.image.load("spaceship.png").convert_alpha()
  15. spaceship_left = pygame.image.load("spaceship_left.png").convert_alpha()
  16. spaceship_right = pygame.image.load("spaceship_right.png").convert_alpha()
  17. bg = pygame.image.load("spaceLOL.jpg").convert_alpha()
  18. icon = pygame.image.load("enemy_sprite.png")
  19. nemy = pygame.image.load("enemy_sprite.png").convert_alpha()# jake
  20. pygame.display.set_icon(icon)
  21. # -------------------------------------------------------------------------------
  22. fps = 30
  23. game_Start = False
  24. font = pygame.font.SysFont(None, 70)
  25. # -------------------------------------------------------------------------------
  26. e_x = 100
  27. e_y = 100
  28. e_mspd = 10
  29. e_right = True
  30. e_left = False
  31. e_range = 10
  32. e_y_area = e_y + nemy.get_rect().height
  33. e_x_area = e_x + nemy.get_rect().width
  34. e_bullet_num = -1
  35. e_bullet = {}
  36. e_lowest_bull = 0
  37. e_sin = 0
  38. bossman_health = 5
  39. boss_interval = 0
  40. difficulty = 0
  41. # -------------------------------------------------------------------------------
  42. bullet = {}
  43. lowest_bull = 0
  44. playerlife = 5
  45.  
  46.  
  47. # -------------------------------------------------------------------------------
  48. def gamemusic():
  49. music = pygame.mixer.Sound("spoopy_scary_skeletor.wav")
  50. music.play()
  51.  
  52.  
  53. def message(msg, color):
  54. screen_text = font.render(msg, True, color)
  55. surface.blit(screen_text,
  56. [(w_width / 2) - (screen_text.get_rect().width / 2),
  57. (w_height / 2) - (screen_text.get_rect().height / 2)])
  58.  
  59.  
  60. def gamerestart():
  61. global bossman_health, e_x, e_y, bullet, bullet_num, e_bullet_num, \
  62. boss_interval, lowest_bull, e_lowest_bull, playerlife
  63. playerlife = 5
  64. e_x = 100
  65. e_y = 100
  66. e_bullet_num = -1
  67. boss_interval = 0
  68. bossman_health = 5
  69. bullet = {}
  70. bullet_num = -1
  71. lowest_bull = 0
  72. e_lowest_bull = 0
  73.  
  74.  
  75. # -------------------------------------------------------------------------------
  76. def ai():
  77. global e_x, e_y, bossman_health, e_right, e_left, \
  78. boss_interval, e_bullet, e_bullet_num, bullet, jump_interval, e_sin
  79. boss_interval += 1
  80. e_sin += 0.1
  81. if e_x <= 0:
  82. e_right = True
  83. e_left = False
  84. elif e_x >= (w_width - nemy.get_rect().width):
  85. e_left = True
  86. e_right = False
  87. if e_right == True:
  88. e_x += e_mspd
  89. if e_left == True:
  90. e_x -= e_mspd
  91. if e_sin >= (math.pi * 2):
  92. e_sin = 0
  93. e_y = 100 + (math.sin(e_sin)*50)
  94. e_y_area = e_y + nemy.get_rect().height
  95. e_x_area = e_x + nemy.get_rect().width
  96. for bull_num in range(lowest_bull, bullet_num + 1):
  97. if bullet[bull_num][0] >= e_x and bullet[bull_num][0] <= e_x_area \
  98. and bullet[bull_num][1] >= e_y and bullet[bull_num][1] <= e_y_area:
  99. bullet[bull_num][0] = w_width + 1
  100. bossman_health -= 1
  101. surface.blit(nemy, (e_x, e_y))
  102. if boss_interval >= fire_rate:
  103. boss_interval = 0
  104. e_bullet_num += 1
  105. e_bullet[e_bullet_num] = [e_x + (nemy.get_rect().width / 2),
  106. e_y + nemy.get_rect().height]
  107.  
  108.  
  109. def player_move(x, y):
  110. global playerlife, e_bullet
  111. p_x_area = x + spaceship.get_rect().width
  112. p_y_area = y + spaceship.get_rect().height
  113. if left == True:
  114. surface.blit(spaceship_left, (x, y))
  115. elif right == True:
  116. surface.blit(spaceship_right, (x, y))
  117. else:
  118. surface.blit(spaceship, (x, y))
  119. for bull_num in range(e_lowest_bull, e_bullet_num + 1):
  120. if e_bullet[bull_num][0] >= x and e_bullet[bull_num][0] <= p_x_area \
  121. and e_bullet[bull_num][1] >= y and e_bullet[bull_num][1] <= p_y_area:
  122. e_bullet[bull_num][0] = w_width + 1
  123. playerlife -= 1
  124.  
  125.  
  126. # -------------------------------------------------------------------------------
  127. def bullet_make(x, y):
  128. global bullet_num, bullet, surface
  129. bullet_num += 1
  130. bullet[bullet_num] = [x + (spaceship.get_rect().width / 2) - 5, y]
  131. def bullet_move():
  132. global bullet, bullet_num, surface, lowest_bull
  133. for bull_num in range(lowest_bull, bullet_num + 1):
  134. try:
  135. bullet[bull_num][1] -= 20
  136. except KeyError:
  137. print("Neverlucky")
  138. if bullet[bull_num][1] <= 0:
  139. bullet.pop(bull_num)
  140. lowest_bull += 1
  141. else:
  142. pygame.draw.rect(surface, (0, 255, 0),
  143. (bullet[bull_num][0], bullet[bull_num][1], 10, 10))
  144.  
  145.  
  146. def ai_bullet_update():
  147. global e_x, e_y, bossman_health, e_bullet, e_lowest_bull
  148. for bull_num in range(e_lowest_bull, e_bullet_num + 1):
  149. e_bullet[bull_num][1] += 10
  150. if e_bullet[bull_num][1] >= w_height:
  151. e_lowest_bull += 1
  152. e_bullet.pop(bull_num)
  153. else:
  154. pygame.draw.rect(surface, (255, 255, 0),
  155. (e_bullet[bull_num][0], e_bullet[bull_num][1], 10, 10))
  156.  
  157.  
  158. # -------------------------------------------------------------------------------
  159. def gamestart():
  160. global game_Start
  161. pygame.display.set_caption("hello there!")
  162. while not game_Start:
  163. for event in pygame.event.get():
  164. if event.type == pygame.QUIT:
  165. pygame.quit()
  166. quit()
  167. if event.type == pygame.KEYDOWN:
  168. if event.key == pygame.K_x:
  169. game_Start = True
  170. gamerestart()
  171. gameloop()
  172. surface.blit(bg, (0, 0))
  173. message("Press the x button to start!", (0, 255, 0))
  174. pygame.display.update()
  175.  
  176.  
  177. def gameloop():
  178. global left, right, bg, game_Start, fire_rate, difficulty, e_mspd, game_Start, bossman_health, e_x, e_y
  179. mspd = 15
  180. p_x_change = 0
  181. p_y_change = 0
  182. p_x = 400
  183. p_y = 300
  184. pygame.display.set_caption("TOUHOU GAME")
  185. game_Exit = False
  186. while not game_Exit:
  187. for event in pygame.event.get():
  188. if event.type == pygame.QUIT:
  189. game_Exit = True
  190. game_Start = False
  191. gamestart()
  192. if event.type == pygame.KEYDOWN:
  193. if event.key == pygame.K_UP:
  194. p_x_change = 0
  195. p_y_change = -mspd
  196.  
  197. if event.key == pygame.K_DOWN:
  198. p_x_change = 0
  199. p_y_change = mspd
  200.  
  201. if event.key == pygame.K_LEFT:
  202. left = True
  203. p_y_change = 0
  204. p_x_change = -mspd
  205.  
  206. if event.key == pygame.K_RIGHT:
  207. right = True
  208. p_y_change = 0
  209. p_x_change = mspd
  210. if event.type == pygame.KEYUP:
  211. if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
  212. p_y_change = 0
  213. if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
  214. left = False
  215. right = False
  216. p_x_change = 0
  217. if event.key == pygame.K_x:
  218. bullet_make(p_x, p_y)
  219. if event.key == pygame.K_z:
  220. difficulty += 1
  221. if event.key == pygame.K_c:
  222. difficulty -= 1
  223. if event.key == pygame.K_a:
  224. e_mspd += 1
  225. if event.key == pygame.K_s:
  226. e_mspd -= 1
  227. p_x += p_x_change
  228. p_y += p_y_change
  229. if p_x <= 0:
  230. p_x = 5
  231. if p_x >= w_width - spaceship_size:
  232. p_x = w_width - spaceship_size
  233. if p_y <= 0:
  234. p_y = 5
  235. if p_y >= w_height - spaceship_size:
  236. p_y = w_height - spaceship_size
  237. fire_rate = 60 - difficulty
  238. surface.blit(bg, (0, 0))
  239. if bossman_health >= 1:
  240. ai()
  241. ai_bullet_update()
  242. if bossman_health <= 0:
  243. difficulty += 5
  244. bossman_health = difficulty * 2
  245. surface.blit(bg, (0, 0))
  246. message("Boss " + str(difficulty/5) + " defeated!", (0,255,0))
  247. pygame.display.update()
  248. time.sleep(2)
  249. e_x = random.randint(0,w_width)
  250. e_y = random.randint(0,200)
  251. if playerlife > 0:
  252. player_move(p_x, p_y)
  253. bullet_move()
  254. elif playerlife <= 0:
  255. message("your died lol", (255, 0, 0))
  256. pygame.display.update()
  257. time.sleep(3)
  258. game_Exit = True
  259. game_Start = False
  260. gamestart()
  261. player_move(p_x, p_y)
  262. pygame.time.Clock().tick(fps)
  263. pygame.display.update()
  264. gamestart()
  265. gamemusic()
  266. pygame.quit()
  267. quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement