import pygame
import random
import os

black = (0, 0, 0)
white = (255,  255, 255)
red = (255, 0, 0)
width = 1000 #ширина
height = 1000 #высота
FPS = 60

pygame.init()
pygame.mixer.init()
display = pygame.display.set_mode((width, height))
pygame.display.set_caption("PatrickGame")
clock = pygame.time.Clock()

my_dir = os.path.dirname(__file__)
self_img = os.path.join(my_dir, 'img')
load = pygame.image.load(os.path.join(self_img, 'img_2.png'))
pygame.transform.scale(load, (100, 100))
load0 = pygame.image.load(os.path.join(self_img, 'bullet.png'))
pygame.transform.scale(load0, (32, 32))

font_name = pygame.font.match_font('arial')
def draw_font(surf, text, size, x, y):
    font = pygame.font.Font(font_name, size)
    text_surace = font.render(text, True, white)
    text_rect = text_surace.get_rect()
    text_rect.midtop = (x, y)
    surf.blit(text_surace, text_rect)

class Bullet(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((10, 20))
        self.image.fill((255, 255, 255))
        self.rect = self.image.get_rect()
        self.rect.center = (10, 10)
        self.speedy = -3.5
        self.coords = (player.rect.bottom, player.rect.center)
    def update(self):
        self.rect += self.speedy
        if self.rect < -10:
            self.kill()

class Mob(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((100, 100))
        self.image.fill(black)
        self.rect = self.image.get_rect()
        self.rect.center = (width / 4, height / 4)
        self.rect.x = random.randint(0, width - self.rect.width)
        self.rect.y = random.randint(-50, -30)
        self.speedx = random.randrange(-5, 5)
        self.speedy = random.randrange(1, 5)

    def update(self):
        self.rect.x += self.speedx
        self.rect.y += self.speedy
        if self.rect.left > width + 50 or self.rect.top > height + 50 or self.rect.right < -50:
            self.rect.x = random.randint(0, width - self.rect.width)
            self.rect.y = random.randint(-50, -30)
            self.speedx = random.randrange(-5, 5)
            self.speedy = random.randrange(1, 5)

class Player(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = load
        self.rect = self.image.get_rect()
        self.rect.center = (10, 10)
        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 = -8
        if keystate[pygame.K_RIGHT]:
            self.speedx = 8
        if keystate[pygame.K_DOWN]:
            self.speedy = -8
        if keystate[pygame.K_UP]:
            self.speedy = 8

        self.rect.x += self.speedx
        self.rect.y -= self.speedy
        if self.rect.right > width:
            self.rect.right = width
        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.top < 0:
            self.rect.top = 0
        if self.rect.bottom > height:
            self.rect.bottom = height
    def shoot(self):
        bullet = Bullet(self.rect.centerx, self.rect.top)
        all_sprites.add(bullet)
        bullets.add(bullet)

bullets = pygame.sprite.Group()
all_bullets = pygame.sprite.Group()
all_mobs = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
player = Player()

for i in range(8):
    m = Mob()
    all_sprites.add(player)
    all_mobs.add(m)


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


    all_sprites.update()
    all_mobs.update()

    display.fill(red)
    all_sprites.draw(display)
    all_mobs.draw(display)
    pygame.display.flip()

pygame.quit()
