Python3.3,msvcrt
import sys, msvcrt
print("Please press a key to see its value")
while 1:
key = msvcrt.getch()
print("the key is")
print(key)
if ord(key) == 27: # key nr 27 is escape
sys.exit()这是我的代码,举个例子。当它到达key = msvcrt.getch()*, or *key = ord(getch())时,代码就会暂停,就在这里,我使用了第一个。我想让这段代码不断地打印,键不是仅仅打印,而是当我给出一个新的输入时(当我按下一个键)。
所以打印的输出应该是这样的:
the key is
the key is
the key is
the key is
the key is
the key is
77
the key is
the key is
the key is这是必要的,如果你想做一些像蛇,你不想让你的游戏暂停,每次你想要的时候,你不想让它暂停,等待输入。
发布于 2013-11-21 16:44:32
使用msvcrt.kbhit检查键是否按下:
import sys, msvcrt
import time
print("Please press a key to see its value")
while 1:
print("the key is")
if msvcrt.kbhit(): # <--------
key = msvcrt.getch()
print(key)
if ord(key) == 27:
sys.exit()
time.sleep(0.1)发布于 2020-04-28 04:53:44
另一个使Python程序停止在一定级别,并等待用户按Enter "Yes“和/或Space表示"No”的示例可以使用pygame生成。例如,我在"No“中使用了Space,但您可以使用touche Escape来表示"No”。你可能不需要进口的librairies。在做抽搐脚趾游戏时需要他们。
守则如下:
import numpy as np
import pygame as pg
from math import floor
import sys
import time
pg.init()
black = (0, 0, 0)
red = (255, 0, 0)
blue = (0, 0, 255)
yellow = (255, 255, 0)
white = (255, 255, 255)
gris = (192, 192, 192)
cell = 100
thickness =2
window = pg.display.set_mode((300, 300))
pg.display.set_caption("by @djilytech")
for col in range(3):
for row in range(3):
pg.draw.rect(window, gris, (row * cell, col * cell, cell - 2, cell - 2), thickness)
pg.time.delay(120)
pg.display.update()
run = False
while not run:
for ev in pg.event.get():
if ev.type == pg.QUIT:
pg.quit()
sys.exit()
if ev.type == pg.KEYDOWN:
if ev.key == pg.K_RETURN:
print(" This mean the user wants to play again or said YES")
# So I can have some code here for what I want
if ev.key == pg.K_SPACE:
print("User does not want to continue")
# Will exit the program
run = Truehttps://stackoverflow.com/questions/20126833
复制相似问题