import pygame
import random

back = pygame.image.load('back.jpg')
manimage = pygame.image.load('char.png')
pygame.mixer.init()
backs = pygame.mixer.Sound('start_screen.wav')
backs.play();
time = pygame.mixer.Sound('timer.wav')
time.set_volume(0.8)
b1=pygame.mixer.Sound('laser.wav')
e1=pygame.mixer.Sound('boom.wav')
e1.set_volume(0.4)
bu_b=pygame.image.load('bu_b.png')
bu_g=pygame.image.load('bu_g.png')
bu_r=pygame.image.load('bu_r.png')

en_b=pygame.image.load('en_b.png')
en_g=pygame.image.load('en_g.png')
en_r=pygame.image.load('en_r.png')
class Block1(pygame.sprite.Sprite):
    def __init__(self, img):
        super().__init__()
        self.image = img 
        self.rect = self.image.get_rect()
class Block2(pygame.sprite.Sprite):
    def __init__(self, img):
        super().__init__()
        self.image = img 
        self.rect = self.image.get_rect() 
class Block3(pygame.sprite.Sprite):
    def __init__(self, img):
        super().__init__()
        self.image = img 
        self.rect = self.image.get_rect()
class Player(pygame.sprite.Sprite):
 
    def __init__(self,img):
        super().__init__()
        
        self.image = img 
        self.rect = self.image.get_rect()
 
    def update(self):
        pos = pygame.mouse.get_pos()
        self.rect.y = pos[1]
 
 
class Bullet1(pygame.sprite.Sprite):
    def __init__(self,img):
        super().__init__()
 
        self.image = img 
 
        self.rect = self.image.get_rect()
 
    def update(self):
        """ Move the bullet. """
        self.rect.x += 3
class Bullet2(pygame.sprite.Sprite):
    def __init__(self,img):
        super().__init__()
 
        self.image = img 
 
        self.rect = self.image.get_rect()
 
    def update(self):
        """ Move the bullet. """
        self.rect.x += 3
class Bullet3(pygame.sprite.Sprite):
    def __init__(self,img):
        super().__init__()
 
        self.image = img 
 
        self.rect = self.image.get_rect()
 
    def update(self):
        """ Move the bullet. """
        self.rect.x += 3
pygame.init()
pygame.mixer.init()
pygame.display.set_caption("Metal Slug")
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode([screen_width, screen_height])
all_sprites_list = pygame.sprite.Group()
block1_list = pygame.sprite.Group()
block2_list = pygame.sprite.Group()
block3_list = pygame.sprite.Group()
bullet1_list = pygame.sprite.Group()
bullet2_list = pygame.sprite.Group()
bullet3_list = pygame.sprite.Group()
 
for i in range(15):
    block1 = Block1(en_b)
    block1.rect.x = random.randrange(450)+200
    block1.rect.y = random.randrange(440)+60
    block1_list.add(block1)
    all_sprites_list.add(block1)
for i in range(15):
    block2 = Block2(en_g)
    block2.rect.x = random.randrange(450)+200
    block2.rect.y = random.randrange(440)+60
    block2_list.add(block2)
    all_sprites_list.add(block2)
for i in range(15):
    block3 = Block3(en_r)
    block3.rect.x = random.randrange(450)+200
    block3.rect.y = random.randrange(440)+60
    block3_list.add(block3)
    all_sprites_list.add(block3)
 
player = Player(manimage)
all_sprites_list.add(player)
done = False
clock = pygame.time.Clock()
font = pygame.font.SysFont("arial", 30)
font1 = pygame.font.SysFont("arial", 100)
font2 = pygame.font.SysFont("arial", 20)
score = 0
enn=0
player.rect.x = 100
tt=0
while not done:
    if enn<45 and tt/1000.0+1<19.0:
      tt = pygame.time.get_ticks()
    tstamp = "Time: %s" % (tt/1000.0)
    text1 = font.render(tstamp, True, (255,0,0))
    text11 = font2.render("PRESS 'Z' ATTACK BLUE ENEMY", True, (0,0,255))
    text12 = font2.render("PRESS 'X' ATTACK GREEN ENEMY", True, (0,0,255))    
    text13 = font2.render("PRESS 'C' ATTACK RED ENEMY", True, (0,0,255))        
    text14 = font2.render("WARNING: YOU ONLY HAVE 15 SECOND", True, (255,0,0))
    text3 = font1.render("YOU WIN", True, (255,0,0))
    if tt/1000.0<18:
      text2 = font.render("score:"+(str)(score), True, (255,0,0))
    if tt/1000.0>=15.0:
      time.play();
    if tt/1000.0>=18:
      time.stop();
      text2 = font1.render("YOU LOSE", True, (255,0,0))
     
    for event in pygame.event.get():
       if event.type == pygame.QUIT:done = True
       if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_z:
                bullet1 = Bullet1(bu_b)
                bullet1.rect.x = player.rect.x
                bullet1.rect.y = player.rect.y
                all_sprites_list.add(bullet1)
                bullet1_list.add(bullet1)
                b1.play();
                score-=0.4
                
            if event.key == pygame.K_x:
                bullet2 = Bullet2(bu_g)
                bullet2.rect.x = player.rect.x
                bullet2.rect.y = player.rect.y
                all_sprites_list.add(bullet2)
                bullet2_list.add(bullet2)
                b1.play();
                score-=0.4
            if event.key == pygame.K_c:
                bullet3 = Bullet3(bu_r)
                bullet3.rect.x = player.rect.x
                bullet3.rect.y = player.rect.y
                all_sprites_list.add(bullet3)
                bullet3_list.add(bullet3)
                b1.play();
                score-=0.4
    all_sprites_list.update()
    for bullet1 in bullet1_list:
        block1_hit_list = pygame.sprite.spritecollide(bullet1, block1_list, True)
 
        for block1 in block1_hit_list:
            bullet1_list.remove(bullet1)
            e1.play();
            all_sprites_list.remove(bullet1)
            score += 2
            enn+=1
        if bullet1.rect.x > 810:
            bullet1_list.remove(bullet1)
            all_sprites_list.remove(bullet1)
    for bullet2 in bullet2_list:
        block2_hit_list = pygame.sprite.spritecollide(bullet2, block2_list, True)
 
        for block2 in block2_hit_list:
            bullet2_list.remove(bullet2)
            e1.play();
            all_sprites_list.remove(bullet2)
            score += 2
            enn+=1
        if bullet2.rect.x > 810:
            bullet2_list.remove(bullet2)
            all_sprites_list.remove(bullet2)
    for bullet3 in bullet3_list:
        block3_hit_list = pygame.sprite.spritecollide(bullet3, block3_list, True)
 
        for block3 in block3_hit_list:
            bullet3_list.remove(bullet3)
            e1.play();
            all_sprites_list.remove(bullet3)
            score += 2
            enn+=1
        if bullet3.rect.x > 810:
            bullet3_list.remove(bullet3)
            all_sprites_list.remove(bullet3)
    screen.blit(back,(0,0))
    screen.blit(text2, (100, 50))
    screen.blit(text1, (520,70))
    screen.blit(text11, (10, 525))
    screen.blit(text12, (10, 550))
    screen.blit(text13, (10, 575))
    screen.blit(text14, (300, 575))
    if enn==45 and tt/1000.0<=18:
      screen.blit(text3, (400, 300))
      time.stop();
    all_sprites_list.draw(screen)
    pygame.display.flip()
    clock.tick(60)
 
pygame.quit()
