python - python pygame animation works without class but as soon as i put it into a class it doesnt want to work?
问题描述
okay, so basically i have this game that im making and before i put it into Object Oriented Programming with classes, it worked perfectly fine, the animation worked when moving, but as soon as i changed it into Object Oriented Programming, my character moves but no animation, he also goes invisible when he moves, only appears when i stop pressing a movement button please help
heres the code:
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()
解决方案
有2个缩进问题。第一个在player.draw
:
class player(object):
# [...]
def draw(self,win):
if self.moveCount + 1 >= 9:
self.moveCount = 0
#<--| INDENTATION !!!
if self.left:
win.blit(moveLeft[self.moveCount//3], (self.x,self.y))
self.moveCount += 1
elif self.right:
# [...]
第二个是在主循环中:
run = True
while run:
# [...]
else: #this is incase the player is not moving
man.left = False
man.right = False
man.up = False
man.down = False
man.moveCount = 0
#<--| INDENTATION !!!
redrawGameWindow()
此外,pygame.transform.scale
代码开头的指令是无用的,因为pygame.transform.scale
. 不缩放表面本身,它返回一个新的缩放表面。
更改代码如下(str
将数字转换为字符串):
moveRight = [pygame.transform.scale(pygame.image.load('R' + str(i+1) + '.png'), (40, 60)) for i in range(3)]
moveLeft = [pygame.transform.scale(pygame.image.load('L' + str(i+1) + '.png'), (40, 60)) for i in range(3)]
moveUp = [pygame.transform.scale(pygame.image.load('U' + str(i+1) + '.png'), (40, 60)) for i in range(3)]
moveDown = [pygame.transform.scale(pygame.image.load('D' + str(i+1) + '.png'), (40, 60)) for i in range(3)]
character = pygame.transform.scale(pygame.image.load('D2.png'), (40, 60))
推荐阅读
- r - R中的函数:如何在同一函数内返回均值、中值、标准差
- android - Firestore 在查询中返回已删除的文档
- matlab - 修复 MATLAB 标题图中的额外空间
- python - 移动平均时间序列分钟 Pandas
- php - 在 laravel phpunit 上检测到恶意软件
- c# - 如何处理带有问号“?”的旧 aspx url 在不使用 IIS 的新 mvc 网站中
- reactjs - 如何使用 react-bootstrap 模式
- cypress - 从 cypress 测试运行程序运行时目录路径不正确
- python-3.x - 烧瓶返回无,request.form 可能存在问题
- r - 跨时间的交互式多面情节?