【问题标题】:Make Python program wait an amount of time让 Python 程序等待一段时间
【发布时间】:2020-07-16 09:01:27
【问题描述】:

我希望我的 python 程序在继续循环之前等待。但其他功能,循环usw。应该管用。如果我使用睡眠功能,整个程序就会进入睡眠状态。示例:

import pygame

def function():
   print("You moved forward")

turtle.listen()
turtle.onkey(function, "f")

print("use f to move forward")
(Wait 5 seconds, so the player can try pressing f and sees what happens)

这是我原始代码的简单版本。我希望玩家尝试 WASD,因为角色会随着 WASD 移动。

如果我使用 time.sleep(),如果玩家按下 f,它将不起作用。

感谢您的任何回答:D

-> 顺便说一句,我的英语不好......

【问题讨论】:

  • 我想做一个游戏教程。播放器使用 WASD 移动,因此应该有一个文本“按 WASD 移动”。然后玩家有几秒钟的时间按下 WASD 并四处移动。
  • input() 怎么样。这将等到玩家按下 Enter 键
  • 对不起,应该是评论,但我不能评论:(试试这个:stackoverflow.com/a/48111882/12658348

标签: python python-2.7 time pygame delay


【解决方案1】:

如果您想等待 5 秒而不是睡眠,那么您可以尝试使用循环并测量循环的运行时间。可能是这样的:

首先通过运行pip install pytictoc确保安装了pytictoc

然后像这样运行一个循环:

from pytictoc import TicToc
t = TicToc() #create instance of class
t.tic() #Start timer
while (t.tocvalue()<5):
    pass
t.toc()

【讨论】:

  • 出现错误:SyntaxError: Non-ASCII character '\xc3' in file test.py on line 141, but no encoding declaration;有关详细信息,请参阅python.org/dev/peps/pep-0263 MyNames-MacBook-Pro:rpg username$
  • 我需要查看更多您的代码,以查看此错误是否来自此代码。它可能与其他事物有关。
  • 我在其他文档中试了一下,没有报错。但它就像 time.sleep() 一样工作,屏幕被暂停。功能不工作。这是我的代码: import turtle from pytictoc import TicToc #添加一个函数 def testfunc(): print("Keypressed") turtle.listen() #First message print("test") #timer t = TicToc() #create instance of class t.tic() #Start timer while (t.tocvalue()
猜你喜欢
  • 2022-07-23
  • 2019-01-15
  • 2010-11-22
  • 1970-01-01
  • 2018-03-20
  • 1970-01-01
  • 2020-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多