Assignment 2 (1)
Assignment 2 (1)
Assignment #2
Graphic Interface Using tkinter
(adapted from Rosanna Heisse Assignment)
Goals:
1) Expand your ability to write a computer program in Python and explain, in English, how your
program works: special focus is now using libraries (classes), creating objects, and sending
them messages.
2) Continue to work in a team, explain concepts to one another, divide up the work, and
evaluate both contributions and progress.
3) Describe how the program control is working, between step-by-step, function calls/returns,
conditionals, loops, and events.
References:
Amos, David. Python GUI Programming with Tkinter, Real Python, 2021.
https://realpython.com/python-gui-tkinter/
Tkinter Dialogs, Python. https://docs.python.org/3/library/dialog.html
Perkovic, Ljubomir. Introduction to Computing Using Python, 2nd ed, Chapter 9.
What To Do:
1) Work as a team to make sure everyone can use tkinter to make a window and then
put some items into the window, e.g. Label, Button, simpledialog
2) Create a simple game, between two players, that gives each player three turns to
claim the most space within a 2d grid of squares. The squares should be made randomly from
two colors, one color per player. On a player’s turn, they must click one square of a different
color and this will turn that square into their own color. In addition, by following a straight line
(up, down, left, or right but not diagonal) every square between the selected square and the
first occurrence of their color is also turned into their color. Make your playing board 9X9.
3) Use pencil and paper, perhaps with graph paper, to design your window(s),
determining how your game will look. You must include the following aspects in your program:
∙ A game board, made out of buttons.
● At least one conditional statement.
● At least one nested loop statement.
● Items of data should be named (variables/constants) and at least four sections
of code must be named (4 functions).
● A file header.
● Documentation.
○ Explaining your code
○ An indication of who was the primary contributor of each section of code.
4) Divide your task up among team members, and meet to combine your code into one
(or more) file. Make sure that you have worked as a team (Steps 1, 2 and 3) before
dividing up.
5) Add at least one feature to the game to make it a little more interesting.
Example of things could be:
Add an option at the start to select between different sizes of grid.
Add an option to give more than 3 turns by players.
Add an option to save/load a game state.
Add an option for the player to save a started setup in case they want to replay it.
Add a feature that keeps a history of previous game results.
Add a feature for players to choose their colors.
Add a 1-player option where the player plays against an AI (e.g., the AI would randomly
click on a valid option).
Online gaming. Add a networking option (hard: do only if you have done the rest) where
two players would play on different computers. One user will create a server session and the
other will join (You may want to look at socket programming tutorials
https://realpython.com/python-sockets/ )
Other ideas?
Sample:
Here is an example of the game in operation. Make a 9X9 grid for the game board,
even though my example shows a 7X7 grid. Set up the game board, according to the size
entered. Your main screen should have a title, simple instructions, the game board, the turn
number, and an indication of whose turn it is.
Suppose the green player clicks on the yellow square in the right in the middle of the board,
marked “A”. It will turn to green, as will any square between this square and the closest green
square following a straight line horizontally or vertically, assuming such a green square exists.
Going left, there is no green square so no square changes color. Going right, the next square
is green so no additional squares change color. Going up, there is one yellow square to turn
green before a green square is found. Going down, there are two yellow squares to turn green
before a green square is found. The board now looks like:
The game continues until each player has had three turns or there is no tile left in that player’s
color. Tally the tiles, and indicate who the winner is at the end. Be careful in cases that might
be tied.
MacOS Users:
MacOS does not allow tkinter to change button colors. If you are a MacOS user, in
addition to changing the color of the button (which will not change on MacOS), you should also
change the text in each button (e.g., mark an “X” for the green player and a “Y” for the other
player).
Alternatively, you may try to use tkmacosx library to force MacOS to change button’s color.
At the start of the program, add:
from sys import platform
if platform == 'darwin': # Assuming your MacOS version is darwin
from tkmacosx import Button
else:
from tkinter import Button
Submit:
1) On eClass: all your python files .py file of your program per group
2) On eClass: one confidential evaluation per student as .pdf file
Marking:
Correctness & Completeness (60%) – how well does your program meet the requirements?
Documentation (30%) – have you used good variable names, structured your program in a
way that makes sense, and described (in English) what has been done?
Style (10%) – does your work follow a reasonable approach? (This is subjective to the marker.)