gpt4 book ai didi

python pygame动画无需类即可工作,但是一旦我将其放入类中,它就不想工作了?

转载 作者:行者123 更新时间:2023-12-04 10:32:07 25 4
gpt4 key购买 nike

好的,所以基本上我有这个我正在制作的游戏,在我将它放入面向对象的编程类之前,它运行得非常好,动画在移动时也能运行,但是一旦我将其更改为面向对象的编程,我的角色就​​会移动,但是没有动画,他移动时也会隐形,只有当我停止按下移动按钮时才会出现,请帮忙

继承人的代码:

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 Indentation问题。第一个在 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))

关于python pygame动画无需类即可工作,但是一旦我将其放入类中,它就不想工作了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60366295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com