角色运动python(pygame)

时间:2013-10-16 22:33:36

标签: python pygame

我试图调用moveleft方法,但没有动作。我传递角色的距离,这应该更新但不是。有任何想法吗?

import pygame, sys
from pygame.locals import *

pygame.init()
pygame.font.init()


width,height=(842,595)
window = pygame.display.set_mode((width,height),0,32)
pygame.display.set_caption("game!")
speedX=3
movingX =0
clock= pygame.time.Clock()



man = pygame.image.load("man.png")
target= pygame.image.load("target.png")




x = 100
y = height-300




def name(name=""):
    myfont = pygame.font.SysFont(None, 15)
    label = myfont.render(name, 1, (255,255,0))
    result=window.blit(label, (100, 100))
    pygame.display.update()
    return name

def moveleft(distanceX):
    movingX =0
    speedX =0
    x=0
    while True:

        pygame.display.update()
        ticks=clock.tick(25)
        time_passedSeconds=ticks/1000.0

        distanceX = time_passeSeconds*speedX
        movingX+=distanceX
        for event in pygame.event.get():
            if event.type==QUIT:
                pygame.quit()
                sys.exit()
            elif event.type==KEYDOWN:
                if event.key ==K_LEFT:
                    x+=distanceX



        window.blit(man, (x,y))
    return movingX




name("werodo!")
moveleft(5)
pygame.display.update()

1 个答案:

答案 0 :(得分:1)

您在(x, y)处绘制角色。您唯一一次更改x

            elif event.type==KEYDOWN:
                if event.key ==K_LEFT:
                    x+=distanceX

什么是distanceX?它改变了循环的每次迭代:

        distanceX = time_passeSeconds*speedX

然而,您只需在函数开头指定speedX一次:

    speedX = 0

所以,你总是在0前进。将speedX更改为50,看看会发生什么。