玩家在屏幕上移动正确的速度,行走周期太快,希望建议正确地做到这一点

时间:2014-11-28 01:16:07

标签: python animation pygame game-engine

我跳过试图成功完成我的第一场比赛的教程。我采取了一种重型的面向对象的方法来管理资源,但我觉得这让我的理解蒙上阴影,所以我重新启动了。我现在让我的角色以我喜欢的速度在屏幕上平滑移动,我的问题是我的步行周期"三张图像快速进入,我的小鸡腿上有持久的视觉效果。我需要放慢速度,到目前为止我能够通过在每个决策循环后弹出对时钟的调用来达到预期的效果,但我担心会减慢整个游戏逻辑,因为我不确定是否调用时钟不止一次是"冷冻"当角色决定移动时我的游戏及时更新。我曾考虑过制作某种类型的方程式,比较时钟上的时间和时钟之前的时间,以减慢步行周期关键帧。任何帮助或建议,是否有更简单的方法?非常感谢。
    import pygame,sys     来自pygame.locals import *

pygame.init()

#Contstants
BLACK = (0, 0, 0)
SCREENWIDTH = 300
SCREENHEIGHT = 300
game_running = True
clock = pygame.time.Clock()

#variables
current = 0
class Player(object):
    def __init__(self):
        self.avatar_front = pygame.image.load("chicken_front.png")
        self.avatar_back = pygame.image.load("chicken_back.png")
        self.avatar_right = pygame.image.load("chicken_right_stand.png")
        self.avatar_left = pygame.image.load("chicken_left_stand.png")
        self.avatar_left_walk = pygame.image.load("chicken_left_walk1.png")
        self.avatar_left_walk2 = pygame.image.load("chicken_left_walk2.png")     
        self.avatar_right = pygame.image.load("chicken_right_stand.png")
        self.avatar_right_walk = pygame.image.load("chicken_right_walk1.png")
        self.avatar_right_walk2 = pygame.image.load("chicken_right_walk2.png")
        self.position = [0, 0]
        self.current_direction = self.avatar_front

#SetUp
myScreen = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))
pygame.display.set_caption("Chicken Rush!")
pygame.display.set_icon(pygame.image.load("chicken_front.png"))
myPlayer = Player()

while game_running:

    myScreen.fill(BLACK)

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    #RIGHT HERE DETERMINES WHICH IMAGE TO DISPLAY FOR WALKING LEFT->This is the part I need to     #       slow down       
    keys_pressed = pygame.key.get_pressed()       
    if keys_pressed[K_LEFT] and myPlayer.position[0] >= 0:
        if current == 0:
            myPlayer.current_direction = myPlayer.avatar_left
            current += 1
        elif current == 1:
            myPlayer.current_direction = myPlayer.avatar_left_walk
            current += 1
        elif current == 2:
            myPlayer.current_direction = myPlayer.avatar_left_walk2
            current = 0
        myPlayer.position[0] -= 3
    if keys_pressed[K_RIGHT] and myPlayer.position[0] < SCREENWIDTH - 32:
        myPlayer.position[0] += 3
    if keys_pressed[K_UP] and myPlayer.position[1] >= 0:
        myPlayer.position[1] -= 3
    if keys_pressed[K_DOWN] and myPlayer.position[1] < SCREENHEIGHT - 35:
        myPlayer.position[1] += 3

    myScreen.blit(myPlayer.current_direction, (myPlayer.position[0], myPlayer.position[1]))

    pygame.display.update()

    clock.tick(28)

1 个答案:

答案 0 :(得分:0)

似乎您可以使用更长的间隔来减慢图像过渡。请注意,您需要使用imageTransitionSpeed的值来实现自己的喜好,但是这样的事情应该有助于减缓过渡:

#RIGHT HERE DETERMINES WHICH IMAGE TO DISPLAY FOR WALKING LEFT->This is the part I need to     #       slow down       
    imageTransitionSpeed = 10
    keys_pressed = pygame.key.get_pressed()       
    if keys_pressed[K_LEFT] and myPlayer.position[0] >= 0:
        if current == 0:
            myPlayer.current_direction = myPlayer.avatar_left
            current += 1
        elif current == imageTransitionSpeed:
            myPlayer.current_direction = myPlayer.avatar_left_walk
            current += 1
        elif current == (imageTransitionSpeed * 2):
            myPlayer.current_direction = myPlayer.avatar_left_walk2
            current = 0
        else:
            current += 1
        myPlayer.position[0] -= 3
    if keys_pressed[K_RIGHT] and myPlayer.position[0] < SCREENWIDTH - 32:
        myPlayer.position[0] += 3
    if keys_pressed[K_UP] and myPlayer.position[1] >= 0:
        myPlayer.position[1] -= 3
    if keys_pressed[K_DOWN] and myPlayer.position[1] < SCREENHEIGHT - 35:
        myPlayer.position[1] += 3

    myScreen.blit(myPlayer.current_direction, (myPlayer.position[0], myPlayer.position[1]))

    pygame.display.update()

    clock.tick(28)