Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
msvcrt getch暂停脚本,必须继续 PYTHON 3.3,msvcrt_Python_Msvcrt - Fatal编程技术网

msvcrt getch暂停脚本,必须继续 PYTHON 3.3,msvcrt

msvcrt getch暂停脚本,必须继续 PYTHON 3.3,msvcrt,python,msvcrt,Python,Msvcrt,这是我的代码,只是一个例子。 当代码到达key=msvcrt.getch()*或*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 如果您想制作sn

这是我的代码,只是一个例子。 当代码到达
key=msvcrt.getch()*或*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
如果您想制作snake之类的东西,您不希望游戏每次都暂停,也不希望游戏暂停,等待输入。

用于检查是否按下了该键:

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)
导入系统,msvcrt 导入时间 打印(“请按键查看其值”) 而1: 打印(“关键是”) 如果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)
导入系统,msvcrt 导入时间 打印(“请按键查看其值”) 而1: 打印(“关键是”)
如果msvcrt.kbhit():#另一个使Python程序在某个级别停止并等待用户按Enter“Yes”和/或空格“No”的示例可以使用pygame生成。 例如,我用空格表示“否”,但你可以用touch Escape表示“否”。 你可能不需要一些进口的图书馆。需要他们,而使tic-toc-toe游戏

代码如下:

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 = True

另一个使Python程序停止在某个级别,并等待用户按Enter“Yes”和/或空格“No”的示例可以使用pygame生成。 例如,我用空格表示“否”,但你可以用touch Escape表示“否”。 你可能不需要一些进口的图书馆。需要他们,而使tic-toc-toe游戏

代码如下:

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 = True

主席先生,你才华横溢,我非常感谢你:Dsir,你才华横溢,我非常感谢你:D