基本上,我正在尝试构建一个在 shell 中玩的乒乓球游戏。到目前为止,阻止我的是用户输入。代码必须响应按键,而无需在之后按 Return。我曾尝试使用 msvcrt,但它没有产生我需要的结果。是否可以在无需编码和事件监听器的情况下完成?这是代码:
import os
import msvcrt
#fixed parameters here
resolution_x=16
resolution_y=8
line_position = 4
char_position = 4
def line_draw(char_position, line_length):
#draws a line with # in it marking the ball
line = ""
if char_position == 0:
for i in range(line_length):
line+="_"
print(line)
if char_position:
for i in range(char_position-1):
line+="_"
line+="#"
for j in range(line_length-char_position):
line+="_"
print(line)
def scr_draw(num_lines, line_position, char_position, line_length):
# draws the court with the ball
# line by line with line_draw()
for i in range(line_position):
line_draw(0,line_length)
line_draw(char_position, line_length)
for i in range(num_lines-line_position):
line_draw(0, line_length)
def draw_paddle(line_length, paddle_position):
# this is the paddle positioning
padline=""
for i in range(paddle_position-3):
paddline+="-"
paddline+="==="
for i in range(line_length-paddle_position):
paddline+="-"
print(paddline)
while 1:
#this loop draws everything, then erases it
#so it can draw it again with updates
scr_draw(resolution_y, line_position, char_position, resolution_x) # draw
os.system("CLS") # clears the screen in a really stupid way , to be changed