# Maria Mauco
# 6/19/2024
# game function

import pygame, sys
from pygame.locals import *
import random

pygame.init()

# Constants
SCREEN_W = 1000
SCREEN_H = 600


# 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)



# Best Score


def draw(text, font, color, surface,x,y):
    text = font.render(text,1,color)
    textrect = text.get_rect()
    textrect.topleft = (x,y)
    surface.blit(text,textrect)

# 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

def maingame(MAXSCORE):
    pygame.init()
    clock = pygame.time.Clock()
    DISPLAYSURF = pygame.display.set_mode((SCREEN_W, SCREEN_H))

    # Store my font
    myFont = pygame.font.SysFont("monospace", 30)
    font = pygame.font.SysFont("monospace", 30)
     
    # List of items
    shells = []
    mushrooms = []

    # Player variables
    posX = 500
    posY = 500
    dx = 0
    dy = 0
    # Gravity
    GROUND = 500
    gravity = 2

    # Game variables
    step = 0 # number of frames
    score = 0 # score
    count = 0 # countdown

    lose = False
    
    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
        if not lose:
            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):
                lose = True
                
        # 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

        if not lose:
            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]))
            
        else:
            draw("GAME OVER", font, "red", DISPLAYSURF,400,250)

            if score > MAXSCORE:
                draw("Your new high score is: " + str(score), font, "red", DISPLAYSURF, 300,300)
            else:
                draw("Your score is: " + str(score), font, "red", DISPLAYSURF, 300,300)
            

        pygame.display.update()
        
        if lose:
            pygame.time.delay(1000)
        step += 1
        count -= 1 # Decrease countdown by 1

        if lose:
            return score




