用于nethack / pong游戏混合的Python交互式输入

时间:2012-09-27 13:25:31

标签: python shell input console game-loop

基本上我正在尝试构建一个在shell中玩的pong游戏。到目前为止阻止我的是用户输入。代码必须响应按键,而不必在事后按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

0 个答案:

没有答案