-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path400Meters.py
More file actions
128 lines (116 loc) · 4.77 KB
/
400Meters.py
File metadata and controls
128 lines (116 loc) · 4.77 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
from tkinter import *
import random
class GUIDie(Canvas):
'''6-sided Die class for GUI'''
def __init__(self,master,valueList=[1,2,3,4,5,6],colorList=['black']*6):
'''GUIDie(master,[valueList,colorList]) -> GUIDie
creates a GUI 6-sided die
valueList is the list of values (1,2,3,4,5,6 by default)
colorList is the list of colors (all black by default)'''
# create a 60x60 white canvas with a 5-pixel grooved border
Canvas.__init__(self,master,width=60,height=60,bg='white',\
bd=5,relief=GROOVE)
# store the valuelist and colorlist
self.valueList = valueList
self.colorList = colorList
# initialize the top value
self.top = 1
def get_top(self):
'''GUIDie.get_top() -> int
returns the value on the die'''
return self.valueList[self.top-1]
def roll(self):
'''GUIDie.roll()
rolls the die'''
self.top = random.randrange(1,7)
self.draw()
def draw(self):
'''GUIDie.draw()
draws the pips on the die'''
# clear old pips first
self.erase()
# location of which pips should be drawn
pipList = [[(1,1)],
[(0,0),(2,2)],
[(0,0),(1,1),(2,2)],
[(0,0),(0,2),(2,0),(2,2)],
[(0,0),(0,2),(1,1),(2,0),(2,2)],
[(0,0),(0,2),(1,0),(1,2),(2,0),(2,2)]]
for location in pipList[self.top-1]:
self.draw_pip(location,self.colorList[self.top-1])
def draw_pip(self,location,color):
'''GUIDie.draw_pip(location,color)
draws a pip at (row,col) given by location, with given color'''
(centerx,centery) = (17+20*location[1],17+20*location[0]) # center
self.create_oval(centerx-5,centery-5,centerx+5,centery+5,fill=color)
def erase(self):
'''GUIDie.erase()
erases all the pips'''
pipList = self.find_all()
for pip in pipList:
self.delete(pip)
class Decath400MFrame(Frame):
'''frame for a game of 400 Meters'''
def __init__(self,master,name):
'''Decath400MFrame(master,name) -> Decath400MFrame
creates a new 400 Meters frame
name is the name of the player'''
# set up Frame object
Frame.__init__(self,master)
self.grid()
# label for player's name
Label(self,text=name,font=('Arial',18)).grid(columnspan=3,sticky=W)
# set up score and rerolls
self.scoreLabel = Label(self,text='Score: 0',font=('Arial',18))
self.scoreLabel.grid(row=0,column=3,columnspan=2)
self.rerollLabel = Label(self,text='Rerolls: 5',font=('Arial',18))
self.rerollLabel.grid(row=0,column=5,columnspan=3,sticky=E)
# initialize game data
self.score = 0
self.rerolls = 5
self.gameround = 0
# set up dice
self.dice = []
for n in range(8):
self.dice.append(GUIDie(self,[1,2,3,4,5,-6],['black']*5+['red']))
self.dice[n].grid(row=1,column=n)
# set up buttons
self.rollButton = Button(self,text='Roll',command=self.roll)
self.rollButton.grid(row=2)
self.keepButton = Button(self,text='Keep',state=DISABLED,command=self.keep)
self.keepButton.grid(row=3)
def roll(self):
'''Decath400MFrame.roll()
handler method for the roll button click'''
# roll both dice
self.dice[self.gameround].roll()
# if this was the first roll of the round, turn on the keep button
if self.keepButton['state'] == DISABLED :
self.keepButton['state'] = ACTIVE
else: # otherwise we just spent a reroll
self.rerolls -= 1
self.rerollLabel['text'] = 'Rerolls: '+str(self.rerolls)
if (self.rerolls == 0): # no rerolls left, so turn off roll button
self.rollButton['state'] = DISABLED
def keep(self):
'''Decath400MFrame.keep()
handler method for the keep button click'''
# add dice to score and update the scoreboard
self.score += self.dice[self.gameround].get_top()
self.scoreLabel['text'] = 'Score: '+str(self.score)
self.gameround += 1 # go to next round
if self.gameround < 8: # move buttons to next pair of dice
self.rollButton.grid(row=2,column=self.gameround)
self.keepButton.grid(row=3,column=self.gameround)
self.rollButton['state'] = ACTIVE
self.keepButton['state'] = DISABLED
else: # game over
self.keepButton.grid_remove()
self.rollButton.grid_remove()
self.rerollLabel['text'] = 'Game over'
# play the game
name = input("Enter your name: ")
root = Tk()
root.title('400 Meters')
game = Decath400MFrame(root,name)
game.mainloop()