As a side project, I\'m trying to create a mashup of Tic Tac Toe and Rock Paper
ID: 3907669 • Letter: A
Question
As a side project, I'm trying to create a mashup of Tic Tac Toe and Rock Paper Scissors. But I'm stuck. I can't even get the buttons to show up. The idea is, the program assigns a piece to the player (Rock, Paper, or Scissors) and the player puts the piece on the grid. Winner is determined by hierarchy Rock, Paper, Scissors vertically, horizontally, or diagonally. Scissors-Scissors-Paper, Paper-Paper-Rock, Rock-Rock-Scissors can also win. Here's the code I have so far.
#RPSMain.py
#Perry Dockins
#import graphics library
from graphics import *
from button import *
class RicPacSoeView:
def __init__(self):
#create graphics window
self.win=GraphWin("grid1", 800,1000)
header= Text(Point(400,80),'''Welcome to Ric-Pac-Soe! A two person game
Each player will be assigned a piece. The player decides where to place the piece in the grid
To win, you must get three squares lined up horizontally, vertically, or diagonally, up or down,
according to Rock, Paper, Scissors hierarchy. For example, S-P-R, P-P-R, S-S-P, R-R-S. ''')
header.setFace("arial")
header.setStyle("bold")
header.setSize(12)
header.draw(self.win)
#draw vertical lines
line1= Line(Point(300,200), Point(300,800)).draw(self.win)
line1.setWidth(3)
line2=Line(Point(500,200), Point(500,800)).draw(self.win)
line2.setWidth(3)
#draw horizontal lines
line3=Line( Point(100,400), Point(700,400)).draw(self.win)
line3.setWidth(3)
line4=Line( Point(100,600), Point(700,600)).draw(self.win)
line4.setWidth(3)
def displayImage(self, pt, fileName):
image = Image(pt, fileName)
print(image.getWidth())
print(image.getHeight())
print(image.getAnchor().getX(), image.getAnchor().getY())
image.draw(self.win)
def widgets(self, win, center, width, height, label):
but1 = Button(self.win,Point(100,750),100,50,'Start')
but1.setFill('green')
but1.draw(win)
but1.activate()
but2 = Button(self.win,Point(400,750),100,50,'Reset').draw(self.win)
but2.setFill('yellow')
but2.activate()
but3 = Button(self.win,Point(650,750),100,50,'Quit').draw(self.win)
but3.setFill('red')
but3.activate()
'''
class buttons:
def __init__(self, win, center, width, height, label):
#Creates rectangular Start Button
self.start.activate()
self.start = Button(win, Point(50,750), 150, 50, "Start")
#Creates rectangular Restart Button
self.restart.activate()
self.restart = Button(win, Point(400, 750), 150, 50, "Restart")
#Creates rectangular Quit Button
self.quit.Button.activate()
quitButton = Button(win, Point(400,750), 2, 1, "Quit")
'''
#RicPacSoe()
def unitTest():
view = RicPacSoeView()
#test paint image
view.displayImage(Point(200,300), "p1Paper.gif")
if __name__ == '__main__':
unitTest()
# button.py
from graphics import *
class Button:
"""A button is a labeled rectangle in a window.
It is activated or deactivated with the activate()
and deactivate() methods. The clicked(p) method
returns true if the button is active and p is inside it."""
def __init__(self, win, center, width, height, label):
""" Creates a rectangular button, eg:
qb = Button(myWin, centerPoint, width, height, 'Quit') """
w,h = width/2.0, height/2.0
x,y = center.getX(), center.getY()
self.xmax, self.xmin = x+w, x-w
self.ymax, self.ymin = y+h, y-h
p1 = Point(self.xmin, self.ymin)
p2 = Point(self.xmax, self.ymax)
self.rect = Rectangle(p1,p2)
self.rect.setFill('yellow')
self.rect.draw(win)
self.label = Text(center, label)
self.label.draw(win)
self.deactivate()
def clicked(self, p):
"Returns true if button active and p is inside"
return (self.active and
self.xmin <= p.getX() <= self.xmax and
self.ymin <= p.getY() <= self.ymax)
def getLabel(self):
"Returns the label string of this button."
return self.label.getText()
def activate(self):
"Sets this button to 'active'."
self.label.setFill('black')
self.rect.setWidth(2)
self.active = True
def deactivate(self):
"Sets this button to 'inactive'."
self.label.setFill('darkgrey')
self.rect.setWidth(1)
self.active = False
Explanation / Answer
#RPSMain.py
#Perry Dockins
#import graphics library
from graphics import *
from button import *
class RicPacSoeView:
def __init__(self):
#create graphics window
self.win=GraphWin("grid1", 800,1000)
header= Text(Point(400,80),'''Welcome to Ric-Pac-Soe! A two person game
Each player will be assigned a piece. The player decides where to place the piece in the grid
To win, you must get three squares lined up horizontally, vertically, or diagonally, up or down,
according to Rock, Paper, Scissors hierarchy. For example, S-P-R, P-P-R, S-S-P, R-R-S. ''')
header.setFace("arial")
header.setStyle("bold")
header.setSize(12)
header.draw(self.win)
#draw vertical lines
line1= Line(Point(300,200), Point(300,800)).draw(self.win)
line1.setWidth(3)
line2=Line(Point(500,200), Point(500,800)).draw(self.win)
line2.setWidth(3)
#draw horizontal lines
line3=Line( Point(100,400), Point(700,400)).draw(self.win)
line3.setWidth(3)
line4=Line( Point(100,600), Point(700,600)).draw(self.win)
line4.setWidth(3)
def displayImage(self, pt, fileName):
image = Image(pt, fileName)
print(image.getWidth())
print(image.getHeight())
print(image.getAnchor().getX(), image.getAnchor().getY())
image.draw(self.win)
def widgets(self, win, center, width, height, label):
but1 = Button(self.win,Point(100,750),100,50,'Start')
but1.setFill('green')
but1.draw(win)
but1.activate()
but2 = Button(self.win,Point(400,750),100,50,'Reset').draw(self.win)
but2.setFill('yellow')
but2.activate()
but3 = Button(self.win,Point(650,750),100,50,'Quit').draw(self.win)
but3.setFill('red')
but3.activate()
'''
class buttons:
def __init__(self, win, center, width, height, label):
#Creates rectangular Start Button
self.start.activate()
self.start = Button(win, Point(50,750), 150, 50, "Start")
#Creates rectangular Restart Button
self.restart.activate()
self.restart = Button(win, Point(400, 750), 150, 50, "Restart")
#Creates rectangular Quit Button
self.quit.Button.activate()
quitButton = Button(win, Point(400,750), 2, 1, "Quit")
'''
#RicPacSoe()
def unitTest():
view = RicPacSoeView()
#test paint image
view.displayImage(Point(200,300), "p1Paper.gif")
if __name__ == '__main__':
unitTest()
# button.py
from graphics import *
class Button:
"""A button is a labeled rectangle in a window.
It is activated or deactivated with the activate()
and deactivate() methods. The clicked(p) method
returns true if the button is active and p is inside it."""
def __init__(self, win, center, width, height, label):
""" Creates a rectangular button, eg:
qb = Button(myWin, centerPoint, width, height, 'Quit') """
w,h = width/2.0, height/2.0
x,y = center.getX(), center.getY()
self.xmax, self.xmin = x+w, x-w
self.ymax, self.ymin = y+h, y-h
p1 = Point(self.xmin, self.ymin)
p2 = Point(self.xmax, self.ymax)
self.rect = Rectangle(p1,p2)
self.rect.setFill('yellow')
self.rect.draw(win)
self.label = Text(center, label)
self.label.draw(win)
self.deactivate()
def clicked(self, p):
"Returns true if button active and p is inside"
return (self.active and
self.xmin <= p.getX() <= self.xmax and
self.ymin <= p.getY() <= self.ymax)
def getLabel(self):
"Returns the label string of this button."
return self.label.getText()
def activate(self):
"Sets this button to 'active'."
self.label.setFill('black')
self.rect.setWidth(2)
self.active = True
def deactivate(self):
"Sets this button to 'inactive'."
self.label.setFill('darkgrey')
self.rect.setWidth(1)
self.active = False
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.