import string
import random
import tkinter as tk
from tkinter import ttk
import yaml
import gameUtils as gutils
import os
root = tk.Tk()
root.title("Word Search Game")
wordPressed = ''
previous = [0, 0]
route = [0, 0]
# ***************> Game Starts <******************
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=
'(C)2022',
)
footer.pack(expand=True, fill=tk.X, pady=0)
def startGame():
frame1 = tk.Frame(master=root, bg="red")
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)
configFile = gutils.readConfigFile()
levelNum = configFile['player']['level']
levelDetails = [
configFile['levels'][levelNum]['name'],
configFile['levels'][levelNum]['words']
]
currScore = tk.StringVar()
currScore.set(0)
def updateScore():
configFile = gutils.readConfigFile()
score = configFile['player']['score']
configFile['player']['score'] = score + 1
currScore.set(score + 1)
gutils.writeConfigFile(configFile)
root.update_idletasks()
frame3 = tk.Frame(master=root)
frame3.pack(fill=tk.BOTH, side=tk.RIGHT, padx=20, pady=30)
labelWelcome = tk.Label(master=frame3,
text="Welcome",
fg='#2c334a',
font=('Helvetica', 12, 'bold')).grid(row=0,
column=0)
labelWName = tk.Label(master=frame3,
text=configFile['player']['name'],
fg='#2c334a',
font=('Helvetica', 12, 'bold')).grid(row=0, column=1)
labelLevelLbl = tk.Label(master=frame3,
text="Level",
fg='#2c334a',
font=('Helvetica', 12)).grid(row=1, column=0)
labelLevel = tk.Label(master=frame3,
text=levelDetails[0],
fg='#2c334a',
font=('Helvetica', 12, 'bold')).grid(row=1, column=1)
labelLevelLbl = tk.Label(master=frame3,
text="Words",
fg='#2c334a',
font=('Helvetica', 12)).grid(row=2, column=0)
labelLevel = tk.Label(master=frame3,
text=levelDetails[1],
fg='#2c334a',
font=('Helvetica', 12, 'bold')).grid(row=2, column=1)
labelLevelLbl = tk.Label(master=frame3,
text="Score",
fg='#2c334a',
font=('Helvetica', 12)).grid(row=3, column=0)
labelLevel = tk.Label(master=frame3,
textvariable=currScore,
fg='#2c334a',
font=('Helvetica', 12, 'bold')).grid(row=3, column=1)
wordList = []
with open(r'data/words.yaml') as file:
wordFile = yaml.load(file, Loader=yaml.FullLoader)
wordList = [word for word in wordFile['words']]
size = numWords = configFile['words_count']
arr = [[0 for x in range(size)] for y in range(size)]
button = [[0 for x in range(size)] for y in range(size)]
check = [0 for numWords in range(size)]
dictionary = [0 for createWordSet in range(numWords)]
directionArr = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1],
[0, -1], [1, -1]]
class Square:
status = False
filled = False
char = ''
def fill(x, y, word, direction):
for i in range(len(word)):
arr[x + direction[0] * i][y + direction[1] * i].char = word[i]
arr[x + direction[0] * i][y + direction[1] * i].filled = True
def wordPlace(j, dictionary):
word = random.choice(wordList)
direction = directionArr[random.randrange(0, 7)]
x = random.randrange(0, size - 1)
y = random.randrange(0, size - 1)
if (x + len(word) * direction[0] > size - 1
or x + len(word) * direction[0] < 0
or y + len(word) * direction[1] > size - 1
) or y + len(word) * direction[1] < 0:
wordPlace(j, dictionary)
return
for i in range(len(word)):
if (arr[x + direction[0] * i][y +
direction[1] * i].filled == True):
if (arr[x + direction[0] * i][y + direction[1] * i].char !=
word[i]):
wordPlace(j, dictionary)
return
dictionary[j] = word
check[j] = tk.Label(frame2,
text=word,
height=1,
width=15,
font=('None %d ' % (10)),
fg='#254359',
bg='#cbe5f7',
anchor='c')
check[j].grid()
fill(x, y, word, direction)
return dictionary
def colourWord(wordPressed, valid):
route[0] *= -1
route[1] *= -1
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='#535edb',
fg='white')
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
if wordPressed in dictionary:
check[int(dictionary.index(wordPressed))].configure(
font=('Helvetica', 1), fg='#f0f0f0', bg='#f0f0f0')
check[int(dictionary.index(wordPressed))].grid()
dictionary[dictionary.index(wordPressed)] = ''
updateScore()
colourWord(wordPressed, True)
else:
colourWord(wordPressed, False)
wordPressed = ''
previous = [0, 0]
def buttonPress(x, y):
global wordPressed, previous, route
newPressed = [x, y]
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]
Word Search Game in Python Free Source Code.zip
139 浏览量
2023-11-03
16:50:00
上传
评论
收藏 275KB ZIP 举报
xiaoshun007~
- 粉丝: 3849
- 资源: 3131
最新资源
- 简单的Python示例,演示了如何使用TCP/IP协议进行基本的客户端和服务器通信
- 考试.sql
- keil2 + proteus + 8051.exe
- 1961ee27df03bd4595d28e24b00dde4e_744c805f7e4fb4d40fa3f695bfbab035_8(1).c
- mediapipe-0.9.0.1-cp37-cp37m-win-amd64.whl.zip
- windows注册表编辑工具
- mediapipe-0.9.0.1-cp37-cp37m-win-amd64.whl.zip
- 校园通行码预约管理系统20240522075502
- 车类型数据集6250张VOC+YOLO格式.zip
- The PyTorch implementation of STGCN.STGCN-main.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