Basic Python connect 4, details in the document

1 answer below »
Basic Python connect 4, details in the document
Answered Same DayJul 25, 2021

Answer To: Basic Python connect 4, details in the document

Roshan answered on Jul 26 2021
152 Votes
"""CS 108 prototype
A connect 4 game
@author: Adrian Poh ajp62
@date: Summer, 2020
"""
# importing all objects in tkinter
from tkinter import *
import winsound
winsound.PlaySound('', winsound.SND_ALIAS | winsound.SND_ASYNC) # i'll add
the sound later
class Piece:
def __init__(self, x, y, circlepiece, color="black"):
"""Function that creates the circle piece"""
self.circlepiece = circlepiece
self.x = x
self.y = y
self.color = color
self.Piece = self.circlepiece.create_oval(self.x + 10, self.y + 10, self.x + 60, self.y + 60, fill=color)
def change_color(self, color):
"""Function that allows the piece to switch color"""
self.circlepiece.itemconfigure(self.Piece, fill=color)
self.color = color
class App(Canvas):
# this code was taken from https://stackoverflow.com/questions/18067915/what-is-the-purpose-of-master-and-master-none-in-an-init-function-in-python
def __init__(self, Master): # Using master=none by default as this class is a subclass to tk.Frame
"""Constructor for the main window of the program"""
Canvas.__init__(self)
#Configure function is used to set width, height and background of main window
self.configure(width=500, height=400, background="Blue")
self.player = 1
self.color = "yellow"
self.empty_list = []
self.place = True
self.terminate = False
for i in range(0, 340, 67): # Setting of board size
range_list = []
for h in range(0, 440, 72):
range_list.append(Piece(h, i, self))
self.empty_list.append(range_list)
self.bind("", self.switching_colors)
def switching_colors(self, event):
"""Function to switch color after every click"""
if self.place:
columnn = int(event.x / 72)
stack = 0 # put yellow or red circle to start from below of window
while stack < len(self.empty_list):
if self.empty_list[0][columnn].color == "red" or self.empty_list[0][0].color == "yellow":
break
if self.empty_list[stack][columnn].color == "red" or self.empty_list[stack][columnn].color == "yellow":
self.empty_list[stack - 1][columnn].change_color(
self.color) # Allowing each piece to stack above another...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here