Python 我在pygame中的行走动画不起作用
我正在尝试使用pygame在python中制作行走动画,但出现以下错误:Python 我在pygame中的行走动画不起作用,python,pygame,Python,Pygame,我正在尝试使用pygame在python中制作行走动画,但出现以下错误: Traceback (most recent call last): File "C:\Users\name\Desktop\game\game.py", line 37, in <module> player1 = player() File "C:\Users\name\Desktop\game\game.py", line 21, in __init__ self.img = py
Traceback (most recent call last):
File "C:\Users\name\Desktop\game\game.py", line 37, in <module>
player1 = player()
File "C:\Users\name\Desktop\game\game.py", line 21, in __init__
self.img = pygame.image.load(self.ani[1])
IndexError: list index out of range
我检查了文件名,我认为不是这样,但可能是“\r”是回车符,使您的文件名为“gameedplayer*.png”
尝试使用“game\\redplayer*.png”或r“game\redplayer*.png”来代替。self.ani的索引看起来比你的索引少了一个。列表中的第一项是索引0,因为您只加载了一个帧
self.img = pygame.image.load(self.ani[0])
我在我的机器上弄乱了你的代码以使它工作,在这里。:)
将每个动画帧预加载到预渲染曲面列表中,然后将适当的帧blit到屏幕上。
还添加了一个简单的游戏循环(按Q键退出),包括一个FPS时钟
import pygame
from pygame.locals import *
pygame.init()
clock = pygame.time.Clock()
FPS = 30
width = 800
height = 600
screen = pygame.display.set_mode((width, height), 0, 32)
class player:
def __init__(self, init_pos = (10,10), init_ani_speed = 10):
self.x = init_pos[0]
self.y = init_pos[1]
self.ani_speed_init = init_ani_speed
self.ani_speed = self.ani_speed_init
self.ani = []
# nux-style code here! :)
# self.ani.append(glob.glob("game\\redplayer_01*.png"))
# self.ani.append(glob.glob("game\\redplayer_02*.png"))
self.ani.append("./ani/super_neko_01.png")
self.ani.append("./ani/super_neko_02.png")
self.ani_max = len(self.ani)-1
self.ani_pos = 0
self.img = []
for item in self.ani:
self.img.append(pygame.image.load(item))
self.update(0)
def update(self, pos = 0):
# init: starts at 10, immediately blits self to screen at x,y
if pos != 0:
self.ani_speed -= 1
self.x += pos # move right by pos pixels
if self.ani_speed == 0:
self.ani_speed = self.ani_speed_init
if self.ani_pos < self.ani_max:
self.ani_pos += 1
else:
self.ani_pos = 0
tempSurf = self.img[self.ani_pos]
screen.blit(tempSurf,(self.x, self.y))
player1 = player()
# Main Game Loop
quit = False
while quit == False:
for event in pygame.event.get():
if (event.type == KEYUP):
if (event.key == K_q):
quit = True
if quit == False:
player1.update(1)
pygame.display.update()
clock.tick(FPS)
pygame.quit()
导入pygame
从pygame.locals导入*
pygame.init()
clock=pygame.time.clock()
FPS=30
宽度=800
高度=600
screen=pygame.display.set_模式((宽度、高度),0,32)
职业球员:
定义初始速度(self,初始位置=(10,10),初始速度=10):
self.x=初始位置[0]
self.y=init_pos[1]
self.aniu\u speed\u init=init\u aniu\u speed
self.ani\u speed=self.ani\u speed\u init
self.ani=[]
#nux样式代码在这里!:)
#self.ani.append(glob.glob(“game\\redplayer\u 01*.png”))
#self.ani.append(glob.glob(“game\\redplayer\u 02*.png”))
self.ani.append(“/ani/super_neko_01.png”)
self.ani.append(“/ani/super_neko_02.png”)
self.ani_max=len(self.ani)-1
self.ani_pos=0
self.img=[]
对于self.ani中的项目:
self.img.append(pygame.image.load(项目))
自我更新(0)
def更新(自我,位置=0):
#初始化:从10开始,在x,y处立即将self点显到屏幕上
如果位置!=0:
自身速度-=1
self.x+=pos#向右移动pos像素
如果self.ani\u速度==0:
self.ani\u speed=self.ani\u speed\u init
如果self.aniu位置
或导入操作系统
资源路径='game'os.path.join(资源路径,'redplayer*.png')
import pygame
from pygame.locals import *
pygame.init()
clock = pygame.time.Clock()
FPS = 30
width = 800
height = 600
screen = pygame.display.set_mode((width, height), 0, 32)
class player:
def __init__(self, init_pos = (10,10), init_ani_speed = 10):
self.x = init_pos[0]
self.y = init_pos[1]
self.ani_speed_init = init_ani_speed
self.ani_speed = self.ani_speed_init
self.ani = []
# nux-style code here! :)
# self.ani.append(glob.glob("game\\redplayer_01*.png"))
# self.ani.append(glob.glob("game\\redplayer_02*.png"))
self.ani.append("./ani/super_neko_01.png")
self.ani.append("./ani/super_neko_02.png")
self.ani_max = len(self.ani)-1
self.ani_pos = 0
self.img = []
for item in self.ani:
self.img.append(pygame.image.load(item))
self.update(0)
def update(self, pos = 0):
# init: starts at 10, immediately blits self to screen at x,y
if pos != 0:
self.ani_speed -= 1
self.x += pos # move right by pos pixels
if self.ani_speed == 0:
self.ani_speed = self.ani_speed_init
if self.ani_pos < self.ani_max:
self.ani_pos += 1
else:
self.ani_pos = 0
tempSurf = self.img[self.ani_pos]
screen.blit(tempSurf,(self.x, self.y))
player1 = player()
# Main Game Loop
quit = False
while quit == False:
for event in pygame.event.get():
if (event.type == KEYUP):
if (event.key == K_q):
quit = True
if quit == False:
player1.update(1)
pygame.display.update()
clock.tick(FPS)
pygame.quit()