亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 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 關注
  • 220 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號