C
C
classicartt2021-11-25 22:12:35
pygame
classicartt, 2021-11-25 22:12:35

How to make animation in pygame?

I watched a bunch of video tutorials, I don’t understand what to write in order for the character animation to work.

import self as self
import pygame as pg
from pygame import *
import pyganim

COMMON_SPEED = 10
SCREEN_HEIGHT = 1080
SCREEN_WIDTH = 1920
HOR, VERT = 50, 50
COLOR = "#888888"

l, r = HOR, SCREEN_WIDTH - HOR
u, d = VERT, SCREEN_HEIGHT - VERT

animCount = 0
left = False
right = False

walkRight = [pg.image.load('графика/4x/1.png'),
             pg.image.load('графика/4x/2.png'),
             pg.image.load('графика/4x/3.png'),
             pg.image.load('графика/4x/4.png'),
             pg.image.load('графика/4x/5.png'),
             pg.image.load('графика/4x/6.png'),
             pg.image.load('графика/4x/7.png'),]

walkLeft = [pg.image.load('графика/4x/left/1.png'),
             pg.image.load('графика/4x/left/2.png'),
             pg.image.load('графика/4x/left/3.png'),
             pg.image.load('графика/4x/left/4.png'),
             pg.image.load('графика/4x/left/5.png'),
             pg.image.load('графика/4x/left/6.png'),
             pg.image.load('графика/4x/left/7.png'),]

idle = pg.image.load('графика/drag-stay.png')


class Player(pg.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pg.transform.scale(pg.image.load("графика/drag.png"), (100, 83))  # загрузка изобраения персонажа
        self.rect = self.image.get_rect()

        self.image.set_colorkey(Color(COLOR))  # делаем фон прозрачным


    def update(self):
        key = pg.key.get_pressed()
        left = key[pg.K_a]
        right = key[pg.K_d]
        speedy = 0
        speedx = 0
        keystate = pg.key.get_pressed()

        if keystate[pg.K_w]:
            speedy = -COMMON_SPEED
        elif keystate[pg.K_s]:
            speedy = COMMON_SPEED

        self.rect.y += speedy

        if self.rect.bottom > d:
            self.rect.bottom = d
        if self.rect.top < u:
            self.rect.top = u

        if keystate[pg.K_a]:
            speedx = -COMMON_SPEED
        elif keystate[pg.K_d]:
            speedx = COMMON_SPEED

        self.rect.x += speedx

        if self.rect.left < l:
            self.rect.left = l
        if self.rect.right > r:
            self.rect.right = r

        if left:
            self.xvel = -COMMON_SPEED  # Лево = x - n

    # def drawWindow(self):
    #     global animCount
    # 
    #     if animCount + 1 >= 30:
    #         animCount = 0
    # 
    #     if left:

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question