简体   繁体   English

按住方向键时,如何让方块在屏幕上不断移动?

[英]How do I get the square to continually move across the screen as a direction key is held down?

When I run this code on a Windows pc the square continues to move across the screen in the appropriate direction as I hold down the respective key, but when I run it on raspberry pi or mac it jumps 5 pixels then stops.当我在 Windows pc 上运行此代码时,当我按住相应的键时,方块会继续以适当的方向在屏幕上移动,但是当我在 raspberry pi 或 mac 上运行它时,它会跳 5 个像素然后停止。 How can I get it to move across the screen When I hold down the respective key?当我按住相应的键时,如何让它在屏幕上移动?

import pygame, sys, time
from pygame.locals import *
pygame.init()
x = 400
screen = pygame.display.set_mode((x, x))
pygame.display.set_caption('This is printed on the top of the tab or window!')

BLACK = (0,0,0)
WHITE = (255,255,255)

moveSpeed = 5

moveLeft = False
moveRight = False
moveDown = False
moveUp = False

player = pygame.Rect(150, 150, 50, 50)
pygame.draw.rect(screen, BLACK, player)

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

        if event.type == KEYDOWN:
            if event.key == K_LEFT:
                moveRight = False
                moveLeft = True
            if event.key == K_RIGHT:
                moveLeft = False
                moveRight = True
            if event.key == K_DOWN:
                moveUp = False
                moveDown = True
            if event.key == K_UP:
                moveDown = False
                moveUp = True
        if event.type == KEYUP:
            if event.key == K_ESCAPE:
                pygame.quit()
            if event.key == K_LEFT:
                moveLeft = False
            if event.key == K_RIGHT:
                moveRight = False
            if event.key == K_DOWN:
                moveDown = False
            if event.key == K_UP:
                moveUp = False
        if moveLeft and player.left > 0:
            player.left -= moveSpeed
        if moveRight and player.right < x:
            player.right += moveSpeed
        if moveDown and player.bottom < x:
            player.bottom += moveSpeed
        if moveUp and player.top > 0:
            player.top -= moveSpeed
        screen.fill(WHITE)
        pygame.draw.rect(screen, BLACK, player)
        pygame.display.update()
        time.sleep(0.02)

I solved this problem by indenting the lines...我通过缩进线解决了这个问题......

    if moveLeft and player.left > 0:
        player.left -= moveSpeed
    if moveRight and player.right < x:
        player.right += moveSpeed
    if moveDown and player.bottom < x:
        player.bottom += moveSpeed
    if moveUp and player.top > 0:
        player.top -= moveSpeed
    screen.fill(WHITE)
    pygame.draw.rect(screen, BLACK, player)
    pygame.display.update()
    time.sleep(0.02)

... Back four spaces. ... 后退四个空格。 Also, i learned, for amateur questions like these, certain sub-reddits like /r/learpython is a great place to get answers.另外,我了解到,对于像这样的业余问题,像 /r/learpython 这样的某些 sub-reddits 是获得答案的好地方。

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

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