I want to write a game in Python (in terminal, not graphical) which captures key inputs but still keeps running and not wait for a key pressed. For instance, I want to move a bat up and down, just by pressing the arrow keys. But, when I do not press anything, the game must continue by i.e. bouncing the ball around.
Can this be done easily, without using extra modules like tkinter or pygame? I mean, is there somthing which can act like 'key_pressed = get_key' (fictional line)
# Sample functions
def print_board(ball_pos, bat_pos):
# Print the entire board
def move_ball(ball_pos, increment):
# Check boundaries, if hit wall, reverse increment
## Code comes here...
# Move ball
ball_pos[0] += increment[0]
ball_pos[1] += increment[2]
return ball_pos
def move_bat(bat_pos_y, increment_y):
# Check boundaries, if hit wall, do not increment
## Code comes here...
bat_pos_y += increment_y
return bat_pos_y
# Sample main routine
## Initialisation comes here...
## Game routine
while True:
bat_inc = 0
# Get a keystroke, without waiting
##key_pressed = get_key
if (key_pressed == UP):
bat_inc = -1
elif (key_pressed == DOWN):
bat_inc = 1
move_ball(ball, inc)
move_bat(bat, bat_inc)
print_board(ball, bat)