Simple python keypress game

I would like to see a sign on the screen, ex can be (hash) '#'. The sign will have some starting position, let's say (0, 0). I would like the sign to move to the right if I press the right arrow, to the left if I press the left arrow, etc. So far my code looks like this and it works for reading pos, but I want to add some kind of "animation" so I can see the sign moving on the screen:

! Update: To give a hint, I created an "icon" and now when u presses right or left, the icon moves in the desired direction.

from msvcrt import getch

icon = chr(254)
pos = [0, 0]
t = []
def fright():
    global pos
    pos[0] += 1
    print ' ' * pos[0], 
    print(icon) 

def fleft():
    global pos 
    pos[0] -= 1
    print ' ' * pos[0], 
    print(icon) 

def fup():
    global pos
    pos[1] += 1

def fdown():
    global pos
    pos[1] -= 1

def appendTab():
    global pos, t
    t.append(pos)

while True:
    print'Distance from zero: ', pos    
    key = ord(getch())

    if key == 27: #ESC
        break
    elif key == 13: #Enter
        print('selected')
        appendTab()
    elif key == 32: #Space, just a small test - skip this line
        print('jump')
        print(t)
    elif key == 224: #Special keys (arrows, f keys, ins, del, etc.)
        key = ord(getch())
        if key == 80: #Down arrow
            print('down')
            fdown()
        elif key == 72: #Up arrow
            print('up')
            fup()
        elif key == 75: #Left arrow
            print('left')
            fleft()
        elif key == 77: #Right arrow
            print('right')
            fright()

      

+3


source to share


1 answer


You can create a playlist that serves as a map and set the player slot to '#'

. Then just print the map and if the player moves, clear the command line / terminal with os.system('cls' if os.name == 'nt' else 'clear')

and print the updated map.



import os
from msvcrt import getch

pos = [0, 0]
# The map is a 2D list filled with '-'.
gamemap = [['-'] * 5 for _ in range(7)]
# Insert the player.
gamemap[pos[1]][pos[0]] = '#'

while True:
    print('Distance from zero: ', pos    )
    key = ord(getch())

    if key == 27: #ESC
        break
    elif key == 224: #Special keys (arrows, f keys, ins, del, etc.)
        key = ord(getch())
        if key in (80, 72, 75, 77):
            # Clear previous tile if player moves.
            gamemap[pos[1]][pos[0]] = '-'
        if key == 80: #Down arrow
            pos[1] += 1
        elif key == 72: #Up arrow
            pos[1] -= 1
        elif key == 75: #Left arrow
            pos[0] -= 1
        elif key == 77: #Right arrow
            pos[0] += 1

    print('clear')
    # Clear the command-line/terminal.
    os.system('cls' if os.name == 'nt' else 'clear')
    # Set the player to the new pos.
    gamemap[pos[1]][pos[0]] = '#'
    # Print the map.
    for row in gamemap:
        for tile in row:
            print(tile, end='')
        print()

      

+1


source







All Articles