For my python class i have to make a game, i decided to make a simon sort of game. I'm not allowed to use any outside modules; for instance pygame.
I have most if not all of the visuals done. But there are two main issues:
- One issue is getting the for loop to display the computer generated pattern accurately, right now its displaying all elements of the pattern at the same time.
-my other issue is the actual logic of allowing the computer to take in the users input, comparing it to the computer generated list and if true, append a new value to the list display the new pattern and so on and so forth.
Any help, ideas or similar project codes that i can get ideas from would be much appreciated.
Thanks!
import random
import time
from tkinter import *
import winsound
LINE_WIDTH = 5
TOP_RIGHT_COORDS =200,75,275,150
TOP_LEFT_COORDS =75,75,150,150
BOT_LEFT_COORDS =75,200,150,275
BOT_RIGHT_COORDS = 200,200,275,275
TOP_RIGHT = 1
TOP_LEFT = 2
BOT_LEFT = 3
BOT_RIGHT = 4
BACKGROUND = 'black'
TOP_RIGHT_COL = "blue"
TOP_LEFT_COL = "yellow"
BOT_LEFT_COL = "red"
BOT_RIGHT_COL = "green"
BEEP_DURATION = 200
FONT = "Helvectica", "16"
SCORE = 0
class Simon:
def __init__(self):
self.__mainWindow = Tk()
self.__scoreVal = IntVar()
self.__canvasFrame = Frame(self.__mainWindow)
self.__scoreFrame = Frame(self.__mainWindow)
self.__canvas = Canvas(self.__canvasFrame, width=350, height=295, bg=BACKGROUND)#initializes the canvas
self.__scoreHead = Label(self.__scoreFrame, text ='Score: ', fg = 'white', bg = 'black', font=FONT,)
self.__scoreLabel = Label(self.__scoreFrame, textvariable =self.__scoreVal, fg = 'white', bg= 'black',font = FONT)
self.__canvas.pack()
self.__scoreHead.pack(side='left')
self.__scoreLabel.pack(side ='left')
self.__canvasFrame.pack()
self.__scoreFrame.pack()
self.__mainWindow.bind_all('<Key>', self.KeyPress)
self.__canvas.create_line(10,300,340,300,fill = 'white')
self.__canvas.create_line(200,75,200,150,fill = TOP_RIGHT_COL, width = LINE_WIDTH)
self.__canvas.create_line(200,75,275,75,fill = TOP_RIGHT_COL, width = LINE_WIDTH)
self.__canvas.create_line(200,150,275,150,fill = TOP_RIGHT_COL, width = LINE_WIDTH)
self.__canvas.create_line(275,150,275,75,fill = TOP_RIGHT_COL, width = LINE_WIDTH)
self.__canvas.create_line(75,75,75,150,fill = TOP_LEFT_COL, width = LINE_WIDTH)
self.__canvas.create_line(75,75,150,75,fill = TOP_LEFT_COL, width = LINE_WIDTH)
self.__canvas.create_line(150,150,150,75,fill = TOP_LEFT_COL, width = LINE_WIDTH)
self.__canvas.create_line(150,150,75,150,fill = TOP_LEFT_COL, width = LINE_WIDTH)
self.__canvas.create_line(75,200,150,200,fill = BOT_LEFT_COL, width = LINE_WIDTH)
self.__canvas.create_line(75,200,75,275,fill = BOT_LEFT_COL, width = LINE_WIDTH)
self.__canvas.create_line(75,275,150,275,fill = BOT_LEFT_COL, width = LINE_WIDTH)
self.__canvas.create_line(150,200,150,275,fill = BOT_LEFT_COL, width = LINE_WIDTH)
self.__canvas.create_line(200,200,275,200,fill = BOT_RIGHT_COL, width = LINE_WIDTH)
self.__canvas.create_line(200,200,200,275,fill = BOT_RIGHT_COL, width = LINE_WIDTH)
self.__canvas.create_line(200,275,275,275,fill = BOT_RIGHT_COL, width = LINE_WIDTH)
self.__canvas.create_line(275,200,275,275,fill = BOT_RIGHT_COL, width = LINE_WIDTH)
self.__pattern = []
self.__userPat = []
mainloop()
def KeyPress(self,event):
self.__waitingForInput = False
if event.keysym =="Return":
self.__pattern.append(random.choice((TOP_RIGHT,TOP_LEFT,BOT_LEFT,BOT_RIGHT)))
for num in range(len(self.__pattern)):
self.playPatternLogic(num)
if event.keysym=="Up":
self.pingTopRight()
self.__userPat.append(1)
self.__scoreVal.set(int(self.__scoreVal.get())+1)
if event.keysym == "Down":
self.pingTopLeft()
self.__userPat.append(2)
if event.keysym == "Left":
self.pingBotLeft()
self.__userPat.append(3)
if event.keysym == "Right":
self.pingBotRight()
self.__userPat.append(4)
#This compares the two lists
if self.__pattern == self.__userPat:
self.__pattern.append(random.choice((TOP_RIGHT,TOP_LEFT,BOT_LEFT,BOT_RIGHT)))
for num in range(len(self.__pattern)):
self.playPatternLogic(num)
#else:
#self.__pattern = []
#self.__userPat = []
# self.__scoreVal.set(0)
#winsound.#failing sound
def playPatternLogic(self,num):
if self.__pattern[num] == 1:
self.pingTopRight()
if self.__pattern[num] == 2:
self.pingTopLeft()
if self.__pattern[num] == 3:
self.pingBotLeft()
if self.__pattern[num] == 4:
self.pingBotRight()
def pingTopRight(self):
self.__canvas.create_rectangle(TOP_RIGHT_COORDS,fill=TOP_RIGHT_COL,width= 0)
self.__canvas.after(1,self.unPingTR)
def pingTopLeft(self):
self.__canvas.create_rectangle(TOP_LEFT_COORDS,fill=TOP_LEFT_COL,width= 0)
self.__canvas.after(1,self.unPingTL)
def pingBotLeft(self):
self.__canvas.create_rectangle(BOT_LEFT_COORDS,fill=BOT_LEFT_COL,width= 0)
self.__canvas.after(1,self.unPingBL)
def pingBotRight(self):
self.__canvas.create_rectangle(BOT_RIGHT_COORDS ,fill=BOT_RIGHT_COL,width= 0)
self.__canvas.after(1,self.unPingBR)
def unPingTR(self):
self.__canvas.create_rectangle(TOP_RIGHT_COORDS,fill=BACKGROUND,width= 0)
winsound.Beep(466,BEEP_DURATION)
def unPingTL(self):
self.__canvas.create_rectangle(TOP_LEFT_COORDS,fill=BACKGROUND,width= 0)
winsound.Beep(554,BEEP_DURATION)
def unPingBL(self):
self.__canvas.create_rectangle(BOT_LEFT_COORDS,fill=BACKGROUND,width= 0)
winsound.Beep(622,BEEP_DURATION)
def unPingBR(self):
self.__canvas.create_rectangle(BOT_RIGHT_COORDS,fill=BACKGROUND,width= 0)
winsound.Beep(740,BEEP_DURATION)
game = Simon()