Advertisement
Mr_kindle

Untitled

Oct 12th, 2022
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.50 KB | Gaming | 0 0
  1. import pygame
  2. import random
  3. import math
  4. from pygame import mixer
  5.  
  6. # intialize pygame
  7.  
  8. pygame.init()
  9.  
  10. #Background music
  11. mixer.music.load('background.wav')
  12. mixer.music.play(-1)
  13.  
  14. # create the screen
  15.  
  16. height = 600
  17. width = 800
  18. screen = pygame.display.set_mode((width, height))  # there must be 2 parenthesis
  19.  
  20. # Title and Icon
  21.  
  22. pygame.display.set_caption("Space Invader")
  23. icon = pygame.image.load('ufo.png')
  24. pygame.display.set_icon(icon)
  25.  
  26. #score
  27. score_value = 0
  28.  
  29. font = pygame.font.Font('freesansbold.ttf',32)
  30.  
  31. textx = 10
  32. texty = 10
  33.  
  34.  
  35.  
  36.  
  37.  
  38. # Player
  39.  
  40. player_image = pygame.image.load('arcade.png')
  41. player_x = width / 2 - 36
  42. player_y = height * 4 / 5
  43. player_x_change = 0
  44.  
  45. # enemy
  46. enemy_image = []
  47. enemy_x = []
  48. enemy_y = []
  49. enemy_x_change = []
  50. enemy_y_change = []
  51. num_of_enemies = 6
  52.  
  53. for i in range(num_of_enemies):
  54.     enemy_image.append(pygame.image.load('alien.png'))
  55.     enemy_x.append(random.randint(0, width - 65))
  56.     enemy_y.append(random.randint(int(height / 12), int(height / 4)))
  57.     enemy_x_change.append(0.3)
  58.     enemy_y_change.append(40)
  59.  
  60. # Bullet
  61.  
  62. # ready = you can't see the bullet on screen
  63. # Fire = Bullet is moving
  64. bullet_image = pygame.image.load('bullet.png')
  65. bullet_x = 0
  66. bullet_y = height * 4 / 5
  67. bullet_x_change = 0
  68. bullet_y_change = 0.5
  69. bullet_state = 'ready'
  70.  
  71. #GameOver
  72.  
  73. over_font = pygame.font.Font('freesansbold.ttf',64)
  74. def gameovertext():
  75.     game_over = over_font.render('* GAME OVER *', True, (255, 255, 255))
  76.     screen.blit(game_over, (200, 250))
  77.  
  78.  
  79. def show_score (x,y):
  80.     score = font.render('score:' +str(score_value),True,(255,255,255))
  81.     screen.blit(score, (x, y))
  82.  
  83.  
  84. def player(x, y):
  85.     screen.blit(player_image, (x, y))
  86.  
  87.  
  88. def enemy(x, y, i):
  89.     screen.blit(enemy_image[i], (x, y))
  90.  
  91.  
  92. def fire_bullet(x, y):
  93.     global bullet_state
  94.     bullet_state = 'fire'
  95.     screen.blit(bullet_image, (x + 16, y + 10))
  96.  
  97.  
  98. def iscollision(enemy_x, enemy_y, bullet_x, bullet_y):
  99.     distance = math.sqrt(pow(enemy_x - bullet_x, 2) + pow(enemy_y - bullet_y, 2))
  100.     if distance <= 27:
  101.         return True
  102.     else:
  103.         return False
  104.  
  105.  
  106. running = True
  107. while running:
  108.  
  109.     # RGB Red Green Blue
  110.     screen.fill((0, 0, 0))
  111.     for event in pygame.event.get():
  112.         if event.type == pygame.QUIT:
  113.             running = False
  114.         if event.type == pygame.KEYDOWN:
  115.             if event.key == pygame.K_LEFT:
  116.                 player_x_change = -0.3
  117.             if event.key == pygame.K_RIGHT:
  118.                 player_x_change = 0.3
  119.             if bullet_state == 'ready':
  120.                 if event.key == pygame.K_SPACE:
  121.                     bullet_sound = mixer.Sound('laser.wav')
  122.                     bullet_sound.play()
  123.                     # Get the current x co-ordinate of spaceship
  124.                     bullet_x = player_x
  125.                     fire_bullet(bullet_x, bullet_y)
  126.         if event.type == pygame.KEYUP:
  127.             if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
  128.                 player_x_change = 0
  129.     # Checking for boundaries so that spaceship doesn't go out of boundary.
  130.     player_x += player_x_change
  131.     if player_x <= 0:
  132.         player_x = 0
  133.     elif player_x > width - 64:
  134.         player_x = width - 64
  135.     # Enemy movement
  136.     for i in range(num_of_enemies):
  137.         if enemy_y[i] > 200:
  138.             for j in range (num_of_enemies):
  139.                 enemy_y[j] = 2000
  140.             gameovertext()
  141.             break
  142.  
  143.         enemy_x[i] += enemy_x_change[i]
  144.         if enemy_x[i] <= 0:
  145.             enemy_x_change[i] = 0.3
  146.             enemy_y[i] += enemy_y_change[i]
  147.         elif enemy_x[i] >= width - 64:
  148.             enemy_x_change[i] = -0.3
  149.             enemy_y[i] += enemy_y_change[i]
  150.             # Checking for collission
  151.         collision = iscollision(enemy_x[i], enemy_y[i], bullet_x, bullet_y)
  152.         if collision:
  153.             explosion_sound = mixer.Sound('explosion.wav')
  154.             explosion_sound.play()
  155.             bullet_y = height * 4 / 5
  156.             bullet_state = 'ready'
  157.             score_value += 1
  158.             enemy_x[i] = random.randint(0, width - 65)
  159.             enemy_y[i] = random.randint(int(height / 12), int(height / 4))
  160.  
  161.         enemy(enemy_x[i], enemy_y[i], i)
  162.     player(player_x, player_y)
  163.     show_score(textx,texty)
  164.  
  165.     # Bullet movement
  166.     if bullet_y <= 0:
  167.         bullet_y = height * 4 / 5
  168.         bullet_state = 'ready'
  169.     if bullet_state == 'fire':
  170.         fire_bullet(bullet_x, bullet_y)
  171.         bullet_y -= bullet_y_change
  172.  
  173.     pygame.display.update()
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement