为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 pygame 中连续循环 KEYDOWN?

如何在 pygame 中连续循环 KEYDOWN?

Cats萌萌 2021-06-03 13:24:05
我是python/ 的新手pygame,我无法弄清楚。每当我按住一个键时,它就不会循环播放KEYDOWN. 另外,如果我按住键盘上的键并同时移动鼠标,它似乎会连续移动。有人能告诉我我做错了什么吗?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')#On Loopwhile 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()
查看完整描述

2 回答

?
至尊宝的传说

TA贡献1789条经验 获得超10个赞

我已经改进了你的代码。您已将屏幕更新(绘制屏幕)部分放在事件循环中,而它应该在while循环中。我有模式的代码有点复杂,但按预期工作。为什么复杂?按住该键时,事件列表为空(您可以打印事件)。我也做了块不要走出屏幕。块的速度很高,所以我将其降低到10.


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

global x_int

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


global topass_a,topass_d

x_int,topass_a,topass_d = 0,0,0

#On Loop

while ON:

    #Screen Event

    events = pygame.event.get()

    def on_a_press():

        global topass_a,x_int

        x_int = -10

        topass_a = 1

    def on_d_press():

        global topass_d,x_int

        x_int = 10

        topass_d = 1

    if len(events) == 0:

         if topass_a == 1:on_a_press()

         if topass_d == 1:on_d_press()

    for Screen in events:

        if Screen.type == pygame.QUIT:

            pygame.quit()

            exit()

        if Screen.type == pygame.KEYDOWN:

            if Screen.key == pygame.K_q:

                pygame.quit()

                exit()

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

            if Screen.key == pygame.K_d or topass_d == 1:

                on_d_press()

            if Screen.key == pygame.K_a or topass_a == 1:

                on_a_press()

        if Screen.type == pygame.KEYUP:

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

                x_int = 0

                topass_a = 0

                topass_d = 0


    x_axis += x_int

    x_int = 0

    if x_axis < 0:x_axis=0

    elif x_axis >= display_width-Block_size:x_axis = display_width-Block_size

    Window.fill((back_color))

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

    pygame.display.update()

您可以根据需要进一步改进代码。


编辑:

为什么复杂?容易的事情是第一位的。我已经意识到没有必要跟踪密钥。pygame.key.get_pressed()返回按下的键。这是一个更小、更好和改进的代码。我还实现了w和s(y_axis) 键。


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

while ON:

    events = pygame.event.get()

    for Screen in events:

        if Screen.type == pygame.QUIT:

            pygame.quit()

            exit()

        if Screen.type == pygame.KEYDOWN:

            if Screen.key == pygame.K_q:

                pygame.quit()

                exit()

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

                    

    keys = pygame.key.get_pressed()

    if keys[pygame.K_a]:

        x_int = -10

    if keys[pygame.K_d]:

        x_int = 10

    # keys controlling y axis, you can remove these lines

    if keys[pygame.K_w]:

        y_int = -10

    if keys[pygame.K_s]:

        y_int = 10

    #x_axis......

    x_axis += x_int

    x_int = 0

    if x_axis < 0:x_axis=0

    elif x_axis >= display_width-Block_size:x_axis = display_width-Block_size

    #y axis

    y_axis += y_int

    y_int = 0

    if y_axis < 0:y_axis=0

    elif y_axis >= display_height-Block_size:y_axis = display_height-Block_size

    #updaing screen

    Window.fill((back_color))

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

    pygame.display.update()


查看完整回答
反对 回复 2021-06-08
?
慕后森

TA贡献1802条经验 获得超5个赞

您仅pygame.KEYDOWN在第一次按下该键时接收- 而不是在按住时接收。简单的解决方案是只在键按下时绘制(即 when x_int != 0)


#On Loop

while ON == True:

    #Screen Event

    for Screen in pygame.event.get():

        # <Removed not relevant code for brevity>

        #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


    # Render only happens if x_int is not zero

    # (Need to add code to force render first time)

    if x_int:

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

随着程序的增长和变得越来越复杂,您需要更好的逻辑来确定何时渲染,但这会让您开始。


查看完整回答
反对 回复 2021-06-08
  • 2 回答
  • 0 关注
  • 182 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信