|
|
import sys
|
|
|
import time
|
|
|
import pygame
|
|
|
import random
|
|
|
from collections import OrderedDict
|
|
|
from PIL import Image, ImageTk
|
|
|
import tkinter as tk
|
|
|
import datetime
|
|
|
|
|
|
#定义棋盘的类
|
|
|
class Qipan:
|
|
|
def __init__(self,size=4):
|
|
|
#随机方向
|
|
|
self.position=[
|
|
|
[1,0],#下
|
|
|
[-1,0],#上
|
|
|
[0,1],#右
|
|
|
[0,-1]#左
|
|
|
]
|
|
|
|
|
|
#棋盘大小
|
|
|
self.size=size
|
|
|
|
|
|
#棋盘点击坐标字典
|
|
|
self.click_react={'x':{},'y':{}}
|
|
|
|
|
|
#pos(有序字典)记录每个xy坐标
|
|
|
self.pos=OrderedDict()
|
|
|
|
|
|
#初始化数字化棋盘
|
|
|
num=1
|
|
|
for i in range(size):#i是行j是列
|
|
|
for j in range(size):
|
|
|
posxy=tuple([i,j])
|
|
|
self.pos[posxy]=num
|
|
|
num+=1
|
|
|
|
|
|
#保证是随机棋盘,从正确的棋盘移动至随机棋盘
|
|
|
for i in range(1000):
|
|
|
positions=random.choice(self.position)
|
|
|
change_op=addtu(posxy,positions)#相加移动
|
|
|
if change_op in self.pos:
|
|
|
tmp=self.pos[change_op]
|
|
|
self.pos[change_op]=size*size
|
|
|
self.pos[posxy]=tmp
|
|
|
posxy=change_op
|
|
|
|
|
|
#将点击区域与坐标相挂钩(xy)
|
|
|
for y in range(self.size):
|
|
|
for x in range(self.size):
|
|
|
#x
|
|
|
x0=x*qizi_size+100
|
|
|
x1=(x+1)*qizi_size+100
|
|
|
click_x=(x0,x1)################################
|
|
|
self.click_react['x'][click_x]=x
|
|
|
|
|
|
#y
|
|
|
y0=y*qizi_size+150
|
|
|
y1=(y+1)*qizi_size+150
|
|
|
click_y=(y0,y1)
|
|
|
self.click_react['y'][click_y]=y
|
|
|
|
|
|
#实现移动棋子
|
|
|
def move(self,x,y):
|
|
|
#判断对应的是第几行第几列
|
|
|
xnum=-1
|
|
|
for i,j in self.click_react['x'].items():
|
|
|
if i[0]<=x<i[1]:
|
|
|
xnum=j
|
|
|
break
|
|
|
if xnum ==-1:
|
|
|
return False
|
|
|
|
|
|
ynum=-1
|
|
|
for i,j in self.click_react['y'].items():
|
|
|
if i[0]<=y<i[1]:
|
|
|
ynum=j
|
|
|
break
|
|
|
if ynum==-1:
|
|
|
return False
|
|
|
#
|
|
|
|
|
|
#判断该棋子周围是否可以移动
|
|
|
for i in self.position:
|
|
|
change_op=addtu((ynum,xnum),i)
|
|
|
if change_op in self.pos:
|
|
|
if self.pos[change_op]==self.size*self.size:
|
|
|
self.pos[change_op],self.pos[(ynum,xnum)]=self.pos[(ynum,xnum)],self.pos[change_op]
|
|
|
break
|
|
|
|
|
|
#判断移动之后是否已经赢了
|
|
|
def win(self):
|
|
|
number=1
|
|
|
for i in range(self.size):
|
|
|
for j in range(self.size):
|
|
|
if self.pos[(i,j)]==number:
|
|
|
number+=1
|
|
|
continue
|
|
|
else:
|
|
|
return False
|
|
|
return True
|
|
|
|
|
|
#常量定义
|
|
|
FPS=60
|
|
|
size=3
|
|
|
qizi_size=100
|
|
|
photo_height=300
|
|
|
photo_width=300
|
|
|
main_page_height=700
|
|
|
main_page_width=1000
|
|
|
game_page_height=700
|
|
|
game_page_width=1000
|
|
|
BLACK = (0,0,0)
|
|
|
button_color = (255, 0, 0)
|
|
|
|
|
|
|
|
|
#背景颜色
|
|
|
background_image = pygame.image.load('background.jpg')
|
|
|
BACKGROUND_QIZI_COLOR="#ffffff"
|
|
|
|
|
|
#定义元组相加
|
|
|
def addtu(tuple1,tuple2):
|
|
|
tmp=(tuple1[0]+tuple2[0],tuple1[1]+tuple2[1])
|
|
|
return tmp
|
|
|
|
|
|
#定义输出文本函数
|
|
|
def print_text(game_page, font, x, y, text, fcolor=(255, 255, 255)):
|
|
|
imgText = font.render(text, True, fcolor)
|
|
|
game_page.blit(imgText, (x, y))
|
|
|
|
|
|
#开始绘画拼图
|
|
|
def draw_img(qipan,game_page):
|
|
|
for i in range(qipan.size):
|
|
|
for j in range(qipan.size):
|
|
|
temp=qipan.pos[(i,j)]
|
|
|
if temp == qipan.size*qipan.size:
|
|
|
color=pygame.Color(BACKGROUND_QIZI_COLOR)
|
|
|
x=qizi_size*j+100
|
|
|
y=qizi_size*i+150
|
|
|
pygame.draw.rect(game_page,color,(x,y,qizi_size,qizi_size))
|
|
|
else:
|
|
|
img=pygame.image.load("photo/img{}.jpg".format(temp)).convert()
|
|
|
game_page.blit(img,(j*qizi_size+100,i*qizi_size+150))
|
|
|
|
|
|
global rand_photo
|
|
|
img=pygame.image.load('photo/{}.jpg'.format(rand_photo)).convert()
|
|
|
game_page.blit(img,(size*qizi_size+360,150))
|
|
|
|
|
|
font = pygame.font.SysFont('KaiTi', 30)
|
|
|
with open('record/note_size{}.txt'.format(tmp_size), 'r', encoding='utf-8') as file:
|
|
|
note = file.read()
|
|
|
if note != '暂无记录':
|
|
|
temp = note.split(' ')
|
|
|
text_note = font.render('最快:'+str(temp[0])+'s', True, (255,0,0))
|
|
|
game_page.blit(text_note,(30,80))
|
|
|
|
|
|
global button_fanhui
|
|
|
global button_tuichu
|
|
|
button_fanhui = pygame.Rect(700, 600, 140, 50)
|
|
|
button_tuichu = pygame.Rect(850, 600, 140, 50)
|
|
|
pygame.draw.rect(game_page, button_color, button_fanhui)
|
|
|
pygame.draw.rect(game_page, button_color, button_tuichu)
|
|
|
|
|
|
# 在按钮上显示文字
|
|
|
text1 = font.render("返 回", True, BLACK)
|
|
|
text2 = font.render("退出游戏", True, BLACK)
|
|
|
game_page.blit(text1, (button_fanhui.x + 10, button_fanhui.y + 10))
|
|
|
game_page.blit(text2, (button_tuichu.x + 10, button_tuichu.y + 10))
|
|
|
#
|
|
|
|
|
|
#
|
|
|
def press(game_over,qipan,NOb,times):
|
|
|
global game_start,start_time,elapsed_time
|
|
|
|
|
|
for event in pygame.event.get():
|
|
|
#设置定时器,记录完成时间
|
|
|
if event.type ==NOb and not game_over:
|
|
|
times+=1
|
|
|
#设置使用时间页面
|
|
|
elif event.type==pygame.QUIT:
|
|
|
pygame.quit()
|
|
|
sys.exit()
|
|
|
#鼠标点击之后的操作
|
|
|
elif event.type==pygame.MOUSEBUTTONUP:
|
|
|
if button_fanhui.collidepoint(event.pos):
|
|
|
pygame.quit()
|
|
|
main_init()
|
|
|
if button_tuichu.collidepoint(event.pos):
|
|
|
pygame.quit()
|
|
|
sys.exit()
|
|
|
if game_start==0:
|
|
|
game_start=1
|
|
|
start_time = time.time()
|
|
|
elapsed_time=0
|
|
|
if event.button == 1 and not game_over:
|
|
|
x,y=event.pos
|
|
|
#qipan.move(x,y)#移动棋盘
|
|
|
xnum=-1
|
|
|
for i,j in qipan.click_react['x'].items():
|
|
|
if i[0]<=x<i[1]:
|
|
|
xnum=j
|
|
|
break
|
|
|
if xnum ==-1:
|
|
|
return False
|
|
|
|
|
|
ynum=-1
|
|
|
for i,j in qipan.click_react['y'].items():
|
|
|
if i[0]<=y<i[1]:
|
|
|
ynum=j
|
|
|
break
|
|
|
if ynum==-1:
|
|
|
return False
|
|
|
#
|
|
|
#判断该棋子周围是否可以移动
|
|
|
for i in qipan.position:
|
|
|
change_op=addtu((ynum,xnum),i)
|
|
|
|
|
|
if change_op in qipan.pos:
|
|
|
if qipan.pos[change_op]==qipan.size*qipan.size:
|
|
|
qipan.pos[change_op],qipan.pos[(ynum,xnum)]=qipan.pos[(ynum,xnum)],qipan.pos[change_op]
|
|
|
break
|
|
|
#####需要修改
|
|
|
if NOb:
|
|
|
return times
|
|
|
#
|
|
|
|
|
|
#游戏结算画面
|
|
|
def game_over_win(game_page,qipan,clock,size, text):
|
|
|
font =pygame.font.SysFont('KaiTi',50)
|
|
|
while True:
|
|
|
if press(True,qipan,None,None):
|
|
|
break
|
|
|
game_page.blit(background_image, (0, 0))
|
|
|
draw_img(qipan,game_page)#绘画整个页面
|
|
|
font1 = pygame.font.SysFont('KaiTi', 50)
|
|
|
tmp_time=size*size*20-elapsed_time
|
|
|
print_text(game_page,font1, 50, 20, '%03d' % tmp_time, (200,40,40))
|
|
|
temp = text.split('\n')
|
|
|
if len(temp) == 1:
|
|
|
game_page.blit(font.render(temp[0], True, (255, 0, 0)),(250,20))
|
|
|
else:
|
|
|
game_page.blit(font.render(temp[0], True, (255, 0, 0)),(250,20))
|
|
|
game_page.blit(font.render(temp[1], True, (255, 0, 0)),(270,80))
|
|
|
pygame.display.update()
|
|
|
clock.tick(FPS)######
|
|
|
#
|
|
|
|
|
|
#输掉比赛
|
|
|
def game_over_lose(game_page,q,clock,text):
|
|
|
font =pygame.font.SysFont('KaiTi',50)
|
|
|
while True:
|
|
|
if press(True,q,None,None):
|
|
|
break
|
|
|
game_page.blit(background_image, (0, 0))
|
|
|
draw_img(q,game_page)#绘画整个页面
|
|
|
font1 = pygame.font.SysFont('KaiTi', 50)
|
|
|
print_text(game_page,font1, 50, 20, '%03d' % 0, (200,40,40))#输出时间为0
|
|
|
game_page.blit(font.render(str(text), True, (255, 0, 0)),(390,20))
|
|
|
pygame.display.update()
|
|
|
clock.tick(FPS)
|
|
|
|
|
|
|
|
|
#"说明"界面实现
|
|
|
def bangzhu():
|
|
|
pygame.init()
|
|
|
bangzhu_page=pygame.display.set_mode((900,600))
|
|
|
pygame.display.set_caption("华容道拼图游戏")
|
|
|
bangzhu_page.blit(background_image, (0, 0))
|
|
|
|
|
|
font = pygame.font.SysFont('KaiTi', 25)
|
|
|
text1 = font.render('华容道是古老的中国民间益智游戏,以其变化多端、百玩不厌', True, (0,0,0))
|
|
|
text2 = font.render('的特点与魔方、独立钻石棋一起被国外智力专家并称为"智力', True, (0,0,0))
|
|
|
text3 = font.render('游戏界的三个不可思议"。它与七巧板、九连环等中国传统益', True, (0,0,0))
|
|
|
text4 = font.render('智玩具还有个代名词叫作"中国的难题"。', True, (0,0,0))
|
|
|
text5 = font.render('1. 点击空白区块,相邻图片将向空白区块移动', True, (0,0,0))
|
|
|
text6 = font.render('2. 将所有分割后的图片拼成完整的图片即可通关', True, (0,0,0))
|
|
|
text7 = font.render('3. 3*3、4*4、5*5, 3种难度等级并有时间限制等你来挑战!', True, (0,0,0))
|
|
|
tip = pygame.Surface((700, 300))
|
|
|
tip.fill(color = (255,255,255))
|
|
|
tip.blit(text1, (20, 30))
|
|
|
tip.blit(text2, (20, 55))
|
|
|
tip.blit(text3, (20, 80))
|
|
|
tip.blit(text4, (20, 105))
|
|
|
tip.blit(text5, (20, 155))
|
|
|
tip.blit(text6, (20, 180))
|
|
|
tip.blit(text7, (20, 205))
|
|
|
bangzhu_page.blit(tip, (100, 70))
|
|
|
|
|
|
button = pygame.Rect(370, 450, 140, 50)
|
|
|
font1 = pygame.font.SysFont('KaiTi', 30)
|
|
|
pygame.draw.rect(bangzhu_page, (255, 0, 0), button)
|
|
|
text = font1.render("返 回", True, BLACK)
|
|
|
bangzhu_page.blit(text, (button.x + 10, button.y + 10))
|
|
|
|
|
|
pygame.display.flip()
|
|
|
|
|
|
running = True
|
|
|
while running:
|
|
|
for event in pygame.event.get():
|
|
|
if event.type == pygame.QUIT:
|
|
|
running = False
|
|
|
pygame.quit()
|
|
|
sys.exit()
|
|
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
|
|
if button.collidepoint(event.pos):
|
|
|
pygame.quit()
|
|
|
main_init()
|
|
|
|
|
|
#"反馈"界面pygame实现
|
|
|
def fankui1():
|
|
|
pygame.init()
|
|
|
fankui_page=pygame.display.set_mode((900,600))
|
|
|
pygame.display.set_caption("华容道拼图游戏")
|
|
|
fankui_page.blit(background_image, (0, 0))
|
|
|
|
|
|
button1 = pygame.Rect(250, 500, 140, 50)
|
|
|
button2 = pygame.Rect(500, 500, 140, 50)
|
|
|
font = pygame.font.SysFont('KaiTi', 30)
|
|
|
pygame.draw.rect(fankui_page, (255, 0, 0), button1)
|
|
|
pygame.draw.rect(fankui_page, (255, 0, 0), button2)
|
|
|
text1 = font.render("提 交", True, BLACK)
|
|
|
text2 = font.render("取 消", True, BLACK)
|
|
|
fankui_page.blit(text1, (button1.x + 10, button1.y + 10))
|
|
|
fankui_page.blit(text2, (button2.x + 10, button2.y + 10))
|
|
|
|
|
|
text = font.render("请留下您宝贵的意见和建议", True, BLACK)
|
|
|
fankui_page.blit(text, (270,20))
|
|
|
pygame.display.flip()
|
|
|
|
|
|
font1 = pygame.font.SysFont('KaiTi', 25)
|
|
|
input_rect = pygame.Rect(100, 70, 700, 350)
|
|
|
color = pygame.Color((255,255,255))
|
|
|
active = False
|
|
|
text = '请输入:'
|
|
|
|
|
|
text_count = 0
|
|
|
running = True
|
|
|
while running:
|
|
|
for event in pygame.event.get():
|
|
|
if event.type == pygame.QUIT:
|
|
|
running = False
|
|
|
pygame.quit()
|
|
|
sys.exit()
|
|
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
|
|
if button1.collidepoint(event.pos):
|
|
|
if text != ' ' and text != '请输入:':
|
|
|
with open('feedback_pygame/count.txt', 'r', encoding='utf-8') as file:
|
|
|
count = int(file.read())
|
|
|
count += 1
|
|
|
with open('feedback_pygame/message-{}.txt'.format(count), 'w', encoding='utf-8') as file:
|
|
|
file.write(text)
|
|
|
with open('feedback_pygame/count.txt'.format(count), 'w', encoding='utf-8') as file:
|
|
|
file.write(str(count))
|
|
|
window1 = tk.Tk()
|
|
|
window1.title("提交成功")
|
|
|
window1.geometry("300x150+600+300")
|
|
|
label1 = tk.Label(window1, text="提交成功", font=("Arial", 24))
|
|
|
label1.pack() # 将标签添加到窗口中并显示出来
|
|
|
label2 = tk.Label(window1, text="感谢您的反馈", font=("Arial", 18))
|
|
|
label2.pack()
|
|
|
button = tk.Button(window1, text="确 认", font=("Arial", 18), command=window1.destroy)
|
|
|
button.pack()
|
|
|
window1.mainloop()
|
|
|
pygame.quit()
|
|
|
main_init()
|
|
|
elif button2.collidepoint(event.pos):
|
|
|
pygame.quit()
|
|
|
main_init()
|
|
|
# 如果鼠标点击了输入区域,激活输入状态
|
|
|
elif input_rect.collidepoint(event.pos):
|
|
|
active = not active
|
|
|
if active==True:
|
|
|
text=''
|
|
|
else:
|
|
|
text='请输入:'
|
|
|
else:
|
|
|
active = False
|
|
|
text='请输入:'
|
|
|
if event.type == pygame.KEYDOWN:
|
|
|
# 如果输入状态激活,处理键盘输入
|
|
|
if active:
|
|
|
if event.key == pygame.K_RETURN:
|
|
|
text += '\n' # 按下回车键时添加换行符
|
|
|
text_count = 0
|
|
|
elif event.key == pygame.K_BACKSPACE:
|
|
|
text = text[:-1]
|
|
|
text_count -= 1
|
|
|
else:
|
|
|
text += event.unicode
|
|
|
text_count += 1
|
|
|
if text_count == 52:
|
|
|
text += '\n'
|
|
|
text_count = 0
|
|
|
|
|
|
|
|
|
# 绘制输入区域
|
|
|
pygame.draw.rect(fankui_page, color, input_rect)
|
|
|
lines = text.split('\n')
|
|
|
for i, line in enumerate(lines):
|
|
|
text_surface = font1.render(line, True, BLACK)
|
|
|
fankui_page.blit(text_surface, (input_rect.x + 5, input_rect.y + 5 + i*font.get_linesize()))
|
|
|
pygame.draw.rect(fankui_page, BLACK, input_rect, 2)
|
|
|
pygame.display.flip()
|
|
|
|
|
|
|
|
|
#"反馈"界面tkinter实现
|
|
|
def fankui2():
|
|
|
def tijiao():
|
|
|
def queren():
|
|
|
window1.destroy
|
|
|
root.destroy()
|
|
|
main_init()
|
|
|
|
|
|
feedback = text.get("1.0", "end-1c") # 获取输入框中的文本内容
|
|
|
with open('feedback_tkinter/count.txt', 'r', encoding='utf-8') as file:
|
|
|
count = int(file.read())
|
|
|
count += 1
|
|
|
with open('feedback_tkinter/message-{}.txt'.format(count), 'w', encoding='utf-8') as file:
|
|
|
file.write(feedback)
|
|
|
with open('feedback_tkinter/count.txt'.format(count), 'w', encoding='utf-8') as file:
|
|
|
file.write(str(count))
|
|
|
window1 = tk.Tk()
|
|
|
window1.title("提交成功")
|
|
|
window1.geometry("300x150+600+300")
|
|
|
label1 = tk.Label(window1, text="反馈成功", font=("KaiTi", 24))
|
|
|
label1.pack() # 将标签添加到窗口中并显示出来
|
|
|
label2 = tk.Label(window1, text="感谢您的反馈", font=("KaiTi", 18))
|
|
|
label2.pack()
|
|
|
button = tk.Button(window1, text="确 认", font=("KaiTi", 18), command=queren)
|
|
|
button.pack()
|
|
|
window1.mainloop()
|
|
|
|
|
|
|
|
|
def fanhui():
|
|
|
root.destroy()
|
|
|
main_init()
|
|
|
|
|
|
|
|
|
root = tk.Tk()
|
|
|
root.title("华容道拼图游戏")
|
|
|
root.geometry("900x600+310+100") # 设置窗口大小和位置
|
|
|
image = Image.open("background.jpg")
|
|
|
# 将图片转换为Tkinter所支持的格式
|
|
|
background_image = ImageTk.PhotoImage(image)
|
|
|
# 创建Label组件并设置背景图片
|
|
|
background_label = tk.Label(root, image=background_image)
|
|
|
background_label.place(relwidth=1, relheight=1)
|
|
|
|
|
|
info_label = tk.Label(root, text="请留下您宝贵的意见和建议", font=('KaiTi', 24))
|
|
|
info_label.place(relx=0.5, rely=0.1, anchor='center')
|
|
|
info_label.configure(bg='red')
|
|
|
|
|
|
text = tk.Text(root, font=('KaiTi', 16), height=15, width=60)
|
|
|
text.place(x=120, y=100)
|
|
|
|
|
|
submit_button = tk.Button(root, text="提 交", command=tijiao, width=8, height=1, font=('KaiTi', 22), bg='red') # 创建提交按钮并设置字体大小为12
|
|
|
submit_button.place(x=250, y=500)
|
|
|
|
|
|
cancel_button = tk.Button(root, text="取 消", command=fanhui, width=8, height=1, font=('KaiTi', 22), bg='red') # 创建取消按钮
|
|
|
cancel_button.place(x=500, y=500)
|
|
|
|
|
|
root.mainloop()
|
|
|
|
|
|
|
|
|
#"查看最快记录"界面实现
|
|
|
def jilu():
|
|
|
pygame.init()
|
|
|
julu_page=pygame.display.set_mode((900,600))
|
|
|
pygame.display.set_caption("华容道拼图游戏")
|
|
|
julu_page.blit(background_image, (0, 0))
|
|
|
|
|
|
button = pygame.Rect(370, 450, 140, 50)
|
|
|
font = pygame.font.SysFont('KaiTi', 30)
|
|
|
pygame.draw.rect(julu_page, (255, 0, 0), button)
|
|
|
text = font.render("返 回", True, BLACK)
|
|
|
julu_page.blit(text, (button.x + 10, button.y + 10))
|
|
|
|
|
|
input_rect = pygame.Rect(80, 130, 740, 250)
|
|
|
pygame.draw.rect(julu_page, (255,255,255), input_rect)
|
|
|
font1 = pygame.font.SysFont('KaiTi', 30)
|
|
|
|
|
|
with open('record/note_size3.txt', 'r', encoding='utf-8') as file:
|
|
|
note_3 = file.read()
|
|
|
with open('record/note_size4.txt', 'r', encoding='utf-8') as file:
|
|
|
note_4 = file.read()
|
|
|
with open('record/note_size5.txt', 'r', encoding='utf-8') as file:
|
|
|
note_5 = file.read()
|
|
|
if note_3 == '暂无记录':
|
|
|
text_3 = font1.render('普通难度:暂无记录...', True, BLACK)
|
|
|
else:
|
|
|
temp = note_3.split(' ')
|
|
|
text = f'普通难度:最快{temp[0]}s,于{temp[1]}日{temp[2]}创下'
|
|
|
text_3 = font1.render(text, True, BLACK)
|
|
|
if note_4 == '暂无记录':
|
|
|
text_4 = font1.render('困难难度:暂无记录...', True, BLACK)
|
|
|
else:
|
|
|
temp = note_4.split(' ')
|
|
|
text = f'困难难度:最快{temp[0]}s,于{temp[1]}日{temp[2]}创下'
|
|
|
text_4 = font1.render(text, True, BLACK)
|
|
|
if note_5 == '暂无记录':
|
|
|
text_5 = font1.render('地狱难度:暂无记录...', True, BLACK)
|
|
|
else:
|
|
|
temp = note_5.split(' ')
|
|
|
text = f'地狱难度:最快{temp[0]}s,于{temp[1]}日{temp[2]}创下'
|
|
|
text_5 = font1.render(text, True, BLACK)
|
|
|
|
|
|
julu_page.blit(text_3, (input_rect.x + 20, input_rect.y + 40))
|
|
|
julu_page.blit(text_4, (input_rect.x + 20, input_rect.y + 110))
|
|
|
julu_page.blit(text_5, (input_rect.x + 20, input_rect.y + 180))
|
|
|
|
|
|
pygame.display.flip()
|
|
|
|
|
|
running = True
|
|
|
while running:
|
|
|
for event in pygame.event.get():
|
|
|
if event.type == pygame.QUIT:
|
|
|
running = False
|
|
|
pygame.quit()
|
|
|
sys.exit()
|
|
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
|
|
if button.collidepoint(event.pos):
|
|
|
pygame.quit()
|
|
|
main_init()
|
|
|
|
|
|
#初始化主界面
|
|
|
def main_init():
|
|
|
pygame.init()
|
|
|
main_page=pygame.display.set_mode((main_page_width,main_page_height))
|
|
|
pygame.display.set_caption("华容道拼图游戏")
|
|
|
main_page.blit(background_image, (0, 0))
|
|
|
|
|
|
font0 = pygame.font.SysFont('KaiTi', 50)
|
|
|
text0 = font0.render('华 容 道 拼 图 游 戏', True, BLACK)
|
|
|
main_page.blit(text0, (250, 40))
|
|
|
|
|
|
font1 = pygame.font.SysFont('KaiTi', 30)
|
|
|
text1 = font1.render('专 业: 计算机科学与技术', True, BLACK)
|
|
|
main_page.blit(text1, (320, 120))
|
|
|
text2 = font1.render('班 级: 计科2105', True, BLACK)
|
|
|
main_page.blit(text2, (320, 160))
|
|
|
text3 = font1.render('学生姓名: 向玉鑫', True, BLACK)
|
|
|
main_page.blit(text3, (320, 200))
|
|
|
text4 = font1.render('学 号: 21412030528', True, BLACK)
|
|
|
main_page.blit(text4, (320, 240))
|
|
|
text5 = font1.render('指导老师: 肖哲', True, BLACK)
|
|
|
main_page.blit(text5, (320, 280))
|
|
|
|
|
|
button2 = pygame.Rect(200, 400, 140, 50)
|
|
|
button3 = pygame.Rect(400, 400, 140, 50)
|
|
|
button4 = pygame.Rect(600, 400, 140, 50)
|
|
|
button5 = pygame.Rect(200, 480, 140, 50)
|
|
|
button6 = pygame.Rect(400, 480, 140, 50)
|
|
|
button7 = pygame.Rect(600, 480, 140, 50)
|
|
|
button8 = pygame.Rect(400, 560, 140, 50)
|
|
|
button9 = pygame.Rect(600, 560, 140, 50)
|
|
|
|
|
|
# 绘制按钮
|
|
|
pygame.draw.rect(main_page, button_color, button2)
|
|
|
pygame.draw.rect(main_page, button_color, button3)
|
|
|
pygame.draw.rect(main_page, button_color, button4)
|
|
|
pygame.draw.rect(main_page, button_color, button5)
|
|
|
pygame.draw.rect(main_page, button_color, button6)
|
|
|
pygame.draw.rect(main_page, button_color, button7)
|
|
|
pygame.draw.rect(main_page, button_color, button8)
|
|
|
pygame.draw.rect(main_page, button_color, button9)
|
|
|
|
|
|
# 在按钮上显示文字
|
|
|
font2 = pygame.font.SysFont('KaiTi', 24)
|
|
|
text2 = font1.render("普通难度", True, BLACK)
|
|
|
text3 = font1.render("困难难度", True, BLACK)
|
|
|
text4 = font1.render("地狱难度", True, BLACK)
|
|
|
text5 = font1.render("说 明", True, BLACK)
|
|
|
text6 = font2.render("反馈pygame", True, BLACK)
|
|
|
text7 = font1.render("最快记录", True, BLACK)
|
|
|
text8 = font1.render("退出游戏", True, BLACK)
|
|
|
text9 = font2.render("反馈tkinter", True, BLACK)
|
|
|
main_page.blit(text2, (button2.x + 10, button2.y + 10))
|
|
|
main_page.blit(text3, (button3.x + 10, button3.y + 10))
|
|
|
main_page.blit(text4, (button4.x + 10, button4.y + 10))
|
|
|
main_page.blit(text5, (button5.x + 10, button5.y + 10))
|
|
|
main_page.blit(text6, (button6.x + 10, button6.y + 10))
|
|
|
main_page.blit(text7, (button7.x + 10, button7.y + 10))
|
|
|
main_page.blit(text8, (button8.x + 10, button8.y + 10))
|
|
|
main_page.blit(text9, (button9.x + 5, button9.y + 10))
|
|
|
pygame.display.flip()
|
|
|
|
|
|
global tmp_size
|
|
|
running = True
|
|
|
while running:
|
|
|
for event in pygame.event.get():
|
|
|
if event.type == pygame.QUIT:
|
|
|
running = False
|
|
|
pygame.quit()
|
|
|
sys.exit()
|
|
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
|
|
if button2.collidepoint(event.pos):
|
|
|
tmp_size = 3
|
|
|
pygame.quit()
|
|
|
game_init()
|
|
|
elif button3.collidepoint(event.pos):
|
|
|
tmp_size = 4
|
|
|
pygame.quit()
|
|
|
game_init()
|
|
|
elif button4.collidepoint(event.pos):
|
|
|
tmp_size = 5
|
|
|
pygame.quit()
|
|
|
game_init()
|
|
|
elif button5.collidepoint(event.pos):
|
|
|
pygame.quit()
|
|
|
bangzhu()
|
|
|
elif button6.collidepoint(event.pos):
|
|
|
pygame.quit()
|
|
|
fankui1()
|
|
|
elif button7.collidepoint(event.pos):
|
|
|
pygame.quit()
|
|
|
jilu()
|
|
|
elif button8.collidepoint(event.pos):
|
|
|
pygame.quit()
|
|
|
sys.exit()
|
|
|
elif button9.collidepoint(event.pos):
|
|
|
pygame.quit()
|
|
|
fankui2()
|
|
|
|
|
|
#初始化游戏界面
|
|
|
def game_init():
|
|
|
pygame.init()
|
|
|
game_page=pygame.display.set_mode((main_page_width,main_page_height))
|
|
|
pygame.display.set_caption("华容道拼图游戏")
|
|
|
game_page.blit(background_image, (0, 0))
|
|
|
|
|
|
#定义时间变量
|
|
|
global game_start,start_time,elapsed_time
|
|
|
game_start=0
|
|
|
start_time=None
|
|
|
elapsed_time=0#使用时间
|
|
|
over_time=tmp_size*tmp_size*20
|
|
|
|
|
|
|
|
|
clock=pygame.time.Clock()
|
|
|
q=Qipan(tmp_size)
|
|
|
NOb=pygame.USEREVENT+1
|
|
|
pygame.time.set_timer(NOb,1000)
|
|
|
times=0
|
|
|
|
|
|
img_dict=["1","2","3"]
|
|
|
rand_nob1 = random.randint(0,2)
|
|
|
global rand_nob
|
|
|
global rand_photo
|
|
|
rand_nob=rand_nob1
|
|
|
rand_photo=img_dict[rand_nob]
|
|
|
img=Image.open('photo/{}.jpg'.format(rand_photo))
|
|
|
img=img.resize((photo_width,photo_height))
|
|
|
img.save("photo/{}.jpg".format(rand_photo))
|
|
|
#重新分割图片
|
|
|
img=Image.open('photo/{}.jpg'.format(rand_photo))
|
|
|
img_width=img.width
|
|
|
img_height=img.height
|
|
|
img_we=img_width/tmp_size
|
|
|
img_he=img_height/tmp_size
|
|
|
#创建切片元组
|
|
|
img_list=[]
|
|
|
count=1
|
|
|
for i in range(tmp_size):
|
|
|
for j in range(tmp_size):
|
|
|
leftup_y=i*img_he
|
|
|
leftup_x=j*img_we
|
|
|
rightdown_y=(i+1)*img_he
|
|
|
rightdown_x=(j+1)*img_we
|
|
|
tmp_s=(leftup_x,leftup_y,rightdown_x,rightdown_y)
|
|
|
tmp_img=img.crop(tmp_s)
|
|
|
tmp_img=tmp_img.resize((qizi_size,qizi_size))
|
|
|
img_list.append(tmp_img)
|
|
|
tmp_img.save('photo/img{}.jpg'.format(count))
|
|
|
count+=1
|
|
|
|
|
|
while True:
|
|
|
if q.win():
|
|
|
break
|
|
|
if elapsed_time==over_time:
|
|
|
break
|
|
|
if game_start==1:
|
|
|
elapsed_time=int(time.time()-start_time)
|
|
|
|
|
|
times=press(False,q,NOb,times)
|
|
|
if times==-1:
|
|
|
break
|
|
|
game_page.blit(background_image, (0, 0))
|
|
|
print_text(game_page, pygame.font.SysFont('KaiTi', 50), 50, 20, '%03d' % (over_time-elapsed_time), (200,40,40))
|
|
|
draw_img(q,game_page)
|
|
|
pygame.display.update()
|
|
|
clock.tick(FPS)
|
|
|
elapsed_time=int(time.time()-start_time)
|
|
|
if elapsed_time==over_time:
|
|
|
game_over_lose(game_page,q,clock,text="游戏失败!")
|
|
|
elif not times==-1:
|
|
|
text = "游戏胜利,总共用时:"+str(elapsed_time)+"s"
|
|
|
with open('record/note_size{}.txt'.format(tmp_size), 'r', encoding='utf-8') as file:
|
|
|
note = file.read()
|
|
|
if note == '暂无记录':
|
|
|
current_time = datetime.datetime.now()
|
|
|
year = current_time.year
|
|
|
month = current_time.month
|
|
|
day = current_time.day
|
|
|
hour = current_time.hour
|
|
|
minute = current_time.minute
|
|
|
second = current_time.second
|
|
|
content = '{:03d}'.format(elapsed_time)+' '+str(year)+'-'+'{:02d}'.format(month)+'-'+'{:02d}'.format(day)+' '+'{:02d}'.format(hour)+':'+'{:02d}'.format(minute)+':'+'{:02d}'.format(second)
|
|
|
with open('record/note_size{}.txt'.format(tmp_size), 'w', encoding='utf-8') as file:
|
|
|
file.write(content)
|
|
|
else:
|
|
|
temp = note.split(' ')
|
|
|
if int(elapsed_time) < int(temp[0]):
|
|
|
text = "游戏胜利,总共用时:"+str(elapsed_time)+"s\n恭喜你打破最快记录"
|
|
|
current_time = datetime.datetime.now()
|
|
|
year = current_time.year
|
|
|
month = current_time.month
|
|
|
day = current_time.day
|
|
|
hour = current_time.hour
|
|
|
minute = current_time.minute
|
|
|
second = current_time.second
|
|
|
content = '{:03d}'.format(elapsed_time)+' '+str(year)+'-'+'{:02d}'.format(month)+'-'+'{:02d}'.format(day)+' '+'{:02d}'.format(hour)+':'+'{:02d}'.format(minute)+':'+'{:02d}'.format(second)
|
|
|
with open('record/note_size{}.txt'.format(tmp_size), 'w', encoding='utf-8') as file:
|
|
|
file.write(content)
|
|
|
game_over_win(game_page,q,clock,tmp_size,text)
|
|
|
|
|
|
def main():
|
|
|
main_init()
|
|
|
|
|
|
|
|
|
if __name__=="__main__":
|
|
|
while True:
|
|
|
main() |