roguelike
차이
문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 양쪽 이전 판이전 판다음 판 | 이전 판 | ||
| roguelike [2018/01/31 04:31] – rex8312 | roguelike [2024/03/23 02:38] (현재) – 바깥 편집 127.0.0.1 | ||
|---|---|---|---|
| 줄 1: | 줄 1: | ||
| ====== Roguelike Game ====== | ====== Roguelike Game ====== | ||
| - | | + | |
| - | * http:// | + | |
| + | * http://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html | ||
| + | * http:// | ||
| + | * http:// | ||
| + | * http:// | ||
| + | * http:// | ||
| + | * http:// | ||
| + | | ||
| + | * http:// | ||
| + | * https:// | ||
| + | |||
| + | * 간단한 예제 | ||
| + | |||
| + | <code python> | ||
| + | |||
| + | import os | ||
| + | import sys | ||
| + | import platform | ||
| + | import random | ||
| + | |||
| + | from colorama import Back, Fore, Style, init | ||
| + | |||
| + | init() | ||
| + | |||
| + | from IPython import embed | ||
| + | |||
| + | |||
| + | if platform.system() == ' | ||
| + | from msvcrt import getch | ||
| + | |||
| + | def get_key(): | ||
| + | return getch().decode().strip() | ||
| + | |||
| + | else: | ||
| + | import sys, tty, termios | ||
| + | def getch(): | ||
| + | |||
| + | fd = sys.stdin.fileno() | ||
| + | old_settings = termios.tcgetattr(fd) | ||
| + | try: | ||
| + | tty.setraw(sys.stdin.fileno()) | ||
| + | ch = sys.stdin.read(1) | ||
| + | finally: | ||
| + | termios.tcsetattr(fd, | ||
| + | return ch | ||
| + | |||
| + | def get_key(): | ||
| + | return getch().strip() | ||
| + | |||
| + | |||
| + | class Environment: | ||
| + | def __init__(self): | ||
| + | |||
| + | # 0 | ||
| + | self.room = {1: | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | 10: | ||
| + | # < | ||
| + | |||
| + | self.stuff = {' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | # self.potion = [' | ||
| + | # self.weapon = [' | ||
| + | |||
| + | self.pos = [] # 0 is X,1 is Y | ||
| + | |||
| + | def reset(self): | ||
| + | if platform.system() ==' | ||
| + | os.system(' | ||
| + | elif platform.system() == ' | ||
| + | os.system(' | ||
| + | return self.room | ||
| + | |||
| + | def render(self): | ||
| + | |||
| + | def gamemap(): | ||
| + | lines = '' | ||
| + | for i in range(1, len(self.room) + 1): | ||
| + | line = "" | ||
| + | line = line.replace('#', | ||
| + | line = line.replace(' | ||
| + | line = line.replace(' | ||
| + | lines += line + ' | ||
| + | sys.stdout.write(lines) | ||
| + | sys.stdout.flush() | ||
| + | |||
| + | # if platform.system() ==' | ||
| + | # | ||
| + | # elif platform.system() == ' | ||
| + | # | ||
| + | |||
| + | sys.stdout.write(' | ||
| + | sys.stdout.write(f' | ||
| + | gamemap() | ||
| + | sys.stdout.write(Fore.RESET + str(self.pos) + ' | ||
| + | sys.stdout.flush() | ||
| + | |||
| + | def step(self, action): | ||
| + | |||
| + | def player_pos(): | ||
| + | for i in range(1, len(self.room)+1): | ||
| + | if self.stuff[' | ||
| + | x_axis = i | ||
| + | y_axis = self.room[i].index(self.stuff[' | ||
| + | del self.pos[: | ||
| + | self.pos.append(x_axis) | ||
| + | self.pos.append(y_axis) | ||
| + | |||
| + | def up(ditcioary, | ||
| + | (ditcioary[self.pos[0]]).pop(self.pos[1]) | ||
| + | (ditcioary[self.pos[0]]).insert(self.pos[1], | ||
| + | (ditcioary[self.pos[0]-1]).pop(self.pos[1]) | ||
| + | (ditcioary[self.pos[0]-1]).insert(self.pos[1], | ||
| + | |||
| + | def down(ditcioary, | ||
| + | (ditcioary[self.pos[0]]).pop(self.pos[1]) | ||
| + | (ditcioary[self.pos[0]]).insert(self.pos[1], | ||
| + | (ditcioary[self.pos[0]+1]).pop(self.pos[1]) | ||
| + | (ditcioary[self.pos[0]+1]).insert(self.pos[1], | ||
| + | |||
| + | def left(ditcioary, | ||
| + | (ditcioary[self.pos[0]]).pop(self.pos[1]) | ||
| + | (ditcioary[self.pos[0]]).insert(self.pos[1], | ||
| + | (ditcioary[self.pos[0]]).pop(self.pos[1]-1) | ||
| + | (ditcioary[self.pos[0]]).insert(self.pos[1]-1, | ||
| + | |||
| + | def right(ditcioary, | ||
| + | (ditcioary[self.pos[0]]).pop(self.pos[1]) | ||
| + | (ditcioary[self.pos[0]]).insert(self.pos[1], | ||
| + | (ditcioary[self.pos[0]]).pop(self.pos[1]+1) | ||
| + | (ditcioary[self.pos[0]]).insert(self.pos[1]+1, | ||
| + | |||
| + | player_pos() | ||
| + | |||
| + | if action in (' | ||
| + | if self.room[self.pos[0]-1][self.pos[1]] is not self.stuff[' | ||
| + | up(self.room, | ||
| + | |||
| + | elif action in (' | ||
| + | if self.room[self.pos[0]+1][self.pos[1]] is not self.stuff[' | ||
| + | down(self.room, | ||
| + | |||
| + | elif action in (' | ||
| + | if self.room[self.pos[0]][self.pos[1]-1] is not self.stuff[' | ||
| + | left(self.room, | ||
| + | |||
| + | elif action in (' | ||
| + | if self.room[self.pos[0]][self.pos[1]+1] is not self.stuff[' | ||
| + | right(self.room, | ||
| + | |||
| + | elif action == ' | ||
| + | exit() | ||
| + | |||
| + | elif ord(action) == 3: # CTRL-C | ||
| + | exit() | ||
| + | |||
| + | return self.room | ||
| + | |||
| + | |||
| + | if __name__ == ' | ||
| + | |||
| + | env = Environment() | ||
| + | state = env.reset() | ||
| + | |||
| + | while True: | ||
| + | env.render() | ||
| + | action = get_key() | ||
| + | state = env.step(action) | ||
| + | </ | ||
| + | |||
| + | {{tag> | ||
roguelike.1517373118.txt.gz · 마지막으로 수정됨: (바깥 편집)