Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you explain this code in detail? import string import random import tkinter as tk from tkinter import ttk root = tk.Tk() root.title(Word Search Game)

Can you explain this code in detail? import string import random import tkinter as tk from tkinter import ttk

root = tk.Tk() root.title("Word Search Game")

wordPressed = '' previous = [0, 0] route = [0, 0]

PlayerName = ''

width = 8 height = 8 xsize = 0 ysize = 0

counter = 0 currScore = tk.StringVar() currScore.set(0)

wordlist = ["RUN","MAGED","TAMER","ANDREW"]

check = [0 for numWords in range(len(wordlist))] print("check => " , check)

grid = [[random.choice(string.ascii_lowercase) for i in range(width)] for j in range(height)]

button = [[random.choice(string.ascii_lowercase) for i in range(width)] for j in range(height)] # [[0 for x in range(size)] for y in range(size)] print("button => " , button)

class buttonSquare: select = False char = ''

for x in range(width): for y in range(height): button[x][y] = buttonSquare()

class Square: status = False filled = False char = ''

arr = [[0 for x in range(width)] for y in range(height)] print("arr => " , arr)

for x in range(width): for y in range(height): arr[x][y] = Square() print("arr => " , arr)

def gameHeader(): game_header = tk.Frame(root) game_header.pack(fill=tk.X, side=tk.TOP)

heading = tk.Label(game_header, text='Word Search Game', font=('Helvetica', 23, 'bold'), fg='blue') heading.pack(expand=True, fill=tk.X, pady=12)

def gameFooter(): game_footer = tk.Frame(root) game_footer.pack(fill=tk.X, side=tk.BOTTOM, pady=12) footer = tk.Label( game_footer, text= 'Made by Cyber Knights', ) footer.pack(expand=True, fill=tk.X, pady=0)

def fill(x, y, word, direction): for i in range(len(word)): arr[x + direction[0] * i][y + direction[1] * i].char = word[i] print("x =>" , x) print("y =>" , y) print ("arr[x + direction[0] * i][y + direction[1] * i].char =>",arr[x + direction[0] * i][y + direction[1] * i].char) print("word[i] =>" , word[i]) arr[x + direction[0] * i][y + direction[1] * i].filled = True

def buttonPress(x, y): global wordPressed, previous, route newPressed = [x, y] print("wordPressed before =>" , wordPressed) print("previous before =>" , previous) print("route before =>" , route) if (len(wordPressed) == 0): previous = newPressed # print(previous) wordPressed = arr[x][y].char button[x][y].configure(bg='yellow', fg='#255059') elif (len(wordPressed) == 1 and (x - previous[0])**2 <= 1 and (y - previous[1])**2 <= 1 and newPressed != previous): wordPressed += arr[x][y].char button[x][y].configure(bg='yellow', fg='#255059') route = [x - previous[0], y - previous[1]] previous = [x, y]

elif (len(wordPressed) > 1 and x - previous[0] == route[0] and y - previous[1] == route[1]): wordPressed += arr[x][y].char button[x][y].configure(bg='yellow', fg='#255059') previous = [x, y] print("wordPressed After =>" , wordPressed) print("previous After =>" , previous) print("route After =>" , route)

# show our words leters on Array cells def put_word(word): word = random.choice([word]) direction = random.choice([[1, 0], [0, 1], [1, 1]]) if direction[0] == 0: xsize = width else: xsize = width - len(word)

if direction[1] == 0: ysize = height else: ysize = height - len(word)

# Ensure that the word does not overlap with existing words while True: x = random.randrange(0, xsize) y = random.randrange(0, ysize) # Check for overlap overlap = False for i in range(len(word)): if arr[x + direction[0] * i][y + direction[1] * i].filled: overlap = True break

if not overlap: break

fill(x, y, word, direction)

def startGame(): frame1 = tk.Frame(master=root) frame1.pack(fill=tk.BOTH, side=tk.LEFT, expand=True, padx=20, pady=12)

frame2 = tk. Frame(master=root) frame2.pack(fill=tk.BOTH, side=tk.LEFT, expand=True, padx=10, pady=12)

frame3 = tk.Frame(master=root) frame3.pack(fill=tk.BOTH, side=tk.RIGHT,expand=True ,padx=20, pady=40) # Put our target words list here for word in wordlist: put_word(word) print("arr => " , arr)

# Show button matrix matched with our Array for x in range(width): #print("y =>",y) for y in range(height): #print("x =>",x) if (arr[x][y].filled == False): arr[x][y].char =random.choice(string.ascii_uppercase) #print("arr =>",arr)

if (arr[x][y].filled == True): print("arr[x][y].char=>",arr[x][y].char) button[x][y] = tk.Button( frame1, text=arr[x][y].char, bg='#255059', fg='white', width=2, height=1, relief=tk.FLAT, command=lambda x=x, y=y: buttonPress(x, y)) button[x][y].grid(row=x, column=y) button[x][y].char = arr[x][y].char

########################################

# Display our target words list on frame to be visable to user def displaywords(i,wordlist): word = wordlist[i] check[i] = tk.Label(frame2, text=word, height=1, width=15, font=('Helvetica', 10), fg='#254359', bg='#cbe5f7', anchor='c')

