from pygame import*
from random import*


# def jump():
#     global make_jump, dy, jump_count
#     if jump_count>=-30:
#         mario.rect.y-=jump_count/2
#         jump_count-=3
#     else:
        # jump_count=30
#         make_jump=False



init()
# ouch='ouch.png'
mixer.music.load('mario_theme.mp3')
mixer.music.play(-1)
mixer.music.set_volume(0.1)
ouchs=mixer.Sound('ouchs.mp3')
bye=mixer.Sound('goomba-destroy.wav')
mx=43
my=80
big=0
game=1
end=mixer.Sound('mario-end.wav')
sound=0
jump_count=30
score = 0
goomba_direction=0
csound=mixer.Sound('coin-sound.wav')
power=mixer.Sound('power-up.wav')
goomba_bye=0
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
white = (255,255,255)
mario_blue = (116,147,246)
question_cooldown=0
width=1000
height =495
map=[
    '    h      h     h         ',
    '                           ',
    '                           ',
    '                           ',
    '       ?                   ',
    '                           ',
    '    ...      c        u    ',
    '          ...?..     .?.          ',
    '  m                        ',
    '                    .      ',
    '                   ...     ',
    '               ...   ..    ',
    '    h                      ',
    '  t -      --g    p      --g     ---------------------',
    '----------------------------------',
    '----------------------------------',
    '----------------------------------',
]

class  Ground (sprite.Sprite):
    def __init__(self, x,y):
        sprite.Sprite.__init__(self)
        self.image = image.load('ground.png')
        self.rect = self.image.get_rect()
        self.rect.center = [x,y]

class  Goomba (sprite.Sprite):
    def __init__(self, x,y):
        sprite.Sprite.__init__(self)
        self.image = image.load('goomba.png')
        self.rect = self.image.get_rect()
        self.rect.center = [x,y]
        self.direction=3
        self.speed=5
        self.generate=randint(1,2)
    def update(self):
        global goomba_bye
        if mario.rect.collidepoint(self.rect.centerx,self.rect.top):
            self.kill()
            global game, size
            # print('ouch ouch ouch!')
            mixer.Sound.play(bye)
            # game=0
            # print(game)
            if self.generate == 1:
                goomba = Goomba(randint(5*size,10*size),13*size)
                goomba_group.add(goomba)
            else:
                goomba = Goomba(randint(27*size,33*size),13*size)
                goomba_group.add(goomba)
            goomba_bye+=1
        elif mario.rect.collidepoint(self.rect.left,self.rect.centery) or mario.rect.collidepoint(self.rect.right,self.rect.centery):
            global sound
            mario.image = image.load('ouch.png')
            if sound == 0:
                mixer.Sound.play(end)
                # self.direction=0
                game=0
                sound+=1
            mario.kill()
        if sprite.spritecollide(self,obstacle_group,False):
            if self.direction==3:
                self.direction=-3
            else:
                self.direction=3
        self.rect.x+= self.direction
class  Mushroom (sprite.Sprite):
    def __init__(self, x,y):
        sprite.Sprite.__init__(self)
        self.image = image.load('mushroom.png')
        self.rect = self.image.get_rect()
        self.rect.center = [x,y]

class  Question (sprite.Sprite):
    def __init__(self, x,y):
        sprite.Sprite.__init__(self)
        self.image = image.load('question.png')
        self.rect = self.image.get_rect()
        self.rect.center = [x,y]
        self.state = True
        self.id = randint(1,2)
        # self.id2 = randint(1,2)
    def update(self):
        # global 
        if self.rect.colliderect(mario.rect) and self.state == True:
            if self.id == 1:
                coin = Coin(self.rect.centerx,self.rect.top-13)
                coin_group.add(coin)
            else:
                mushroom = Mushroom(self.rect.centerx,self.rect.top-13)
                mushroom_group.add(mushroom)
            self.image = image.load('empty.png')
            # print('yay')
            self.state = False
            obstacle_group.add(question)
            

class  Brick (sprite.Sprite):
    def __init__(self, x,y):
        sprite.Sprite.__init__(self)
        self.image = image.load('mario-brick.png')
        self.rect = self.image.get_rect()
        self.rect.center = [x,y]

class  Tnt (sprite.Sprite):
    def __init__(self, x,y):
        sprite.Sprite.__init__(self)
        self.image = image.load('tnt.png')
        self.rect = self.image.get_rect()
        self.rect.center = [x,y]

class  Coin (sprite.Sprite):
    def __init__(self, x,y):
        sprite.Sprite.__init__(self)
        self.image = image.load('coin2.png')
        self.rect = self.image.get_rect()
        self.rect.center = [x,y]
    def update(self):
        global score
        if sprite.spritecollide(self, mario_group, False):
            self.kill()
            mixer.Sound.play(csound)
            score+=1
            # print(score)

