如何在 pygame 中连续循环 KEYDOWN?

我是python/ 的新手pygame,我无法弄清楚。每当我按住一个键时,它就不会循环播放KEYDOWN. 另外,如果我按住键盘上的键并同时移动鼠标,它似乎会连续移动。


有人能告诉我我做错了什么吗?


import pygame

import random

pygame.init()


#Colors

white = 255, 255, 255

black = 0, 0, 0

back_color = 48, 255, 124

light_color = 34, 155, 78


#Display W/H

display_width = 800

display_height = 600


#X/Y

x_axis = 400

y_axis = 580


Block_size = 20

x_int = 0

y_int = 0


ON = True




Window = pygame.display.set_mode((display_width, display_height))

pygame.display.set_caption('Game')




#On Loop

while ON == True:

    #Screen Event

    for Screen in pygame.event.get():

        #Quit Screen

        if Screen.type == pygame.QUIT:

            pygame.quit()

            exit()

        #Quit Full Screen

        if Screen.type == pygame.KEYDOWN:

            if Screen.key == pygame.K_q:

                pygame.quit()

                exit()


        #Full Screen !!!!!!!! EDIT THIS !!!!!!!!

        if Screen.type == pygame.KEYDOWN:

            if Screen.key == pygame.K_1:

                pygame.display.set_mode((display_width, display_height),pygame.FULLSCREEN)

            if Screen.key == pygame.K_2:

                pygame.display.set_mode((display_width, display_height))


        #Player Movement K DOWN

        if Screen.type == pygame.KEYDOWN:

            if Screen.key == pygame.K_d:

                x_int = 20

            if Screen.key == pygame.K_a:

                x_int = -20

       #Player Movement K UP

        if Screen.type == pygame.KEYUP:

            if Screen.key == pygame.K_d or Screen.key == pygame.K_a:

                x_int = 0


        x_axis += x_int




        Window.fill((back_color))

        Player = pygame.draw.rect(Window, light_color, [x_axis, y_axis, Block_size, Block_size])




        pygame.display.update()

quit()


Cats萌萌
浏览 183回答 2
2回答

至尊宝的传说