check[i].grid()

for i in range(len(wordlist)): displaywords(i,wordlist) # End Display our target words list on frame to be visable to user ###Show the Check word button def colourWord(wordPressed, valid): route[0] *= -1 route[1] *= -1 print("wordPressed=>",wordPressed) print("previous=>",previous) print("route=>",route) for i in range(len(wordPressed)): if valid == True or arr[previous[0] + i * route[0]][previous[1] + i * route[1]].status == True: button[previous[0] + i * route[0]][previous[1] + i * route[1]].config(bg='red', fg='white') #535edb # state=DISABLED arr[previous[0] + i * route[0]][previous[1] + i * route[1]].status = True elif (arr[previous[0] + i * route[0]][previous[1] + i * route[1]].status == False): button[previous[0] + i * route[0]][previous[1] + i * route[1]].config( bg='#255059', fg='white')

def checkWord(): global wordPressed global counter global currScore

if wordPressed in wordlist: check[int(wordlist.index(wordPressed))].configure( font=('Helvetica', 10), fg='white', bg='blue') colourWord(wordPressed, True) counter = counter+1 #print("counter=>",counter) currScore.set(counter) #print("currScore=>",currScore) # Disable buttons for i in range(len(wordPressed)): button[previous[0] + i * route[0]][previous[1] + i * route[1]].config(state=tk.DISABLED) # Reset the wordPressed variable wordPressed = ""

checkWordBtn = tk.Button(frame2, text="Check Word", height=1, width=15, anchor='c', bg="#70889c", font=('Helvetica', 10), fg='white', command=checkWord) checkWordBtn.grid() ########################################

# Show display lables for score #print("PlayerName",PlayerName) labelWelcome = tk.Label(master=frame3, text="Welcome", fg='#2c334a', Frame(master=root) frame2.pack(fill=tk.BOTH, side=tk.LEFT, expand=True, padx=10, pady=12)

frame3 = tk.Frame(master=root) frame3.pack(fill=tk.BOTH, side=tk.RIGHT,expand=True ,padx=20, pady=40) # Put our target words list here for word in wordlist: put_word(word) print("arr => " , arr)

# Show button matrix matched with our Array for x in range(width): #print("y =>",y) for y in range(height): #print("x =>",x) if (arr[x][y].filled == False): arr[x][y].char =random.choice(string.ascii_uppercase) #print("arr =>",arr)

if (arr[x][y].filled == True): print("arr[x][y].char=>",arr[x][y].char) button[x][y] = tk.Button( frame1, text=arr[x][y].char, bg='#255059', fg='white', width=2, height=1, relief=tk.FLAT, command=lambda x=x, y=y: buttonPress(x, y)) button[x][y].grid(row=x, column=y) button[x][y].char = arr[x][y].char

########################################

# Display our target words list on frame to be visable to user def displaywords(i,wordlist): word = wordlist[i] check[i] = tk.Label(frame2, text=word, height=1, width=15, font=('Helvetica', 10), fg='#254359', bg='#cbe5f7', anchor='c')

check[i].grid()

for i in range(len(wordlist)): displaywords(i,wordlist) # End Display our target words list on frame to be visable to user ###Show the Check word button def colourWord(wordPressed, valid): route[0] *= -1 route[1] *= -1 print("wordPressed=>",wordPressed) print("previous=>",previous) print("route=>",route) for i in range(len(wordPressed)): if valid == True or arr[previous[0] + i * route[0]][previous[1] + i * route[1]].status == True: button[previous[0] + i * route[0]][previous[1] + i * route[1]].config(bg='red', fg='white') #535edb # state=DISABLED arr[previous[0] + i * route[0]][previous[1] + i * route[1]].status = True elif (arr[previous[0] + i * route[0]][previous[1] + i * route[1]].status == False): button[previous[0] + i * route[0]][previous[1] + i * route[1]].config( bg='#255059', fg='white')

def checkWord(): global wordPressed global counter global currScore

if wordPressed in wordlist: check[int(wordlist.index(wordPressed))].configure( font=('Helvetica', 10), fg='white', bg='blue') colourWord(wordPressed, True) counter = counter+1 #print("counter=>",counter) currScore.set(counter) #print("currScore=>",currScore) # Disable buttons for i in range(len(wordPressed)): button[previous[0] + i * route[0]][previous[1] + i * route[1]].config(state=tk.DISABLED) # Reset the wordPressed variable wordPressed = ""

checkWordBtn = tk.Button(frame2, text="Check Word", height=1, width=15, anchor='c', bg="#70889c", font=('Helvetica', 10), fg='white', command=checkWord) checkWordBtn.grid() ########################################

# Show display lables for score #print("PlayerName",PlayerName) labelWelcome = tk.Label(master=frame3, text="Welcome", fg='#2c334a',

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Information Quality Dimensions, Principles And Techniques

Authors: Carlo Batini, Monica Scannapieco

1st Edition

3319241060, 9783319241067

More Books

Students also viewed these Databases questions

Question

What is a force majeure clause?

Answered: 1 week ago