蒙娜丽莎的微笑小游戏(pygame)
前言
由于在网上找的蒙娜丽莎的图片不太好看就直接用数字代替了
# -*- coding: utf-8 -*-
# @Time : 2021/12/30 10:42
# @Author : wang
# @FileName: nine_puzzle.py
# @Software: PyCharm
import numpy as np
import random
import pygame
import sys
import time
import os
class Puzzle():
def __init__(self):
self.map_true = [[1, 2, 3],
[4, 5, 6],
[7, 8, 0]]
def make_shuffer_map(self):
"""
打乱二维数组的顺序
:return: 打乱的二维数组
"""
number_shuffer_list = [i for i in range(9)]
random.shuffle(number_shuffer_list)
show_map = np.array(number_shuffer_list).reshape((3, 3))
return show_map
def find_number(self, map, target_number):
"""
找到二维数组中的数字
:param map: 二维数组
:param target_number:目标数字
:return: 目标数字在二维数组中的索引
"""
for map_row_index in range(len(map)):
for map_column_index in range(len(map[map_row_index])):
if map[map_row_index][map_column_index] == target_number:
return map_row_index, map_column_index
def move_right(self, map):
r, c = self.find_number(map, 0)
if c - 1 < 0:
return
map[r][c], map[r][c - 1] = map[r][c - 1], map[r][c]
def move_left(self, map):
r, c = self.find_number(map, 0)
if c + 1 >= len(map):
return
map[r][c], map[r][c + 1] = map[r][c + 1], map[r][c]
def move_up(self, map):
r, c = self.find_number(map, 0) # 2,0 2,1
if r + 1 >= len(map):
return
map[r][c], map[r + 1][c] = map[r + 1][c], map[r][c]
def move_down(self, map):
r, c = self.find_number(map, 0)
if r - 1 < 0:
return
map[r][c], map[r - 1][c] = map[r - 1][c], map[r][c]
def sum_times(start_time, now_time):
"""
计算时间差值
:param start_time:
:param now_time:
:return:
"""
res = int(now_time) - int(start_time)
time_second = res % 60
time_minute = res // 60
return time_minute, time_second
def showText(screen, fontObj, text):
"""
显示时间
"""
textSurfaceObj = fontObj.render(text, False, (0, 0, 0), (255, 255, 255))
textRectObj = textSurfaceObj.get_rect()
textRectObj.center = (150, 550)
screen.blit(textSurfaceObj, textRectObj)
def main():
load_img_path = os.path.join(os.getcwd(), r"img\res") # 获取图片目录
img_dict = {} # 图片路径字典
img_count = 0
running = True
default_dict = {(0, 0): (10, 10), (0, 1): (140, 10), (0, 2): (270, 10), # 默认位置
(1, 0): (10, 180), (1, 1): (140, 180), (1, 2): (270, 180),
(2, 0): (10, 350), (2, 1): (140, 350), (2, 2): (270, 350)}
pygame.init()
if not pygame.font.get_init():
pygame.font.init()
# 显示时间
COUNT = pygame.USEREVENT + 1
pygame.time.set_timer(COUNT, 1000)
start_time = "%0.0f" % time.time()
my_font = pygame.font.SysFont("arial", 50) # 字体
# pygame初始化
size = width, height = 400, 600
screen = pygame.display.set_mode(size)
screen.fill((255, 255, 255))
puzzle = Puzzle()
map_true_return = {}
num_dict = {}
map_true = puzzle.map_true
for true_number in range(9):
true_r, true_c = puzzle.find_number(map_true, true_number)
map_true_return[true_number] = (true_r, true_c)
shuffer_map = puzzle.make_shuffer_map()
# 加载图片模块
for img_name in os.listdir(load_img_path):
img_path = os.path.join(load_img_path, img_name)
img_dict["map_number{}".format(img_count)] = img_path
img_count += 1
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
puzzle.move_left(shuffer_map)
elif event.key == pygame.K_RIGHT:
puzzle.move_right(shuffer_map)
elif event.key == pygame.K_DOWN:
puzzle.move_down(shuffer_map)
elif event.key == pygame.K_UP:
puzzle.move_up(shuffer_map)
elif event.key == pygame.K_r:
textSurfaceObj = my_font.render("GOOD", False, (255, 255, 255))
textRectObj = textSurfaceObj.get_rect()
textRectObj.center = (300, 550)
screen.blit(textSurfaceObj, textRectObj)
start_time = "%0.0f" % time.time()
shuffer_map = puzzle.make_shuffer_map()
running = True
if running:
now_time = "%0.0f" % time.time()
time_minute, time_second = sum_times(start_time, now_time)
draw_time = "{}:{}".format(str(time_minute).zfill(2), str(time_second).zfill(2))
showText(screen, my_font, draw_time)
for item in range(9):
r, c = puzzle.find_number(shuffer_map, item)
num_dict[item] = (r, c)
for k, v in num_dict.items():
screen.blit(pygame.image.load(img_dict.get(("map_number{}".format(k)))), default_dict.get(num_dict.get(k)))
pygame.display.update()
if num_dict == map_true_return:
textSurfaceObj = my_font.render("GOOD", False, (0, 0, 0))
textRectObj = textSurfaceObj.get_rect()
textRectObj.center = (300, 550)
screen.blit(textSurfaceObj, textRectObj)
running = False
continue
pygame.display.update()
pygame.quit()
if __name__ == '__main__':
main()
# -*- coding: utf-8 -*-
# @Time : 2021/12/30 17:59
# @Author :
# @FileName: resize.py
# @Software: PyCharm
"""
将图片缩放到120*160
"""
import cv2
import os
img_root_path = "./img/img"
img_res_path = "./img/res"
if not os.path.exists(img_res_path):
os.makedirs(img_res_path)
for i in os.listdir(img_root_path):
img_path = os.path.join(img_root_path, i)
im = cv2.imread(img_path)
res_img = cv2.resize(im, (120, 160))
img_name = "number" + (i.split("n")[1]).split(".")[0] + ".png"
res_img_path = os.path.join(img_res_path, img_name)
cv2.imwrite(res_img_path, res_img)
关于
这个小游戏有个BUG,就是每次随机出来的有可能拼不出来一个完整的图片