-
Notifications
You must be signed in to change notification settings - Fork 0
/
Brontis.py
138 lines (111 loc) · 4.13 KB
/
Brontis.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from os import walk
from Player import Player
from time import sleep
from random import randint
from json import loads
class Brontis(Player):
# constructor init
def __init__(self, game, x, y, speed, sprite, player):
super().__init__(x, y, speed, sprite)
self.x: int = x
self.y: int = y
self.game = game
self.player = player
self.speed: int = speed
self.coords: list = [
[265, 0, 0],
[0, 240, 0],
[-210, 0, 0],
[130, -80, 0],
[80, 0, 0],
[0, 20, 0],
[70, 0, 0],
[0, -150, 0],
[0, 0, 300],
[70, 0, 0],
[0, -30, 0],
[-70, 0, 0],
[0, 180, 0],
[-50, 0, 0],
[-50, -50, 0],
[-130, 0, 0],
[0, -30, 0],
[130, 0, 0],
[0, 30, 0],
[0, -60, 0],
[30, 0, 0],
[0, -60, 0],
[-265, 0, 0],
[0, 0, 600]
]
self.questions: str = loads(open("questions.json", "r").read())
self.floor: list = [[0, 0] for x in self.coords]
self.current_walk_step: int = 0
self.pause: bool = False
self.pause_current_time: int = 0
self.adapt_limits()
# adapt coors in function of the speed, to prevent coords from being multiplied
def adapt_limits(self) -> None:
new_limits: list = []
for limit_arr in self.coords:
new_limit_array = []
for limit in limit_arr:
limit /= self.speed
new_limit_array.append(limit)
new_limits.append(new_limit_array)
self.coords = new_limits
# pick a question among the questions.json file
def pick_a_random_question(self) -> dict:
i: int = randint(0, len(self.questions) - 1)
return self.questions[i]
# trigger game states and functions to handle the rest of the questions part
def ask_question(self) -> None:
question = self.pick_a_random_question()
self.game.question = None
self.game.is_answering = True
self.pause = True
self.game.question = question
self.game.handle_answering(question)
# check if brontis is next to the player
def is_near_of_the_player(self) -> None:
radius: int = 50
[playerX, playerY] = self.player.position
[brontisX, brontisY] = self.position
if (playerX - brontisX < radius and playerX - brontisX > -radius) and (playerY - brontisY < radius and playerY - brontisY > -radius):
if not self.game.is_answering and not self.player.protected:
self.ask_question()
# brontis moves
def walk(self) -> None:
# the third value from the coords means there is a break time to move to next coord
if self.coords[self.current_walk_step][2] > 0:
if self.pause_current_time < self.coords[self.current_walk_step][2]:
self.pause_current_time += 1
self.pause = True
else:
if self.game.is_answering == False:
self.pause = False
self.pause_current_time = 0
if not self.pause:
# we use the abs function to handle negative values to be intepreted as a move_left() or a move_down() but without interfering with the floor which is always increasing
if abs(self.floor[self.current_walk_step][0]) < abs(self.coords[self.current_walk_step][0]) or abs(self.floor[self.current_walk_step][1]) < abs(self.coords[self.current_walk_step][1]):
if abs(self.floor[self.current_walk_step][0]) < abs(self.coords[self.current_walk_step][0]):
if self.coords[self.current_walk_step][0] > 0:
super().move_right()
self.floor[self.current_walk_step][0] += 1
else:
super().move_left()
self.floor[self.current_walk_step][0] -= 1
if abs(self.floor[self.current_walk_step][1]) < abs(self.coords[self.current_walk_step][1]):
if self.coords[self.current_walk_step][1] > 0:
super().move_up()
self.floor[self.current_walk_step][1] += 1
else:
super().move_down()
self.floor[self.current_walk_step][1] -= 1
else:
if self.current_walk_step < len(self.coords) - 1:
self.current_walk_step += 1
else:
self.current_walk_step = 0
self.floor = [[0, 0] for x in self.coords]
self.is_near_of_the_player()