-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen.py
487 lines (387 loc) · 14.8 KB
/
screen.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
import sys
import tkinter
import tkinter.messagebox
from turtle import *
class ShapesMaster:
ASTEROID_BASE_SHAPE = "asteroid%d"
SHIP_SHAPE = "ship"
TORPEDO_SHAPE = "torpedo"
ASTEROIDS_TYPES = 3
ASTEROID_3_LAYOUT = ((-20, -16),(-21, 0), (-20,18),(0,27),(17,15),
(25,0),(16,-15),(0,-21))
ASTEROID_2_LAYOUT = ((-15, -10),(-16, 0), (-13,12),(0,19),(12,10),
(20,0),(12,-10),(0,-13))
ASTEROID_1_LAYOUT = ((-10,-5),(-12,0),(-8,8),(0,13),(8,6),(14,0),(12,0),(8,-6),(0,-7))
ASTEROIDS_LAYOUTS = [ASTEROID_1_LAYOUT, ASTEROID_2_LAYOUT, ASTEROID_3_LAYOUT]
SHIP_LAYOUT = ((-10,-10),(0,-5),(10,-10),(0,10))
TORPEDO_LAYOUT = ((-2,-4),(-2,4),(2,4),(2,-4))
def __init__(self, screen):
"""
This initializes the shapes controller, the screen passed is the screen
controling the game, you should not call this method anywhere in your
code.
"""
self.screen = screen
self._shapes = {}
self._updated = False
self._add_base_shapes()
def add_shape(self,name,cords,override = False):
if override or name not in self._shapes:
self._shapes[name] = cords
self.screen.register_shape(name,cords)
def _add_base_shapes(self):
for i in range(ShapesMaster.ASTEROIDS_TYPES):
self.add_shape(ShapesMaster.ASTEROID_BASE_SHAPE%(i+1), \
ShapesMaster.ASTEROIDS_LAYOUTS[i])
self.add_shape(ShapesMaster.SHIP_SHAPE, ShapesMaster.SHIP_LAYOUT)
self.add_shape(ShapesMaster.TORPEDO_SHAPE, ShapesMaster.TORPEDO_LAYOUT)
def get_shapes_dict(self):
"""
Returns a dictionary of all the shapes in the game in the format of
(name, coordinates).
You have no reason of calling this method anywhere in your code...
"""
return self._shapes
class Screen:
SCREEN_MIN_X = -500
SCREEN_MIN_Y = -500
SCREEN_MAX_X = 500
SCREEN_MAX_Y = 500
def __init__(self):
"""
This inits our graphics class.
"""
self._boundKeys = []
self._init_keys_values()
self._init_graphics()
self._bind_keys()
self._screen.listen()
self._ship = self._get_ship_obj(self._cv)
def _init_keys_values(self):
self._specialTorpedFired = 0
self._rightClicks = 0
self._leftClicks = 0
self._upClicks = 0
self._fireClicks = 0
self._endGame = False
self._lives = []
self._asteroids = {}
self._torpedos = {}
def _init_graphics(self):
self._root = tkinter.Tk()
self._root.title("Asteroids!")
self._cv = ScrolledCanvas(self._root,600,600,600,600)
self._cv.pack(side = tkinter.LEFT)
self._t = RawTurtle(self._cv)
self._screen = self._t.getscreen()
self._screen.setworldcoordinates(
Screen.SCREEN_MIN_X,
Screen.SCREEN_MIN_Y,
Screen.SCREEN_MAX_X,
Screen.SCREEN_MAX_X
)
self._shapeMaster = ShapesMaster(self._screen)
shapes = self._shapeMaster.get_shapes_dict()
frame = tkinter.Frame(self._root)
frame.pack(side = tkinter.RIGHT,fill=tkinter.BOTH)
# add scores frame
self._score_val = tkinter.StringVar()
self._score_val.set("0")
scoreTitle = tkinter.Label(frame,text="Score")
scoreTitle.pack()
scoreFrame = tkinter.Frame(frame,height=2, bd=1, \
relief=tkinter.SUNKEN)
scoreFrame.pack()
score = tkinter.Label(scoreFrame,height=2,width=20,\
textvariable=self._score_val,fg="Yellow",bg="black")
################
score.pack()
# Add Lives Frame
# livesTitle = tkinter.Label(frame, \
# text="Extra Lives Remaining")
# livesTitle.pack()
# livesFrame = tkinter.Frame(frame, \
# height=30,width=60,relief=tkinter.SUNKEN)
# livesFrame.pack()
# self._lives_canvas = ScrolledCanvas(livesFrame,150,40,150,40)
# self._lives_canvas.pack()
# livesTurtle = RawTurtle(self._lives_canvas)
# livesTurtle.ht()
# livesScreen = livesTurtle.getscreen()
# livesScreen.register_shape(ShapesMaster.SHIP_SHAPE, shapes[ShapesMaster.SHIP_SHAPE])
# Add Lives Frame
livesTitle = tkinter.Label(frame, \
text="Extra Lives Remaining")
livesTitle.pack()
livesFrame = tkinter.Frame(frame, \
height=30,width=60,relief=tkinter.SUNKEN)
livesFrame.pack()
livesCanvas = ScrolledCanvas(livesFrame,150,40,150,40)
livesCanvas.pack()
livesTurtle = RawTurtle(livesCanvas)
livesTurtle.ht()
livesScreen = livesTurtle.getscreen()
livesScreen.register_shape(ShapesMaster.SHIP_SHAPE, shapes[ShapesMaster.SHIP_SHAPE])
life1 = self._get_ship_obj(livesCanvas) # SpaceShip(livesCanvas,-35,0,0,0)
life2 = self._get_ship_obj(livesCanvas) #SpaceShip(livesCanvas,0,0,0,0)
life3 = self._get_ship_obj(livesCanvas) #SpaceShip(livesCanvas,35,0,0,0)
self._draw_object(life1,-35,0)
self._draw_object(life2,0,0)
self._draw_object(life3,35,0)
self._lives = [life1, life2, life3]
self._t.ht()
quitButton = tkinter.Button(frame, text = "Quit", command=self._handle_exit)
quitButton.pack()
self._screen.tracer(0)
def ontimer(self, func, milli):
"""
This method is used to create a repeating action in your game.
.. warning::
**You don't need to call this method, it was already called for you at the end of the main game loop.**
:param func: The function to repeat after **milli** milliseconds have passed
:type func: function
:param milli: The amount of milliseconds to wait before starting the given
function
:type milli: int
"""
self._screen.ontimer(func,milli)
def _bind_key(self, key, func):
"""
This method is to allow you to add some functionality of your own,
it allows you to bind the provided function to the desired input key.
If there is already a function bound to this key it will do nothing.
:param key: A key to bind.
:type key: str
:param func: The function to bind
:type func: function
"""
if key not in self._boundKeys:
self._screen.onkeypress(func,key)
self._boundKeys.append(key)
def _bind_keys(self):
self._bind_key("Left", self._handle_left)
self._bind_key("Right", self._handle_right)
self._bind_key("Up", self._handle_up)
self._bind_key("space", self._handle_space)
self._bind_key("q", self._handle_exit)
self._bind_key("s", self._handle_special_torpedo)
def _handle_special_torpedo(self):
self._specialTorpedFired += 1
def _handle_exit(self):
self._endGame = True
def _handle_left(self):
self._leftClicks += 1
def _handle_right(self):
self._rightClicks += 1
def _handle_up(self):
self._upClicks += 1
def _handle_space(self):
self._fireClicks += 1
def start_screen(self):
"""
This is called to start our game (grphaics-wise).
.. warning::
**This method should not be called by you**
"""
tkinter.mainloop()
def update(self):
"""
This is called to update our game (grphaics-wise).
.. warning::
**This method should not be called by you**
"""
self._screen.update()
def set_score(self, val):
"""
Sets the current game score
:param val: The game score
:type val: int
"""
self._score_val.set(str(val))
def _get_ship_obj(self, canvas):
ship = RawTurtle(canvas)
ship.shape(ShapesMaster.SHIP_SHAPE)
ship.color("purple")
return ship
def _get_asteroid_object(self, size):
asteroid = RawTurtle(self._cv)
asteroid.shape(ShapesMaster.ASTEROID_BASE_SHAPE%size)
return asteroid
def _get_torpedo_object(self):
torpedo = RawTurtle(self._cv)
torpedo.shape(ShapesMaster.TORPEDO_SHAPE)
torpedo.color("blue")
return torpedo
def _draw_object(self,obj,x,y,heading=None):
obj.penup()
obj.goto(x,y)
if heading:
obj.setheading(heading)
obj.pendown()
def remove_life(self):
"""
Remove one icon of life (starts with 3 lives)
"""
deadship = self._lives.pop()
deadship.ht()
def register_asteroid(self, asteroid, size):
"""
This is called to register a new asteroid in our system
:param asteroid: This is your asteroid object
:type asteroid: Asteroid
:param size: The size of the asteroid (this should be in [1,2,3])
:type size: int
"""
if size not in [1,2,3]:
print("Error: Wrong asteroid size: %d"%size)
sys.exit(0)
elif id(asteroid) in self._asteroids:
print("Error: Asteroid id (%d) already exists"%asteroid_id)
sys.exit(0)
asteroid_obj = self._get_asteroid_object(size)
self._asteroids[ id(asteroid) ] = asteroid_obj
def register_torpedo(self, torpedo):
"""
This is called to register a new torpedo in our system
:param asteroid: This is your torpedo object
:type asteroid: Torpedo
"""
if id(torpedo) in self._torpedos:
print("Error: Torpedo id (%d) already exists"%torpedo_id)
sys.exit(0)
torpedo_obj = self._get_torpedo_object()
self._torpedos[ id(torpedo) ] = torpedo_obj
def draw_ship(self,x,y, heading):
"""
Draw the ship at the given coordinates with the given heading
:param x: This is the X coordinate of the ship
:type x: int
:param y: This is the Y coordinate of the ship
:type y: int
:param heading: This is the heading of the ship (in degrees)
:type heading: float
"""
self._draw_object(self._ship, x, y, heading)
def draw_asteroid(self, asteroid, x, y):
"""
Draw the given asteroid on the specified (x,y) coordinates
:param asteroid: This is your asteroid object (remember to register it before)
:type asteroid: Asteroid
:param x: This is the X coordinate of the asteroid
:type x: int
:param y: This is the Y coordinate of the asteroid
:type y: int
"""
asteroid_id = id(asteroid)
if asteroid_id not in self._asteroids:
print("Error: Asteroid id (%d) not found. "%asteroid_id +
"Are you sure there is such an asteroid?")
sys.exit(0)
self._draw_object(self._asteroids[asteroid_id], x, y)
def draw_torpedo(self, torpedo, x, y, heading):
"""
Draw the given torpedo on the specified (x,y) coordinates with the given heading
:param asteroid: This is your torpedo object (remember to register it before)
:type asteroid: Torpedo
:param x: This is the X coordinate of the torpedo
:type x: int
:param y: This is the Y coordinate of the torpedo
:type y: int
:param heading: This is the heading of the torpedo
:type heading: float
"""
torpedo_id = id(torpedo)
if torpedo_id not in self._torpedos:
print("Torpedo id (%d) not found. "%torpedo_id +
"Are you sure there is such a torpedo?")
sys.exit(0)
self._draw_object(self._torpedos[torpedo_id], x, y, heading)
def _remove_object(self, obj):
obj.penup()
obj.ht()
obj.goto(Screen.SCREEN_MAX_X, Screen.SCREEN_MAX_Y*2)
def unregister_torpedo(self, torpedo):
"""
This is called to un-register an existing torpedo in our system
:param asteroid: This is your torpedo object
:type asteroid: Torpedo
"""
torpedo_id = id(torpedo)
if torpedo_id not in self._torpedos:
print("Torpedo id (%d) not found. "%torpedo_id +
"Are you sure there is such a torpedo?")
sys.exit(0)
torpedo_obj = self._torpedos[ torpedo_id ]
self._remove_object( torpedo_obj )
self._torpedos.pop( torpedo_id )
def unregister_asteroid(self, asteroid):
"""
This is called to un-register an existing asteroid in our system
:param asteroid: This is your asteroid object
:type asteroid: Asteroid
"""
asteroid_id = id(asteroid)
if asteroid_id not in self._asteroids:
print("Asteroid id (%d) not found. "%asteroid_id +
"Are you sure there is such an asteroid?")
sys.exit(0)
asteroid_obj = self._asteroids[ asteroid_id ]
self._remove_object( asteroid_obj )
self._asteroids.pop( asteroid_id )
def _clear_screen(self):
self._cv.delete('all')
def should_end(self):
"""
:returns: True if the game should end or not (if "q" was pressed or not)
"""
return self._endGame
def is_left_pressed(self):
"""
:returns: True if the left key was pressed, else False
"""
res = self._leftClicks > 0
self._leftClicks -= 1 if res else 0
return res
def is_up_pressed(self):
"""
:returns: True if the up key was pressed, else False
"""
res = self._upClicks > 0
self._upClicks -= 1 if res else 0
return res
def is_right_pressed(self):
"""
:returns: True if the right key was pressed, else False
"""
res = self._rightClicks > 0
self._rightClicks -= 1 if res else 0
return res
def is_space_pressed(self):
"""
:returns: True if the fire key was pressed, else False
"""
res = self._fireClicks > 0
self._fireClicks -= 1 if res else 0
return res
def is_special_pressed(self):
"""
:returns: True if the fire key was pressed, else False
"""
res = self._specialTorpedFired > 0
self._specialTorpedFired -= 1 if res else 0
return res
def show_message(self,title, msg):
"""
This is a method used to show messages in the game.
:param title: The title of the message box.
:type title: str
:param msg: The message to show in the message box.
:type msg: str
"""
tkinter.messagebox.showinfo(str(title), str(msg) )
def end_game(self):
"""
This ends the current game.
"""
self._root.destroy()
self._root.quit()