2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CR(キャリッジリターン)を活用してゲームを作ろう!

Last updated at Posted at 2023-12-21

*この記事では、wsl2のubuntu(Ubuntu 22.04.2 LTS)を使用しています。

ある日、私はやることが特に思いつかずにぼーっとしていました。

しばらくぼーっとしていたところに唐突にやりたいことが降ってきました。
「あっ...なんかコマンドプロンプトでパックマンみたいなゲームがやりたい。つくろu」

そして、作ったのがこちらです。

コード
import sys

maze_list = [
        [" ","c","+","+","+","+"," "],
        ["+"," "," "," "," "," ","+"],
        ["+"," "," "," "," "," ","+"],
        ["+"," "," "," "," "," ","+"],
        ["+"," "," "," "," "," ","+"],
        [" ","+","+","+","+","g"," "]
]
cur_y = 0
cur_x = 1
mz_str = ""
n = len(maze_list)
print("↑:w,→:s,↓:z,←:a")
for i in range(0,n):
    for j in range(0,len(maze_list[i])):
        mz_str += maze_list[i][j]
        mz_str += " "
    print(mz_str)
    mz_str = ""

while (cur_y != 5) or (cur_x != 5):
    inp = input()

    if (inp == "w") and ((maze_list[cur_y][cur_x] != maze_list[0][1]) and (maze_list[cur_y - 1][cur_x] != "+")):
        ch = maze_list[cur_y - 1][cur_x]
        maze_list[cur_y - 1][cur_x] = maze_list[cur_y][cur_x]
        maze_list[cur_y][cur_x] = ch
        cur_y -= 1

    elif (inp == "s") and (maze_list[cur_y][cur_x + 1] != "+"):
        ch = maze_list[cur_y][cur_x + 1]
        maze_list[cur_y][cur_x + 1] = maze_list[cur_y][cur_x]
        maze_list[cur_y][cur_x] = ch
        cur_x += 1

    elif (inp == "z") and (maze_list[cur_y + 1][cur_x] != "+"):
        ch = maze_list[cur_y + 1][cur_x]
        maze_list[cur_y + 1][cur_x] = maze_list[cur_y][cur_x]
        maze_list[cur_y][cur_x] = ch
        cur_y += 1

    elif (inp == "a") and (maze_list[cur_y][cur_x - 1] != "+"):
        ch = maze_list[cur_y][cur_x - 1]
        maze_list[cur_y][cur_x - 1] = maze_list[cur_y][cur_x]
        maze_list[cur_y][cur_x] = ch
        cur_x -= 1

    for i in range(0,len(maze_list)):
        for j in range(0,len(maze_list[i])):
                mz_str += maze_list[i][j]
                mz_str += " "
        print(mz_str)
        mz_str = ""

print("goal!!")

しかし、このコードで動かすと1操作ごとに新たに画面が表示されます。

スクリーンショット (73).png

もう少し見やすくするためどうしようと考えました。
そして、それを解決するのに適したとあるものを思い出しました。

コマンドプロンプトで新たなライブラリをダウンロードする際などによく見る、プログレスバーです。

あれと同じようなことができるならば、新たに画面を表示することなく一画面でゲームを完結させることができると思い、以下の記事を参考にプログラムを変更しました。

コード
# キャリッジリターンの活用
import sys

maze_list = [
        [" "," "," "," "," "," "," "," "],
        [" ","c","+","+","+","+"," "," "],
        ["+",".",".",".",".",".","+"," "],
        ["+",".",".",".",".",".","+"," "],
        ["+",".",".",".",".",".","+"," "],
        ["+",".",".",".",".",".","+"," "],
        [" ","+","+","+","+","g"," "," "]
]
cur_y = 1
cur_x = 1
cnt = 0
mz_str = ""
n = len(maze_list)
ch = " "
print("↑:w,→:s,↓:z,←:a"+"\n")
for i in range(0,n):
    for j in range(0,len(maze_list[i])):
        mz_str += maze_list[i][j]
        mz_str += " "
    print(mz_str)
    mz_str = ""

while (cur_y != 6) or (cur_x != 5):
    inp = input()

    if (inp == "w") and ((cur_y != 0) and (maze_list[cur_y - 1][cur_x] != "+")):
        if maze_list[cur_y - 1][cur_x] == ".":
            cnt += 1
        maze_list[cur_y - 1][cur_x] = maze_list[cur_y][cur_x]
        maze_list[cur_y][cur_x] = ch
        cur_y -= 1

    elif (inp == "s") and ((maze_list[cur_y][cur_x + 1] != "+") and (cur_x != 7)):
        if maze_list[cur_y][cur_x + 1] == ".":
            cnt += 1
        maze_list[cur_y][cur_x + 1] = maze_list[cur_y][cur_x]
        maze_list[cur_y][cur_x] = ch
        cur_x += 1

    elif (inp == "z") and ((maze_list[cur_y + 1][cur_x] != "+") and (cur_y != 6)):
        if maze_list[cur_y + 1][cur_x] == ".":
            cnt += 1
        maze_list[cur_y + 1][cur_x] = maze_list[cur_y][cur_x]
        maze_list[cur_y][cur_x] = ch
        cur_y += 1

    elif (inp == "a") and ((maze_list[cur_y][cur_x - 1] != "+") and (cur_x != 0)):
        if maze_list[cur_y][cur_x - 1] == ".":
            cnt += 1
        maze_list[cur_y][cur_x - 1] = maze_list[cur_y][cur_x]
        maze_list[cur_y][cur_x] = ch
        cur_x -= 1

    sys.stdout.write("\033[2K\033[G")
    print("\033[8A",end="")
    for i in range(0,len(maze_list)):
        for j in range(0,len(maze_list[i])):
                mz_str += maze_list[i][j]
                mz_str += " "
        print(mz_str)
        mz_str = ""

print("goal!!")
print('{0}'.format(cnt),end="")
print("point")

play3.gif

キャリッジリターンを活用することで、ゲームの表示位置を固定して
遊ぶことができるようになりました。やったね。

できるのならばエンターボタンを押す作業も省きたかったのですが、
それを行うためのkeyboardライブラリの扱いが面倒そうなので
この記事で使うのは断念しました。いつかまた挑みます。

以上です、とても拙い記事ですが閲覧していただきありがとうございました。

2
1
0

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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?