角色动画发生得非常快
Character Animation happens very fast
请使用 PyGame 零库
查找以下代码
尽管应用了 clock.schedule
,但函数 trex_move()
中的角色动画发生得非常快。
因此,动画看起来不自然。有什么办法可以减慢动画速度吗?
import random
TITLE = "T-Rex Run"
WIDTH=500
HEIGHT=500
GAP = 200
trex = Actor('run1',(100,235))
cactus = Actor('cactus1',(200,235))
trex_run = ['run1','run2']
all_cactus = ['cactus1','cactus2','cactus3','cactus4','cactus5']
JUMP = 0.15
JUMP_VELOCITY = -7
SPEED = 5
x=0
trex.vy = 0
def draw():
screen.fill((255,255,255))
screen.blit('floor-1',(0,250))
trex.draw()
cactus.draw()
def reset_cactus():
cactus.image = random.choice(all_cactus)
cactus.pos = (WIDTH, 235)
def update_cactus():
cactus.left -= SPEED
if cactus.right < 0:
reset_cactus()
def move_cactus():
speed = 5
cactus.left-=SPEED
if cactus.left>WIDTH:
cactus.right=0
def test():
#pass
trex.image = trex_run[x]
print("test1")
def trex_move():
global x
if trex.y == 235:
trex.image = trex_run[x]
x += 1
if x > 1:
x = 0
clock.schedule(test,1.0)
else:
trex.image = 'jump'
def update():
update_cactus()
trex_move()
if keyboard.up or keyboard.SPACE:
trex.vy = JUMP_VELOCITY
trex.vy += JUMP*2
trex.y += trex.vy
if(trex.y > 235):
trex.y = 235
Actor 有两个帧,尽管定义了一个有 1 秒延迟的时钟,但两个帧现在都运行超快。
您必须为多个帧显示相同的图像。定义同一图像应显示多少帧(例如 frames_per_image
)并在超出帧数后切换图像。使用 //
(底除法)运算符(参见 Binary arithmetic operations)计算当前图像的索引 (x // frames_per_image
)。例如:
def trex_move():
global x
if trex.y == 235:
frames_per_image = 10
trex.image = trex_run[x // frames_per_image]
x += 1
if x // frames_per_image >= len(trex_run):
x = 0
else:
trex.image = 'car64'
请使用 PyGame 零库
查找以下代码尽管应用了 clock.schedule
,但函数 trex_move()
中的角色动画发生得非常快。
因此,动画看起来不自然。有什么办法可以减慢动画速度吗?
import random
TITLE = "T-Rex Run"
WIDTH=500
HEIGHT=500
GAP = 200
trex = Actor('run1',(100,235))
cactus = Actor('cactus1',(200,235))
trex_run = ['run1','run2']
all_cactus = ['cactus1','cactus2','cactus3','cactus4','cactus5']
JUMP = 0.15
JUMP_VELOCITY = -7
SPEED = 5
x=0
trex.vy = 0
def draw():
screen.fill((255,255,255))
screen.blit('floor-1',(0,250))
trex.draw()
cactus.draw()
def reset_cactus():
cactus.image = random.choice(all_cactus)
cactus.pos = (WIDTH, 235)
def update_cactus():
cactus.left -= SPEED
if cactus.right < 0:
reset_cactus()
def move_cactus():
speed = 5
cactus.left-=SPEED
if cactus.left>WIDTH:
cactus.right=0
def test():
#pass
trex.image = trex_run[x]
print("test1")
def trex_move():
global x
if trex.y == 235:
trex.image = trex_run[x]
x += 1
if x > 1:
x = 0
clock.schedule(test,1.0)
else:
trex.image = 'jump'
def update():
update_cactus()
trex_move()
if keyboard.up or keyboard.SPACE:
trex.vy = JUMP_VELOCITY
trex.vy += JUMP*2
trex.y += trex.vy
if(trex.y > 235):
trex.y = 235
Actor 有两个帧,尽管定义了一个有 1 秒延迟的时钟,但两个帧现在都运行超快。
您必须为多个帧显示相同的图像。定义同一图像应显示多少帧(例如 frames_per_image
)并在超出帧数后切换图像。使用 //
(底除法)运算符(参见 Binary arithmetic operations)计算当前图像的索引 (x // frames_per_image
)。例如:
def trex_move():
global x
if trex.y == 235:
frames_per_image = 10
trex.image = trex_run[x // frames_per_image]
x += 1
if x // frames_per_image >= len(trex_run):
x = 0
else:
trex.image = 'car64'