forked from ccollins12000/Yahtzee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
YahtzeeGameViews.py
307 lines (251 loc) · 10.7 KB
/
YahtzeeGameViews.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
"""This module contains the main windows displayed while initializing and playing a Yahtzee game
"""
from tkinter import *
from tkinter import ttk as tk
from ScoreCardView import *
from PlayerView import *
from DieView import *
class GameSummary:
"""
This is a class for displaying the results of the game
"""
def __init__(self, master):
"""
The constructor for ScoreBoxView
Notes:
Players must be added to the view throw the add player method.
Show view must be called to display the window
Args:
master (:obj:): The master tk window the view is displayed into
"""
self._tk_master = master
self._main_frame = tk.Frame(self._tk_master)
self._players = []
self._images = []
self._avatar_images = []
self._player_names = []
self._score_cards = []
def add_player(self, avatar_file, player_name):
"""
Adds players to the view
Args:
avatar_file (str:): The path to the avatar file
player_name (str:) The name of the player
"""
self._images.append(PhotoImage(file=avatar_file))
score_frame = tk.Frame(self._main_frame)
self._players.append(
{
"Name": tk.Label(self._main_frame, text=player_name),
"Image": tk.Label(self._main_frame, image=self._images[-1]),
"Score Card Frame": score_frame,
"Score Card View": ScoreCardView(score_frame)
}
)
player = self._players[-1]
player_index = len(self._players) - 1
player["Name"].grid(row=0, column=player_index, sticky=N + S + E + W)
player["Image"].grid(row=1, column=player_index, sticky=N + S + E + W)
player["Score Card Frame"].grid(row=2, column=player_index, sticky=N + S + E + W)
def show_view(self):
"""Shows the view in the main tk window"""
self._main_frame.pack()
def hide_view(self):
"""Hides the view in the main tk window"""
self._main_frame.pack_forget()
class PlayersView:
"""
A class object that is a UI for entering all the player names
Attributes:
players (str): All the player views
main_frame (obj): The main tk window the view is displayed into
"""
def __init__(self, master, start_game_function):
"""
The constructor for the players view
Args:
master (obj): the tk master object that the player entry form is grid into
start_game_function (function) : the function that is executed when the start game button is clicked
"""
# initialize objects
master.title('Enter Player Names: ')
row_count = 6 # all avatar images in directory will get packed into rows and columns
self.main_frame = tk.Frame(master)
player_names = ["Player1", "Player2", "Player3", "Player4", "Player5", "Player6"]
self.players = [PlayerView(self.main_frame, "Avatar" + str(index) + ".png", player_names[index]) for index in range(6)]
for index, player in enumerate(self.players):
player.main_frame.grid(row=int(index/row_count), column=index%row_count)
# button for adding players
self._btn_start_game = tk.Button(self.main_frame, text="Start Game", command=start_game_function)
self._btn_start_game.grid(row=int(len(self.players)/row_count), column=len(self.players)%row_count, columnspan=row_count - len(self.players)%row_count, stick=N + S + W + E)
# add first player and pack in frame
def get_players(self):
"""
obj: Gets all the player views for the added players in the game
"""
all_players = []
for player in self.players:
if player.added:
all_players.append(player)
return all_players
def show_view(self):
"""
Packs the objects into the master TK object
"""
self.main_frame.pack()
def hide_view(self):
"""
Hides the objects into the master TK object
"""
self.main_frame.pack_forget()
class YahtzeeView:
"""
This is a class for showing a yahtzee game window
"""
def __init__(self, tk_master, roll_function, assign_function, end_turn_function):
"""
The constructor for yahtzee game play view
Args:
tk_master (obj): the tkinter master view object or window the yahtzee game will be packed into
roll_function (obj): the function that is run when the roll sdice button is clicked
assign_function (obj): the function that is executed when the assign roll button is clicked
end_turn_function (obj): the function that is executed when the end turn button is clicked
"""
self._main_frame = tk.Frame(tk_master)
master = self._main_frame
self._avatar_image = None
self._avatar_image_box = tk.Label(master, text='Yahtzee')
self._avatar_image_box.grid(row=0, column=0, rowspan = 2, sticky=N + S + E + W)
#Dice views
self._dice_frame = tk.Frame(master)
self._dice = []
die_col = 0
for die_index in range(5):
self._dice.append(DieView(self._dice_frame, 6))
self._dice[-1].view.grid(row=0, column=die_col)
die_col += 1
self._btn_roll = tk.Button(master, text='Roll Dice', command=roll_function)
self._dice_frame.grid(row=0, column=1, rowspan=2, sticky=NSEW)
self._btn_roll.grid(row=0, column=2, sticky=N + S + E + W)
self._rollsRemainingTxt = StringVar()
self._rolls_remaining = 0
self._rollsRemainingTxt.set('Rolls Remaining: ' + str(self._rolls_remaining))
self._lbl_rolls_remaining = tk.Label(master, textvariable = self._rollsRemainingTxt)
self._lbl_rolls_remaining.grid(row=1, column=2, sticky=N + S + E + W)
#Score Card
self._score_card_frame = tk.Frame(master)
self._score_card = ScoreCardView(self._score_card_frame)
self._score_card_frame.grid(row=2, column=0, sticky=W)
self._btn_assign_roll = tk.Button(master, text="Assign Roll", command=assign_function)
self._btn_assign_roll.grid(row=3, column=0, sticky=N + S + E + W)
self._btn_end_turn = tk.Button(master, text="End Turn", command=end_turn_function)
self._btn_end_turn.grid(row=4, column=0, sticky=N + S + E + W)
#Game stats/control properties
self._game_stats_frame = tk.Frame(master)
self._game_stats_frame.grid(row=2, column=1, rowspan=2, columnspan=2)
self._can_roll = True
self._player_name = StringVar()
#self._lbl_player_name = tk.Label(self._game_stats_frame, textvariable= self._player_name)
#self._lbl_player_name.grid(row=0,column=0, sticky=N + S + E + W)
self._instructions = StringVar()
self._lbl_instructions = tk.Label(self._game_stats_frame, textvariable= self._instructions)
self._lbl_instructions.grid(row=0, column=0, sticky=N + S + E + W)
def lock_commands(self):
"""Disables the command buttons from being able to be clicked"""
self._btn_assign_roll.config(state=DISABLED)
self._btn_end_turn.config(state=DISABLED)
self._btn_roll.config(state=DISABLED)
def unlock_commands(self):
"""Enables the command buttons so they can be clicked"""
self._btn_assign_roll.config(state=NORMAL)
self._btn_end_turn.config(state=NORMAL)
self._btn_roll.config(state=NORMAL)
@property
def instructions(self):
"""str: The instructions displayed on the game view"""
return self._instructions.get()
@instructions.setter
def instructions(self, value):
self._instructions.set(value)
@property
def player_name(self):
"""str: The name of the current player"""
return self.player_name.get()
@player_name.setter
def player_name(self, name):
self._player_name.set(name + ' may now take their turn.')
@property
def avatar_image(self):
"""str: The file path to the avatar file of the current player"""
return self._avatar_image
@avatar_image.setter
def avatar_image(self, image_file):
self._avatar_image = PhotoImage(file=image_file)
self._avatar_image_box.configure(image=self.avatar_image)
def update_die(self, die_index, value):
"""
Update the value displayed on a die at a given index in the game view
Args:
die_index (int): The index of the die to update
value (int): The value to display on the die
"""
self._dice[die_index].last_roll = value
def die_selected(self, die_index):
"""
Get whether or not a die is selected
Args:
die_index (int): The index of the die to retrieve the selected status
"""
return self._dice[die_index].selected
def update_die_selected(self, die_index, selected):
"""
Update whether or not a die is selected
Args:
die_index (int): The index of the die to update
selected (bool): Whether or not the die is selected
"""
self._dice[die_index].selected = selected
def update_box(self, box_name, points, enabled):
"""
Update a box on the score card (points/enabled) given a certain box nam e
Args:
box_name (str): The name of the box to update
points (int): The number of points to display in the box
enabled (bool): Whether or not the box can be selected/is enabled
"""
self._score_card.assign_points(box_name, points)
self._score_card.box_enabled(box_name, enabled)
@property
def selected_box(self):
"""Get which score box is selected"""
return self._score_card.selection
@property
def rolls_remaining(self):
"""Get or set the number of rolls remaining displayed in the yahtzee game view"""
return self._rolls_remaining
@rolls_remaining.setter
def rolls_remaining(self, value):
self._rolls_remaining = value
self._rollsRemainingTxt.set('Rolls Remaining: ' + str(self._rolls_remaining))
def show_view(self):
"""
Packs the objects into the master TK object
"""
self._main_frame.pack()
def hide_view(self):
"""
Hides the objects into the master TK object
"""
self._main_frame.pack_forget()
def main():
"""Preview of the Game Views"""
yahtzee_tk = Tk()
y = GameSummary(yahtzee_tk)
y.add_player("Avatar0.png", "Player1")
y.add_player("Avatar1.png", "Player2")
y.add_player("Avatar2.png", "Player3")
y.add_player("Avatar3.png", "Player4")
y.show_view()
yahtzee_tk.mainloop()
if __name__ == "__main__":
main()