# Maria Mauco
# 6/18/24

import pygame, sys
from pygame.locals import *
import random

pygame.init()
clock = pygame.time.Clock()

# Display variables
SCREEN_W = 1000
SCREEN_H = 600
DISPLAYSURF = pygame.display.set_mode((SCREEN_W, SCREEN_H))


# Store and scale images
mario = pygame.image.load("mario.png") # mario
mario = pygame.transform.scale(mario, (64,64))

bg = pygame.image.load("background.jpg") # background
bg = pygame.transform.scale(bg, (SCREEN_W,SCREEN_H))

shell = pygame.image.load("shell.png") # shells
shell = pygame.transform.scale(shell, (32,32))

mushroom = pygame.image.load("mushroom.png") # mushrooms
mushroom = pygame.transform.scale(mushroom, (32,32))

# Play background music on a loop forever
pygame.mixer.music.load("theme.wav")
pygame.mixer.music.play(-1)

# Store my font
myFont = pygame.font.SysFont("monospace", 30)


# Player variables
posX = 500
posY = 500
dx = 0
dy = 0
# Gravity
GROUND = 500
gravity = 2

# List of items
shells = []
mushrooms = []

# Game variables
step = 0 # number of frames
score = 0 # score
count = 0 # countdown

# Function to check if mario hit an object
def hit(posX,posY, item):
    if posX + 64 >= item[0] and posX <= item[0]+ 32 and posY  >= item[1] - 32:
        return True
    return False

# Game loop
while True:
    clock.tick(60)
    # Events
    for event in pygame.event.get(): # Quit
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN: # Key Inputs
            if event.key == K_w: # Jump
                dy = -30
            if event.key == K_a: # Move left
                dx = -10
            if event.key == K_d: # Move right
                dx = 10
        if event.type == KEYUP: # Stops moving if key is not pressed anymore
            dx = 0

    # Adds a shell every 100 frames
    if step % 100 == 0:
        shells.append([SCREEN_W, GROUND+32, -5, shell])
    # Adds a mushroom every 256 frames
    if step % 256 == 0:
        mushrooms.append([SCREEN_W,GROUND+32, -7, mushroom])

    # Move mario's x
    posX += dx

    # Move the items
    for shelly in shells:
        shelly[0] += shelly[2]
    for powerup in mushrooms:
        powerup[0] += powerup[2]

    # Make mario jump
    dy += gravity
    posY += dy

    # Doesn't let mario fall through the screen    
    if posY+10 >= GROUND and dy > 0: 
        dy = 0
        posY = GROUND

    # Check if a shell hit mario and quit the game
    for shelly in shells:
        if hit(posX,posY, shelly):
            pygame.quit()
            sys.exit()
            
    # Add 20 points if mario hit a mushrooom
    for powerup in mushrooms :
        if hit(posX,posY, powerup) and count <= 0:
            score += 20
            count = 200

    # Wraps mario around screen
    if posX >= SCREEN_W:
        posX = 0
    elif posX <= 0:
        posX = SCREEN_W


    # Displays everything on screen
    DISPLAYSURF.blit(bg, (0,0)) # background
    DISPLAYSURF.blit(mario, (posX,posY)) # mario
    scoreText = myFont.render("Score: "+ str(score), 1, "black") # render score text
    DISPLAYSURF.blit(scoreText, (SCREEN_W - 200, 0)) # display score

    # Displays every item
    for shelly in shells:
        DISPLAYSURF.blit(shelly[3], (shelly[0],shelly[1]))
    for powerup in mushrooms:
        DISPLAYSURF.blit(powerup[3], (powerup[0],powerup[1]))
    
    
    
    pygame.display.update()
    step += 1
    count -= 1 # Decrease countdown by 1
