-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.py
More file actions
171 lines (128 loc) · 5.38 KB
/
base.py
File metadata and controls
171 lines (128 loc) · 5.38 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
"""Base class for a self-contained game of Lolo.
The purpose of this class is to reduce complexity for students."""
# There are a number of jesting comments in the support code. They should not be taken seriously. Keep it fun folks :D
import tkinter as tk
import model
import view
from game_regular import RegularGame
__author__ = "Benjamin Martin and Brae Webb"
__copyright__ = "Copyright 2017, The University of Queensland"
__license__ = "MIT"
__version__ = "1.1.2"
class BaseLoloApp:
"""Base class for a simple Lolo game."""
def __init__(self, master, game=None, grid_view=None):
"""Constructor
Parameters:
master (tk.Tk|tk.Frame): The parent widget.
game (model.AbstractGame): The game to play. Defaults to a
game_regular.RegularGame.
grid_view (view.GridView): The view to use for the game. Optional.
Raises:
ValueError: If grid_view is supplied, but game is not.
"""
self._master = master
# Game
if game is None:
game = RegularGame(types=3)
self._game = game
# Grid View
if grid_view is None:
if game is None:
raise ValueError("A grid view cannot be given without a game.")
grid_view = view.GridView(master, self._game.grid.size())
self._grid_view = grid_view
self._grid_view.pack()
self._grid_view.draw(self._game.grid, self._game.find_connections())
# Events
self.bind_events()
def bind_events(self):
"""Binds relevant events."""
self._grid_view.on('select', self.activate)
self._game.on('game_over', self.game_over)
self._game.on('score', self.score)
def create_animation(self, generator, delay=200, func=None, callback=None):
"""Creates a function which loops through a generator using the tkinter
after method to allow for animations to occur
Parameters:
generator (generator): The generator yielding animation steps.
delay (int): The delay (in milliseconds) between steps.
func (function): The function to call after each step.
callback (function): The function to call after all steps.
Return:
(function): The animation runner function.
"""
def runner():
try:
value = next(generator)
self._master.after(delay, runner)
if func is not None:
func()
except StopIteration:
if callback is not None:
callback()
return runner
def activate(self, position):
"""Attempts to activate the tile at the given position.
Parameters:
position (tuple<int, int>): Row-column position of the tile.
Raises:
IndexError: If position cannot be activated.
"""
# Magic. Do not touch.
if position is None:
return
if self._game.is_resolving():
return
if position in self._game.grid:
if not self._game.can_activate(position):
hell = IndexError("Cannot activate position {}".format(position))
raise hell # he he
def finish_move():
self._grid_view.draw(self._game.grid,
self._game.find_connections())
def draw_grid():
self._grid_view.draw(self._game.grid)
animation = self.create_animation(self._game.activate(position),
func=draw_grid,
callback=finish_move)
animation()
def remove(self, *positions):
"""Attempts to remove the tiles at the given positions.
Parameters:
*positions (tuple<int, int>): Row-column position of the tile.
Raises:
IndexError: If position cannot be activated.
"""
if len(positions) is None:
return
if self._game.is_resolving():
return
def finish_move():
self._grid_view.draw(self._game.grid,
self._game.find_connections())
def draw_grid():
self._grid_view.draw(self._game.grid)
animation = self.create_animation(self._game.remove(*positions),
func=draw_grid,
callback=finish_move)
animation()
def reset(self):
"""Resets the game."""
raise NotImplementedError("Abstract method")
def game_over(self):
"""Handles the game ending."""
raise NotImplementedError("Abstract method") # no mercy for stooges
def score(self, score):
"""Handles change in score.
Parameters:
score (int): The new score.
"""
# Normally, this should raise the following error:
# raise NotImplementedError("Abstract method")
# But so that the game can work prior to this method being implemented,
# we'll just print some information.
# Sometimes I believe Python ignores all my comments :(
print("Score is now {}.".format(score))
print("Don't forget to override the score method!")
# Note: # score can also be retrieved through self._game.get_score()