#pygame template - skeleton for a new pygame project
import pygame
import random
import os

WIDTH = 800
HEIGHT = 600
FPS = 30

#define colors
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)

#set up assets folder
game_folder = os.path.dirname(__file__)
img_folder = os.path.join(game_folder, "img")

class Player(pygame.sprite.Sprite):
    #sprite for the player
    def __init__(self):
        #This is a compulsory init for sprites
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(os.path.join(img_folder,"p1_jump.png")).convert() #pygame.surface is somethign we can draw on
        #self.image.set_colorkey() - only used to ignore the Background colour of the image
        #rectangle that encloses the sprite
        self.rect = self.image.get_rect() #this figures out where the rectangle needs to be
        #where do we want sprite to start off i.e. middle of the screen
        self.rect.center = (WIDTH/2, HEIGHT/2)
        self.y_speed = 5
    
    def update(self):
        self.rect.x += 5
        self.rect.y += self.y_speed
        if self.rect.bottom > HEIGHT - 200:
            self.y_speed = -5
        if self.rect.top < 200:
            self.y_speed = 5
        if self.rect.left > WIDTH:
            self.rect.right = 0
            
#initalize pygame and create window
pygame.init()
#sound
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("My Game")
clock = pygame.time.Clock()

all_sprites = pygame.sprite.Group()

player = Player()
all_sprites.add(player)

#Game Loop
running = True
while running:
    #keep loop running at the right speed. regulates speed
    clock.tick(FPS)
    #Processing input (events)
    for event in pygame.event.get():
        #check for closing window
        if event.type == pygame.QUIT:
            running = False
            
    #update - what needs to change about the sprite
    #sprite groups - collection of sprite so we don't have to update individually. 
    all_sprites.update()
    
    #Draw/ render
    screen.fill(BLACK)
    all_sprites.draw(screen)
    # *after* draw everything, flip the display
    pygame.display.flip()

    
pygame.quit()
quit()