2
배경 이미지를 표시하고 3 개의 프레임으로 간단한 스프라이트에 애니메이션을 적용하는 간단한 프로그램을 만들고 있습니다. 나는 그것을 실행할 때마다, 그것은 나에게 내가 지금 잠시 동안이 작업했던 오류파이 게임에서 스프라이트 시트 사용하기
screen.blit(firstexp[currentimage], (xpos,ypos)) IndexError: list index out of range
를 제공하고 더 이상 할 모르겠어요. 당신은 고양이 클래스의 update()
방법을 변경해야
import pygame, sys, random
from pygame.locals import *
pygame.init()
class Cat(pygame.sprite.Sprite):
animationArray = []
numframes = 0
currentframe = 0
anispeed = 0
lastdraw = 0
xspeed = 5
xpos = 10
ypos = 10
def __init__(self, imgname, startx, starty, w, h, numframes, anispeed):
pygame.sprite.Sprite.__init__(self)
spsheet = pygame.image.load(imgname)
f1 = spsheet.subsurface(0,0,120,60)
self.animationArray.append(f1)
f2 = spsheet.subsurface(0,60,120,60)
self.animationArray.append(f2)
f3 = spsheet.subsurface(0,120,120,60)
self.animationArray.append(f3)
self.numframes = 3
self.currentframe = 0
self.anispeed = anispeed
def update(self, secs):
self.xpos = self.xpos + self.xspeed
self.lastdraw = self.lastdraw+secs
if self.lastdraw self.anispeed:
self.currentframe = self.currentframe+1
if self.currentframe==2:
self.currentframe=0
self.lastdraw = 0
self.image = self.animationArray[self.currentframe]
self.rect = (self.xpos,0,0,120,180)
def setEnv(background):
clock = pygame.time.Clock()
backimage = pygame.image.load(background)
w = backimage.get_rect().w
h = backimage.get_rect().h
screen = pygame.display.set_mode((w,h))
return clock, backimage, screen
###main game code
clock, backimage, screen = setEnv("landscape-illustration.jpg")
allSprites = pygame.sprite.Group() collSprites = pygame.sprite.Group()
catx = Cat("runningcat.png", 0,0,120,180,3,1)
allSprites.add(catx)
collSprites.add(catx)
screen.blit(backimage,(0,0))
while True:
secs = clock.tick(30)/1000
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
allSprites.clear(screen, backimage)
allSprites.update(secs)
allSprites.draw(screen)
collSprites.draw(screen)
pygame.display.flip()
왜 'allSprites' 그룹과'collSprites' 그룹을 그려야하는지 알지 못합니다. 한 번만 그리는 것이 훨씬 쉽습니다. – cweb