Question 1

Write a program that creates a GUI window with following objects and their functionalities:

  • Two buttons, left and right, with colors different from the window background.
  • Mouse click on the left button changes the background color of a button for a short period (to indicate mouse click on the button) and increments a counter inside the button.
  • Mouse click on the right button changes the background color of a button for a short period (to indicate mouse click on the button), closes the window and terminates the program.

Layout and the content of the GUI window after 5 clicks on the left button is shown in the diagram below. see image.

Implement function delay(d), which returns after a time delay specified by inter d.

Implement function is_button(click, rect), which returns True if point click is within rectangle rect and returns False otherwise.

Initially put text "0" into the left button, and text Quit into the right button. If the click was within left button:

  • Turn the left button to another color, for example to grey: rect.setFill('grey')
  • Use function delay(d) to wait for some time. Experiment with various time delays d to find the one, which gives a natural impression of pressing a button.
  • Turn the left button back to the original color.
  • Increment a counter and display its value on the text within the left button.

Slide 3, lecture 8.5, explains how to use object Text to display a text on a button. Use method setText(), slide 4, to change the value of the counter on the left button.

The algorithm for the main function looks like this:

Create a window (win = GraphWin(…))
Draw left button and text on the left button
Draw right button and text on the right button
while (True):
click = win.getMouse()
if the right button clicked, break to terminate program
elif the left button clicked
change color of the left button
delay(1000)
return the button to original color
increment counter on the left button
close the window

Question 2

Write a program that uses a graphical user interface (GUI) to calculate body mass index (BMI). The GUI window should have two objects for entering the height in inches and weight in pounds, a text object for displaying BMI and two buttons:

  • Click on "Calculate BMI" button reads user inputs, calculates and displays BMI
  • Click on "Quit" button closes the window and terminates the program.

Ignore invalid inputs.

Assignment 6, question 2, describes how to calculate the BMI.

The layout of the GUI window: see image.

I suggest that you start with program that you developed in Question 1 and gradually modify it to implement this program. In the first step just insert additional objects, then add operations triggered by a click on the left button and right button.

Slide 7, lecture 8.5, illustrates the use of an entry object.

Question 3

In this question, you will implement a program that keeps track of a game, described in Assignment 7, Question 5, but this time with graphical user interface. Please see Assignment 7, question 5 for the description of how to score the game.

Write a program that uses a graphical user interface (GUI) to keep track of the score in the game and displays a player who wins the game.

The GUI is supposed to contain the following objects:

  • Text objects to display "Player A" and Player B, the scores of each player and the winner.
  • One button object to click if player A wins a point, and another button to click if player B wins a point.
  • "Quit" button to close the window and terminate the program.

The buttons have to change color when clicked to mimic button click.

Layout of the GUI window: see image.

I suggest the following approach

  • Start with the program that you implemented in question 2, and gradually modify it to produce the required GUI and functionality.
  • Reuse code from Assignment 7, question 5, to implement functionality of the buttons that indicate the player who won a point.

Question 4

Write a program that creates a window with a circle that bounces from the window walls. The program terminates when the user hits "q" key.

The layout of the graphics window: see image.

Use method move(dx, dy) to move a circle from the current position dx in the x direction and dy in the y direction. Initially set dx = 1 and dy = 1. If a circle hits a vertical wall than change the horizontal direction (dx = - dx). If the circle hits a horizontal wall, then change the vertical direction (dy = - dy).

The circle will hit a wall if the center of the circle is within radius distance of the wall. Use method getCenter() to obtain a point which is the center of the circle. The following code will bounce the circle from a vertical wall:

# r is a radius, "width" is the width of the window
c = circ.getCenter()
if ((c.getX() - r) <= 0) or ((c.getX() + r) >= width):
dx = - dx

Note that you will have to slow down the movement of the circle; otherwise, the circle would move too fast. The algorithm looks like this:

create window
draw a circle
dx = 1
dy = 1
while True
move circle by dx and dy
delay(50)
change x direction if circle hits a vertical wall
change y direction if circle hits a horizontal wall
break if the user terminates the program
close the window

Terminate the program by entering "q" on the keyboard. The following code breaks the event loop to quit the program on key q:

key = win.checkKey()
if key == "q": # loop exit
break

Question 5

Write a program that creates a circle at a random point in a window. If the user clicks within the circle, it will disappear and the program draws a new circle at a random point.

If the user does not click on the circle within a specified time interval:

  • The circle disappears,
  • Appears a message "You missed the circle! Game over! Click mouse to close the window."
  • The program terminates on next mouse click.

Sample outputs: see image.

You will need function randrange(m, n) from the random library to generate a random coordinates of a circle within the windows. If r is the radius, width and height the dimensions of the window, than generate a random center point of the circle with statements:

from random import randrange
center = Point(randrange(r, width -r), randrange(r, height - r))

The algorithm:

create window
while True:
create a circle at a random point within the window
delay
# experiment with various delay intervals
if there is a mouse click:
break if the mouse click is not in the circle
break if there is no mouse click
display label for game over
wait for mouse click
close the window
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.