From ca13938f5401eeac93e212e47d11af052db498e3 Mon Sep 17 00:00:00 2001 From: ProgramSnail Date: Tue, 30 Mar 2021 00:01:25 +0300 Subject: [PATCH 01/11] init --- proj/__init__.py | 1 + proj/main.py | 0 requirements.txt | 0 3 files changed, 1 insertion(+) create mode 100644 proj/__init__.py create mode 100644 proj/main.py create mode 100644 requirements.txt diff --git a/proj/__init__.py b/proj/__init__.py new file mode 100644 index 0000000..8ee9bae --- /dev/null +++ b/proj/__init__.py @@ -0,0 +1 @@ +import main diff --git a/proj/main.py b/proj/main.py new file mode 100644 index 0000000..e69de29 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e69de29 From 39eeef1dc8f6da2d03d4fe80f0298202c4615705 Mon Sep 17 00:00:00 2001 From: ProgramSnail Date: Mon, 12 Apr 2021 21:17:06 +0300 Subject: [PATCH 02/11] init curses --- proj/main.py | 32 ++++++++++++++++++++++++++++++++ requirements.txt | 1 + 2 files changed, 33 insertions(+) diff --git a/proj/main.py b/proj/main.py index e69de29..38fdf5a 100644 --- a/proj/main.py +++ b/proj/main.py @@ -0,0 +1,32 @@ +import curses + + +stdscr = curses.initscr() +curses.noecho() +curses.cbreak() +curses.curs_set(0) +stdscr.keypad(True) + +curses.start_color() + +SIMPLE = 1 +ACTIVE = 2 + +curses.init_pair(SIMPLE, curses.COLOR_WHITE, curses.COLOR_BLACK) +curses.init_pair(ACTIVE, curses.COLOR_BLACK, curses.COLOR_WHITE) + +def draw(): + stdscr.addstr(0, 0, "Hello World!", curses.color_pair(SIMPLE)) + + +while(True): + stdscr.clear() + draw() + stdscr.refresh() + stdscr.getkey() + + +curses.nocbreak() +stdscr.keypad(False) +curses.echo() +curses.endwin() diff --git a/requirements.txt b/requirements.txt index e69de29..a653857 100644 --- a/requirements.txt +++ b/requirements.txt @@ -0,0 +1 @@ +curses \ No newline at end of file From 9003c495be63a34bdc1393e931e5c6187e167516 Mon Sep 17 00:00:00 2001 From: ProgramSnail Date: Mon, 12 Apr 2021 22:11:38 +0300 Subject: [PATCH 03/11] init curses --- proj/main.py | 86 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 62 insertions(+), 24 deletions(-) diff --git a/proj/main.py b/proj/main.py index 38fdf5a..4bcb5e7 100644 --- a/proj/main.py +++ b/proj/main.py @@ -1,32 +1,70 @@ import curses +import random -stdscr = curses.initscr() -curses.noecho() -curses.cbreak() -curses.curs_set(0) -stdscr.keypad(True) - -curses.start_color() - -SIMPLE = 1 -ACTIVE = 2 - -curses.init_pair(SIMPLE, curses.COLOR_WHITE, curses.COLOR_BLACK) -curses.init_pair(ACTIVE, curses.COLOR_BLACK, curses.COLOR_WHITE) - -def draw(): - stdscr.addstr(0, 0, "Hello World!", curses.color_pair(SIMPLE)) +SIMPLE_COLOR = 1 +ACTIVE_COLOR = 2 -while(True): - stdscr.clear() - draw() - stdscr.refresh() +def begin_curses(): + stdscr = curses.initscr() + curses.noecho() + curses.cbreak() + curses.curs_set(0) + stdscr.keypad(True) + + curses.start_color() + + curses.init_pair(SIMPLE_COLOR, curses.COLOR_WHITE, curses.COLOR_BLACK) + curses.init_pair(ACTIVE_COLOR, curses.COLOR_BLACK, curses.COLOR_WHITE) + + return stdscr + + +def end_curses(stdscr): + curses.nocbreak() + stdscr.keypad(False) + curses.echo() + curses.endwin() + + +def generate_map(map, seed, sz_x, sz_y, map_symbols): + random.seed(seed) + for i in range(0, sz_x): + map.append(str()) + for j in range(0, sz_y): + x = random.randint(0, len(map_symbols) - 1) + map[i] += map_symbols[x] + + +def game_step(map): + pass + + +def draw(stdscr, map): + stdscr.addstr(0, 0, "Hello World!", curses.color_pair(SIMPLE_COLOR)) + + +def input(stdscr): stdscr.getkey() -curses.nocbreak() -stdscr.keypad(False) -curses.echo() -curses.endwin() +def main(): + map_symbols = [' ', '#', '$', '&', '*'] + + stdscr = begin_curses() + + map = [] + + generate_map(map, 2434343, 100, 100, map_symbols) + + while(True): + stdscr.clear() + game_step(map) + draw(stdscr, map) + stdscr.refresh() + input(stdscr) + + end_curses(stdscr) + +main() \ No newline at end of file From 5041b0595be23b5a45e249c3e9006086e8a78716 Mon Sep 17 00:00:00 2001 From: ProgramSnail Date: Mon, 12 Apr 2021 22:18:20 +0300 Subject: [PATCH 04/11] map --- proj/main.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/proj/main.py b/proj/main.py index 4bcb5e7..45d6640 100644 --- a/proj/main.py +++ b/proj/main.py @@ -1,6 +1,8 @@ import curses import random +MAP_POS_X = 10 +MAP_POS_Y = 10 SIMPLE_COLOR = 1 ACTIVE_COLOR = 2 @@ -30,11 +32,20 @@ def end_curses(stdscr): def generate_map(map, seed, sz_x, sz_y, map_symbols): random.seed(seed) + + rand_sum = 0 + for i in map_symbols: + rand_sum += map_symbols[i][1] + for i in range(0, sz_x): map.append(str()) for j in range(0, sz_y): - x = random.randint(0, len(map_symbols) - 1) - map[i] += map_symbols[x] + x = random.randint(1, rand_sum) + for k in map_symbols: + i -= map_symbols[k][1] + if i <= 0: + map[i] += map_symbols[k][0] + break def game_step(map): @@ -43,6 +54,8 @@ def game_step(map): def draw(stdscr, map): stdscr.addstr(0, 0, "Hello World!", curses.color_pair(SIMPLE_COLOR)) + for i in map: + stdscr.addstr(i + MAP_POS_Y, MAP_POS_X, map[i], curses.color_pair(SIMPLE_COLOR)) def input(stdscr): @@ -50,7 +63,7 @@ def input(stdscr): def main(): - map_symbols = [' ', '#', '$', '&', '*'] + map_symbols = [{'#', 10}, {'$', 1}, {'&', 2}, {'*', 1}] stdscr = begin_curses() From ebd4671c491fbb3f137e32d9040c82b481939eba Mon Sep 17 00:00:00 2001 From: ProgramSnail Date: Tue, 13 Apr 2021 01:20:31 +0300 Subject: [PATCH 05/11] monsters, etc --- proj/main.py | 203 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 176 insertions(+), 27 deletions(-) diff --git a/proj/main.py b/proj/main.py index 45d6640..195b81a 100644 --- a/proj/main.py +++ b/proj/main.py @@ -1,11 +1,25 @@ import curses import random +import time +import copy MAP_POS_X = 10 -MAP_POS_Y = 10 +MAP_POS_Y = 1 + +PLAYER = 'P' +EMPTY = ' ' +DOOR = '?' +MARK = '!' +WALL = '#' SIMPLE_COLOR = 1 ACTIVE_COLOR = 2 +WALL_COLOR = 3 +GOLD_COLOR = 4 +WEAPON_COLOR = 5 +HIGHLIGHTED_COLOR = 6 +PLAYER_COLOR = 7 +DOOR_COLOR = 8 def begin_curses(): @@ -14,70 +28,205 @@ def begin_curses(): curses.cbreak() curses.curs_set(0) stdscr.keypad(True) + stdscr.nodelay(True) curses.start_color() curses.init_pair(SIMPLE_COLOR, curses.COLOR_WHITE, curses.COLOR_BLACK) curses.init_pair(ACTIVE_COLOR, curses.COLOR_BLACK, curses.COLOR_WHITE) + curses.init_pair(WALL_COLOR, curses.COLOR_WHITE, curses.COLOR_BLACK) + curses.init_pair(GOLD_COLOR, curses.COLOR_YELLOW, curses.COLOR_BLACK) + curses.init_pair(WEAPON_COLOR, curses.COLOR_CYAN, curses.COLOR_BLACK) + curses.init_pair(HIGHLIGHTED_COLOR, curses.COLOR_CYAN, curses.COLOR_BLACK) + curses.init_pair(PLAYER_COLOR, curses.COLOR_BLUE, curses.COLOR_WHITE) + curses.init_pair(DOOR_COLOR, curses.COLOR_GREEN, curses.COLOR_BLACK) return stdscr def end_curses(stdscr): - curses.nocbreak() + stdscr.nodelay(False) stdscr.keypad(False) + curses.curs_set(1) + curses.nocbreak() curses.echo() curses.endwin() -def generate_map(map, seed, sz_x, sz_y, map_symbols): - random.seed(seed) - +#!! add cellular automata !! +def generate_map(map, sz_x, sz_y, map_symbols): rand_sum = 0 - for i in map_symbols: - rand_sum += map_symbols[i][1] + for symbol in map_symbols: + rand_sum += symbol[1] for i in range(0, sz_x): - map.append(str()) + map.append([]) for j in range(0, sz_y): + if i == 0 or j == 0 or i == \ + sz_x - 1 or j == sz_y - 1: + map[i].append((WALL, WALL_COLOR)) + continue x = random.randint(1, rand_sum) - for k in map_symbols: - i -= map_symbols[k][1] - if i <= 0: - map[i] += map_symbols[k][0] + for symbol in map_symbols: + x -= symbol[1] + if x <= 0: + map[i].append(symbol[0]) break -def game_step(map): +def map_mark_dfs(map, i, j): + positions = [(i, j)] + directions = [ + [1, 0], + [-1, 0], + [0, 1], + [0, -1] + ] + map[i][j] = (MARK, SIMPLE_COLOR) + for d in directions: + if len(map) > d[0] + i and d[0] + i >= 0 and \ + len(map[i + d[0]]) > d[1] + j and d[1] + j > 0: + positions += map_mark_dfs(map, i + d[0], j + d[1]) + + return positions + + +def generate_doors(map): + doors = [] + tmp_map = copy.deepcopy(map) + for i in range(len(tmp_map)): + for j in range(len(tmp_map[i])): + if tmp_map[i][j][0] == EMPTY: + map_mark_dfs(tmp_map, i, j) + map[i][j] = (DOOR, DOOR_COLOR) + doors.append((i, j)) + + +def random_direction(): + k = random.randint(0, 3) + return (((k % 2) * 2 - 1) * (k // 2), + ((k % 2) * 2 - 1) * (1 - k // 2)) + + +def generate_monsters(map, rand_range, rand_monster): + monsters = [] + for i in range(len(map)): + for j in range(len(map[i])): + if (map[i][j][0] == EMPTY): + if random.randint(1, rand_range) <= rand_monster: + + monsters.append(((i, j), random_direction())) + # position, move direction + + return monsters + + +def move_monsters(map, monsters): + for i in range(len(monsters)): + pos = monsters[i][0] + monsters[i][1] + if map[pos[0]][pos[1]] == EMPTY: + elif map[pos[0]][pos[1]] == PLAYER: pass -def draw(stdscr, map): - stdscr.addstr(0, 0, "Hello World!", curses.color_pair(SIMPLE_COLOR)) - for i in map: - stdscr.addstr(i + MAP_POS_Y, MAP_POS_X, map[i], curses.color_pair(SIMPLE_COLOR)) +def game_step(map, monsters, player_pos): + if player_pos == + move_monsters(map, monsters) + pass -def input(stdscr): - stdscr.getkey() +def draw(stdscr, map, monsters, player_pos): + #stdscr.addstr(MAP_POS_Y - 1, 0, "Items:", + # curses.color_pair(HIGHLIGHTED_COLOR)) + + for i in range(len(map)): + stdscr.addstr(MAP_POS_Y - 1, i + MAP_POS_X, + '-', curses.color_pair(SIMPLE_COLOR)) + stdscr.addstr(len(map[0]) + MAP_POS_Y, i + MAP_POS_X, + '-', curses.color_pair(SIMPLE_COLOR)) + + for i in range(len(map[0])): # ?? better ?? + stdscr.addstr(i + MAP_POS_Y, MAP_POS_X - 1, + '|', curses.color_pair(SIMPLE_COLOR)) + stdscr.addstr(i + MAP_POS_Y, len(map) + MAP_POS_X, + '|', curses.color_pair(SIMPLE_COLOR)) + + for i in range(len(map)): + for j in range(len(map[i])): + stdscr.addstr(j + MAP_POS_Y, i + MAP_POS_X, + map[i][j][0], curses.color_pair(map[i][j][1])) + + for m in monsters: + stdscr.addstr(m[0][1] + MAP_POS_Y, m[0][0] + MAP_POS_X, + MONSTER, MONSTER_COLOR) + + stdscr.addstr(MAP_POS_Y + player_pos[1], + MAP_POS_X + player_pos[0], PLAYER, + curses.color_pair(PLAYER_COLOR)) + + +def input(stdscr): # not work + """ + m = (0, 0) + try: + key = stdscr.getkey() + if key == 'w': + m[1] -= 1 + elif key == 's': + m[1] += 1 + elif key == 'a': + m[0] -= 1 + elif key == 'd': + m[0] += 1 + except: + pass + # no input + return m + """ + pass def main(): - map_symbols = [{'#', 10}, {'$', 1}, {'&', 2}, {'*', 1}] + map_symbols = [ + ((' ', SIMPLE_COLOR), 30), + (('#', WALL_COLOR), 10), + (('$', GOLD_COLOR), 1), + (('&', WEAPON_COLOR), 2), + (('*', WEAPON_COLOR), 1) + ] stdscr = begin_curses() + map_size = (curses.COLS // 2, curses.LINES // 2) + + player_pos = (0, 0) + map = [] - generate_map(map, 2434343, 100, 100, map_symbols) + generate_map(map, map_size[0], map_size[1], map_symbols) - while(True): - stdscr.clear() - game_step(map) - draw(stdscr, map) - stdscr.refresh() - input(stdscr) + doors = generate_doors(map) + + monsters = generate_monsters(map, 100, 7) + + main_loop_exception = False + + try: + while(True): + stdscr.clear() + game_step(map, monsters, player_pos) + draw(stdscr, map, monsters, player_pos) + stdscr.refresh() + player_pos += input(stdscr) + time.sleep(0.1) + + except: + main_loop_exception = True end_curses(stdscr) + if main_loop_exception: + print("Error during main loop"); + + main() \ No newline at end of file From 3eac12c38772bd50a85d4ce345e64350ddbe78e5 Mon Sep 17 00:00:00 2001 From: ProgramSnail Date: Wed, 14 Apr 2021 23:50:31 +0300 Subject: [PATCH 06/11] basic functions --- proj/main.py | 171 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 111 insertions(+), 60 deletions(-) diff --git a/proj/main.py b/proj/main.py index 195b81a..69e6ccc 100644 --- a/proj/main.py +++ b/proj/main.py @@ -3,12 +3,13 @@ import random import time import copy -MAP_POS_X = 10 -MAP_POS_Y = 1 +MAP_POS_X = 16 +MAP_POS_Y = 2 PLAYER = 'P' +MONSTER = 'M' EMPTY = ' ' -DOOR = '?' +DOOR = '>' MARK = '!' WALL = '#' @@ -20,6 +21,8 @@ WEAPON_COLOR = 5 HIGHLIGHTED_COLOR = 6 PLAYER_COLOR = 7 DOOR_COLOR = 8 +MONSTER_COLOR = 9 +ADD_HEALTH_COLOR = 10 def begin_curses(): @@ -28,7 +31,7 @@ def begin_curses(): curses.cbreak() curses.curs_set(0) stdscr.keypad(True) - stdscr.nodelay(True) + #stdscr.nodelay(True) curses.start_color() @@ -38,14 +41,16 @@ def begin_curses(): curses.init_pair(GOLD_COLOR, curses.COLOR_YELLOW, curses.COLOR_BLACK) curses.init_pair(WEAPON_COLOR, curses.COLOR_CYAN, curses.COLOR_BLACK) curses.init_pair(HIGHLIGHTED_COLOR, curses.COLOR_CYAN, curses.COLOR_BLACK) - curses.init_pair(PLAYER_COLOR, curses.COLOR_BLUE, curses.COLOR_WHITE) - curses.init_pair(DOOR_COLOR, curses.COLOR_GREEN, curses.COLOR_BLACK) + curses.init_pair(PLAYER_COLOR, curses.COLOR_BLACK, curses.COLOR_WHITE) + curses.init_pair(DOOR_COLOR, curses.COLOR_BLUE, curses.COLOR_BLACK) + curses.init_pair(MONSTER_COLOR, curses.COLOR_RED, curses.COLOR_BLACK) + curses.init_pair(ADD_HEALTH_COLOR, curses.COLOR_GREEN, curses.COLOR_BLACK) return stdscr def end_curses(stdscr): - stdscr.nodelay(False) + #stdscr.nodelay(False) stdscr.keypad(False) curses.curs_set(1) curses.nocbreak() @@ -53,40 +58,52 @@ def end_curses(stdscr): curses.endwin() -#!! add cellular automata !! +""" +def gen_map_step(map): + tmp_map = copy.deepcopy(map) + pass +""" + + +#!! needed to add cellular automata !! def generate_map(map, sz_x, sz_y, map_symbols): rand_sum = 0 for symbol in map_symbols: rand_sum += symbol[1] - for i in range(0, sz_x): + for i in range(sz_x): map.append([]) - for j in range(0, sz_y): - if i == 0 or j == 0 or i == \ - sz_x - 1 or j == sz_y - 1: - map[i].append((WALL, WALL_COLOR)) - continue - x = random.randint(1, rand_sum) - for symbol in map_symbols: - x -= symbol[1] - if x <= 0: - map[i].append(symbol[0]) - break + for j in range(sz_y): + if i == 0 or j == 0 or i == sz_x - 1 or j == sz_y - 1: + map[i].append([WALL, WALL_COLOR]) + else: + x = random.randint(1, rand_sum) + for symbol in map_symbols: + x -= symbol[1] + if x <= 0: + map[i].append(symbol[0]) + break def map_mark_dfs(map, i, j): + if map[i][j][0] != EMPTY: + return [] + positions = [(i, j)] + directions = [ [1, 0], [-1, 0], [0, 1], [0, -1] ] + map[i][j] = (MARK, SIMPLE_COLOR) for d in directions: - if len(map) > d[0] + i and d[0] + i >= 0 and \ - len(map[i + d[0]]) > d[1] + j and d[1] + j > 0: - positions += map_mark_dfs(map, i + d[0], j + d[1]) + # walls on all sides, then no check needed there + new_positions = map_mark_dfs(map, i + d[0], j + d[1]) + for pos in new_positions: + positions.append(pos) return positions @@ -94,18 +111,21 @@ def map_mark_dfs(map, i, j): def generate_doors(map): doors = [] tmp_map = copy.deepcopy(map) + for i in range(len(tmp_map)): for j in range(len(tmp_map[i])): if tmp_map[i][j][0] == EMPTY: map_mark_dfs(tmp_map, i, j) - map[i][j] = (DOOR, DOOR_COLOR) - doors.append((i, j)) + map[i][j] = [DOOR, DOOR_COLOR] + doors.append([i, j]) + + return doors def random_direction(): k = random.randint(0, 3) - return (((k % 2) * 2 - 1) * (k // 2), - ((k % 2) * 2 - 1) * (1 - k // 2)) + return [((k % 2) * 2 - 1) * (k // 2), + ((k % 2) * 2 - 1) * (1 - k // 2)] def generate_monsters(map, rand_range, rand_monster): @@ -115,7 +135,7 @@ def generate_monsters(map, rand_range, rand_monster): if (map[i][j][0] == EMPTY): if random.randint(1, rand_range) <= rand_monster: - monsters.append(((i, j), random_direction())) + monsters.append([[i, j], random_direction()]) # position, move direction return monsters @@ -123,21 +143,39 @@ def generate_monsters(map, rand_range, rand_monster): def move_monsters(map, monsters): for i in range(len(monsters)): - pos = monsters[i][0] + monsters[i][1] - if map[pos[0]][pos[1]] == EMPTY: - elif map[pos[0]][pos[1]] == PLAYER: + pos = [0, 0] + pos[0] = monsters[i][0][0] + monsters[i][1][0] + pos[1] = monsters[i][0][1] + monsters[i][1][1] + if map[pos[0]][pos[1]][0] == EMPTY: + monsters[i][0] = pos + pass + else: + monsters[i][1] = random_direction() + pass + + +def damage_player(monsters, player_state): + for m in monsters: + if abs(player_state[0][0] - m[0][0]) + abs(player_state[0][1] - m[0][1]) <= 1: + player_state[1] -= 1 pass -def game_step(map, monsters, player_pos): - if player_pos == +def game_step(map, monsters, player_state, doors): + #if player_pos == door for any door ? move_monsters(map, monsters) + damage_player(monsters, player_state) pass -def draw(stdscr, map, monsters, player_pos): - #stdscr.addstr(MAP_POS_Y - 1, 0, "Items:", - # curses.color_pair(HIGHLIGHTED_COLOR)) +def draw(stdscr, map, monsters, player_state): + if player_state[1] <= 0: + stdscr.addstr(max(curses.COLS // 2 - 4, 0), + max(curses.LINES // 2 - 4, 0), "You lose(") + return + + stdscr.addstr(MAP_POS_Y - 1, 0, "Lives: " + str(player_state[1]), + curses.color_pair(HIGHLIGHTED_COLOR)) for i in range(len(map)): stdscr.addstr(MAP_POS_Y - 1, i + MAP_POS_X, @@ -158,16 +196,16 @@ def draw(stdscr, map, monsters, player_pos): for m in monsters: stdscr.addstr(m[0][1] + MAP_POS_Y, m[0][0] + MAP_POS_X, - MONSTER, MONSTER_COLOR) + MONSTER, curses.color_pair(MONSTER_COLOR)) - stdscr.addstr(MAP_POS_Y + player_pos[1], - MAP_POS_X + player_pos[0], PLAYER, + stdscr.addstr(MAP_POS_Y + player_state[0][1], + MAP_POS_X + player_state[0][0], PLAYER, curses.color_pair(PLAYER_COLOR)) def input(stdscr): # not work - """ - m = (0, 0) + m = [0, 0] + try: key = stdscr.getkey() if key == 'w': @@ -181,25 +219,35 @@ def input(stdscr): # not work except: pass # no input + return m - """ pass +def move_player(map, player_state, pos_change): + new_player_pos = copy.deepcopy(player_state[0]) + + new_player_pos[0] += pos_change[0] + new_player_pos[1] += pos_change[1] + + if map[new_player_pos[0]][new_player_pos[1]][0] != WALL: + player_state[0] = new_player_pos + + def main(): map_symbols = [ - ((' ', SIMPLE_COLOR), 30), - (('#', WALL_COLOR), 10), - (('$', GOLD_COLOR), 1), - (('&', WEAPON_COLOR), 2), - (('*', WEAPON_COLOR), 1) + [[EMPTY, SIMPLE_COLOR], 30], + [[WALL, WALL_COLOR], 10], + [['$', GOLD_COLOR], 1], + [['/', WEAPON_COLOR], 2], + [['+', ADD_HEALTH_COLOR], 1] ] stdscr = begin_curses() - map_size = (curses.COLS // 2, curses.LINES // 2) + map_size = [20, 20] - player_pos = (0, 0) + player_state = [[0, 0], 10] # pos, lives map = [] @@ -207,21 +255,24 @@ def main(): doors = generate_doors(map) + player_state[0] = doors[0] + monsters = generate_monsters(map, 100, 7) main_loop_exception = False - try: - while(True): - stdscr.clear() - game_step(map, monsters, player_pos) - draw(stdscr, map, monsters, player_pos) - stdscr.refresh() - player_pos += input(stdscr) - time.sleep(0.1) + #try: + while(True): + stdscr.clear() + game_step(map, monsters, player_state, doors) + draw(stdscr, map, monsters, player_state) + stdscr.refresh() + pos_change = input(stdscr) + move_player(map, player_state, pos_change) + #time.sleep(0.1) - except: - main_loop_exception = True + #except: + # main_loop_exception = True end_curses(stdscr) @@ -229,4 +280,4 @@ def main(): print("Error during main loop"); -main() \ No newline at end of file +main() From 31ae63fa68d4dee7ceb21182066c39fc83eb9235 Mon Sep 17 00:00:00 2001 From: ProgramSnail Date: Thu, 15 Apr 2021 00:12:26 +0300 Subject: [PATCH 07/11] celluler automata dungeon generation added --- proj/main.py | 58 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/proj/main.py b/proj/main.py index 69e6ccc..34e4d40 100644 --- a/proj/main.py +++ b/proj/main.py @@ -24,6 +24,20 @@ DOOR_COLOR = 8 MONSTER_COLOR = 9 ADD_HEALTH_COLOR = 10 +DIRECTIONS = [ + [1, 0], + [-1, 0], + [0, 1], + [0, -1] +] + +EXTENDED_DIRECTIONS = DIRECTIONS + [ + [1, 1], + [1, -1], + [-1, 1], + [-1, -1] +] + def begin_curses(): stdscr = curses.initscr() @@ -58,15 +72,24 @@ def end_curses(stdscr): curses.endwin() -""" + def gen_map_step(map): tmp_map = copy.deepcopy(map) - pass -""" + for i in range(1, len(map) - 1): + for j in range(1, len(map[i]) - 1): + x = 0 + for d in EXTENDED_DIRECTIONS: + if tmp_map[i][j] == WALL: + x += 1 + if x < 2 or x >= 4: + map[i][j] = (EMPTY, SIMPLE_COLOR) + elif x == 3: + map[i][j] = (WALL, WALL_COLOR) -#!! needed to add cellular automata !! -def generate_map(map, sz_x, sz_y, map_symbols): +def generate_map(map, sz_x, sz_y, map_symbols, + automata_iterations, wall_rand_range, + wall_rand_gen): rand_sum = 0 for symbol in map_symbols: rand_sum += symbol[1] @@ -77,29 +100,34 @@ def generate_map(map, sz_x, sz_y, map_symbols): if i == 0 or j == 0 or i == sz_x - 1 or j == sz_y - 1: map[i].append([WALL, WALL_COLOR]) else: + if random.randint(1, wall_rand_range) <= wall_rand_gen: + map[i].append([WALL, WALL_COLOR]) + else: + map[i].append([EMPTY, SIMPLE_COLOR]) + + for i in range(automata_iterations): + gen_map_step(map) + + for i in range(1, len(map) - 1): + for j in range(1, len(map[i]) - 1): + if (map[i][j][0] == EMPTY): x = random.randint(1, rand_sum) for symbol in map_symbols: x -= symbol[1] if x <= 0: - map[i].append(symbol[0]) + map[i][j] = symbol[0] break + def map_mark_dfs(map, i, j): if map[i][j][0] != EMPTY: return [] positions = [(i, j)] - directions = [ - [1, 0], - [-1, 0], - [0, 1], - [0, -1] - ] - map[i][j] = (MARK, SIMPLE_COLOR) - for d in directions: + for d in DIRECTIONS: # walls on all sides, then no check needed there new_positions = map_mark_dfs(map, i + d[0], j + d[1]) for pos in new_positions: @@ -251,7 +279,7 @@ def main(): map = [] - generate_map(map, map_size[0], map_size[1], map_symbols) + generate_map(map, map_size[0], map_size[1], map_symbols, 100, 100, 45) doors = generate_doors(map) From 9396b569ab6ee4eefb6a28a74487935d3a482096 Mon Sep 17 00:00:00 2001 From: ProgramSnail Date: Thu, 15 Apr 2021 00:24:44 +0300 Subject: [PATCH 08/11] items added --- proj/main.py | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/proj/main.py b/proj/main.py index 34e4d40..753b59f 100644 --- a/proj/main.py +++ b/proj/main.py @@ -1,6 +1,5 @@ import curses import random -import time import copy MAP_POS_X = 16 @@ -12,6 +11,9 @@ EMPTY = ' ' DOOR = '>' MARK = '!' WALL = '#' +GOLD = '$' +WEAPON = '/' +ADD_HEALTH = '+' SIMPLE_COLOR = 1 ACTIVE_COLOR = 2 @@ -121,7 +123,7 @@ def generate_map(map, sz_x, sz_y, map_symbols, def map_mark_dfs(map, i, j): - if map[i][j][0] != EMPTY: + if map[i][j][0] == WALL or map[i][j][0] == MARK: return [] positions = [(i, j)] @@ -205,6 +207,12 @@ def draw(stdscr, map, monsters, player_state): stdscr.addstr(MAP_POS_Y - 1, 0, "Lives: " + str(player_state[1]), curses.color_pair(HIGHLIGHTED_COLOR)) + stdscr.addstr(MAP_POS_Y, 0, "Weapon: " + str(player_state[2]), + curses.color_pair(HIGHLIGHTED_COLOR)) + + stdscr.addstr(MAP_POS_Y + 1, 0, "Gold: " + str(player_state[3]), + curses.color_pair(HIGHLIGHTED_COLOR)) + for i in range(len(map)): stdscr.addstr(MAP_POS_Y - 1, i + MAP_POS_X, '-', curses.color_pair(SIMPLE_COLOR)) @@ -236,13 +244,13 @@ def input(stdscr): # not work try: key = stdscr.getkey() - if key == 'w': + if key == 'w' or key == "KEY_UP": m[1] -= 1 - elif key == 's': + elif key == 's' or key == "KEY_DOWN": m[1] += 1 - elif key == 'a': + elif key == 'a' or key == "KEY_LEFT": m[0] -= 1 - elif key == 'd': + elif key == 'd' or key == "KEY_RIGHT": m[0] += 1 except: pass @@ -253,29 +261,40 @@ def input(stdscr): # not work def move_player(map, player_state, pos_change): - new_player_pos = copy.deepcopy(player_state[0]) + p = copy.deepcopy(player_state[0]) - new_player_pos[0] += pos_change[0] - new_player_pos[1] += pos_change[1] + p[0] += pos_change[0] + p[1] += pos_change[1] - if map[new_player_pos[0]][new_player_pos[1]][0] != WALL: - player_state[0] = new_player_pos + x = map[p[0]][p[1]][0] + + if x != WALL: + player_state[0] = p + if x == ADD_HEALTH: + player_state[1] += 1 + map[p[0]][p[1]] = [EMPTY, SIMPLE_COLOR] + elif x == WEAPON: + player_state[2] += 1 + map[p[0]][p[1]] = [EMPTY, SIMPLE_COLOR] + elif x == GOLD: + player_state[3] += 1 + map[p[0]][p[1]] = [EMPTY, SIMPLE_COLOR] def main(): map_symbols = [ [[EMPTY, SIMPLE_COLOR], 30], [[WALL, WALL_COLOR], 10], - [['$', GOLD_COLOR], 1], - [['/', WEAPON_COLOR], 2], - [['+', ADD_HEALTH_COLOR], 1] + [[GOLD, GOLD_COLOR], 1], + [[WEAPON, WEAPON_COLOR], 2], + [[ADD_HEALTH, ADD_HEALTH_COLOR], 1] ] stdscr = begin_curses() map_size = [20, 20] - player_state = [[0, 0], 10] # pos, lives + player_state = [[0, 0], 10, 0, 0] # pos, lives, weapon, money map = [] From b0190cd8dc65e1a86a7802bf4292d0f27de83a30 Mon Sep 17 00:00:00 2001 From: ProgramSnail Date: Thu, 15 Apr 2021 14:25:58 +0300 Subject: [PATCH 09/11] added help, weapon, some bugs fixed --- proj/main.py | 254 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 197 insertions(+), 57 deletions(-) diff --git a/proj/main.py b/proj/main.py index 753b59f..f372972 100644 --- a/proj/main.py +++ b/proj/main.py @@ -5,6 +5,8 @@ import copy MAP_POS_X = 16 MAP_POS_Y = 2 +WEAPON_SPEED = 1 + PLAYER = 'P' MONSTER = 'M' EMPTY = ' ' @@ -13,7 +15,9 @@ MARK = '!' WALL = '#' GOLD = '$' WEAPON = '/' +ACTIVE_WEAPON = '.' ADD_HEALTH = '+' +SCORE = '@' SIMPLE_COLOR = 1 ACTIVE_COLOR = 2 @@ -25,12 +29,36 @@ PLAYER_COLOR = 7 DOOR_COLOR = 8 MONSTER_COLOR = 9 ADD_HEALTH_COLOR = 10 +SCORE_COLOR = 11 +ACTIVE_WEAPON_COLOR = 12 + +MONSTER_DAMAGE = 1 + +P_POS = 0 +P_HEALTH = 1 +P_WEAPON = 2 +P_GOLD = 3 +P_ACTIVE_DOOR = 4 +P_SCORE = 5 + +MOVE_UP_ACTION = "KEY_UP" +MOVE_DOWN_ACTION = "KEY_DOWN" +MOVE_LEFT_ACTION = "KEY_LEFT" +MOVE_RIGHT_ACTION = "KEY_RIGHT" + +ATTACK_UP_ACTION = 'w' +ATTACK_DOWN_ACTION = 's' +ATTACK_LEFT_ACTION = 'a' +ATTACK_RIGHT_ACTION = 'd' + +NEXT_DOOR_ACTION = 'q' +PREV_DOOR_ACTION = 'e' DIRECTIONS = [ - [1, 0], - [-1, 0], + [0, -1], [0, 1], - [0, -1] + [-1, 0], + [1, 0] ] EXTENDED_DIRECTIONS = DIRECTIONS + [ @@ -61,6 +89,8 @@ def begin_curses(): curses.init_pair(DOOR_COLOR, curses.COLOR_BLUE, curses.COLOR_BLACK) curses.init_pair(MONSTER_COLOR, curses.COLOR_RED, curses.COLOR_BLACK) curses.init_pair(ADD_HEALTH_COLOR, curses.COLOR_GREEN, curses.COLOR_BLACK) + curses.init_pair(SCORE_COLOR, curses.COLOR_GREEN, curses.COLOR_BLACK) + curses.init_pair(ACTIVE_WEAPON_COLOR, curses.COLOR_YELLOW, curses.COLOR_BLACK) return stdscr @@ -81,12 +111,12 @@ def gen_map_step(map): for j in range(1, len(map[i]) - 1): x = 0 for d in EXTENDED_DIRECTIONS: - if tmp_map[i][j] == WALL: + if tmp_map[i + d[0]][j + d[1]][0] == WALL: x += 1 if x < 2 or x >= 4: - map[i][j] = (EMPTY, SIMPLE_COLOR) - elif x == 3: - map[i][j] = (WALL, WALL_COLOR) + map[i][j] = [EMPTY, SIMPLE_COLOR] + elif x >= 3 and x <= 3: + map[i][j] = [WALL, WALL_COLOR] def generate_map(map, sz_x, sz_y, map_symbols, @@ -176,7 +206,7 @@ def move_monsters(map, monsters): pos = [0, 0] pos[0] = monsters[i][0][0] + monsters[i][1][0] pos[1] = monsters[i][0][1] + monsters[i][1][1] - if map[pos[0]][pos[1]][0] == EMPTY: + if map[pos[0]][pos[1]][0] != WALL: monsters[i][0] = pos pass else: @@ -186,8 +216,8 @@ def move_monsters(map, monsters): def damage_player(monsters, player_state): for m in monsters: - if abs(player_state[0][0] - m[0][0]) + abs(player_state[0][1] - m[0][1]) <= 1: - player_state[1] -= 1 + if abs(player_state[P_POS][0] - m[0][0]) + abs(player_state[P_POS][1] - m[0][1]) <= 1: + player_state[P_HEALTH] -= MONSTER_DAMAGE pass @@ -198,20 +228,41 @@ def game_step(map, monsters, player_state, doors): pass -def draw(stdscr, map, monsters, player_state): - if player_state[1] <= 0: - stdscr.addstr(max(curses.COLS // 2 - 4, 0), - max(curses.LINES // 2 - 4, 0), "You lose(") - return +def draw(stdscr, map, monsters, active_weapon, player_state): + stdscr.addstr(0, 0, str(player_state)) - stdscr.addstr(MAP_POS_Y - 1, 0, "Lives: " + str(player_state[1]), - curses.color_pair(HIGHLIGHTED_COLOR)) + info_str = [ + "Health: " + str(player_state[P_HEALTH]), + "Weapon: " + str(player_state[P_WEAPON]), + "Gold: " + str(player_state[P_GOLD]), + "", + "Help:", + "", + "ARROWS - move", + "", + "WASD - use", + "weapon", + "", + "QE - move", + "through doors", + "", + "You must", + "collect", + "all score", + "", + "% - weapon", + "# - wall", + "@ - score", + "M - monster", + "P - player", + "> - door", + "$ - money", + "+ - add health" + ] - stdscr.addstr(MAP_POS_Y, 0, "Weapon: " + str(player_state[2]), - curses.color_pair(HIGHLIGHTED_COLOR)) - - stdscr.addstr(MAP_POS_Y + 1, 0, "Gold: " + str(player_state[3]), - curses.color_pair(HIGHLIGHTED_COLOR)) + for i in range(len(info_str)): + stdscr.addstr(MAP_POS_Y - 1 + i, 0, info_str[i], + curses.color_pair(HIGHLIGHTED_COLOR)) for i in range(len(map)): stdscr.addstr(MAP_POS_Y - 1, i + MAP_POS_X, @@ -228,35 +279,40 @@ def draw(stdscr, map, monsters, player_state): for i in range(len(map)): for j in range(len(map[i])): stdscr.addstr(j + MAP_POS_Y, i + MAP_POS_X, - map[i][j][0], curses.color_pair(map[i][j][1])) + map[i][j][0], curses.color_pair(map[i][j][1])) for m in monsters: stdscr.addstr(m[0][1] + MAP_POS_Y, m[0][0] + MAP_POS_X, MONSTER, curses.color_pair(MONSTER_COLOR)) - stdscr.addstr(MAP_POS_Y + player_state[0][1], - MAP_POS_X + player_state[0][0], PLAYER, + for w in active_weapon: + stdscr.addstr(w[0][1] + MAP_POS_Y, w[0][0] + MAP_POS_X, + ACTIVE_WEAPON, curses.color_pair(ACTIVE_WEAPON_COLOR)) + + stdscr.addstr(MAP_POS_Y + player_state[P_POS][1], + MAP_POS_X + player_state[P_POS][0], PLAYER, curses.color_pair(PLAYER_COLOR)) def input(stdscr): # not work m = [0, 0] + key = "" try: key = stdscr.getkey() - if key == 'w' or key == "KEY_UP": + if key == MOVE_UP_ACTION: m[1] -= 1 - elif key == 's' or key == "KEY_DOWN": + elif key == MOVE_DOWN_ACTION: m[1] += 1 - elif key == 'a' or key == "KEY_LEFT": + elif key == MOVE_LEFT_ACTION: m[0] -= 1 - elif key == 'd' or key == "KEY_RIGHT": + elif key == MOVE_RIGHT_ACTION: m[0] += 1 except: pass # no input - return m + return (m, key) pass @@ -271,60 +327,144 @@ def move_player(map, player_state, pos_change): if x != WALL: player_state[0] = p if x == ADD_HEALTH: - player_state[1] += 1 - map[p[0]][p[1]] = [EMPTY, SIMPLE_COLOR] + player_state[P_HEALTH] += 1 elif x == WEAPON: - player_state[2] += 1 - map[p[0]][p[1]] = [EMPTY, SIMPLE_COLOR] + player_state[P_WEAPON] += 1 elif x == GOLD: - player_state[3] += 1 + player_state[P_GOLD] += 1 + elif x == SCORE: + player_state[P_SCORE] += 1 + + if x != WALL and x != DOOR: + map[p[0]][p[1]] = [EMPTY, SIMPLE_COLOR] + + +def use_weapon(map, active_weapon, player_state, direction): + p = copy.deepcopy(player_state[P_POS]) + p[0] += direction[0] + p[1] += direction[1] + if map[p[0]][p[1]][0] == WALL: + if p[0] != 0 and p[1] != 0 and p[0] != len(map) - 1 and p[1] != len(map[0]): map[p[0]][p[1]] = [EMPTY, SIMPLE_COLOR] + else: + active_weapon.append([p, copy.deepcopy(direction)]) + + +def player_actions(map, doors, active_weapon, player_state, player_action): + if doors[player_state[P_ACTIVE_DOOR]]: + if player_action == NEXT_DOOR_ACTION: + player_state[P_ACTIVE_DOOR] += 1 + player_state[P_ACTIVE_DOOR] %= len(doors) + player_state[P_POS] = copy.deepcopy( + doors[player_state[P_ACTIVE_DOOR]]) + elif player_action == PREV_DOOR_ACTION: + player_state[P_ACTIVE_DOOR] = (player_state[P_ACTIVE_DOOR] + + len(doors) - 1) % len(doors) + player_state[P_POS] = copy.deepcopy( + doors[player_state[P_ACTIVE_DOOR]]) + k = -1 + if player_action == ATTACK_UP_ACTION: + k = 0 + elif player_action == ATTACK_DOWN_ACTION: + k = 1 + elif player_action == ATTACK_LEFT_ACTION: + k = 2 + elif player_action == ATTACK_RIGHT_ACTION: + k = 3 + + if k >= 0: + use_weapon(map, active_weapon, player_state, DIRECTIONS[k]) + + + +def move_weapon(map, active_weapon, player_state): # needed to test + remove_list = [] + for i in range(len(active_weapon)): + p = copy.deepcopy(active_weapon[i][0]) + p[0] += active_weapon[i][1][0] + p[1] += active_weapon[i][1][1] + if map[p[0]][p[1]][0] == WALL or \ + map[p[0]][p[1]][0] == DOOR: + p = active_weapon[i][0] + if map[p[0]][p[1]][0] == SCORE: + player_state[P_SCORE] += 1 + map[p[0]][p[1]] = [WALL, WALL_COLOR] + remove_list.append(i) + else: + active_weapon[i][0] = p + + for i in remove_list: + active_weapon.remove(active_weapon[i]) + + +def calc_max_score(map): + max_score = 0 + for row in map: + for cell in row: + if cell[0] == SCORE: + max_score += 1 + return max_score def main(): map_symbols = [ - [[EMPTY, SIMPLE_COLOR], 30], - [[WALL, WALL_COLOR], 10], - [[GOLD, GOLD_COLOR], 1], - [[WEAPON, WEAPON_COLOR], 2], - [[ADD_HEALTH, ADD_HEALTH_COLOR], 1] + [[EMPTY, SIMPLE_COLOR], 90], + [[GOLD, GOLD_COLOR], 2], + [[WEAPON, WEAPON_COLOR], 4], + [[ADD_HEALTH, ADD_HEALTH_COLOR], 2], + [[SCORE, SCORE_COLOR], 2] ] stdscr = begin_curses() - map_size = [20, 20] + map_size = [40, 20] - player_state = [[0, 0], 10, 0, 0] # pos, lives, weapon, money + active_weapon = [] # positions of active weapon map = [] - generate_map(map, map_size[0], map_size[1], map_symbols, 100, 100, 45) + generate_map(map, map_size[0], map_size[1], map_symbols, 20, 100, 45) doors = generate_doors(map) - player_state[0] = doors[0] + monsters = generate_monsters(map, 100, 3) - monsters = generate_monsters(map, 100, 7) + max_score = calc_max_score(map) + + player_state = [doors[0], 10, 2, 0, 0, 0] + # pos = [x, y], lives, weapon, money, active door, score main_loop_exception = False - #try: - while(True): - stdscr.clear() - game_step(map, monsters, player_state, doors) - draw(stdscr, map, monsters, player_state) - stdscr.refresh() - pos_change = input(stdscr) - move_player(map, player_state, pos_change) - #time.sleep(0.1) + player_win = False - #except: - # main_loop_exception = True + try: + while(True): + stdscr.clear() + game_step(map, monsters, player_state, doors) + draw(stdscr, map, monsters, active_weapon, player_state) + stdscr.refresh() + pos_change, player_action = input(stdscr) + move_player(map, player_state, pos_change) + player_actions(map, doors, active_weapon, player_state, player_action) + for i in range(WEAPON_SPEED): + move_weapon(map, active_weapon, player_state) + if player_state[P_HEALTH] <= 0: + break + if player_state[P_SCORE] == max_score: + player_win = True + break # win + except: + main_loop_exception = True end_curses(stdscr) if main_loop_exception: - print("Error during main loop"); + print("Error during main loop") + elif player_win: + print("You win!") + else: + print("You lose(") main() From 583958cead23968e414f0f0ed9552b45e44098ef Mon Sep 17 00:00:00 2001 From: ProgramSnail Date: Fri, 16 Apr 2021 01:35:25 +0300 Subject: [PATCH 10/11] bug fixes --- proj/main.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/proj/main.py b/proj/main.py index f372972..2ef3af3 100644 --- a/proj/main.py +++ b/proj/main.py @@ -235,6 +235,7 @@ def draw(stdscr, map, monsters, active_weapon, player_state): "Health: " + str(player_state[P_HEALTH]), "Weapon: " + str(player_state[P_WEAPON]), "Gold: " + str(player_state[P_GOLD]), + "Score: " + str(player_state[P_SCORE]), "", "Help:", "", @@ -340,14 +341,18 @@ def move_player(map, player_state, pos_change): def use_weapon(map, active_weapon, player_state, direction): + if player_state[P_WEAPON] <= 0: + return p = copy.deepcopy(player_state[P_POS]) p[0] += direction[0] p[1] += direction[1] if map[p[0]][p[1]][0] == WALL: if p[0] != 0 and p[1] != 0 and p[0] != len(map) - 1 and p[1] != len(map[0]): map[p[0]][p[1]] = [EMPTY, SIMPLE_COLOR] + player_state[P_WEAPON] -= 1 else: active_weapon.append([p, copy.deepcopy(direction)]) + player_state[P_WEAPON] -= 1 def player_actions(map, doors, active_weapon, player_state, player_action): From f2a1c269106fd9939174e00e094a3eaa5827d019 Mon Sep 17 00:00:00 2001 From: ProgramSnail Date: Sat, 29 May 2021 19:39:39 +0300 Subject: [PATCH 11/11] fixes --- proj/main.py | 102 +++++++++------------------------------------ proj/var_config.py | 65 +++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 83 deletions(-) create mode 100644 proj/var_config.py diff --git a/proj/main.py b/proj/main.py index f372972..e69d5c4 100644 --- a/proj/main.py +++ b/proj/main.py @@ -2,71 +2,7 @@ import curses import random import copy -MAP_POS_X = 16 -MAP_POS_Y = 2 - -WEAPON_SPEED = 1 - -PLAYER = 'P' -MONSTER = 'M' -EMPTY = ' ' -DOOR = '>' -MARK = '!' -WALL = '#' -GOLD = '$' -WEAPON = '/' -ACTIVE_WEAPON = '.' -ADD_HEALTH = '+' -SCORE = '@' - -SIMPLE_COLOR = 1 -ACTIVE_COLOR = 2 -WALL_COLOR = 3 -GOLD_COLOR = 4 -WEAPON_COLOR = 5 -HIGHLIGHTED_COLOR = 6 -PLAYER_COLOR = 7 -DOOR_COLOR = 8 -MONSTER_COLOR = 9 -ADD_HEALTH_COLOR = 10 -SCORE_COLOR = 11 -ACTIVE_WEAPON_COLOR = 12 - -MONSTER_DAMAGE = 1 - -P_POS = 0 -P_HEALTH = 1 -P_WEAPON = 2 -P_GOLD = 3 -P_ACTIVE_DOOR = 4 -P_SCORE = 5 - -MOVE_UP_ACTION = "KEY_UP" -MOVE_DOWN_ACTION = "KEY_DOWN" -MOVE_LEFT_ACTION = "KEY_LEFT" -MOVE_RIGHT_ACTION = "KEY_RIGHT" - -ATTACK_UP_ACTION = 'w' -ATTACK_DOWN_ACTION = 's' -ATTACK_LEFT_ACTION = 'a' -ATTACK_RIGHT_ACTION = 'd' - -NEXT_DOOR_ACTION = 'q' -PREV_DOOR_ACTION = 'e' - -DIRECTIONS = [ - [0, -1], - [0, 1], - [-1, 0], - [1, 0] -] - -EXTENDED_DIRECTIONS = DIRECTIONS + [ - [1, 1], - [1, -1], - [-1, 1], - [-1, -1] -] +from var_config import * def begin_curses(): @@ -192,11 +128,9 @@ def generate_monsters(map, rand_range, rand_monster): monsters = [] for i in range(len(map)): for j in range(len(map[i])): - if (map[i][j][0] == EMPTY): - if random.randint(1, rand_range) <= rand_monster: - - monsters.append([[i, j], random_direction()]) - # position, move direction + if map[i][j][0] == EMPTY and \ + random.randint(1, rand_range) <= rand_monster: + monsters.append([[i, j], random_direction()]) return monsters @@ -351,17 +285,6 @@ def use_weapon(map, active_weapon, player_state, direction): def player_actions(map, doors, active_weapon, player_state, player_action): - if doors[player_state[P_ACTIVE_DOOR]]: - if player_action == NEXT_DOOR_ACTION: - player_state[P_ACTIVE_DOOR] += 1 - player_state[P_ACTIVE_DOOR] %= len(doors) - player_state[P_POS] = copy.deepcopy( - doors[player_state[P_ACTIVE_DOOR]]) - elif player_action == PREV_DOOR_ACTION: - player_state[P_ACTIVE_DOOR] = (player_state[P_ACTIVE_DOOR] - + len(doors) - 1) % len(doors) - player_state[P_POS] = copy.deepcopy( - doors[player_state[P_ACTIVE_DOOR]]) k = -1 if player_action == ATTACK_UP_ACTION: k = 0 @@ -374,7 +297,20 @@ def player_actions(map, doors, active_weapon, player_state, player_action): if k >= 0: use_weapon(map, active_weapon, player_state, DIRECTIONS[k]) + + if not doors[player_state[P_ACTIVE_DOOR]]: + return + if player_action == NEXT_DOOR_ACTION: + player_state[P_ACTIVE_DOOR] += 1 + player_state[P_ACTIVE_DOOR] %= len(doors) + player_state[P_POS] = copy.deepcopy( + doors[player_state[P_ACTIVE_DOOR]]) + elif player_action == PREV_DOOR_ACTION: + player_state[P_ACTIVE_DOOR] = (player_state[P_ACTIVE_DOOR] + + len(doors) - 1) % len(doors) + player_state[P_POS] = copy.deepcopy( + doors[player_state[P_ACTIVE_DOOR]]) def move_weapon(map, active_weapon, player_state): # needed to test @@ -466,5 +402,5 @@ def main(): else: print("You lose(") - -main() +if (__name__ == "__main__"): + main() diff --git a/proj/var_config.py b/proj/var_config.py new file mode 100644 index 0000000..fd68f47 --- /dev/null +++ b/proj/var_config.py @@ -0,0 +1,65 @@ +MAP_POS_X = 16 +MAP_POS_Y = 2 + +WEAPON_SPEED = 1 + +PLAYER = 'P' +MONSTER = 'M' +EMPTY = ' ' +DOOR = '>' +MARK = '!' +WALL = '#' +GOLD = '$' +WEAPON = '/' +ACTIVE_WEAPON = '.' +ADD_HEALTH = '+' +SCORE = '@' + +SIMPLE_COLOR = 1 +ACTIVE_COLOR = 2 +WALL_COLOR = 3 +GOLD_COLOR = 4 +WEAPON_COLOR = 5 +HIGHLIGHTED_COLOR = 6 +PLAYER_COLOR = 7 +DOOR_COLOR = 8 +MONSTER_COLOR = 9 +ADD_HEALTH_COLOR = 10 +SCORE_COLOR = 11 +ACTIVE_WEAPON_COLOR = 12 + +MONSTER_DAMAGE = 1 + +P_POS = 0 +P_HEALTH = 1 +P_WEAPON = 2 +P_GOLD = 3 +P_ACTIVE_DOOR = 4 +P_SCORE = 5 + +MOVE_UP_ACTION = "KEY_UP" +MOVE_DOWN_ACTION = "KEY_DOWN" +MOVE_LEFT_ACTION = "KEY_LEFT" +MOVE_RIGHT_ACTION = "KEY_RIGHT" + +ATTACK_UP_ACTION = 'w' +ATTACK_DOWN_ACTION = 's' +ATTACK_LEFT_ACTION = 'a' +ATTACK_RIGHT_ACTION = 'd' + +NEXT_DOOR_ACTION = 'q' +PREV_DOOR_ACTION = 'e' + +DIRECTIONS = [ + [0, -1], + [0, 1], + [-1, 0], + [1, 0] +] + +EXTENDED_DIRECTIONS = DIRECTIONS + [ + [1, 1], + [1, -1], + [-1, 1], + [-1, -1] +] \ No newline at end of file