import pygame
import os
import random
ELEFAN = (0,0,0)
begib = (230,218,166)
karii = (139,69,19)
mehogon = (76,47,39)
W, H = (1000 , 500)
fps = 60

pygame.init()
pygame.mixer.init()
skrin = pygame.display.set_mode((W, H))
pygame.display.set_caption("зв демо")
cloc = pygame.time.Clock()

my = os.path.dirname(__file__)
image_folder = os.path.join(my, "img")
pl_im = pygame.image.load(os.path.join(image_folder, "m.png"))
pl_im = pygame.transform.scale(pl_im,(350,150))

class Luw (pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pl_im
        self.rect = self.image.get_rect()
        self.rect.centerx = W / 2
        self.rect.centery = H-100
        self.spidx = 0
    def update(self):
        self.spidx =0
        kd = pygame.key.get_pressed()
        if kd[pygame.K_a]:
            self.spidx -=8
        if kd[pygame.K_d]:
            self.spidx +=8
        self.rect.x += self.spidx
        if self.rect.right > W + 100:
            self.rect.right = W + 100
        if self.rect.left < -100:
            self.rect.left = -100

class Mob (pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50, 50))
        self.image.fill(karii)
        self.rect = self.image.get_rect()
        self.rect.x = random.randint(0,W)
        self.rect.y =  20
        self.spidx = 3
    def update(self):
        self.rect.x += self.spidx
        if self.rect.right > W+20:
            self.rect.x = 0
    def shoot(self):
        bullet = puli(self.rect.centerx, self.rect.top)
        all_sprite.add(bullet)
        bullet.add(bullet)

class puli (pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((10, 20))
        self.image.fill(mehogon)
        self.rect = self.image.get_rect()
        self.rect.bottom = y
        self.rect.centerx = x
        self.spidy = -10

    def update(self):
        self.rect.y = self.spidy
        if self.rect.bottom < 0:
            self.kill()
all_sprite = pygame.sprite.Group()
luw = Luw()
fon = Mob()
all_sprite.add(fon, bullet, luw)


r = True
while r:
    cloc.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            r = False
    skrin.fill(ELEFAN)
    all_sprite.update()
    all_sprite.draw(skrin)
    pygame.display.flip()
pygame.quit()
