사용자 도구

사이트 도구


roguelike

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

다음 판
이전 판
roguelike [2018/01/31 04:31] – 만듦 rex8312roguelike [2024/03/23 02:38] (현재) – 바깥 편집 127.0.0.1
줄 1: 줄 1:
 ====== Roguelike Game ====== ====== Roguelike Game ======
  
-  * https://python-tdl.readthedocs.io/en/latest/index.html+ 
 +  * 문서 
 +    * http://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html 
 +    * http://www.roguebasin.com/index.php?title=Category:FOV 
 +      * http://www.roguebasin.com/index.php?title=Eligloscode 
 +    * http://rogueliketutorials.com/tutorials/tcod/v2/ 
 +    * http://www.gamasutra.com/blogs/JoshGe/20181029/329512/How_to_Make_a_Roguelike.php 
 +    * http://www.roguebasin.com/index.php?title=Roguelike_Tutorial,_using_python3%2Btdl 
 +    * http://www.roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python%2Blibtcod,_part_1#Showing_the_.40_on_screen 
 +    * http://www.roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python%2Blibtcod 
 +    * https://github.com/htdt/lwm/blob/master/pol/pol_env.py 
 + 
 +  * 간단한 예제 
 + 
 +<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() == 'Windows': 
 +    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, termios.TCSADRAIN, old_settings) 
 +        return ch 
 + 
 +    def get_key(): 
 +        return getch().strip() 
 + 
 + 
 +class Environment: 
 +    def __init__(self): 
 + 
 +                       # 0                    10   11  12  13  14  15 
 +        self.room = {1:['#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'], # ^ 
 +                     2:['#','.','.','.','.','.','.','.','.','.','.','.','.','.','.','#'], # | 
 +                     3:['#','.','.','.','.','.','.','.','.','#','#','#','#','.','.','#'], # | 
 +                     4:['#','.','.','.','.','.','.','#','.','#','.','.','.','.','.','#'], # | 
 +                     5:['#','.','.','.','.','.','@','#','.','#','.','.','#','.','.','#'], # | 
 +                     6:['#','.','.','.','.','.','.','#','.','.','.','.','#','.','.','#'], # x 
 +                     7:['#','.','.','.','.','#','#','#','.','#','#','.','#','.','.','#'], # | 
 +                     8:['#','.','.','.','.','.','.','.','.','.','#','#','#','.','.','#'], # | 
 +                     9:['#','.','.','.','.','.','.','.','.','.','.','.','.','.','.','#'], # | 
 +                    10:['#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#']} # | 
 +                       # <------------------------------y---------------------------------> 
 + 
 +        self.stuff = {'wall': "#", 
 +                      'player': "@", 
 +                      'empty': ".", 
 +                      'money': "$", 
 +                      'chest': "C"
 +        # self.potion = ['hp','xp','dmg'
 +        # self.weapon = ['sword','nuclear bomb','TNT'
 + 
 +        self.pos = [] # 0 is X,1 is Y 
 + 
 +    def reset(self): 
 +        if platform.system() =='Windows': 
 +            os.system('cls'
 +        elif platform.system() == 'Linux': 
 +            os.system('clear') #For linux.. I don't know what to do about mac :/ 
 +        return self.room 
 + 
 +    def render(self): 
 + 
 +        def gamemap(): 
 +            lines = '' 
 +            for i in range(1, len(self.room) + 1): 
 +                line = "".join(self.room[i]) 
 +                line = line.replace('#', Fore.GREEN + '#'
 +                line = line.replace('.', Fore.YELLOW + '.'
 +                line = line.replace('@', Fore.RESET + '@'
 +                lines += line + '\n' 
 +            sys.stdout.write(lines) 
 +            sys.stdout.flush() 
 + 
 +        # if platform.system() =='Windows': 
 +        #     os.system('cls'
 +        # elif platform.system() == 'Linux': 
 +        #     os.system('clear') #For linux.. I don't know what to do about mac :/ 
 + 
 +        sys.stdout.write('\u001b[1000D'
 +        sys.stdout.write(f'\u001b[{len(self.room) + 1}A'
 +        gamemap() 
 +        sys.stdout.write(Fore.RESET + str(self.pos) + '\n'
 +        sys.stdout.flush() 
 + 
 +    def step(self, action): 
 + 
 +        def player_pos(): 
 +            for i in range(1, len(self.room)+1): 
 +                if self.stuff['player'] in self.room[i]: 
 +                    x_axis = i 
 +                    y_axis = self.room[i].index(self.stuff['player']) 
 +                    del self.pos[:
 +                    self.pos.append(x_axis) 
 +                    self.pos.append(y_axis) 
 +         
 +        def up(ditcioary, inst_replace, inst_player): 
 +            (ditcioary[self.pos[0]]).pop(self.pos[1]) 
 +            (ditcioary[self.pos[0]]).insert(self.pos[1], inst_replace) 
 +            (ditcioary[self.pos[0]-1]).pop(self.pos[1]) 
 +            (ditcioary[self.pos[0]-1]).insert(self.pos[1], inst_player) 
 + 
 +        def down(ditcioary, inst_replace, inst_player): 
 +            (ditcioary[self.pos[0]]).pop(self.pos[1]) 
 +            (ditcioary[self.pos[0]]).insert(self.pos[1], inst_replace) 
 +            (ditcioary[self.pos[0]+1]).pop(self.pos[1]) 
 +            (ditcioary[self.pos[0]+1]).insert(self.pos[1], inst_player) 
 + 
 +        def left(ditcioary, inst_replace, inst_player): 
 +            (ditcioary[self.pos[0]]).pop(self.pos[1]) 
 +            (ditcioary[self.pos[0]]).insert(self.pos[1], inst_replace) 
 +            (ditcioary[self.pos[0]]).pop(self.pos[1]-1) 
 +            (ditcioary[self.pos[0]]).insert(self.pos[1]-1, inst_player) 
 + 
 +        def right(ditcioary, inst_replace, inst_player): 
 +            (ditcioary[self.pos[0]]).pop(self.pos[1]) 
 +            (ditcioary[self.pos[0]]).insert(self.pos[1], inst_replace) 
 +            (ditcioary[self.pos[0]]).pop(self.pos[1]+1) 
 +            (ditcioary[self.pos[0]]).insert(self.pos[1]+1, inst_player) 
 + 
 +        player_pos() 
 + 
 +        if action in ('w', 'W'): 
 +            if self.room[self.pos[0]-1][self.pos[1]] is not self.stuff['wall']: 
 +                up(self.room, self.stuff['empty'], self.stuff['player']) 
 +             
 +        elif action in ('s', 'S'): 
 +            if self.room[self.pos[0]+1][self.pos[1]] is not self.stuff['wall']: 
 +                down(self.room, self.stuff['empty'], self.stuff['player']) 
 +             
 +        elif action in ('a', 'A'): 
 +            if self.room[self.pos[0]][self.pos[1]-1] is not self.stuff['wall']: 
 +                left(self.room, self.stuff['empty'], self.stuff['player']) 
 + 
 +        elif action in ('d', 'D'): 
 +            if self.room[self.pos[0]][self.pos[1]+1] is not self.stuff['wall']: 
 +                right(self.room, self.stuff['empty'], self.stuff['player']) 
 + 
 +        elif action == 'Q': 
 +            exit() 
 + 
 +        elif ord(action) == 3: # CTRL-C 
 +            exit() 
 + 
 +        return self.room 
 + 
 + 
 +if __name__ == '__main__': 
 + 
 +    env = Environment() 
 +    state = env.reset() 
 + 
 +    while True: 
 +        env.render() 
 +        action = get_key() 
 +        state = env.step(action) 
 +</code> 
 + 
 +{{tag>roguelike "AI platform" python environment "game engine"}}
roguelike.1517373101.txt.gz · 마지막으로 수정됨: 2024/03/23 02:37 (바깥 편집)