хорошо, так что в основном у меня есть эта игра, которую я делаю, и прежде чем я положил ее в объектно-ориентированное программирование с классами, она работала отлично, анимация работала при движении, но как только я изменил ее на объектно-ориентированное программирование, мой персонаж движется, но без анимации, он также становится невидимым, когда он движется, появляется только тогда, когда я прекращаю нажимать кнопку перемещения, помогите
вот код:
import time
import pygame
pygame.init()
win = pygame.display.set_mode((1280,720)) #creates a window size of 640 x 480 pixels
pygame.display.set_caption("World of Python") #The caption of the window is "World of Python"
pygame.transform.scale(pygame.image.load('R1.png'), (40, 60))
pygame.transform.scale(pygame.image.load('R2.png'), (40, 60))
pygame.transform.scale(pygame.image.load('R3.png'), (40, 60))
pygame.transform.scale(pygame.image.load('L1.png'), (40, 60))
pygame.transform.scale(pygame.image.load('L2.png'), (40, 60))
pygame.transform.scale(pygame.image.load('L3.png'), (40, 60))
pygame.transform.scale(pygame.image.load('U1.png'), (40, 60))
pygame.transform.scale(pygame.image.load('U2.png'), (40, 60))
pygame.transform.scale(pygame.image.load('U3.png'), (40, 60))
pygame.transform.scale(pygame.image.load('D1.png'), (40, 60))
pygame.transform.scale(pygame.image.load('D2.png'), (40, 60))
pygame.transform.scale(pygame.image.load('D3.png'), (40, 60))
moveRight = [pygame.image.load('R1.png'), pygame.image.load('R2.png'), pygame.image.load('R3.png')] #list of frames
moveLeft = [pygame.image.load('L1.png'), pygame.image.load('L2.png'), pygame.image.load('L3.png')]#list of frames
moveUp = [pygame.image.load('U1.png'), pygame.image.load('U2.png'), pygame.image.load('U3.png')]#list of frames
moveDown = [pygame.image.load('D1.png'), pygame.image.load('D2.png'), pygame.image.load('D3.png')]#list of frames
character = pygame.image.load('D2.png') #standard frame
bg = pygame.image.load('Grass.png') #background
character = pygame.transform.scale(character, (40, 60))
class player(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.left = False
self.right = False
self.down = False
self.up = False
self.moveCount = 0
def draw(self,win):
if self.moveCount + 1 >= 9: #if move is greater than or equal to 9
self.moveCount = 0
if self.left:
win.blit(moveLeft[self.moveCount//3], (self.x,self.y)) #goes through the frames integer division (MOD), 1,2,3 because 3 frames for each movement.
self.moveCount += 1
elif self.right:
win.blit(moveRight[self.moveCount//3], (self.x,self.y))#goes through the frames integer division (MOD), 1,2,3 because 3 frames for each movement.
self.moveCount += 1
elif self.up:
win.blit(moveUp[self.moveCount//3], (self.x,self.y))#goes through the frames integer division (MOD), 1,2,3 because 3 frames for each movement.
self.moveCount += 1
elif self.down:
win.blit(moveDown[self.moveCount//3], (self.x,self.y))#goes through the frames integer division (MOD), 1,2,3 because 3 frames for each movement.
self.moveCount += 1
else:
win.blit(character, (self.x,self.y)) #if character is standing, draw character in its position
#main
def redrawGameWindow():
win.blit(bg, (0,0)) #spawns background at coordinate 0,0
man.draw(win)
pygame.display.update()
man = player(920, 240, 40, 60)
run = True
while run: #while loop
pygame.time.delay(25)#framerate of 40, (milliseconds)
for event in pygame.event.get():
if event.type == pygame.QUIT: #if you pressed x, you exit
run = False
keys = pygame.key.get_pressed() #defines keys pressed
if keys[pygame.K_LEFT] and man.x > man.vel: #checks for border and if a button is pressed
man.x -= man.vel
man.left = True
man.right = False
elif keys[pygame.K_RIGHT]and man.x < 1280 - man.width:#checks for border and if a button is pressed
man.x += man.vel
man.right = True
man.left = False
elif keys[pygame.K_UP] and man.y > man.vel:#checks for border and if a button is pressed
man.y -= man.vel
man.up = True
man.down = False
elif keys[pygame.K_DOWN] and man.y < 720 - man.height:#checks for border and if a button is pressed
man.y += man.vel
man.down = True
man.up = False
else: #this is incase the player is not moving
man.left = False
man.right = False
man.up = False
man.down = False
man.moveCount = 0
redrawGameWindow()
pygame.quit()