140 lines
4.1 KiB
Python
140 lines
4.1 KiB
Python
# https://www.youtube.com/watch?v=f4coFYbYQzw&list=PLjcN1EyupaQkAQyBCYKyf1jt1M1PiRJEp
|
|
import os
|
|
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
|
|
from pygame.sprite import Group
|
|
import pygame
|
|
from pygame.locals import *
|
|
import random
|
|
|
|
|
|
WIDTH = 600
|
|
HEIGHT = 800
|
|
WINDOW_SIZE = (WIDTH, HEIGHT)
|
|
FPS = 60
|
|
|
|
|
|
ROWS = 5
|
|
COLS = 5
|
|
|
|
# load background image
|
|
bg = pygame.image.load("img/bg.png") # FUCK THIS IN THE ASS FIXXED IT FUCK YEA
|
|
def draw_bg():
|
|
screen.blit(bg, (0, 0))
|
|
|
|
# create spaceship class
|
|
class Spaceship(pygame.sprite.Sprite):
|
|
def __init__(self,x ,y, health):
|
|
pygame.sprite.Sprite.__init__(self) # review init function
|
|
self.image = pygame.image.load("img/spaceship.png")
|
|
self.rect = self.image.get_rect()
|
|
self.rect.center = [x,y]
|
|
self.health_start = health
|
|
self.health_remaining = health
|
|
self.last_shot = pygame.time.get_ticks()
|
|
def update(self):
|
|
speed = 8
|
|
cooldown = 400 # millisconds
|
|
key = pygame.key.get_pressed()
|
|
if key[pygame.K_a] and self.rect.left > 0: # Why zero not width tried width would not move
|
|
self.rect.x -= speed # why speed not pixal + or - like others??
|
|
if key[pygame.K_d] and self.rect.right < WIDTH:
|
|
self.rect.x += speed
|
|
|
|
time_now = pygame.time.get_ticks()
|
|
|
|
# shoot
|
|
if key[pygame.K_w] and time_now - self.last_shot > cooldown:
|
|
bullet = Bullets(self.rect.centerx, self.rect.top)
|
|
bullet_group.add(bullet)
|
|
self.last_shot = time_now
|
|
|
|
pygame.draw.rect(screen, RED, (self.rect.x, (self.rect.bottom +10), self.rect.width, 15))
|
|
if self.health_remaining > 0:
|
|
pygame.draw.rect(screen, GREEN, (self.rect.x, (self.rect.bottom +10), int(self.rect.width * (self.health_remaining / self.health_start)), 15) )
|
|
|
|
|
|
class Bullets(pygame.sprite.Sprite):
|
|
def __init__(self,x ,y):
|
|
pygame.sprite.Sprite.__init__(self) # review init function
|
|
self.image = pygame.image.load("img/bullet.png")
|
|
self.rect = self.image.get_rect()
|
|
self.rect.center = [x, y]
|
|
def update(self):
|
|
self.rect.y -= 5
|
|
if self.rect.bottom < 0:
|
|
self.kill()
|
|
# video for this https://www.youtube.com/watch?v=mqz1_wRSMwo
|
|
class Aliens(pygame.sprite.Sprite):
|
|
def __init__(self,x ,y):
|
|
pygame.sprite.Sprite.__init__(self)
|
|
self.image = pygame.image.load("img/alien" + str(random.randint(1, 5)) + ".png") # List this will call 1-5 but thought it would be 0-4 or is that place holders
|
|
self.rect = self.image.get_rect()
|
|
self.rect.center = [x, y]
|
|
self.move_counter = 0
|
|
self.move_direction = 1
|
|
# WONT MOVE TO THE RIGHT ONLY TO THE LEFT AND STOP
|
|
def update(self):
|
|
self.rect.x += self.move_direction
|
|
self.move_counter += 1
|
|
if abs(self.move_counter) > 75:
|
|
self.move_direction *= -1
|
|
self.move_counter += self.move_direction
|
|
|
|
# create sprite groups
|
|
spaceship_group = pygame.sprite.Group()
|
|
bullet_group = pygame.sprite.Group()
|
|
|
|
# create player
|
|
spaceship = Spaceship(int(WIDTH / 2), HEIGHT - 100, 3)
|
|
spaceship_group.add(spaceship)
|
|
alien_group = pygame.sprite.Group()
|
|
|
|
def create_aliens():
|
|
for row in range(ROWS):
|
|
for item in range(COLS):
|
|
alien = Aliens(100 + item * 100, 100 + row * 70)
|
|
alien_group.add(alien)
|
|
|
|
create_aliens()
|
|
|
|
|
|
|
|
|
|
WHITE = (255, 255, 255)
|
|
BLACK = (0, 0, 0)
|
|
RED = (255, 0, 0)
|
|
GREEN = (0, 255, 0)
|
|
BLUE = (0, 0, 255)
|
|
|
|
pygame.init()
|
|
pygame.mixer.init()
|
|
screen = pygame.display.set_mode(WINDOW_SIZE)
|
|
pygame.display.set_caption("Spave Invaders")
|
|
clock = pygame.time.Clock()
|
|
|
|
|
|
|
|
running = True # IF RUNNING USING THE BUTTON NOT CONSOLE IT CRASHES WHY??????
|
|
while running:
|
|
clock.tick(FPS)
|
|
|
|
draw_bg()
|
|
|
|
# event handlers
|
|
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
running = False
|
|
|
|
spaceship.update()
|
|
bullet_group.update()
|
|
alien_group.update()
|
|
# draw sprite groups on screen
|
|
spaceship_group.draw(screen)
|
|
bullet_group.draw(screen)
|
|
alien_group.draw(screen)
|
|
pygame.display.update() # Go over it again FLIP vs UPDATE Which to use When
|
|
|
|
|
|
|
|
pygame.quit() |