class  Mario (sprite.Sprite):
    def __init__(self, x,y):
        sprite.Sprite.__init__(self)
        self.image = image.load('mario1.png')
        self.rect = self.image.get_rect()
        self.rect.center = [x,y]
        self.direction = 'down'
        self.make_jump = False
        self.fall_speed = 2
        self.on_ground = False
        self.mx=36
        self.my=48
    def update(self):
        self.rect.y+=self.fall_speed
        self.fall_speed+=0.5
        # print(self.fall_speed)
        if sprite.spritecollide(self,mushroom_group,True):
            global big
            big=1
            self.mx=36
            self.my=60
            # self.image = transform.scale(self.image,(43,150))
            # print('mushroom')
            mixer.Sound.play(power)
        if sprite.spritecollide(self,tnt_group,False):
            global goomba_bye
            self.image = image.load('ouch.png')
            mixer.Sound.play(ouchs)
            # goomba_bye+=1
        # if self.rect.collidepoint(goomba.rect.centerx,goomba.rect.top):
        #     goomba.kill()
        #     print('ouch')
        # if goomba_bye==0:
        #     if self.rect.collidepoint(goomba.rect.left,goomba.rect.centery) or self.rect.collidepoint(goomba.rect.right,goomba.rect.centery):
        #         # goomba.kill()
        #         self.image = image.load('ouch.png')
        #         mixer.Sound.play(ouchs)
        #         # display.update()
        #         # time.delay(5000)
        #         self.kill()
        bricks=sprite.spritecollide(self,obstacle_group,False)        
        for w in bricks:
                self.rect.bottom = w.rect.top
                self.fall_speed=2
                self.on_ground=True
        keys = key.get_pressed()
        if keys[K_RIGHT]:
            # if big == 1:
                if self.direction != 'right':
                    self.image = image.load('mario1.png')
                    self.direction='right'
                    self.image = transform.scale(self.image,(self.mx,self.my))
                else:
                    self.rect.x += 10
            # else:
            #     if self.direction != 'right':
            #         self.image = image.load('mario1.png')
            #         self.direction='right'
            #     else:
            #         self.rect.x += 10
        if keys[K_LEFT]:
            # if big == 1:
                if self.direction != 'left':
                    self.image = image.load('mario1-left.png')
                    self.direction='left'
                    self.image = transform.scale(self.image,(self.mx,self.my))
                    self.rect.update(self.rect.x,self.rect.y,self.mx,self.my)
                else:
                    self.rect.x -= 10
            # else:
            #     if self.direction != 'left':
            #         self.image = image.load('mario1.png')
            #         self.direction='left'
            #     else:
            #         self.rect.x -= 10
        bricks=sprite.spritecollide(self,obstacle_group,False)        
        for w in bricks:
            if self.direction == 'left':
                self.rect.left = w.rect.right
            if self.direction == 'right':
                self.rect.right = w.rect.left
        if self.on_ground == True:
            if keys[K_UP]:
                        self.direction='up'
                        self.rect.y-=80
                        self.on_ground = False
    #     if self.make_jump:
    #         self.jump()
    #         bricks=sprite.spritecollide(self,obstacle_group,False)        
    #         for w in bricks:
    #             if self.direction == 'up':
    #                 self.rect.bottom = w.rect.top
    #                 self.make_jump = False
    def jump(self):
        global jump_count
        if jump_count>=-30:
            self.rect.y-=jump_count/2
            jump_count-=3
        else:
            jump_count=30
            self.make_jump=False
goomba_group = sprite.Group()
mushroom_group = sprite.Group()
obstacle_group = sprite.Group()
question_group = sprite.Group()
mario_group = sprite.Group()
coin_group = sprite.Group()
tnt_group = sprite.Group()
size=30
x=0
y=0
for ro in map:
    for s in ro:
        if s == '-':
            ground = Ground(x,y)
            obstacle_group.add(ground)

        if s == '?':
            question = Question(x,y)
            question_group.add(question)
        
        if s == '.':
            brick = Brick(x,y)
            obstacle_group.add(brick)
        if s == 'm':
            mario = Mario(x,y-10)
            mario_group.add(mario)
        if s == 't':
            tnt = Tnt(x,y)
            tnt_group.add(tnt)
        if s == 'g':
            goomba = Goomba(x,y)
            goomba_group.add(goomba)
        # if s == 'c':
        #     coin = Coin(x,y)
        #     coin_group.add(coin)
        x+=size
    x=0
    y+=size
size_window = (width, height)
window = display.set_mode(size_window)
display.set_caption('Game ')
run=True
while run:
    for e in event.get():
        if e.type == QUIT:
            run = False
    window.fill(mario_blue)
    if game==1:
        goomba_group.update()
        coin_group.update()
        mario_group.update()
        question_group.update()
    if game==0:
        mixer.music.fadeout(1000)
        display.update()
        time.delay(3500)
        run=False

    tnt_group.draw(window)
    obstacle_group.draw(window)
    mushroom_group.draw(window)
    question_group.draw(window)
    coin_group.draw(window)
    mario_group.draw(window)
    goomba_group.draw(window)
    
    # if make_jump==True:
    #     # jump()


    display.update()
    time.delay(50)
    

quit()