#Bryan Medina
#6/27/17
#badmario.py
#Crappy Mario....

import pygame, sys
from math import *
from random import randint
from pygame.locals import *

pygame.init()

DISPLAYSURF = pygame.display.set_mode((1000, 600))

mario = pygame.image.load("mario.png")
mario = pygame.transform.scale(mario, (64, 64)) #Resizing Mario image

background = pygame.image.load("background.jpg")
background = pygame.transform.scale(background, (1000, 600)) #Resizing background image

shell = pygame.image.load("shell.png")
shell = pygame.transform.scale(shell, (32, 32)) #Resizing shell

pygame.mixer.music.load("theme.wav") #loads the music
pygame.mixer.music.play(-1) #play music infinitely

shells = [] #List for all the shell

#variables to keep track of the width and height of the screen
width  = 1000
height = 600


velocity     = 0 #Mario's Y velocity
velX         = 0 #Mario's X velocity

posX         = 500 #x position of our ball
posY         = 530 #y position of our ball
GROUND       = 500 #the surface our ball is sitting on

gravity      = 2 #The reason why we always fall back to the ground



while True: #Game loop

    for event in pygame.event.get():

        if event.type == QUIT:
            pygame.quit()
            sys.quit()

        if event.type == KEYDOWN:
            if event.key == K_w: #if w is pressed
                velocity = -30 #inital y velocity will be non zero

            if event.key == K_a: #if a is pressed
                velX = -10 #Mario will move to the left

            if event.key == K_d: #if d is pressed
                velX = 10 #Mario will move to the right

            if event.key == K_SPACE: #if space is pressed
                shells.append([posX+60, posY, 20, shell]) #Shell will be thrown
                #WARNING: this list will get huge fast... Make sure to remove offscreen shells

        if event.type == KEYUP: #if the key is released
            velX = 0 #Mario's X velocity will be 0


    for shelly in shells: #Update the position of every shell in the list
        shelly[0] += shelly[2]


    velocity += gravity #add gravity to velocity
    posY += velocity # add velocity to position

    posX += velX #Change position of mario

    if posX >= width: # make him wrap around the screen
        posX = 0

    elif posX <= 0:
        posX = width

    if posY + 20 >= GROUND and velocity > 0: #This keeps Mario above (or on theground)
        velocity = 0
        posY = GROUND


    DISPLAYSURF.blit(background, (0, 0))  #Display background
    DISPLAYSURF.blit(mario, (posX, posY)) #Display Mario

    for shelly in shells: #Display all shells
        DISPLAYSURF.blit(shelly[3], (shelly[0], shelly[1]))

    pygame.display.update()
