/
breakback.py
230 lines (191 loc) · 8.04 KB
/
breakback.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import os
import res
import cal
import pygame
import game_map
from arrow import Arrow
from game_map import GameMap
from player_panel import PowerBar
from player_panel import PlayerPanel
class ArrowInGame(object):
def __init__( self, arrow ):
self.arrow = arrow
def set_grid_position( self, current_position ):
self.current_grid_position = current_position
def game_map_turn_correct( arrow, play_map, direction ):
# Is arrow near grid center that need to check current grid bound
arrow_grid = play_map.detect_grid( arrow.position )
grid_position = play_map.grid_center( arrow_grid )
if ( cal.distance(arrow.position, grid_position) < res.distance_grid_turn*arrow.speed ):
if ( direction!=arrow.direction and direction!=cal.reverse(arrow.direction) ):
if ( game_map.direction_to_wall(direction) &
play_map.grids[arrow_grid[1]*play_map.map_size[0]+arrow_grid[0]] == 0):
arrow.set_direction( direction )
arrow.set_position( grid_position )
def game_map_bump_correct( arrow, play_map ):
# Is arrow near grid center that need to check current grid bound
arrow_grid = play_map.detect_grid( arrow.position )
grid_position = play_map.grid_center( arrow_grid )
if ( cal.unit( cal.vector( grid_position, arrow.position ) ) == arrow.direction ):
# convert vector to wall definition
arrow_bump_wall = game_map.direction_to_wall( arrow.direction )
# arrow bumps wall
if ( arrow_bump_wall &
play_map.grids[arrow_grid[1]*play_map.map_size[0]+arrow_grid[0]] != 0 ):
arrow.set_position( grid_position )
def game_arrow_map_event( arrow_list, play_map ):
for gameArrow in arrow_list:
current_grid_position = play_map.detect_grid( gameArrow.arrow.position )
# check if arrow moves to another grid
if (gameArrow.current_grid_position != current_grid_position):
# check if there is something should happen as arrow enters this grid
# get a kit?
for kitinmap in list(play_map.kit_list):
if ( kitinmap.grid_position == current_grid_position ):
gameArrow.arrow.kit_save( kitinmap.kit )
play_map.kit_list.remove( kitinmap )
gameArrow.set_grid_position( current_grid_position )
def game_arrow_encounter(arrow_p1, arrow_p2):
# arrow encounter detect and handle
if( cal.distance(arrow_p1.position, arrow_p2.position) < res.distance_collision):
# pygame.draw.rect(gameDisplay, res.color_obj, [0,0,30,30], 5)
if(arrow_p1.direction == arrow_p2.direction):
return res.game_brokeback
elif(arrow_p1.direction == cal.reverse(arrow_p2.direction)):
arrow_p1.set_direction( cal.reverse(arrow_p1.direction) )
arrow_p1.position = (arrow_p1.position[0]+arrow_p1.direction[0]*res.distance_collision,
arrow_p1.position[1]+arrow_p1.direction[1]*res.distance_collision)
arrow_p2.set_direction( cal.reverse(arrow_p2.direction) )
arrow_p2.position = (arrow_p2.position[0] + arrow_p2.direction[0]*res.distance_collision,
arrow_p2.position[1] + arrow_p2.direction[1]*res.distance_collision)
return res.game_collision
else:
# if p1 bumps p2
if ( arrow_p1.direction == cal.unit( cal.vector(arrow_p1.position, arrow_p2.position) ) ):
arrow_p1.set_direction( cal.reverse(arrow_p1.direction) )
else:
arrow_p2.set_direction( cal.reverse(arrow_p2.direction) )
return res.game_side_collision
def scene_start( gameDisplay ):
# message
surface_msg = res.font_msg.render("Press [space] to start", 1, res.color_msg)
size_msg = surface_msg.get_size()
# background image
surface_start = pygame.image.load( os.path.join(res.dir_res, res.name_start) )
# render
gameDisplay.fill(res.color_background)
gameDisplay.blit(surface_start, (0,0))
gameDisplay.blit(surface_msg,
( int((res.size_display[0]-size_msg[0])/2), int(res.size_display[1]/3*2) ) )
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
return True
res.clock.tick( res.tick_game )
def scene_over( gameDisplay ):
surface_msg = res.font_msg.render("Press [space] to play again", 1, res.color_msg)
size_msg = surface_msg.get_size()
gameDisplay.fill(res.color_background)
gameDisplay.blit(surface_msg,
( int((res.size_display[0]-size_msg[0])/2), int(res.size_display[1]/3*2) ) )
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return True
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
return False
res.clock.tick( res.tick_game )
def scnene_game( gameDisplay ):
# create game object
# map
play_map = GameMap(res.position_game_map, res.size_game_map, res.color_map,
res.size_grid, res.game_map_grids,
res.width_game_map_wall, res.color_wall)
play_map.kit_reset( res.kit_max, res.kit_freq )
play_map.kit_gen()
play_map.kit_gen()
# player arrow
arrow_p1 = Arrow(res.size_arrow, play_map.grid_center(res.grid_position_start_p1), (1,0), 2, res.speed_max, res.color_p1)
arrow_p2 = Arrow(res.size_arrow, play_map.grid_center(res.grid_position_start_p2), (-1,0), 2, res.speed_max, res.color_p2)
# player panel
bar_p1 = PowerBar( res.position_panel_p1+res.size_panel_p1, res.speed_max, res.color_p1, (0,0,0) )
bar_p2 = PowerBar( res.position_panel_p2+res.size_panel_p2, res.speed_max, res.color_p2, (0,0,0) )
panel_p1 = PlayerPanel( res.position_panel_p1, res.size_panel_p1, res.color_p1 )
panel_p1.set_item_slot( res.slot_layout )
panel_p1.set_player( arrow_p1 )
panel_p1.set_power_bar( bar_p1 )
panel_p2 = PlayerPanel( res.position_panel_p2, res.size_panel_p2, res.color_p2 )
panel_p2.set_item_slot( res.slot_layout )
panel_p2.set_player( arrow_p2 )
panel_p2.set_power_bar( bar_p2 )
# add object to game
gameArrowList = []
gameArrowList.append( ArrowInGame(arrow_p1) )
gameArrowList.append( ArrowInGame(arrow_p2) )
for gameArrow in gameArrowList:
gameArrow.set_grid_position( play_map.detect_grid(gameArrow.arrow.position) )
# register key done event
key_control = { res.control_p1['right']:lambda :game_map_turn_correct(arrow_p1, play_map, (1,0)),
res.control_p1['left']: lambda :game_map_turn_correct(arrow_p1, play_map, (-1,0)),
res.control_p1['up']: lambda :game_map_turn_correct(arrow_p1, play_map, (0,-1)),
res.control_p1['down']: lambda :game_map_turn_correct(arrow_p1, play_map, (0,1)),
res.control_p1['kit']: lambda :arrow_p1.kit_invoke(),
res.control_p2['right']:lambda :game_map_turn_correct(arrow_p2, play_map, (1,0)),
res.control_p2['left']: lambda :game_map_turn_correct(arrow_p2, play_map, (-1,0)),
res.control_p2['up']: lambda :game_map_turn_correct(arrow_p2, play_map, (0,-1)),
res.control_p2['down']: lambda :game_map_turn_correct(arrow_p2, play_map, (0,1)),
res.control_p2['kit']: lambda :arrow_p2.kit_invoke()
}
game_over = False
while not game_over:
# handle event
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
if event.type == pygame.KEYDOWN:
for key in key_control:
if key == event.key:
key_control[key]()
# print(event)
# raise game event
if (game_arrow_encounter(arrow_p1, arrow_p2) == res.game_brokeback):
return True
game_arrow_map_event( gameArrowList, play_map )
# progress game
play_map.kit_progress()
arrow_p1.progress()
game_map_bump_correct( arrow_p1, play_map )
arrow_p2.progress()
game_map_bump_correct( arrow_p2, play_map )
# render
gameDisplay.fill(res.color_background)
play_map.draw( gameDisplay )
panel_p1.draw( gameDisplay )
panel_p2.draw( gameDisplay )
arrow_p1.draw( gameDisplay )
arrow_p2.draw( gameDisplay )
pygame.display.update()
res.clock.tick( res.tick_game )
def game_loop( gameDisplay ):
if( not scene_start( gameDisplay ) ):
return
while True:
if( not scnene_game( gameDisplay ) ):
break
if( scene_over( gameDisplay ) ):
break
def main():
# initialize pygame
pygame.init()
gameDisplay = pygame.display.set_mode( res.size_display )
pygame.display.set_caption( res.text_caption )
pygame.display.set_icon( res.surface_icon )
game_loop( gameDisplay )
pygame.quit()
quit()
if __name__ == '__main__':
main()