roguelike
Roguelike Game
- 문서
- 간단한 예제
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 1 2 3 4 5 6 7 8 9 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)
roguelike.txt · 마지막으로 수정됨: 2024/03/23 02:38 저자 127.0.0.1