import pygame
from random import randrange, choice
pygame.init()
pygame.font.init()
clock = pygame.time.Clock()
fps = 60
screen_size = (1280, 740)
screen = pygame.display.set_mode(screen_size)
# Colors
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
# Create sprite group
allsprites = pygame.sprite.Group()
playersprite = pygame.sprite.Group()
asteroidsprite = pygame.sprite.Group()
bulletsprite = pygame.sprite.Group()
def create_color():
color = ()
for i in range(3):
n = randrange(50,255)
color = color + (n,)
return color
class Player(pygame.sprite.Sprite):
''' Player class create the player '''
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((50,50))
self.image.fill(red)
self.rect = self.image.get_rect()
self.rect.centerx = screen_size[0]/2 - self.rect.width/2
self.rect.bottom = screen_size[1]-5
self.speed = 0
self.score = 0
playersprite.add(self)
allsprites.add(playersprite)
def update(self):
''' Method updates player movement '''
self.speed = 0
state = pygame.key.get_pressed()
if state[pygame.K_LEFT]:
self.speed = -8
if state[pygame.K_RIGHT]:
self.speed = 8
self.rect.x += self.speed
if self.rect.left <= 0:
self.rect.left = 0
if self.rect.right >= screen_size[0]:
self.rect.right = screen_size[0]
def fire(self):
''' Method creates the bullet from Bullet class'''
bullet = Bullet(self.rect.centerx, self.rect.top)
class Bullet(pygame.sprite.Sprite):
''' Class creates the bullet '''
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((5,20))
self.image.fill(green)
self.rect = self.image.get_rect()
self.rect.centerx = x
self.rect.centery = y
self.speed = 10
bulletsprite.add(self)
allsprites.add(bulletsprite)
def update(self):
''' Method update bullet movement '''
self.rect.y -= self.speed
# If bullet goes of the top of screen, kill the sprite
if self.rect.bottom <= 0:
self.kill()
class Asteroid(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image0 = pygame.Surface((randrange(25,50), randrange(25,50)), pygame.SRCALPHA)
self.image0.fill(create_color())
# Create copy of image for use of rotating animations
self.image = self.image0.copy()
self.rect = self.image.get_rect()
self.rect.x = randrange(20, screen_size[0]-20)
self.rect.y = randrange(-20, -5)
self.speedx = randrange(-5, 8)
self.speedy = self.speedy = randrange(3, 8)
# Below is used for rotation
self.rot = 0
self.rot_speed = choice((randrange(-8,-4), randrange(4,8)))
self.last_update = pygame.time.get_ticks()
asteroidsprite.add(self)
allsprites.add(asteroidsprite)
def update(self):
# Get current pygame time
now = pygame.time.get_ticks()
# If greater then 100, rotate and update
if now - self.last_update > 100:
self.last_update = now
self.rot = (self.rot + self.rot_speed)%360
new_image = pygame.transform.rotate(self.image0, self.rot)
# To keep center of image
old_center = self.rect.center
self.image = new_image
self.rect = self.image.get_rect()
self.rect.center = old_center
self.rect.x += self.speedx
self.rect.y += self.speedy
if self.rect.right <= -5:
self.kill()
Asteroid()
if self.rect.left >= screen_size[0]+10:
self.kill()
Asteroid()
if self.rect.top >= screen_size[1]+10:
self.rect.y = randrange(-30, -10)
self.rect.x = randrange(20, screen_size[0]-20)
# Create player and add to sprite group
player = Player()
# Create random range of asteroids
n = randrange(5, 8)
for i in range(n ):
Asteroid()
stop = False
running = True
while running:
screen.fill(black)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and not stop:
player.fire()
if stop and event.key == pygame.K_ESCAPE:
running = False
if stop and event.key == pygame.K_RETURN:
player.score = 0
stop = False
player = Player()
for i in range(randrange(5,8)):
Asteroid()
# Detect collisions
bullet_collision = pygame.sprite.groupcollide(bulletsprite, asteroidsprite, True, True)
asteroid_collision = pygame.sprite.groupcollide(asteroidsprite, playersprite, True, True)
# If a bullet hits asteroid create another asteroid
if bullet_collision:
player.score += 1
Asteroid()
# If asteroid hits player game over
if asteroid_collision:
stop = True
allsprites.empty()
playersprite.empty()
asteroidsprite.empty()
bulletsprite.empty()
if stop:
font = pygame.font.SysFont(None, 45)
text = 'Press esc to exit'
text_surface = font.render(text, True, white)
screen.blit(text_surface, (screen_size[0]/2-font.size(text)[0]/2, screen_size[1]/2))
text = 'Press return/enter to play again'
text_surface = font.render(text, True, green)
screen.blit(text_surface, (screen_size[0]/2-font.size(text)[0]/2, screen_size[1]/2+screen_size[1]*0.15))
# Create font, create font surface blit to screen
font = pygame.font.SysFont(None, 45)
text_surface = font.render(f'Score: {player.score}', True, white)
screen.blit(text_surface, (10,10))
# Update and draw all sprites
allsprites.update()
allsprites.draw(screen)
pygame.display.update()
clock.tick(fps)
pygame.quit()