14
Сен
2021

Создание вокруг игрока прямоугольника

Захотел сделать анимацию удара, но для этого нужно cоздать вокруг основного персонажа( class player) прямоугольник(rect), который будет следовать за ним. Но данный прямоугольник не появляется, из-за чего персонаж бьёт только при движение по оси x. Помогите пожалуйста)

pygame.init()
win = pygame.display.set_mode((1000, 600))

pygame.display.set_caption("Runner")

walkRight = [pygame.image.load('1_right.png'), pygame.image.load('2_right.png'),pygame.image.load('1_right.png'),pygame.image.load('2_right.png'),pygame.image.load('1_right.png'),pygame.image.load('2_right.png')]

walkLeft = [pygame.image.load("1_left.png"), pygame.image.load("2_left.png"),pygame.image.load("1_left.png"),pygame.image.load("2_left.png"),pygame.image.load("1_left.png"),pygame.image.load("2_left.png")]
            
swordAttackright = [pygame.image.load("sword_atack1.png"),pygame.image.load("sword_atack2.png"),pygame.image.load("sword_atack3.png"),pygame.image.load("sword_atack4.png"),pygame.image.load("sword_atack5.png"),pygame.image.load("sword_atack6.png")]

jump = pygame.image.load("jump.png")

playerStand = pygame.image.load("idle1.png")

monu1 = pygame.image.load("bg.png")

clock = pygame.time.Clock()

x = 50
y = 430
width = 60
height = 71
speed = 15

isSword = False
swordCount = 10
isJump = False
jumpCount = 10

attack = False
left = False
up = False
down = False
right = False
animCount = 0
lastMove = "right"

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

class Player(pygame.sprite.Sprite):
    def __init__(self,x,y,dir,):
        super().__init__()
        self.image = pygame.Surface(width, height)
        self.dir = dir
        self.rect = (40, 40 , 40, 40)
        self.attack = pygame.image.load("sword_atack1.png")
class snaryad():
    def __init__(self, x , y , radius, color, facing):
        self.x = x
        self.y = y
        self.radius = radius
        self.color = color
        self.facing = facing
        self.vel = 15 * facing
    def draw(self, win):
        pygame.draw.circle(win, self.color, (self.x, self.y), self.radius)
def drawWindow():
    global animCount
    win.blit(monu1, (0, 0))

    if animCount + 1 >= 30:
        animCount = 0

    if left:
        win.blit( walkLeft[animCount // 5], (x, y) )
        animCount += 1
    elif right:
        win.blit( walkRight[animCount // 5], (x, y) )
        animCount += 1
    else:
        win.blit(playerStand, (x, y))

    if animCount + 1 >= 30:
        animCount = 0
    if attack:
        win.blit (swordAttackright [animCount // 5], (rect)) 
        animCount += 1
    else:
        win.blit(playerStand, (x, y))

    for bullet in bullets:
        bullet.draw(win)
    
    pygame.display.update()

run = True
bullets = []
sword = []
while run:
    clock.tick(30)
    
    for event in pygame.event.get():
            
        if event.type == pygame.QUIT:
            run = False   
    
    keys = pygame.key.get_pressed()
    if event.type == pygame.MOUSEBUTTONDOWN:
        if lastMove == "right":
            facing = 1
        else:
            facing = -1
        if len(bullets) < 5:
            bullets.append(snaryad(round(x + width // 2), round(y + height // 2), 5, (255, 0, 0), facing))
                
    for bullet in bullets:
        if bullet.x < 1000 and bullet.x > 0:
            bullet.x += bullet.vel
        else:
            bullets.pop(bullets.index(bullet))
    if keys[pygame.K_q] and x < 1000 - width - 5:
        x += speed * 2.5
    if keys[pygame.K_e] and x > 5:
        x -= speed * 2.5
    if keys[pygame.K_w] and y > 5:
        y -= speed
    if keys[pygame.K_s] and y < 600 - height -5:
        y += speed
    if keys[pygame.K_a] and x > 5:
        x -= speed
        left = True
        right = False
        lastMove = "left"
    elif keys[pygame.K_d] and x < 1000 - width - 5:
        x += speed
        left = False
        right = True
        lastMove = "right"
    else:
        left = False
        right = False
        animCount = 0
                
    if not(isJump):
        if keys[pygame.K_SPACE]:
            isJump = True
    else:
        if jumpCount >= -10:
            if jumpCount < 0:
                y += (jumpCount ** 2) / 2
            else:
                y -= (jumpCount ** 2) / 2
            jumpCount -= 1
        else:
            isJump = False
            jumpCount = 10
    if keys[pygame.K_f]:
        attack = True
    else:
        if swordCount >= -10:
            if swordCount < 0:
                y = y
                x = x
            else:
                attack = False
            
    drawWindow()

pygame.quit()```


Источник: https://ru.stackoverflow.com/questions/1328501/%D0%A1%D0%BE%D0%B7%D0%B4%D0%B0%D0%BD%D0%B8%D0%B5-%D0%B2%D0%BE%D0%BA%D1%80%D1%83%D0%B3-%D0%B8%D0%B3%D1%80%D0%BE%D0%BA%D0%B0-%D0%BF%D1%80%D1%8F%D0%BC%D0%BE%D1%83%D0%B3%D0%BE%D0%BB%D1%8C%D0%BD%D0%B8%D0%BA%D0%B0

Тебе может это понравится...

Добавить комментарий