1

Basically I am trying to build a game of pong that plays in the shell. So far the thing stopping me is the user input. The code has to respond to key presses without having to press Return afterwards. I have tried using msvcrt but it does not yield the result I need. Is it possible to be done without having to code and event listener? Here is the code:

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
Todor Markov
  • 151
  • 9
  • 1
    using the [msvcrt](http://docs.python.org/library/msvcrt.html) documentation `getch()` comes to mind. – hochl Sep 27 '12 at 13:32
  • although really you should maybe consider curses or pygame for this kind of thing ... – Joran Beasley Sep 27 '12 at 14:13
  • `msvcrt` implies it's on Windows where you don't have `curses` (at least that's what the [docs](http://docs.python.org/library/curses.html) say). – hochl Sep 27 '12 at 14:15

0 Answers0