space shooter game in python

Solutions on MaxInterview for space shooter game in python by the best coders in the world

showing results for - "space shooter game in python"
Alberto
19 May 2020
1# importing pygame
2import pygame
3import random
4# importing math
5import math
6# importing mixer
7from pygame import mixer
8import android
9mixer.init()
10r = 'ready'
11b = 2
12space = 0
13x = 50
14y = 515
15start = 10
16width = 40
17height = 5
18# Asteroid
19Asteroid1 = []
20Asteroid_x = []
21Asteroid_y = []
22Asteroid_x_change = []
23Asteroid_y_change = []
24for i in range(30):
25    Asteroid1.append(pygame.image.load("alien.png"))
26    Asteroid_x.append(random.randint(0, 800))
27    Asteroid_y.append(random.randint(50, 150))
28    Asteroid_x_change.append(3.8)
29    Asteroid_y_change.append(6)
30pygame.init()
31# Changing the icon of the screen
32icon = pygame.image.load('ufo.png')
33pygame.display.set_icon(icon)
34# Creating the screen
35screen = pygame.display.set_mode((800, 600))
36# background pic
37background = pygame.image.load('space.jpg')
38pygame.display.set_caption("Space shooter")
39# Colour
40BLUE = (100, 100, 255)
41RED = (255, 0, 0)
42LIME = (180, 255, 100)
43'''Asteroid'''
44# Player
45player = pygame.image.load("player.png")
46player_x = 370
47player_y = 480
48# Bullet
49Bullet = pygame.image.load("Bullet.png")
50Bullet_x = 370
51Bullet_y = 480
52Bullet_x_change = 9
53Bullet_y_change = 60
54Bullet_state = r
55# score
56score_value = 0
57font = pygame.font.Font('freesansbold.ttf', 25)
58text_x = 200
59text_y = 10
60
61
62
63
64
65
66# Creating score function
67def score(X, Y):
68    scores = font.render('Score : {}'.format(score_value), True, BLUE)
69    screen.blit(scores, (X, Y))
70
71
72# Creating fire function
73def fire(x, y):
74    global Bullet_state
75    Bullet_state = fire
76    screen.blit(Bullet, (x + 18, y + 10))
77
78
79# Creating fire function
80def play(X, Y):
81    screen.blit(player, (X, Y))
82
83
84# Creating  Asteroid function
85def asteroid_function(X, Y, i):
86    screen.blit(Asteroid1[i], (X, Y))
87
88
89# Creating background function
90def background_function():
91    screen.blit(background, (0, 0))
92
93
94# While loop
95run = True
96while run:
97    # calling the background function
98    background_function()
99    for event in pygame.event.get():
100        if event.type == pygame.QUIT:
101            run = False
102    keys = pygame.key.get_pressed()
103    # player movement
104    if keys[pygame.K_LEFT]:
105        player_x -= 3
106    if keys[pygame.K_RIGHT]:
107        player_x += 3
108    # Bullet shooting
109        laser_sound = mixer.Sound("C:\\laser.wav")
110        laser_sound.play()
111        Bullet_x = player_x
112        fire(Bullet_x, Bullet_y)
113        fire(Bullet_x, Bullet_y)
114        Bullet_y -= Bullet_y_change
115    if Bullet_y <= 0:
116        Bullet_y = 480
117        Bullet_state = r
118        # collision
119    if score_value == 300:
120        print('you win')
121        run = False
122
123
124    def collision(Bullet_x, Bullet_y, Asteroid_x, Asteroid_y):
125        distance = math.sqrt(math.pow(Asteroid_x - Bullet_x, 2) + math.pow(Asteroid_y - Bullet_y, 2))
126        if distance < 27:
127            return True
128        else:
129            return False
130        # collision
131    # boundaries
132    if player_x <= 0:
133        player_x = 0
134    elif player_x >= 736:
135        player_x = 736
136    # boundaries enemy
137    for i in range(30):
138        Asteroid_x[i] += Asteroid_x_change[i]
139        if Asteroid_x[i] <= 0:
140            Asteroid_x_change[i] = 3.8
141            Asteroid_y[i] += Asteroid_y_change[i]
142        elif Asteroid_x[i] >= 736:
143            Asteroid_x_change[i] = -3.8
144            Asteroid_y[i] += Asteroid_y_change[i]
145        collisions = collision(Asteroid_x[i], Asteroid_y[i], Bullet_x, Bullet_y)
146        if collisions:
147            Bullet_y = 480
148            Bullet_state = 'ready'
149            score_value = score_value +  1
150            laser_sound = mixer.Sound("C:\\explosion.wav")
151            laser_sound.play()
152            Asteroid_x.append(random.randint(0, 800))
153            Asteroid_y.append(random.randint(50, 150))
154        # calling the asteroid function
155        asteroid_function(Asteroid_x[i], Asteroid_y[i], i)
156    if event.type == pygame.KEYDOWN:
157        # quitting the game
158        if event.key == ord('q'):
159            pygame.quit()
160        # changing the spaceship
161        if event.key == ord('s'):
162            b = b + 1
163            if b == 1:
164                player = pygame.image.load("player.png")
165            if b == 2:
166                player = pygame.image.load("l.png")
167            if b == 3:
168                player = pygame.image.load('player2.png')
169            if b == 4:
170                b = 1
171    # calling the player function
172    play(player_x, player_y)
173    # calling the score function
174    score(text_x, text_y)
175    # Refresh Screen
176    pygame.display.flip()
177
178