Python只运行一次while循环

2022-01-15 00:00:00 python python-3.x pygame windows-10

问题描述

import pygame

r_colour = (200, 100,100)
bg_colour = (0,175,200)
(width, height) = (600, 600)

screen = pygame.display.set_mode((width, height))
screen.fill(bg_colour)
pygame.draw.rect(screen, r_colour, (30, 30, 100, 100), 0)

pygame.display.flip()

running = True
while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_s:
                screen.fill(bg_colour)
                pygame.draw.rect(screen, r_colour, (20, 30, 100, 100), 0)
                pygame.display.update()


if running == True:
      for event in pygame.event.get():
          if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_s:
                    screen.fill(bg_colour)
                    pygame.draw.rect(screen, r_colour, (20, 30, 100, 100), 0)
                    pygame.display.update()



while running:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      break
      running = False



pygame.quit()

我试图让红色方块在按下s"键时移动,不知道为什么它只移动一次然后停止.对编程非常陌生,所以很抱歉,如果它很长或难以阅读.

I am trying to get the red square to move when pressing the 's' key, not sure as to why it only moves once and then stops. Very new to programming, so I am sorry, if it's long or hard to read.


解决方案

一个典型的应用程序有 1 个单一的应用程序循环.应用程序循环:

A typical application has 1 single application loop. The application loop does:

  • 处理事件并根据事件更改状态
  • 清除显示
  • 绘制场景
  • 更新显示

KEYDOWY 事件在按下某个键时发生一次,但在按住某个键时不会连续发生.
对于连续运动,您可以通过 <代码>pygame.key.get_pressed():

The KEYDOWY event occurs once when a key is pressed, but it does not occur continuously when a key is hold down.
For a continuously movement you can get the state of the keys by pygame.key.get_pressed():

keys = pygame.key.get_pressed()

例如如果 s 的状态被按下,可以通过 keys[pygame.K_s] 来评估.

e.g. If the state of s is pressed can be evaluated by keys[pygame.K_s].

为矩形的位置添加坐标(x, y).当按键被按下时,在主应用程序循环中不断地操作位置.

Add coordinates (x, y) for the position of the rectangle. Continuously manipulate the position in the main application loop, when a key is pressed.

例如
如果按下 d,则增加 x,如果按下 a,则减少 x.
如果按下 s,则增加 y,如果按下 w,则减少 y:

e.g.
Increment x if d is pressed and decrement x if a is pressed.
Increment y if s is pressed and decrement y if w is pressed:

import pygame

r_colour = (200, 100,100)
bg_colour = (0,175,200)
(width, height) = (600, 600)
x, y = 20, 30

screen = pygame.display.set_mode((width, height))

running = True
while running:

    # handle the events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # change coordinates
    keys = pygame.key.get_pressed()
    if keys[pygame.K_d]:
        x += 1
    if keys[pygame.K_a]:
        x -= 1
    if keys[pygame.K_s]:
        y += 1
    if keys[pygame.K_w]:
        y -= 1

    # clear the display
    screen.fill(bg_colour)
    # draw the scene
    pygame.draw.rect(screen, r_colour, (x, y, 100, 100), 0)
    # update the display
    pygame.display.update()

pygame.quit()

相关文章