python按键简单游戏 [英] python keypress simple game

查看:94
本文介绍了python按键简单游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在屏幕上看到一个标志,例如,可能是(哈希)#".符号将具有一些起始位置,比方说(0,0).如果我按向右箭头,希望看到标志向右移动,如果按向左箭头,则希望看到向左移动,依此类推. 到目前为止,我的代码看起来像这样,并且可以读取pos,但是我想添加某种动画",以便可以在屏幕上看到符号在移动:

I would like to see on screen a sign, e.x might be (hash) '#'. Sign will have some starting position, let's say (0, 0). I would like to see sign moving right if I press right arrow, left if I press 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 sign is moving on the screen:

!更新:为了给您一个线索,我创建了图标",现在当您按向右或向左键时,图标会朝所需的方向移动.

!Update: Just to give u a clue, I created "icon" and now when u press right or left, icon moves in 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()

推荐答案

您可以创建用作地图的列表列表,并将播放器的单元格设置为'#'.然后仅打印地图,如果玩家移动,请使用 os.system('cls' if os.name == 'nt' else 'clear') 清除命令行/终端并打印更新的地图.

You could create a list of lists that serves as the map and set the cell of the player 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()

这篇关于python按键简单游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