
In this lab, you will explore event-based programming through screen click events.
Create a Lab11 folder on your computer where you store CS151 projects and labs, hopefully somewhere that is backed up (e.g. in Dropbox, Google Drive, Microsoft One Drive, etc.)
Open up a Terminal and change the directory (cd) to the Lab11 folder.
Button ClassDownload a copy of the Abstract Class Game and put it in your project directory.
Inside the provided Game class, define a nested class called Button. This inner class will allow any game inheriting from Game to easily create and manage clickable UI buttons.
Button constructorCreate a constructor with the following parameters:
selfturt: a turtle.Turtle object used to draw the buttontopLeftX (int), topLeftY (int): coordinates of the button's top-left cornerbottomRightX (int), bottomRightY (int): coordinates of the button's bottom-right cornerbuttonText (str): the label displayed inside the buttonborderColor (default "Black")fill (boolean)fillColor (default "White")fontColor (default "Blue")fontSize (int, default 12) Inside the constructor:
turt, topLeftX, topLeftY, bottomRightX, bottomRightY, text, fill, fontColor, and borderColor).font assigned to a tuple ('Arial', fontSize, 'normal').self.draw() at the end of the constructor so the button automatically renders upon creation.draw methodCreate a draw method inside Button that uses self.turt to render the button box and label:
self.topLeftX, self.topLeftY) and the bottom right corner, (self.bottomRightX, self.bottomRightY). The rectangle's border color should be self.borderColor, the fill color should be self.fillColor and whether or not it should be filled should be controlled by self.fill.self.turt to write self.text, with the keyword arguments align="center" and font=self.font.At the bottom of GameABC.py, outside of the class, add a main function to test your button implementation:
Game.Button named quitButton. Choose your own location, border color, fill color, font color and font size. The text should be "Quit".screen.mainloop() to hold the window open.Run GameABC.py. Your button should appear correctly.
pointInside methodCreate a pointInside(self, pointX, pointY) method inside Button.
pointX, pointY) fall within the rectangular bounds of the button, returning True if inside and False otherwise.Inside your main function after defining quitButton and before calling mainloop.
clickFunction(mouseX, mouseY) inside of your main function that checks if quitButton.pointInside(mouseX, mouseY) is True. If so, print "Quit clicked" and close the window using turtle's bye method. screen.onclick, then call screen.listen() before calling screen.mainloop().
Run GameABC.py. Your button should appear and clicking inside it should print "Quit clicked" to the terminal and close the window.
In your screen recording demo your button working.
I suggest using the screen recording feature of Quicktime on macOS or Xbox Game Bar on Windows 10.
Upload and submit GameABC.py and your screen recording on Google Classroom.
Once you are done with each of the lab tasks, you may start on the project checkin.
© 2021 Hannen Wolfe and Oliver Layton