简体   繁体   English

角色移动python(pygame)

[英]character movement python (pygame)

Im trying to call moveleft method but there is no movement. 我试图调用moveleft方法,但没有动作。 i pass the distance of the character and this should be updated but its not. 我通过字符的距离,这应该更新,但不是。 Any ideas? 有任何想法吗?

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()

You draw the character at (x, y) . 您在(x, y)处绘制字符。 The only time you change x is here: 唯一更改x时间是在这里:

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

What is distanceX ? 什么是distanceX It changes every iteration of the loop: 它更改循环的每次迭代:

        distanceX = time_passeSeconds*speedX

Yet you only assign speedX once at the start of the function: 然而,在功能开始时,您只分配一次speedX

    speedX = 0

So, you're always moving by 0 . 因此,您总是移动0 Change speedX to 50 and see what happens. speedX更改为50然后看看会发生什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM