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

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

如果我在游戲中連續按兩次鍵,我想讓角色跑得更快

如果我在游戲中連續按兩次鍵,我想讓角色跑得更快

largeQ 2023-10-31 15:48:40
這是我編寫的代碼的一部分。enter code here    import pygame    from pygame.locals import QUIT, KEYDOWN, KEYUP,\        K_LEFT, K_RIGHT, K_DOWN, K_SPACE, K_UP    pygame.init()    pygame.key.set_repeat(30, 30)    while True:        to_x1 = 0        for event in pygame.event.get():                if event.type == pygame.QUIT:                    pygame.quit()                    sys.quit()                keys = pygame.key.get_pressed()                if keys[pygame.K_LEFT]:                    to_x1 = -5                if keys[pygame.K_RIGHT]:                    to_x1 = 5        CHARIC_1.xpos1 = CHARIC_1.xpos1 + to_x1CHARIC_1.xpos1 是角色的 x 坐標。當我快速按下左右鍵兩次時,我想讓我的角色跑得更快。我不知道我的一生。如果你知道怎么做的話,我希望你能告訴我。
查看完整描述

1 回答

?
牛魔王的故事

TA貢獻1830條經驗 獲得超3個贊

您必須使用鍵盤事件KEYDOWN(請參閱pygame.event)而不是pygame.key.get_pressed()。用于獲取自調用pygame.time.get_ticks()以來的毫秒數。pygame.init()存儲按下按鍵的時間。如果在一定時間內按下按鈕兩次,則增加速度:

key_time = 0

fast_key_time = 500 # 0.5 seconds


while True:

? ? current_time = pygame.time.get_ticks()


? ? # [...]


? ? for event in pygame.event.get():

? ? ? ? # [...]


? ? ? ? if event.type == pygame.KEYDOWN:

? ? ? ? ? ? if event.key == pygame.K_LEFT:

? ? ? ? ? ? ? ? if to_x1 == -5 and current_time < key_time + fast_key_time:

? ? ? ? ? ? ? ? ? ? to_x1 = -10

? ? ? ? ? ? ? ? else:

? ? ? ? ? ? ? ? ? ? to_x1 = -5

? ? ? ? ? ? ? ? key_time = current_time

? ? ? ? ? ? if event.key == pygame.K_RIGHT:

? ? ? ? ? ? ? ? if to_x1 == 5 and current_time < key_time + fast_key_time:

? ? ? ? ? ? ? ? ? ? to_x1 = 10

? ? ? ? ? ? ? ? else:

? ? ? ? ? ? ? ? ? ? to_x1 = 5

? ? ? ? ? ? ? ? key_time = current_time


? ? CHARIC_1.xpos1 = CHARIC_1.xpos1 + to_x1


? ? # [...]

最小的例子:


import pygame


pygame.init()

window = pygame.display.set_mode((500, 500))

clock = pygame.time.Clock()


rect = pygame.Rect(0, 0, 20, 20)

rect.center = window.get_rect().center


to_x1 = 0

key_time = 0

fast_key_time = 500 # 0.5 seconds


run = True

while run:

? ? current_time = pygame.time.get_ticks()

? ? clock.tick(60)

? ? for event in pygame.event.get():

? ? ? ? if event.type == pygame.QUIT:

? ? ? ? ? ? run = False

? ? ? ? if event.type == pygame.KEYDOWN:

? ? ? ? ? ? if event.key == pygame.K_LEFT:

? ? ? ? ? ? ? ? if to_x1 == -5 and current_time < key_time + fast_key_time:

? ? ? ? ? ? ? ? ? ? to_x1 = -10

? ? ? ? ? ? ? ? else:

? ? ? ? ? ? ? ? ? ? to_x1 = -5

? ? ? ? ? ? ? ? key_time = current_time

? ? ? ? ? ? if event.key == pygame.K_RIGHT:

? ? ? ? ? ? ? ? if to_x1 == 5 and current_time < key_time + fast_key_time:

? ? ? ? ? ? ? ? ? ? to_x1 = 10

? ? ? ? ? ? ? ? else:

? ? ? ? ? ? ? ? ? ? to_x1 = 5

? ? ? ? ? ? ? ? key_time = current_time


? ? rect.x += to_x1

? ? ? ??

? ? rect.centerx = rect.centerx % window.get_width()

? ? rect.centery = rect.centery % window.get_height()


? ? window.fill(0)

? ? pygame.draw.rect(window, (255, 0, 0), rect)

? ? pygame.display.flip()


查看完整回答
反對 回復 2023-10-31
  • 1 回答
  • 0 關注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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