Recently I've been trying to learn more advanced things such as using classes and creating methods and such to implement OOP into my coding. This is the first time I've tried and I want opinion on what I could do to make this better or more efficient, or maybe what I'm doing wrong
Optional Read For context, I watched a beginner tutorial on YouTube on how to create a game, and then after I finished the tutorial, I decided to try and implement classes and objects. Essentially the game is just a spaceship (red rectangle) at the bottom of the screen that moves left and right to try and avoid the stars (white rectangles) that fall from the top of your screen to the bottom
Main Class:
import pygame
import random
import time
from Player import Player
from Star import Star
pygame.init()
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 700
FONT = pygame.font.SysFont("comicsans", 30)
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Space Avoiders")
background = pygame.transform.scale(pygame.image.load("background1.jpg"), (SCREEN_WIDTH, SCREEN_HEIGHT + 15))
player = Player(SCREEN_WIDTH/2 - 20, SCREEN_HEIGHT - 60, 20, 60)
start_time = time.time()
elapsed_time = 0
star_spawn_rate = 2000
star_spawn = 1000
star_spawn_rate_decrease = 50
stars_to_spawn = 3
stars = []
hit = False
def starSpawner():
for _ in range(3):
randx = random.randint(0, 980)
stars.append(Star(randx, -40, 20, 40))
def redrawGameWindow():
screen.blit(background, (0, 0))
time_text = FONT.render(f'Time: {round(elapsed_time)}s', 1, "white")
screen.blit(time_text, (10, 10))
player.draw(screen)
for star in stars:
star.draw(screen)
star.fall()
pygame.display.update()
run = True
clock = pygame.time.Clock()
while run:
star_spawn += clock.tick(60)
elapsed_time = time.time() - start_time
playerRect = pygame.Rect(player.x, player.y, player.width, player.height)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
break
if star_spawn >= max(200, star_spawn_rate):
star_spawn_rate -= star_spawn_rate_decrease
star_spawn = 0
starSpawner()
for star in stars[:]:
starRect = pygame.Rect(star.x, star.y, star.width, star.height)
if star.y >= SCREEN_HEIGHT:
stars.remove(star)
elif star.y + star.height >= player.y and starRect.colliderect(playerRect):
stars.remove(star)
hit = True
break
if hit:
lose_text = FONT.render("You Lost!", 1, "white")
screen.blit(lose_text, (SCREEN_WIDTH/2 - lose_text.get_width()/2, SCREEN_HEIGHT/2 - lose_text.get_height()/2))
pygame.display.update()
pygame.time.delay(4000)
break
player.move()
redrawGameWindow()
pygame.quit()
Player Class:
import pygame
class Player:
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 3
def draw(self, screen):
pygame.draw.rect(screen, "red", (self.x, self.y, self.width, self.height))
def move(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.x -= self.vel
elif keys[pygame.K_RIGHT]:
self.x += self.vel
Star Class:
import pygame
class Star:
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
def draw(self, screen):
pygame.draw.rect(screen, "white", (self.x, self.y, self.width, self.height))
def fall(self):
self.y += self.vel