import pygame
import os
import random


black = (0,0,0)
blue = (0,0,255)
w, h = 1000, 800
fps = 60

game_fold = os.path.dirname(__file__)
img_fold = os.path.join(game_fold, "img")
ppl_img = pygame.image.load(os.path.join(img_fold, "player.png"))
enn_img = pygame.image.load(os.path.join(img_fold, "enemy.png"))
bullet_img = pygame.image.load(os.path.join(img_fold, "ПУЛЯ-1.png"))
bback_img = pygame.image.load(os.path.join(img_fold, "fon1.png"))
back_img = pygame.transform.scale(bback_img, (w, h))
back_rect = back_img.get_rect()
pl_img = pygame.transform.scale(ppl_img, (70, 130))
en_img = pygame.transform.scale(enn_img, (70, 130))
bullet_img = pygame.transform.scale(bullet_img, (70, 130))

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pl_img
        self.rect = self.image.get_rect()
        self.rect.center = (w / 2, h / 2)
        self.speedx = 0
        self.speedy = 0

    def update(self):
        self.speedx = 0
        self.speedy = 0
        keystate = pygame.key.get_pressed()
        if keystate[pygame.K_LEFT]:
            self.speedx = -5
        if keystate[pygame.K_RIGHT]:
            self.speedx = 5
            self.rect.x += self.speedx
        if self.rect.right > w:
            self.rect.right = w
        if self.rect.left < 0:
            self.rect.left = 0
        if keystate[pygame.K_DOWN]:
            self.speedy = 5
        if keystate[pygame.K_UP]:
            self.speedy = -5
        if self.rect.bottom > h:
            self.rect.bottom = h
        if self.rect.top < 0:
            self.rect.top = 0
        self.rect.x += self.speedx
        self.rect.y += self.speedy
    def shoot(self):
        bullet = p(self.rect.centerx, self.rect.top)
        all_sprites.add(bullet)
        bullets.add(bullet)

class Mob(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = en_img
        self.rect = self.image.get_rect()
        self.rect.x = random.randrange(0, w - self.rect.width)
        self.rect.y = random.randrange(-50, -30)
        self.speedx = random.randrange(-8, 8)
        self.speedy = random.randrange(1, 8)
    def update(self):
        self.rect.x += self.speedx
        self.rect.y += self.speedy
        if self.rect.top > 800 + 10 or self.rect.left < -30 or self.rect.right > w + 30:
            self.rect.x = random.randrange(w - self.rect.width)
            self.rect.y = random.randrange(-50, -30)
            self.speedy = random.randint(-8, 8)
            self.speedx = random.randint(1, 8)

class p(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = bullet_img
        self.image.set_colorkey(blue)
        self.rect = self.image.get_rect()
        self.rect.bottom = y
        self.rect.centerx = x
        self.speedy = -10

    def update(self):
        self.rect.y += self.speedy
        if self.rect.bottom < 0:
            self.kill()

pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((w,h))
pygame.display.set_caption("My Game")
clock = pygame.time.Clock()

mobs = pygame.sprite.Group()
bullets = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
player = Player()
for i in range(5):
    m = Mob()
    all_sprites.add(m)
    mobs.add(m)
all_sprites.add(player)

running = True
while running:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                player.shoot()

    all_sprites.update()
    hits = pygame.sprite.groupcollide(mobs, bullets, True, True)
    for hit in hits:
        m = Mob()
        all_sprites.add(m)
        mobs.add(m)


    hits = pygame.sprite.spritecollide(player, mobs, False)
    if hits:
        running = False
    font_name = pygame.font.match_font("arial")


    def draw_text(surf, text, size, x, y):
        font = pygame.font.Font(font_name, size)
        text_surface = font.render(text, True, w)
        text_rect = text_surface.get_rect()
        text_rect.midtop = (x, y)
        surf.blit(text_surface, text_rect)


    screen.fill(blue)
    screen.blit(back_img, back_rect)
    all_sprites.draw(screen)


    pygame.display.flip()

pygame.quit()