-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx7_BubbleBobbleGame.py
144 lines (121 loc) · 5.95 KB
/
x7_BubbleBobbleGame.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#progetto eseguito da Leonardo Fiore(322767) e Federico Putamorsi(320041)
from actor import Actor, Arena
from x7_BB import Wall, Enemy, Dragon, Bubble, Score
class BubbleBobbleGame:
def __init__(self):
self._arena = Arena((512, 456))
self._scores = Score()
self._levels = ["BB_main_menu.csv", "BB_level_1.csv", "BB_level_2.csv", "BB_end.csv"]
self._config_Enemy, self._config_Dragon = "BB_config_Enemy_out_of_map.csv", "BB_config_Dragon_out_of_map.csv"
self._game_length = (len(self._levels) - 1)
self._cont_enemy, self._cont_killed = 0, 0
self._points1, self._points2 = 0, 0
self._coordinates1 = []
self._coordinates2 = []
self._enemies = []
self._walls = []
self._player = []
self._win = False
self._player1_ready, self._player2_ready = False, False
self.levels(0, True, True)
def levels(self, n, player1_ready, player2_ready): #metodo che gestisce i livelli
self._current_level = self._levels[n]
self._player1_ready, self._player2_ready = player1_ready, player2_ready
self._cont_enemy = 0
self._cont_killed = 0
if 0 < n < self._game_length: #in base al livello corrente, cambia la configurazione dei vari oggetti
self._config_Enemy, self._config_Dragon = "BB_config_Enemy.csv", "BB_config_Dragon.csv"
else:
self._config_Enemy, self._config_Dragon = "BB_config_Enemy_out_of_map.csv", "BB_config_Dragon_out_of_map.csv"
#parte di codice che ripulisce l'arena dai vari oggetti, utile nel cambio livello
for obj in self._player:
obj.remove_bubbles()
obj.remove()
for obj in self._enemies:
obj.death()
for obj in self._walls:
obj.remove()
with open(self._config_Dragon, "r") as config_dragon: #legge file configurazione giocatori
first_line = config_dragon.readline()
splitted_line = first_line.split(', ')
self._x_dragon1 = int(splitted_line[0])
self._y_dragon1 = int(splitted_line[1])
second_line = config_dragon.readline()
splitted_line = second_line.split(', ')
self._x_dragon2 = int(splitted_line[0])
self._y_dragon2 = int(splitted_line[1])
#viene deciso, in base a quale giocatore è pronto, chi è player 1 e 2
if self._player1_ready and self._player2_ready:
self._player1 = Dragon(self._arena, (self._x_dragon1, self._y_dragon1), 0)
self._player2 = Dragon(self._arena, (self._x_dragon2, self._y_dragon2), 1)
self._player.append(self._player1)
self._player.append(self._player2)
elif self._player1_ready and not(self._player2_ready):
self._player1 = Dragon(self._arena, (self._x_dragon1, self._y_dragon1), 0)
self._player.append(self._player1)
elif self._player2_ready and not(self._player1_ready):
self._player2 = Dragon(self._arena, (self._x_dragon2, self._y_dragon2), 1)
self._player.append(self._player2)
with open(self._config_Enemy, "r") as config_enemy: #legge file configurazione nemici e crea tali oggetti
for line in config_enemy:
splitted_line = line.split(', ')
self._x_enemy = int(splitted_line[0])
self._y_enemy = int(splitted_line[1])
self._enemies.append( Enemy(self._arena, (self._x_enemy, self._y_enemy)) )
self._cont_enemy += 1
with open(self._current_level, "r") as config_wall: #legge file configurazione muri e crea tali oggetti
for line in config_wall:
splitted_line = line.split(', ')
self._x_wall = int(splitted_line[0])
self._y_wall = int(splitted_line[1])
self._w_wall = int(splitted_line[2])
self._h_wall = int(splitted_line[3])
self._walls.append( Wall(self._arena, (self._x_wall, self._y_wall), (self._w_wall, self._h_wall)) )
def arena(self):
return self._arena
def player1(self):
return self._player1
def player2(self):
return self._player2
def game_won(self): #metodo che controlla se si ha vinto
if self._player1.enemy_killed():
self._cont_killed += 1
self._points1 += 500
if self._player2.enemy_killed():
self._cont_killed += 1
self._points2 += 500
if self._cont_killed == self._cont_enemy:
self._win = True
else:
self._win = False
return self._win
def game_over(self): #metodo che controlla se si ha perso
if self._player1_ready and self._player2_ready:
if self._player1.dead() and self._player1.dead():
self._lost = True
self._scores.reset(0)
self._scores.reset(1)
else:
self._lost = False
else:
if self._player1_ready:
if self._player1.dead():
self._lost = True
self._scores.reset(0)
else:
self._lost = False
elif self._player2_ready:
if self._player2.dead():
self._lost = True
self._scores.reset(1)
else:
self._lost = False
else:
self._lost = False
return self._lost
def write_scores(self): #metodo che restituisce le coordinate dei simboli dei punteggi, tramite la classe Score
self._coordinates1, self._coordinates2 = self._scores.score(self._points1, self._points2)
return self._coordinates1, self._coordinates2
def total_levels(self):
return self._game_length
BubbleBobbleGame()