-
Notifications
You must be signed in to change notification settings - Fork 0
/
island_game.py
executable file
·97 lines (84 loc) · 2.82 KB
/
island_game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python3
""" A game heavily inspired by Phantasie II and rogue """
import curses
import logging
import time
import traceback
# Specific to this game
import src.maps
import src.monster
import src.log
import src.util
import src.game
def init_game(win):
""" Initialize game """
src.log.setup_logging()
game_data = src.game.Game() # container for commonly used data
game_data.set_win(win)
mem_map = src.maps.init_mem_map()
game_data.set_mem_map(mem_map)
src.maps.load_map(1, game_data) # Load map from disk
creatures = src.monster.init_player(game_data) # Add player
game_data.set_creatures(creatures)
return game_data
def exit_screen(win):
""" Quit ncurses and return to terminal """
win.keypad(0) # Restore terminal on exit
win.echo()
win.nocbreak()
win.endwin()
traceback.print_exc() # Print exception if any
def main(win):
""" main function """
curses.curs_set(0) # Rendeds an error by flake8
game_data = init_game(win)
# splash_screen?
render_all(game_data)
still_in_game = 1
while still_in_game:
time.sleep(0.1)
key = win.getch()
logging.debug("pressed key: %d", key)
if key == 81: # Q quits game
still_in_game = False
elif key == 104: # KEY_LEFT
update_game('LEFT', game_data)
elif key == 106: # KEY_DOWN
update_game('DOWN', game_data)
elif key == 107: # KEY_UP
update_game('UP', game_data)
elif key == 108: # KEY_RIGHT
update_game('RIGHT', game_data)
def update_game(key, game_data):
""" update game with user input """
win = game_data.get_win()
creatures = game_data.get_creatures()
player = creatures[0]
new_ypos = player.get_ypos()
new_xpos = player.get_xpos()
if key == 'LEFT':
new_xpos = new_xpos - 1
if src.maps.is_accessible(new_ypos, new_xpos, game_data.get_mem_map()):
player.set_xpos(new_xpos)
if key == 'RIGHT':
new_xpos = new_xpos + 1
if src.maps.is_accessible(new_ypos, new_xpos, game_data.get_mem_map()):
player.set_xpos(new_xpos)
if key == 'DOWN':
new_ypos = new_ypos + 1
if src.maps.is_accessible(new_ypos, new_xpos, game_data.get_mem_map()):
player.set_ypos(new_ypos)
if key == 'UP':
new_ypos = new_ypos - 1
if src.maps.is_accessible(new_ypos, new_xpos, game_data.get_mem_map()):
player.set_ypos(new_ypos)
win.erase()
render_all(game_data)
def render_all(game_data):
""" draw map elements and monster positions """
src.maps.mem2map(game_data)
src.maps.update_creatures(game_data)
game_data.refresh()
if __name__ == '__main__':
# The wrapper initializes curses and restores terminal on exit
curses.wrapper(main)