simple space shooter in python code example
Example: space shooter game in python
import pygame
import random
import math
from pygame import mixer
import android
mixer.init()
r = 'ready'
b = 2
space = 0
x = 50
y = 515
start = 10
width = 40
height = 5
Asteroid1 = []
Asteroid_x = []
Asteroid_y = []
Asteroid_x_change = []
Asteroid_y_change = []
for i in range(30):
Asteroid1.append(pygame.image.load("alien.png"))
Asteroid_x.append(random.randint(0, 800))
Asteroid_y.append(random.randint(50, 150))
Asteroid_x_change.append(3.8)
Asteroid_y_change.append(6)
pygame.init()
icon = pygame.image.load('ufo.png')
pygame.display.set_icon(icon)
screen = pygame.display.set_mode((800, 600))
background = pygame.image.load('space.jpg')
pygame.display.set_caption("Space shooter")
BLUE = (100, 100, 255)
RED = (255, 0, 0)
LIME = (180, 255, 100)
'''Asteroid'''
player = pygame.image.load("player.png")
player_x = 370
player_y = 480
Bullet = pygame.image.load("Bullet.png")
Bullet_x = 370
Bullet_y = 480
Bullet_x_change = 9
Bullet_y_change = 60
Bullet_state = r
score_value = 0
font = pygame.font.Font('freesansbold.ttf', 25)
text_x = 200
text_y = 10
def score(X, Y):
scores = font.render('Score : {}'.format(score_value), True, BLUE)
screen.blit(scores, (X, Y))
def fire(x, y):
global Bullet_state
Bullet_state = fire
screen.blit(Bullet, (x + 18, y + 10))
def play(X, Y):
screen.blit(player, (X, Y))
def asteroid_function(X, Y, i):
screen.blit(Asteroid1[i], (X, Y))
def background_function():
screen.blit(background, (0, 0))
run = True
while run:
background_function()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player_x -= 3
if keys[pygame.K_RIGHT]:
player_x += 3
laser_sound = mixer.Sound("C:\\laser.wav")
laser_sound.play()
Bullet_x = player_x
fire(Bullet_x, Bullet_y)
fire(Bullet_x, Bullet_y)
Bullet_y -= Bullet_y_change
if Bullet_y <= 0:
Bullet_y = 480
Bullet_state = r
if score_value == 300:
print('you win')
run = False
def collision(Bullet_x, Bullet_y, Asteroid_x, Asteroid_y):
distance = math.sqrt(math.pow(Asteroid_x - Bullet_x, 2) + math.pow(Asteroid_y - Bullet_y, 2))
if distance < 27:
return True
else:
return False
if player_x <= 0:
player_x = 0
elif player_x >= 736:
player_x = 736
for i in range(30):
Asteroid_x[i] += Asteroid_x_change[i]
if Asteroid_x[i] <= 0:
Asteroid_x_change[i] = 3.8
Asteroid_y[i] += Asteroid_y_change[i]
elif Asteroid_x[i] >= 736:
Asteroid_x_change[i] = -3.8
Asteroid_y[i] += Asteroid_y_change[i]
collisions = collision(Asteroid_x[i], Asteroid_y[i], Bullet_x, Bullet_y)
if collisions:
Bullet_y = 480
Bullet_state = 'ready'
score_value = score_value + 1
laser_sound = mixer.Sound("C:\\explosion.wav")
laser_sound.play()
Asteroid_x.append(random.randint(0, 800))
Asteroid_y.append(random.randint(50, 150))
asteroid_function(Asteroid_x[i], Asteroid_y[i], i)
if event.type == pygame.KEYDOWN:
if event.key == ord('q'):
pygame.quit()
if event.key == ord('s'):
b = b + 1
if b == 1:
player = pygame.image.load("player.png")
if b == 2:
player = pygame.image.load("l.png")
if b == 3:
player = pygame.image.load('player2.png')
if b == 4:
b = 1
play(player_x, player_y)
score(text_x, text_y)
pygame.display.flip()