import pygame
import random
import os

Dark_QUAN = (36, 99, 91)
White = (255, 255, 255)
FPS = 30
W = 734
H = 734

game_foulder = os.path.dirname(__file__)
img_foulder = os.path.join(game_foulder, 'img')
IMG_img = pygame.image.load(os.path.join(img_foulder, 'Forest.jpg')) #спрайт фона
player_img = pygame.image.load(os.path.join(img_foulder, 'ghost.png')) #спрайт игрока
Enemy_img = pygame.image.load(os.path.join(img_foulder, 'cloud.png')) #спрайт врага
Mob_img = pygame.image.load(os.path.join(img_foulder, 'cloud.png')) #спрайт моба
Bullet_img = pygame.image.load(os.path.join(img_foulder, 'pipis.png'))#спрайт пули

pygame.transform.scale(player_img, (30, 30))
class IMG (pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = IMG_img
        self.rect = self.image.get_rect()
        self.rect.center = (H / 2, W / 2)

class Player (pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = player_img
        self.rect = self.image.get_rect()
        self.rect.center = (H / 2, W / 2)
    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_UP]:
            self.speedy = -8
        if keystate[pygame.K_DOWN]:
            self.speedy = 8
        self.rect.x += self.speedx
        self.rect.y += self.speedy
        if self.rect.right > W:
            self.rect.right = W
        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.top < 0:
            self.rect.top = 0
        if self.rect.bottom > H:
            self.rect.bottom = H
    def shoot(self):
        bullet = Bullet(self.rect.centerx, self.rect.top)
        all_sprites.add(bullet)
        bullets.add(bullet)

class Enemy (pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = Enemy_img
        self.rect = self.image.get_rect()
        self.rect.center = (0, 25)
        self.d = 0
        self.speed = 25

    def update(self):
        if self.d == 0: # right
            self.rect.x += self.speed
            if self.rect.right >= 734:
                self.rect.right = 734
                self.d = 1
        elif self.d == 1: # down
            self.rect.y += self.speed
            if self.rect.bottom >= 734:
                self.rect.bottom = 734
                self.d = 2
        elif self.d == 2: # left
            self.rect.x -= self.speed
            if self.rect.left <= 0:
                self.rect.left = 0
                self.d = 3
        elif self.d == 3: # top
            self.rect.y -= self.speed
            if self.rect.top <= 0:
                self.rect.top = 0
                self.d = 0

class Mob (pygame.sprite.Sprite):
    def __init__ (self):
        pygame.sprite.Sprite.__init__(self)
        self.image = Mob_img
        self.rect = self.image.get_rect()
        self.rect.x = random.randrange(0, W - self.rect.width)
        self.rect.y = random.randrange(-60, -40)
        self.speedx = random.randint(-5, 10)
        self.speedy = random.randint(5, 10)
    def update(self):
        self.rect.x += self.speedx
        self.rect.y += self.speedy
        if self.rect.top > H or self.rect.left > W or self.rect.right < 0:
            self.rect.x = random.randrange(0, W - self.rect.width)
            self.rect.y = random.randrange(-60, -40)
            self.speedx = random.randint(-5, 7)
            self.speedy = random.randint(0, 5)
        #if self.rect.right <= W: #and self.rect.top == 0:
         #   self.rect. x += 10
        #elif self.rect.bottom <= H:
         #   self.rect. y += 10
        #if self.rect.left >= W: #and self.rect.bottom == W:
         #   self.rect.x -= 10
        #elif self.rect.bottom >= HIGHT:
        #    self.rect.y += 50
       #if self.rect. == 0:
        #   return


class Bullet(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = Bullet_img
        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



pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((W, H))
Clock = pygame.time.Clock()

all_sprites = pygame.sprite.Group()
mobs = pygame.sprite.Group()
bullets = pygame.sprite.Group()
player = Player()
enemy = Enemy()
all_sprites.add(player, enemy)

IMG_sprites = pygame.sprite.Group()
IMG = IMG()
IMG_sprites.add(IMG)
Score = 0

for i in range(5):
    m = Mob()
    all_sprites.add(m)
    mobs.add(m)

font_game = pygame.font.match_font("arial")
def draw_text(surf, text, size, x, y):
    font = pygame.font.Font(font_game, size)
    text_surface = font.render(text, True, White)
    text_rect = text_surface.get_rect()
    text_rect.midtop = (x, y)
    surf.blit(text_surface, text_rect)

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_ESCAPE:
                running = False
            elif event.key == pygame.K_SPACE:
                player.shoot()

    #red = random.randint(0, 255)
    #blue = random.randint(0, 255)
    #green = random.randint(0, 255)
    all_sprites.update()
    hits = pygame.sprite.spritecollide(player, mobs, False)
    if hits:
        running = False

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

    IMG_sprites.draw(screen)
    #screen.fill((Dark_QUAN))
    all_sprites.draw(screen)
    draw_text(screen, str(Score), 30, W / 2, 15 )
    pygame.display.update()


pygame.quit()




