我想知道如何为按下的特定键按钮添加计时器/冷却时间。例如,用户正在发送攻击键(在我的例子中是 p),我想添加一个冷却时间到人们无法发送此按钮的地方(空中和地面)。 这是问题所在: https://drive.google.com/file/d/1XH-u-1qm2I0ftlCLPSsPMjTJjJGQdsUp/view?usp=sharing
正如您在视频中看到的那样,此人可以无限期地向攻击按钮发送垃圾邮件,从而消除游戏的目的。
我试过设置一个 sleep 定时器,比如 定义卧铺(): 如果输入()==“p”: 小号(20) 但我不认为你是这样做的。 除此之外,我不知道还有什么其他方法可以尝试。
链接到我的整个代码: https://hastebin.com/zujanifisu.py
更具体的代码:
def movement(self, window):
pygame.time.delay(20)
if self.runcount >= 3:
self.runcount = 0
if self.running == True:
window.blit(run[self.runcount],(int(self.player_x),int(self.player_y)))
self.runcount +=1
self.hitbox_running = pygame.Rect(self.player_x+30,self.player_y+20,48,70)
pygame.draw.rect(window,(255,0,0),self.hitbox_running, 2)
if (keys[pygame.K_DOWN]) or ((keys[pygame.K_DOWN]) and keys[pygame.K_p]):
if self.player_y == 378:
self.running = False
if self.slidecount >= 4:
self.slidecount = 0
if self.sliding:
window.blit(slide[self.slidecount],(int(self.player_x),int(self.player_y)))
self.slidecount +=1
pygame.draw.rect(window,(255,0,0),self.hitbox_sliding, 2)
if event.type == pygame.KEYDOWN:
if (event.key == pygame.K_DOWN )and self.player_y < self.width:
self.running = False
self.jumping = False
self.fallspeed += 0.2
if self.fallingcount >= 1:
self.fallingcount = 0
if self.fall:
window.blit(falling[self.fallingcount], (int(self.player_x),int(self.player_y)))
self.hitbox_falling = pygame.Rect(self.player_x+30,self.player_y,35,80)
pygame.draw.rect(window,(255,0,0),self.hitbox_falling, 2)
self.fallingcount +=1
if keys[pygame.K_UP] and keys[pygame.K_p] :
self.fallspeed = 0.3
self.running = False
self.jumping = False
self.sliding = False
if self.attackcount >= 16:
self.attackcount = 0
if self.attacking:
window.blit(attack[self.attackcount],(int(self.player_x),int(self.player_y)))
self.attackcount += 1
self.hitbox_attacking = pygame.Rect(self.player_x+30,self.player_y+20,38,70)
self.hitbox_sword = pygame.Rect(self.player_x+72, self.player_y+20, 20, 50)
pygame.draw.rect(window,(255,0,0),self.hitbox_attacking, 2)
pygame.draw.rect(window,(255,0,0),self.hitbox_sword, 2)
if self.jumpingcount >= 20:
self.jumpingcount = 0
if self.jumping and self.player_y < self.width:
window.blit(jump[self.jumpingcount],(int(self.player_x),int(self.player_y)))
self.hitbox_jumping = pygame.Rect((self.player_x+20),(self.player_y+20),52,55)
pygame.draw.rect(window,(255,0,0),self.hitbox_jumping, 2)
self.jumpingcount +=1
self.fallspeed = 0.3
if keys[pygame.K_UP]:
self.fallspeed = 0.3
self.running = False
if self.jumpingcount >= 20:
self.jumpingcount = 0
if self.jumping and self.player_y < self.width:
window.blit(jump[self.jumpingcount],(int(self.player_x),int(self.player_y)))
self.hitbox_jumping = pygame.Rect((self.player_x+20),(self.player_y+20),52,55)
pygame.draw.rect(window,(255,0,0),self.hitbox_jumping, 2)
self.jumpingcount +=1
self.fallspeed = 0.3
if keys[pygame.K_p] and not keys[pygame.K_UP]:
self.running = False
self.jumping = False
self.sliding = False
if self.attackcount >= 16:
self.attackcount = 0
if self.attacking:
self.hitbox_attacking = pygame.Rect(self.player_x+30,self.player_y+20,38,70)
self.hitbox_sword = pygame.Rect(self.player_x+72, self.player_y+20, 20, 50)
window.blit(attack[self.attackcount],(int(self.player_x),int(self.player_y)))
self.attackcount += 1
pygame.draw.rect(window,(255,0,0),self.hitbox_attacking, 2)
pygame.draw.rect(window,(255,0,0),self.hitbox_sword, 2)
if keys[pygame.K_DOWN] and keys[pygame.K_UP]:
self.running = False
if event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN:
self.running = True
self.jumping = True
self.fallspeed = 0.3
if event.key == pygame.K_UP:
self.running=True
if event.key == pygame.K_p:
self.running = True
self.jumping = True
self.sliding = True
大部分问题出在按键上[pygame.k_p]
我想给整个按钮 p 添加一个计时器,所以当按下 2-3 次时,它不能再按下 2-3 秒。
请您参考如下方法:
当按下一个键时,您必须存储允许再次“按下”该键的时间。
为此使用字典:
key_timeout = {}
在全局范围内编写一个函数,检查是否按下了某个键以及是否允许按下。该函数的参数是键的状态(pygame.key.get_pressed())、键和以毫秒为单位的超时时间:
def getPressed(keys, key, timeout):
如果未按下该键,则该函数返回 False:
if keys[key] == False:
return False
使用pygame.time.get_ticks()以毫秒为单位获取时间:
current_time = pygame.time.get_ticks()
如果字典中存储了超时时间,并且该时间小于允许再次按下该键的时间,则该函数返回False:
if key in key_timeout and key_timeout[key] > current_time:
return Fase
将超时时间加到当前时间上,即允许再次按下该键的时间,存入字典。
key_timeout[key] = current_time + timeout
函数的完整代码:
key_timeout = {}
def getPressed(keys, key, timeout):
global key_timeout
if keys[key] == False:
return False
current_time = pygame.time.get_ticks()
if key in key_timeout and key_timeout[key] > current_time:
return False
key_timeout[key] = current_time + timeout
return True
代替 if keys[pygame.k_p]: 你可以调用:
if getPressed(keys, pygame.k_p, 2000): # 2000 milliseconds == 2 seconds
# [...]
如果您在一个函数中多次请求某个键的状态(例如 keys[pygame.k_p]),那么您必须存储 getPressed()< 的结果 到变量并改用该变量。例如:
key_p_pressed = getPressed(keys, pygame.k_p, 2000)
if (keys[pygame.K_DOWN]) or ((keys[pygame.K_DOWN]) and key_p_pressed ):
# [...]
if keys[pygame.K_UP] and key_p_pressed:
# [...]
if key_p_pressed and not keys[pygame.K_UP]:
# [...]
