获取关键输入的最佳方法是什么?

时间:2021-02-13 03:33:45

标签: python

所以我感到无聊并决定编写一个简短的脚本,打印出一个相当大的网格供玩家绘制和填充,但在获取用户的关键输入时遇到了麻烦。我决定使用 while 循环,因为我不确定如何以其他方式进行,但是使用这种方法时,它的 CPU 使用率非常高,这让我想知道为什么这么简单的任务如此占用资源要求很高。

无论如何,这是整个脚本,因为这实际上是我第一次用 python 编写的类,我不确定我是否在做任何正确或有效的事情。

from keyboard import is_pressed
from subprocess import Popen


def clear():
    """Clear the terminal."""
    Popen("cls", shell=True).wait()


class Board():
    """
    Initialize sizeable grid as the play area for a player to freely move around in.
    """
    def __init__(self, rows, columns):

        self.area = [[" " for _ in range(rows)] for _ in range(columns)]

        self.xMax = columns
        self.yMax = rows
        
        self.x = int(columns / 2)
        self.y = int(rows / 2)

        self.area[self.y][self.x] = " "


    #  Replace current location with empty
    def place(self):
        self.area[self.y][self.x] = "O"
    
    #  Replace current location with player
    def change(self):
        self.area[self.y][self.x] = "_"


    #  Translate a single unit rightwards
    def right(self):
        new = self.x + 1

        if ((new > -1) and (new < self.xMax)):
            self.place()
            self.x = new
            self.change()

    #  Translate a single unit leftwards
    def left(self):
        new = self.x - 1

        if ((new > -1) and (new < self.xMax)):
            self.place()
            self.x = new
            self.change()

    #  Translate a single unit upwards
    def up(self):
        new = self.y - 1

        if ((new > -1) and (new < self.yMax)):
            self.place()
            self.y = new
            self.change()

    #  Translate a single unit downwards
    def down(self):
        new = self.y + 1

        if ((new > -1) and (new < self.yMax)):
            self.place()
            self.y = new
            self.change()


    def toString(self):
        for r in self.area:
            for i, c in enumerate(r):
                if i == len(r) - 1:
                    print(c)
                    continue

                print(c, end=" ")


#  Clear terminal beforehand
clear()

#  Custom playable grid size  |  Having different values doesn't work for some reason
obj = Board(25, 25)

#  Print default location
obj.toString()


#  Begin key detection loop  |  This honestly uses a lot of my cpu  |  The `while` loops within each for statement are to prevent the player from continuous translation but there is some wierd pause after releasing the held key
while True:

    if is_pressed("d"):
        clear()
        obj.right()
        obj.toString()

#       while is_pressed("d"):
#           continue

    elif is_pressed("a"):
        clear()
        obj.left()
        obj.toString()

#       while is_pressed("a"):
#           continue

    elif is_pressed("w"):
        clear()
        obj.up()
        obj.toString()

#       while is_pressed("w"):
#           continue

    elif is_pressed("s"):
        clear()
        obj.down()
        obj.toString()

#       while is_pressed("s"):
#           continue

    elif is_pressed("esc"):
        break

0 个答案:

没有答案
相关问题