forked from sd18spring/InteractiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtron-hn01.py
More file actions
241 lines (198 loc) · 7.56 KB
/
tron-hn01.py
File metadata and controls
241 lines (198 loc) · 7.56 KB
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
231
232
233
234
235
236
237
238
239
240
241
"Naomi's Working Space"
import pygame
from pygame.locals import*
import time
class PyGameWindowView(object):
def __init__(self,model,width=640,height=480):
self.model = model
size = (width,height)
self.model.screen = pygame.display.set_mode(size)
def _init_draw(self):
self.model.screen.fill((105,105,105))
self.model.cells = {}
cell_size = (self.model.cell_length, self.model.cell_length)
for i in range(self.model.height):
for j in range(self.model.width):
cell_coord = (i*self.model.cell_length,j*self.model.cell_length)
self.model.cells[(i,j)] = Cellview(self.model.screen,cell_coord,cell_size)
all_cells = self.model.cells.values()
for cell in all_cells:
cell.draw()
def draw(self):
self.model._draw_players()
pygame.display.update()
class TronModelView(object):
def __init__(self,cell_length=10,width=640,height=480):
pygame.init()
size = (width,height)
self.screen = pygame.display.set_mode(size)
self.width = width
self.height = height
self.cell_length = cell_length
self.cell_lst = []
self.player_paths = []
self.player1 = Player(self.screen,10,(self.width/2+100),(self.height/2),"r",(255,140,0))
self.player2 = Player(self.screen,10,(self.width/2-100),(self.height/2),"l",(0,255,0))
self.cells_loc = {}
for i in range(self.height//cell_length):
for j in range(self.width//cell_length):
cell_coords = (i*self.cell_length, j*self.cell_length)
self.cell_lst.append(Cell(cell_coords, cell_length))
def in_cell(self):
for cell in self.cell_lst:
if self.player1.x in cell.xrange and self.player1.y in cell.yrange:
self.player1.current_cell = cell
break
for cell in self.cell_lst:
if self.player2.x in cell.xrange and self.player2.y in cell.yrange:
self.player2.current_cell = cell
break
def _draw_players(self):
self.player1.draw()
self.player2.draw()
def update(self):
self.player1.update()
self.player2.update()
if self.player1.crash():
self.end_game("PLAYER 2 ")
self.player1.dir = "None"
self.player2.dir = "None"
if self.player2.crash():
self.end_game("PLAYER 1 ")
self.player1.dir = "None"
self.player2.dir = "None"
"""last_seen_p1 = self.player1.current_cell
last_seen_p2 = self.player2.current_cell
self.in_cell()
if self.player1.current_cell != last_seen_p1:
self.player_paths.append(last_seen_p1)
if self.player2.current_cell != last_seen_p2:
self.player_paths.append(last_seen_p2)"""
if self.player1.current_cell in self.player_paths:
self.end_game("PLAYER 2 ")
self.player1.dir = "None"
self.player2.dir = "None"
if self.player2.current_cell in self.player_paths:
self.end_game("PLAYER 1 ")
self.player1.dir = "None"
self.player2.dir = "None"
def end_game(self,player):
pygame.display.set_caption(player + "WINS!")
class Cell(object):
def __init__(self,coords, cell_length):
self.xmin = coords[0]
self.ymin = coords[1]
self.xmax = coords[0] + cell_length
self.ymax = coords[1] + cell_length
self.xrange = range(self.xmin, self.xmax)
self.yrange = range(self.ymin, self.ymax)
class Cellview(object):
def __init__(self, draw_screen, coordinates, cell_length):
self.draw_screen = draw_screen
self.coordinates = coordinates
self.side_length = cell_length
self.color = (0, 0, 0)
def draw(self):
line_width = 1
rect = pygame.Rect(self.coordinates, self.side_length)
pygame.draw.rect(self.draw_screen, self.color, rect, line_width)
class Player(object):
def __init__(self, draw_screen, dimension, start_posx, start_posy, direction, color=(255,255,255)):
self.draw_screen = draw_screen
self.width = dimension
self.height = dimension
self.x = start_posx
self.y = start_posy
self.vx = 0
self.vy = 0
self.dir = direction
self.color = color
self.current_cell = None
def draw(self):
line_width = .5
pygame.draw.rect(self.draw_screen,self.color,pygame.Rect(self.x,self.y,self.width,self.height))
def update(self):
if self.dir == "r":
self.vx = 10
self.vy = 0
elif self.dir == "l":
self.vx = -10
self.vy = 0
elif self.dir == "u":
self.vx = 0
self.vy = -10
elif self.dir == "d":
self.vx = 0
self.vy = 10
elif self.dir == "None":
self.vx = 0
self.vy = 0
self.x += self.vx
self.y += self.vy
def crash(self):
if self.x == 640 or self.x == -10:
return True
if self.y == -10 or self.y == 480:
return True
return False
class PlayerPath(object):
def __init__(self,model):
self.model = model
self.model.cells_loc = {}
for i in range(self.model.height):
for j in range(self.model.width):
cell_coords = (i*self.model.cell_length,j*self.model.cell_length)
self.model.cells_loc[(i,j)] = Cell(self.model.screen,cell_coords,cell_size)
self.model.hit_cells = [(self.model.player1.x, self.model.player1.y), (self.model.player2.x, self.model.player2.y)]
def update(self):
if (self.model.player1.x, self.model.player1.y) not in self.hit_cells:
self.hit_cells.append((player1.x, player1.y))
if (self.model.player2.x, self.model.player2.y) not in self.hit_cells:
self.hit_cells.append
class KeyControl(object):
def __init__(self, model):
self.model = model
def handle_event(self, event):
if event.type != KEYDOWN:
return
if event.key == pygame.K_LEFT:
if self.model.player1.dir != "r":
self.model.player1.dir = "l"
if event.key == pygame.K_RIGHT:
if self.model.player1.dir != "l":
self.model.player1.dir = "r"
if event.key == pygame.K_DOWN:
if self.model.player1.dir != "u":
self.model.player1.dir = "d"
if event.key == pygame.K_UP:
if self.model.player1.dir != "d":
self.model.player1.dir = "u"
if event.key ==pygame.K_a:
if self.model.player2.dir != "r":
self.model.player2.dir = "l"
if event.key == pygame.K_d:
if self.model.player2.dir != "l":
self.model.player2.dir = "r"
if event.key == pygame.K_s:
if self.model.player2.dir != "u":
self.model.player2.dir = "d"
if event.key == pygame.K_w:
if self.model.player2.dir != "d":
self.model.player2.dir = "u"
if __name__ == '__main__':
pygame.init()
model = TronModelView()
view = PyGameWindowView(model)
view._init_draw()
controller = KeyControl(model)
running = True
while running:
view._init_draw
for event in pygame.event.get():
if event.type == QUIT:
running = False
controller.handle_event(event)
model.update()
view.draw()
time.sleep(.2)
pygame.quit()