-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.py
83 lines (68 loc) · 2.56 KB
/
input.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
import getch
from display import Display
from map import Map
from utils import print_and_wait
class Input:
def __init__(self, game_map: Map, display: Display) -> None:
self._game_map = game_map
self._display = display
@property
def game_map(self):
return self._game_map
@property
def display(self):
return self._display
def input_direction(self) -> None:
print("waiting for input...")
direction_mapping = {
"w": (-1, 0), # Move up
"a": (0, -1), # Move left
"s": (1, 0), # Move down
"d": (0, 1), # Move right
"h": None, # Hunt (handled separately)
"p": None, # Drink potion (handled separately)
"q": None, # Quit (handled separately)
}
direction = getch.getch()
while direction not in direction_mapping:
print_and_wait(
"Invalid input. Use W/A/S/D to move, P to drink potions, H to hunt, or Q to quit."
)
direction = getch.getch()
if direction == "q":
print_and_wait("Goodbye!")
exit()
if direction == "p":
self.game_map.player.drink_potion()
self._display.render_screen()
self.input_direction()
elif direction == "h":
self.handle_hunt()
else:
self.handle_movement(direction_mapping[direction])
def handle_hunt(self) -> None:
if self.game_map.map_data[self.game_map.player.y][
self.game_map.player.x
].hunted:
print_and_wait("You have already hunted here.")
return
self.game_map.player.hunt(self.game_map)
self.game_map.map_data[self.game_map.player.y][
self.game_map.player.x
].hunted = True
def handle_movement(self, movement: tuple[int, int]) -> None:
new_y, new_x = (
self.game_map.player.y + movement[0],
self.game_map.player.x + movement[1],
)
if 0 <= new_y < self.game_map.height and 0 <= new_x < self.game_map.width:
current_biome = self.game_map.map_data[self.game_map.player.y][
self.game_map.player.x
]
if self.game_map.map_data[new_y][new_x].walkable:
self.game_map.player.x, self.game_map.player.y = new_x, new_y
self.game_map.player.stamina -= current_biome.stamina_cost
else:
print_and_wait("You can't move there.")
else:
print_and_wait("Invalid movement.")