我已经改进了你的代码。您已将屏幕更新(绘制屏幕)部分放在事件循环中,而它应该在while循环中。我有模式的代码有点复杂,但按预期工作。为什么复杂?按住该键时,事件列表为空(您可以打印事件)。我也做了块不要走出屏幕。块的速度很高,所以我将其降低到10.import pygameimport randompygame.init()#Colorswhite = 255, 255, 255black = 0, 0, 0back_color = 48, 255, 124light_color = 34, 155, 78#Display W/Hdisplay_width = 800display_height = 600#X/Yx_axis = 400y_axis = 580global x_intBlock_size = 20x_int = 0y_int = 0ON = TrueWindow = pygame.display.set_mode((display_width, display_height))pygame.display.set_caption('Game')global topass_a,topass_dx_int,topass_a,topass_d = 0,0,0#On Loopwhile ON:&nbsp; &nbsp; #Screen Event&nbsp; &nbsp; events = pygame.event.get()&nbsp; &nbsp; def on_a_press():&nbsp; &nbsp; &nbsp; &nbsp; global topass_a,x_int&nbsp; &nbsp; &nbsp; &nbsp; x_int = -10&nbsp; &nbsp; &nbsp; &nbsp; topass_a = 1&nbsp; &nbsp; def on_d_press():&nbsp; &nbsp; &nbsp; &nbsp; global topass_d,x_int&nbsp; &nbsp; &nbsp; &nbsp; x_int = 10&nbsp; &nbsp; &nbsp; &nbsp; topass_d = 1&nbsp; &nbsp; if len(events) == 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if topass_a == 1:on_a_press()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if topass_d == 1:on_d_press()&nbsp; &nbsp; for Screen in events:&nbsp; &nbsp; &nbsp; &nbsp; if Screen.type == pygame.QUIT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pygame.quit()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit()&nbsp; &nbsp; &nbsp; &nbsp; if Screen.type == pygame.KEYDOWN:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Screen.key == pygame.K_q:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pygame.quit()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Screen.key == pygame.K_1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pygame.display.set_mode((display_width, display_height),pygame.FULLSCREEN)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Screen.key == pygame.K_2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pygame.display.set_mode((display_width, display_height))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Screen.key == pygame.K_d or topass_d == 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; on_d_press()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Screen.key == pygame.K_a or topass_a == 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; on_a_press()&nbsp; &nbsp; &nbsp; &nbsp; if Screen.type == pygame.KEYUP:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Screen.key == pygame.K_d or Screen.key == pygame.K_a:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x_int = 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; topass_a = 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; topass_d = 0&nbsp; &nbsp; x_axis += x_int&nbsp; &nbsp; x_int = 0&nbsp; &nbsp; if x_axis < 0:x_axis=0&nbsp; &nbsp; elif x_axis >= display_width-Block_size:x_axis = display_width-Block_size&nbsp; &nbsp; Window.fill((back_color))&nbsp; &nbsp; Player = pygame.draw.rect(Window, light_color, [x_axis, y_axis, Block_size, Block_size])&nbsp; &nbsp; pygame.display.update()您可以根据需要进一步改进代码。编辑:为什么复杂?容易的事情是第一位的。我已经意识到没有必要跟踪密钥。pygame.key.get_pressed()返回按下的键。这是一个更小、更好和改进的代码。我还实现了w和s(y_axis) 键。import pygameimport randompygame.init()#Colorswhite = 255, 255, 255black = 0, 0, 0back_color = 48, 255, 124light_color = 34, 155, 78#Display W/Hdisplay_width = 800display_height = 600#X/Yx_axis = 400y_axis = 580Block_size = 20x_int = 0y_int = 0ON = TrueWindow = pygame.display.set_mode((display_width, display_height))pygame.display.set_caption('Game')while ON:&nbsp; &nbsp; events = pygame.event.get()&nbsp; &nbsp; for Screen in events:&nbsp; &nbsp; &nbsp; &nbsp; if Screen.type == pygame.QUIT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pygame.quit()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit()&nbsp; &nbsp; &nbsp; &nbsp; if Screen.type == pygame.KEYDOWN:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Screen.key == pygame.K_q:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pygame.quit()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Screen.key == pygame.K_1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pygame.display.set_mode((display_width, display_height),pygame.FULLSCREEN)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Screen.key == pygame.K_2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pygame.display.set_mode((display_width, display_height))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; keys = pygame.key.get_pressed()&nbsp; &nbsp; if keys[pygame.K_a]:&nbsp; &nbsp; &nbsp; &nbsp; x_int = -10&nbsp; &nbsp; if keys[pygame.K_d]:&nbsp; &nbsp; &nbsp; &nbsp; x_int = 10&nbsp; &nbsp; # keys controlling y axis, you can remove these lines&nbsp; &nbsp; if keys[pygame.K_w]:&nbsp; &nbsp; &nbsp; &nbsp; y_int = -10&nbsp; &nbsp; if keys[pygame.K_s]:&nbsp; &nbsp; &nbsp; &nbsp; y_int = 10&nbsp; &nbsp; #x_axis......&nbsp; &nbsp; x_axis += x_int&nbsp; &nbsp; x_int = 0&nbsp; &nbsp; if x_axis < 0:x_axis=0&nbsp; &nbsp; elif x_axis >= display_width-Block_size:x_axis = display_width-Block_size&nbsp; &nbsp; #y axis&nbsp; &nbsp; y_axis += y_int&nbsp; &nbsp; y_int = 0&nbsp; &nbsp; if y_axis < 0:y_axis=0&nbsp; &nbsp; elif y_axis >= display_height-Block_size:y_axis = display_height-Block_size&nbsp; &nbsp; #updaing screen&nbsp; &nbsp; Window.fill((back_color))&nbsp; &nbsp; Player = pygame.draw.rect(Window, light_color, [x_axis, y_axis, Block_size, Block_size])&nbsp; &nbsp; pygame.display.update()

慕后森

您仅pygame.KEYDOWN在第一次按下该键时接收- 而不是在按住时接收。简单的解决方案是只在键按下时绘制(即 when x_int != 0)#On Loopwhile ON == True:&nbsp; &nbsp; #Screen Event&nbsp; &nbsp; for Screen in pygame.event.get():&nbsp; &nbsp; &nbsp; &nbsp; # <Removed not relevant code for brevity>&nbsp; &nbsp; &nbsp; &nbsp; #Player Movement K DOWN&nbsp; &nbsp; &nbsp; &nbsp; if Screen.type == pygame.KEYDOWN:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Screen.key == pygame.K_d:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x_int = 20&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Screen.key == pygame.K_a:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x_int = -20&nbsp; &nbsp; &nbsp; &nbsp;#Player Movement K UP&nbsp; &nbsp; &nbsp; &nbsp; if Screen.type == pygame.KEYUP:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Screen.key == pygame.K_d or Screen.key == pygame.K_a:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x_int = 0&nbsp; &nbsp; # Render only happens if x_int is not zero&nbsp; &nbsp; # (Need to add code to force render first time)&nbsp; &nbsp; if x_int:&nbsp; &nbsp; &nbsp; &nbsp; x_axis += x_int&nbsp; &nbsp; &nbsp; &nbsp; Window.fill((back_color))&nbsp; &nbsp; &nbsp; &nbsp; Player = pygame.draw.rect(Window, light_color, [x_axis, y_axis, Block_size, Block_size])&nbsp; &nbsp; &nbsp; &nbsp; pygame.display.update()随着程序的增长和变得越来越复杂,您需要更好的逻辑来确定何时渲染,但这会让您开始。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python