Question
This is the code I have completed so far. I am having a hard time getting the time function to work properly. I also am
This is the code I have completed so far. I am having a hard time getting the time function to work properly. I also am confused about how to make the second window for all the balls to appear.
Simcon graphic:
https://purdue.brightspace.com/d2l/le/dropbox/307111/360988/DownloadAttachment?fid=7209304
#import graphics and time
from graphics import *
import time
#import graphics and time
from graphics import *
import time
#define if clicked
def clicked(mouse, shape):
if not mouse:
return False
# get the x,y coordinates of the mouse click
mx, my = mouse.getX(), mouse.getY()
# get the x,y coordinates of the corners of the Rectangle
x1, y1 = shape.getP1().getX(), shape.getP1().getY()
x2, y2 = shape.getP2().getX(), shape.getP2().getY()
# return True if mouse click is within Rectangle
#or False if it is not within the Rectangle
return (x1 < mx < x2) and (y1 < my < y2)
# define a function called makepanel()
def makepanel():
# Create a 400x300 graphics wintow
# title control panel with light grey background
window = GraphWin('Control Panel', 400, 300)
# set background to light grey
window.setBackground('light grey')
# Display simulation control gif
SIMCON = Image(Point(200, 25), "SIMCON.gif")
#draw in window
SIMCON.draw(window)
# create a 100x 30 red exit rectangle along with text
Exit = Rectangle(Point(150, 265), Point(250, 295))
#fill rectangle with red
Exit.setFill('red')
#draw in window
Exit.draw(window)
#exit text
Exit_text = Text(Point(200, 280), 'Exit')
# size text
Exit_text.setSize(20)
#set text to white
Exit_text.setOutline('white')
# create bold text centered within the red rectangle "EXIT"
Exit_text.setStyle("bold")
#draw in window
Exit_text.draw(window)
# Create a 150x50 white-filled rectangle object , 50 pixelds below the SimCon window
RUNTIME = Rectangle(Point(125, 75), Point(275, 115))
#fill rectangle with white
RUNTIME.setFill('white')
#draw in window
RUNTIME.draw(window)
# Create the RUNTIME display Text object using red,bold Text centered within the white rectangle
# set the default String for the runtime display the Text to "00:00:00"
RUNTIME_text = Text(Point(200, 95), "00:00:00")
#set RUNTIME text to red
RUNTIME_text.setFill("red")
#bold text
RUNTIME_text.setStyle("bold")
# adjust size of text
RUNTIME_text.setSize(18)
#draw in window
RUNTIME_text.draw(window)
# Create three 100x30 rectangle objects pause
Pause = Rectangle(Point(150, 130), Point(250, 160))
#fill pause with yellow
Pause.setFill("yellow")
#draw in window
Pause.draw(window)
# Pause text
Pause_text = Text(Point(200, 145), "PAUSE")
#pause text color
Pause_text.setFill("black")
#pause text style
Pause_text.setStyle("bold")
#Set text size
Pause_text.setSize(18)
#draw in window
Pause_text.draw(window)
# Create three 100x30 rectangle objects run
Run = Rectangle(Point(45, 130), Point(145, 160))
#Run rectangle color
Run.setFill("green")
#draw in window
Run.draw(window)
# Run text
Run_text = Text(Point(95, 145), "RUN")
#run text color
Run_text.setFill("white")
#set run text to bold
Run_text.setStyle("bold")
#Set text size
Run_text.setSize(18)
#draw in window
Run_text.draw(window)
# Create three 100x30 rectangle objects reset
Reset = Rectangle(Point(255, 130), Point(355, 160))
#make reset rectangle blue
Reset.setFill("blue")
#draw in window
Reset.draw(window)
# reset text
Reset_text = Text(Point(305, 145), "RESET")
#reset text color
Reset_text.setFill("white")
#bold reset text
Reset_text.setStyle("bold")
#Set text size
Reset_text.setSize(18)
#draw in window
Reset_text.draw(window)
#draw pink box
pink=Rectangle(Point(85,210),Point(135,240))
#set rectangle color to pink
pink.setFill("pink")
#draw in window
pink.draw(window)
#pink text
pink_t=Text(Point(110,225),"0")
#set text color
pink_t.setStyle("bold")
#set text size
pink_t.setSize(18)
#draw in window
pink_t.draw(window)
#draw purple rectangle
purple=Rectangle(Point(145,210),Point(195,240))
#set color to purple
purple.setFill("purple")
#draw in window
purple.draw(window)
#purple text
purple_t=Text(Point(170,225),"0")
#set text color
purple_t.setStyle("bold")
#set text size
purple_t.setSize(18)
#draw in window
purple_t.draw(window)
#grey rectangle
grey=Rectangle(Point(205,210),Point(255,240))
#set color to grey
grey.setFill("grey")
#draw in window
grey.draw(window)
#grey text
grey_t=Text(Point(230,225),"0")
#set text color
grey_t.setStyle("bold")
#set text size
grey_t.setSize(18)
#draw in window
grey_t.draw(window)
#teal rectangle
teal=Rectangle(Point(265,210),Point(315,240))
#set color to teal
teal.setFill("light blue")
#draw in window
teal.draw(window)
#teal text
teal_t=Text(Point(290,225),"0")
#set text color
teal_t.setStyle("bold")
#set text size
teal_t.setSize(18)
#draw in window
teal_t.draw(window)
# Return simconwidow and red exit rectangle and RUNTIME and RUN and pause and reset
return window, Exit, RUNTIME, Pause, Run, Reset, RUNTIME_text,pink,purple.grey,teal,pink_t,purple_t,grey_t,teal_t
#def dictionary function containing 30 circles
#def dictionary():
#each circle should be created and drawn in a random location in the SCOPE window
#all circles must be fully visible within the windows boundaries
#Define a random radius for each individual circle between 8 and 30
#each circle object should be assigned a unique set of dx and dy
#set the fill color of circle to eiher pink,purple,grey,light blue
#track number of times each color is selected
#return the newly created dictionary and a list containing the number of times
#each fill color was selected
# define main()
def main():
# call makePanel() funtion assigning th eobjects returned to variables ( simconwidow and red exit rectangle and RUNTIME and RUN and pause and reset)
window, Exit, RUNTIME, Pause, Run, Reset, RUNTIME_text,pink,purple,grey,teal = makepanel()
# initialize variables
# set time to 0
#set elapsed time to 0
t = 0
elapsed_time = 0
# set program to pause
program = "Pause"
# use check mouse to look for mouse click within SimCon window
mouse = window.checkMouse()
# define while loop that repreats until a mouse click is within exit rectangle
while not clicked(mouse, Exit):
# update the current time in minutes seconds and fractions
if program == "Run":
elapsed_time += (time.time() - t)
# if RESET rectangle is clicked set time to 0
if clicked(mouse, Reset):
t = time.time()
elapsed_time = 0
# if the pause control rectangle is clicked set the program to pause
if clicked(mouse, Pause):
#pause mode
program = "Pause"
# If the run control rectangle is clicked set program to run
if clicked(mouse, Run):
#set program to run
program = "Run"
t = time.time()
# update the current RUNTIME to display current run time
# time module does not provide microseconds
# so we use the first 2 digits of the fractional part returned by time()part returned by time()
#obtaining the fractional time
fractional_part = str(int((elapsed_time - int(elapsed_time))*100))
#total run time in the 00:00:00 format
elapsed_time_str = time.strftime('%M:%S', time.gmtime(elapsed_time))+":"+fractional_part
RUNTIME_text.setText(elapsed_time_str)
# use check mouse to look for a mouse click
mouse = window.checkMouse()
# after the while loop ends close the Sim Con window and end the program
window.close()
# call main
main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started