Advertisement
Guest User

star wars

a guest
Jan 18th, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. import pygame
  2. import random
  3. import math
  4. pygame.init()
  5. from pygame import mixer
  6.  
  7. clock = pygame.time.Clock()
  8.  
  9. screen = pygame.display.set_mode((800, 500))
  10. pygame.display.set_caption('star wars')
  11. d = mixer.Sound("o.wav")
  12. d.play()
  13.  
  14. playerImg = pygame.image.load('ttt.png')
  15. playerX = 380
  16. playerY = 370
  17. playerX_change = 0
  18.  
  19. alienImg = []
  20. alienX = []
  21. alienY = []
  22. alienX_change = []
  23. alienY_change = []
  24. num_of_aliens = 12
  25.  
  26. p = 0
  27. playing = True
  28. falling = False
  29. count = 0
  30.  
  31. for i in range(num_of_aliens):
  32. alienImg.append(pygame.image.load('alien.png'))
  33. alienX.append(random.randint(0, 800))
  34. alienY.append(random.randint(20, 120))
  35. alienX_change.append(20)
  36. alienY_change.append(70)
  37.  
  38. bulletImg = pygame.image.load('bullet.png')
  39. bulletX = 1000
  40. bulletY = 378
  41. bulletY_change = 28
  42. bullet_state = "ready"
  43.  
  44. score_value = 0
  45. font = pygame.font.Font('Starjedi.ttf',32)
  46.  
  47. textY = 10
  48. textX = 10
  49.  
  50. game_font = pygame.font.Font('Broken-Detroit - PERSONAL USE ONLY.ttf',64)
  51.  
  52.  
  53. def show_score(x, y):
  54. score = font.render("score: " + str(score_value),True,(255,255,255))
  55. screen.blit(score, (x, y))
  56.  
  57. def you_win():
  58. win_text = game_font.render("YOU WIN",True, (255, 255, 255))
  59. screen.blit(win_text, (250, 180))
  60.  
  61.  
  62. def player(x, y):
  63. screen.blit(playerImg, (x, y))
  64.  
  65.  
  66. def alien(x, y, i):
  67. screen.blit(alienImg[i], (x, y))
  68.  
  69.  
  70. def fire_bullet(x, y):
  71. global bullet_state
  72. bullet_state = "fire"
  73. screen.blit(bulletImg, (x + 50, y + 10))
  74.  
  75. def isCollision(alienX,alienY,bulletX,bulletY):
  76. distance = math.sqrt(math.pow(alienX - bulletX, 2) + (math.pow(alienY - bulletY, 2)))
  77. if distance < 27:
  78. return True
  79. else:
  80. return False
  81.  
  82. def fall():
  83. global falling
  84. over_text = game_font.render("GAME OVER", True, (255, 255, 255))
  85. screen.blit(over_text, (250, 180))
  86. if not falling:
  87. explosion = mixer.Sound("game oveeer!.wav")
  88. explosion.play()
  89. falling = True
  90.  
  91.  
  92. running = True
  93. while running:
  94. clock.tick(220)
  95. screen.fill((0, 0, 0))
  96.  
  97.  
  98. for event in pygame.event.get():
  99. if event.type == pygame.QUIT:
  100. running = False
  101.  
  102. if event.type == pygame.KEYDOWN:
  103. if event.key == pygame.K_LEFT:
  104. playerX_change = -6
  105. if event.key == pygame.K_RIGHT:
  106. playerX_change = 6
  107. if event.key == pygame.K_SPACE:
  108. if bullet_state is "ready":
  109. bulletX = playerX
  110. laser = mixer.Sound("laser.wav")
  111. laser.play()
  112. fire_bullet(bulletX,bulletY)
  113. if event.type == pygame.KEYUP:
  114. if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
  115. playerX_change = 0
  116.  
  117.  
  118.  
  119.  
  120. playerX += playerX_change
  121.  
  122. if playerX == 0:
  123. playerX = 0
  124. elif playerX >= 690:
  125. playerX = 690
  126.  
  127. for i in range(num_of_aliens):
  128.  
  129.  
  130.  
  131. alienX[i] += alienX_change[i]
  132. if alienX[i] <= 0:
  133. alienX_change[i] = 10.2
  134. alienY[i] += alienY_change[i]
  135. elif alienX[i] >= 736:
  136. alienX[i] += alienX_change[i]
  137. alienX_change[i] = -10.2
  138.  
  139.  
  140. if alienY[i] > 350 and alienX[i] >= playerX - 30:
  141. fall()
  142. for j in range(num_of_aliens):
  143. alienY[j] = 3000
  144. break
  145.  
  146. collision = isCollision(alienX[i], alienY[i], bulletX, bulletY)
  147. if collision:
  148. explosion = mixer.Sound("explosion.wav")
  149. explosion.play()
  150. bulletY = 370
  151. bullet_state = "ready"
  152. score_value += 1
  153. alienX[i] = random.randint(0, 800)
  154. alienY[i] = random.randint(20, 120)
  155.  
  156. alien(alienX[i], alienY[i], i)
  157.  
  158. if bulletY <= 0:
  159. bulletY = 378
  160. bullet_state = "ready"
  161.  
  162. if bullet_state is "fire":
  163. fire_bullet(bulletX, bulletY)
  164. bulletY -= bulletY_change
  165.  
  166.  
  167. player(playerX,playerY)
  168. show_score(textX, textY)
  169. pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement