LoginSignup
5
7

More than 5 years have passed since last update.

pythonでCUIの2Dゲーム基盤

Posted at

pythonを勉強し始めて半年ほど経ったので書きました。
大学の課題でC言語とかで宝探しゲームとか作らされるやつです。
WASD移動でX座標とY座標それぞれ動きます。

python3です!

スクリーンショット 2017-12-18 15.52.58.png

maze.py
field = [
    ["-","-","-","-","-"],
    ["-","-","-","-","-"],
    ["-","-","-","-","-"],
    ["-","-","-","-","-"],
    ["-","-","-","-","-"]
]
p_x = 1
p_y = 1

def ref():
    field[p_y][p_x] = '-'

def draw():
    field[p_y][p_x] = '*'

def move(command):
    global p_x
    global p_y
    if command == 'w':
        if p_y > 0:
            ref()
            p_y -= 1
            draw()
        else:
            pass
    elif command == 's':
        if p_y < 4:
            ref()
            p_y += 1
            draw()
        else:
            pass
    elif command == 'a':
        if p_x > 0:
            ref()
            p_x -= 1
            draw()
        else:
            pass
    elif command == 'd':
        if p_x < 4:
            ref()
            p_x += 1
            draw()
        else:
            pass

if __name__ == '__main__':
    draw()
    while True:
        for i in field:
            for j in i:
                print(j, end="")
            print("")
        print("======")
        print('up:w left:a down:s right:d')
        key = input('>> ')
        move(key)
5
7
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
7