Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

python - 多次按下键以在pygame中移动图像(Key to be pressed multiple times to move image in pygame)

While I was learning how to move images in pygame, when I held down a key(any one: up, down, left, right) to move my image(a racecar), the image only moved once, even though holding down the key should've made the image move move in that particular directions continuously, but it only moved one unit.

(当我学习如何在pygame中移动图像时,当我按住一个键(任何一个:上,下,左,右)来移动图像(赛车)时,即使按住该键,图像也只能移动一次。应该使图像移动沿特定方向连续移动,但是它只移动了一个单位。)

The fix that I found was that the declaration of the variables used to change the original position (x_vary and y_vary, original position: x and y) were not be added in the main while loop of my game.

(我发现的解决方法是,用于更改原始位置(x_vary和y_vary,原始位置:x和y)的变量的声明未添加到游戏的主while循环中。)

My question is why?

(我的问题是为什么?)

Why does it make a difference where I declare x_vary and y_vary

(为什么在声明x_vary和y_vary的地方有所不同)

Here is the unfixed code:

(这是未固定的代码:)

import pygame

pygame.init()

clock = pygame.time.Clock()

game_window = pygame.display.set_mode((800, 600))
game_icon = pygame.image.load('icon2.jpg')
game_caption = pygame.display.set_caption('Racecar Game')

x = 800 * 0.45
y = 600 * 0.08

racecar = pygame.image.load('racecar.png')

def car(x, y):
    game_window.blit(racecar,(x,y)) 

game_run = False

while game_run == False:

    x_vary = 0
    y_vary = 0

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_run = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x_vary = -10
            elif event.key == pygame.K_RIGHT:
                x_vary = +10
            elif event.key == pygame.K_UP:
                y_vary = -10
            elif event.key == pygame.K_DOWN:
                y_vary = +10
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                x_vary = 0
                y_vary = 0

    x += x_vary
    y += y_vary

    game_window.fill((128, 0, 0))
    car(x, y)

    pygame.display.update()
    clock.tick(60)

pygame.quit()
quit()
  ask by r_chan____ translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The keyboard events ( pygame.KEYDOWN , pygame.KEYUP ) occur only one time when a key is pressed, respectively released.

(键盘事件( pygame.KEYDOWNpygame.KEYUP )仅在分别按下和释放一个键时发生一次。)

If x_vary = 0 and y_vary = 0 are declared in the main application loop, then they are set when a button is pressed, but they are reinitialized (0) immediately in the next frame.

(如果在主应用程序循环中声明了x_vary = 0y_vary = 0 ,则在按下按钮时将它们置位,但它们将在下一帧立即重新初始化(0)。)

You've to presse a button again to set them.

(您必须再次按下按钮进行设置。)

If x_vary = 0 and y_vary = 0 are declared before the main application loop, then they keep the value, which is set when a button is pressed.

(如果在主应用程序循环之前声明x_vary = 0y_vary = 0 ,则它们将保留该值,该值在按下按钮时设置。)

If a button is released, then both are set 0.

(如果释放按钮,则两者都设置为0。)

Use pygame.key.get_pressed() , to evaluate if the state of a key is pressed.

(使用pygame.key.get_pressed()评估是否按下了键的状态。)

pygame.key.get_pressed() get the state of all keyboard buttons and a the state of a key is True as long the keyboard is pressed down.

(pygame.key.get_pressed()获取所有键盘按钮的状态,并且只要按下键盘,键的状态为True 。)

eg:

(例如:)

game_run = False
while game_run == False:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_run = True

    keys = pygame.key.get_pressed()
    x_vary, y_vary = 0, 0
    if keys[pygame.K_LEFT]:
        x_vary = -10
    if keys[pygame.K_RIGHT]:
        x_vary = +10
    if keys[pygame.K_UP]:
        y_vary = -10
    if keys[pygame.K_DOWN]:
        y_vary = +10

    x += x_vary
    y += y_vary

    game_window.fill((128, 0, 0))
    car(x, y)

    pygame.display.update()
    clock.tick(60)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...