import  pygame, random
pygame.init()
winWidth=1920
winHeight=1080
win=pygame.display.set_mode((winWidth,winHeight))
pygame.display.set_caption('ходилка-бродилка v1.07')

bul=pygame.image.load('bullet.png')
bul=pygame.transform.scale(bul,(50,50))

bg=pygame.image.load('dde0bfd077cdd0d91d654fb8beb0f449.jpeg')
player=pygame.image.load('idle.png')
right=[pygame.image.load('right_1.png'),
       pygame.image.load('right_2.png'),
       pygame.image.load('right_3.png'),
       pygame.image.load('right_4.png'),
       pygame.image.load('right_5.png'),
       pygame.image.load('right_6.png')]
left=[pygame.image.load('left_1.png'),
      pygame.image.load('left_2.png'),
      pygame.image.load('left_3.png'),
      pygame.image.load('left_4.png'),
      pygame.image.load('left_5.png'),
      pygame.image.load('left_6.png')]

clock=pygame.time.Clock()
moveright=False
moveleft=False
animCount = 0



x=50
y=900
width=60
height=71
speed=5
isJump=False
jumpCount=13

lastMove='right'
countBullets=1000000

class Obstacle():
    def __init__(self,x,y,hp):
        self.x=x
        self.y=y
        self.hp=hp
    def draw(self):
        pygame.draw.rect(win,(0,0,255),(self.x,self.y,40,90))
obstacle=Obstacle(random.randint(200,winWidth-200),890,random.randint(10,30))

class Bullet():
    def __init__(self,x,y,radius,color,lastMove):
        self.x=x
        self.y=y
        self.radius=radius
        self.color=color
        if lastMove=='right':
            self.velocity=18
        else:
            self.velocity=-18
    def draw(self,win):
        win.blit(bul, (self.x,self.y))


bullets=[]

run=True

def drawGame():
    global animCount

    win.blit(bg,( 0, 0))

    # pygame.draw.rect(win, (0, 0, 255), (x, y, width, height))
    if animCount>=30:
       animCount=0
    if moveleft:
        win.blit(left[animCount//5],(x,y))
        animCount+=1
    elif moveright:
        win.blit(right[animCount//5],(x,y))
        animCount+=1
    else:
        win.blit(player,(x,y))

    for bullet in bullets:
        bullet.draw(win)

    font=pygame.font.SysFont('Arial',30)
    if obstacle.hp>0:
        obstacle.draw()
        hpText=font.render(str(obstacle.hp),True,(255,255,255))
        win.blit(hpText,(obstacle.x+5,obstacle.y+15))

    font2=pygame.font.SysFont('Arial',16)
    creator=font2.render('create by Aleksey Pastushenok',True,(255,255,255))
    win.blit(creator,(10,10))


    pygame.display.update()

while run:
    pygame.time.delay(25)

    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            run=False
        elif event.type==pygame.KEYUP:
            if keys[pygame.K_l]:
                if len(bullets) < countBullets:
                    bullets.append(Bullet(x + width // 2, y, 5, (255, 0, 0), lastMove))


    for bullet in bullets:
        if bullet.x>0 and bullet.x<winWidth:
            if obstacle.hp>0 and bullet.y+50>obstacle.y:
               if bullet.x+25 not in range(obstacle.x,obstacle.x+40):
                   bullet.x+=bullet.velocity
               else:
                   obstacle.hp -=1
                   bullets.pop(bullets.index(bullet))
            else:
                bullet.x += bullet.velocity
        else:
            bullets.pop(bullets.index(bullet))

    keys=pygame.key.get_pressed()



    if keys[pygame.K_a] and x>speed :
        if y+height>890 and obstacle.hp>0:
            if x>obstacle.x+40+speed or x+width<obstacle.x:
                x-=speed
                moveleft=True
                moveright=False
                lastMove='left'
        else:
            x -= speed
            moveleft = True
            moveright = False
            lastMove = 'left'
    elif keys[pygame.K_d] and x<winWidth-speed-width:
        if y+height>890 and obstacle.hp>0:
            if x>obstacle.x+40 or x+width<obstacle.x-speed:
                x+=speed
                moveleft=False
                moveright=True
                lastMove='right'
        else:
            x+=speed
            moveleft=False
            moveright=True
            lastMove='right'
    elif keys[pygame.K_b]:
        if obstacle.hp==0:
            obstacle.hp=random.randint(10,30)
            obstacle.x=random.randint(200,winWidth-200)


    else:
        moveleft=False
        moveright=False
    if not(isJump):
        if keys[pygame.K_SPACE]:
            isJump=True
    else:
        if jumpCount>=-13:
            if jumpCount<0:
                y+=(jumpCount**2)//2
            else:
                y-=(jumpCount**2)//2
            jumpCount-=1
        else:
            isJump=False
            jumpCount=13

    drawGame()

pygame.quit()
