python实现拼图小游戏


Posted in Python onFebruary 22, 2020

Python小白一只,正在成长,程序自己设计,很多不足,算法很多地方能优化。欢迎大佬来指教。

游戏效果

python实现拼图小游戏

创建设置类,储存游戏基础数据

可以不使用这个类,在程序中直接使用相应的数据。但是使用这个类更便于程序阅读和修改基础数据。

class Settings:
 def __init__(self):
 self.picture_num = 4 # 每行图片数
 self.screen_width = 408 # 窗口宽度
 self.screen_length = 809 # 窗口长度
 self.picture_length = 100 # 每个正方形图片的长
 self.screen_bgcol = (96, 127, 255) # 背景颜色
 self.picture_bian = 1 # 每个图片的边缘宽度 ,便于分清每个照片
 self.picture_distance = 102 # 两个图片之间的距离

创建图片类,储存游戏需要的图片

这样可以在游戏的开始把游戏用到的图片一起读到内存,显示照片时直接使用创建的图像对象列表即可。
类的构造函数要接收一个数字,按着这个数字读生成相应图片的路径和名称 picture_name。在按照这个打开相应的照片。
pygame相应方法可以简单学习一下。

class Picture:
 def __init__(self, num):
 self.picture_name = 'images/p{}.gif'.format(num)
 self.picture = pygame.image.load(self.picture_name) # 打开照片
 self.picture_rect = self.picture.get_rect() # 获得照片属性类
 def display_picture(self, screen, x, y): # 在屏幕上显示图片方法
 self.picture_rect.x = x
 self.picture_rect.y = y
 screen.blit(self.picture, self.picture_rect)

生成初始数据,创建窗口

游戏数据用两个4*4二维列表存储,一个存储图片位置,一个存储图片对象。
游戏开始,图片的顺序的应该是乱的。
先要对数据进行打乱,打乱时要按照原有的顺序打乱,不然可能会出现图片不可能复原的情况。

数据打乱函数

def data_begin(caozuoshu, p0, data):
 for i in caozuoshu:
 move(i, p0, data)

def move(i, p0, data):
 if i == 3 and p0[1] > 0:
 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]][p0[1]-1]
 data[p0[0]][p0[1]-1] = t
 p0[1] -= 1
 elif i == 4 and p0[1] < 3:
 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]][p0[1]+1]
 data[p0[0]][p0[1]+1] = t
 p0[1] += 1
 elif i == 1 and p0[0] > 0:
 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]-1][p0[1]]
 data[p0[0]-1][p0[1]] = t
 p0[0] -= 1
 elif i == 2 and p0[0] < 3:
 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]+1][p0[1]]
 data[p0[0]+1][p0[1]] = t
 p0[0] += 1

def create_caozuoshu():
 n = 30
 caozuo = [1, 2, 3, 4]
 caozuoshu = []
 for i in range(n):
 caozuoshu.append(random.choice(caozuo))
 return caozuoshu

这样之后,把data列表打乱

在按照data生成picture列表

def create_pictures(picture, data, set):
 for i in range(set.picture_num):
 for j in range(set.picture_num):
  p = Picture(data[i][j])
  picture[i][j] = p

创建窗口函数

def screen_create(set):
 pygame.init()
 screen = pygame.display.set_mode((set.screen_length, set.screen_width))
 pygame.display.set_caption("拼图")
 return screen

主函数

if __name__ == '__main__':
 set = Settings()
 # 初始数据
 data = [[9, 1, 3, 4],
  [2, 16, 14, 8],
  [6, 10, 5, 12],
  [13, 7, 11, 15]]
 p0 = [1, 1]
 caozuoshu = create_caozuoshu()
 data_begin(caozuoshu, p0, data)
 bushu = [0]
 # 创建图片
 picture = [[None, None, None, None],
  [None, None, None, None],
  [None, None, None, None],
  [None, None, None, None]]
 yuantu = Picture(17)
 create_pictures(picture, data, set) # 按照data生成相应顺序的picture列表
 # 创建窗口
 screen = screen_create(set)

 # 游戏主循环
 while True:
 check_events(picture, p0, data, bushu)
 screen_updata(picture, screen, set, yuantu)

响应按键控制

响应按键是,picture和data列表都要同步改变,data用来判断是否拼图完成。

响应按键,产生相应的控制

def check_events(picture, p0, data, bushu):
 for event in pygame.event.get():
 if event.type == pygame.QUIT:
  sys.exit()
 elif event.type == pygame.KEYDOWN and game_over(data, set, bushu):
  if event.key == pygame.K_DOWN and p0[0] > 0:
  xinhao = 1
  bushu[0] += 1
  updata(xinhao, picture, p0, data)
  elif event.key == pygame.K_UP and p0[0] < 3:
  xinhao = 2
  bushu[0] += 1
  updata(xinhao, picture, p0, data)
  elif event.key == pygame.K_RIGHT and p0[1] > 0:
  xinhao = 3
  bushu[0] += 1
  updata(xinhao, picture, p0, data)
  elif event.key == pygame.K_LEFT and p0[1] < 3:
  xinhao = 4
  bushu[0] += 1
  updata(xinhao, picture, p0, data)

按照控制数,更新picture和data

def updata(xinhao, picture, p0, data):
 if xinhao == 3:
 tmp = picture[p0[0]][p0[1]]
 picture[p0[0]][p0[1]] = picture[p0[0]][p0[1]-1]
 picture[p0[0]][p0[1]-1] = tmp

 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]][p0[1]-1]
 data[p0[0]][p0[1]-1] = t
 p0[1] -= 1

 elif xinhao == 4:
 tmp = picture[p0[0]][p0[1]]
 picture[p0[0]][p0[1]] = picture[p0[0]][p0[1] + 1]
 picture[p0[0]][p0[1] + 1] = tmp

 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]][p0[1]+1]
 data[p0[0]][p0[1]+1] = t
 p0[1] += 1
 elif xinhao == 1:
 tmp = picture[p0[0]][p0[1]]
 picture[p0[0]][p0[1]] = picture[p0[0] - 1][p0[1]]
 picture[p0[0] - 1][p0[1]] = tmp

 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]-1][p0[1]]
 data[p0[0]-1][p0[1]] = t
 p0[0] -= 1
 elif xinhao == 2:
 tmp = picture[p0[0]][p0[1]]
 picture[p0[0]][p0[1]] = picture[p0[0] + 1][p0[1]]
 picture[p0[0] + 1][p0[1]] = tmp

 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0] + 1][p0[1]]
 data[p0[0] +1][p0[1]] = t
 p0[0] += 1

判断是否拼图完成

def game_over(data, set,bushu):
 datao = [[1, 2, 3, 4],
  [5, 6, 7, 8],
  [9, 10, 11, 12],
  [13, 14, 15, 16]]
 for i in range(set.picture_num):
 for j in range(set.picture_num):
  if datao[i][j] != data[i][j]:
  return True
 print("牛逼!\n 游戏结束!\n 步数:{}".format(bushu[0]))
 return False

此函数要在响应按键函数中实时使用,监测是否完成拼图。

完整程序

import pygame
import random
import sys

class Settings:
 def __init__(self):
 self.picture_num = 4
 self.screen_width = 408
 self.screen_length = 809
 self.picture_length = 100
 self.screen_bgcol = (96, 127, 255)
 self.picture_speed = 5
 self.picture_bian = 1
 self.picture_distance = 102

class Picture:
 def __init__(self, num):
 self.picture_name = 'images/p{}.gif'.format(num)
 self.picture = pygame.image.load(self.picture_name)
 self.picture_rect = self.picture.get_rect()
 def display_picture(self, screen, x, y):
 self.picture_rect.x = x
 self.picture_rect.y = y
 screen.blit(self.picture, self.picture_rect)
'''def data_begin(data,p0):
 n = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
 ns = 16
 for i in range(4):
 for j in range(4):
  num = random.randint(0, ns-1)
  ns -= 1
  data[i][j] = n.pop(num)
  if data[i][j] == 16:
  p0[0] = i
  p0[1] = j'''
def data_begin(caozuoshu, p0, data):
 for i in caozuoshu:
 move(i, p0, data)

def move(i, p0, data):
 if i == 3 and p0[1] > 0:
 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]][p0[1]-1]
 data[p0[0]][p0[1]-1] = t
 p0[1] -= 1
 elif i == 4 and p0[1] < 3:
 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]][p0[1]+1]
 data[p0[0]][p0[1]+1] = t
 p0[1] += 1
 elif i == 1 and p0[0] > 0:
 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]-1][p0[1]]
 data[p0[0]-1][p0[1]] = t
 p0[0] -= 1
 elif i == 2 and p0[0] < 3:
 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]+1][p0[1]]
 data[p0[0]+1][p0[1]] = t
 p0[0] += 1

def create_caozuoshu():
 n = 30
 caozuo = [1, 2, 3, 4]
 caozuoshu = []
 for i in range(n):
 caozuoshu.append(random.choice(caozuo))
 return caozuoshu

def create_pictures(picture, data, set):
 for i in range(set.picture_num):
 for j in range(set.picture_num):
  p = Picture(data[i][j])
  picture[i][j] = p

def screen_updata(picture, screen, set, yuantu):
 screen.fill(set.screen_bgcol)
 x, y = 402, set.picture_bian
 for i in range(set.picture_num):
 for j in range(set.picture_num):
  picture[i][j].display_picture(screen, x, y)
  x += set.picture_distance
 x = 402
 y += set.picture_distance
 yuantu.display_picture(screen, 1, 4)
 pygame.display.flip()

def screen_create(set):
 pygame.init()
 screen = pygame.display.set_mode((set.screen_length, set.screen_width))
 pygame.display.set_caption("拼图")
 return screen

def game_over(data, set,bushu):
 datao = [[1, 2, 3, 4],
  [5, 6, 7, 8],
  [9, 10, 11, 12],
  [13, 14, 15, 16]]
 for i in range(set.picture_num):
 for j in range(set.picture_num):
  if datao[i][j] != data[i][j]:
  return True
 print("牛逼!\n 游戏结束!\n 步数:{}".format(bushu[0]))
 return False

def updata(xinhao, picture, p0, data):
 if xinhao == 3:
 tmp = picture[p0[0]][p0[1]]
 picture[p0[0]][p0[1]] = picture[p0[0]][p0[1]-1]
 picture[p0[0]][p0[1]-1] = tmp

 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]][p0[1]-1]
 data[p0[0]][p0[1]-1] = t
 p0[1] -= 1

 elif xinhao == 4:
 tmp = picture[p0[0]][p0[1]]
 picture[p0[0]][p0[1]] = picture[p0[0]][p0[1] + 1]
 picture[p0[0]][p0[1] + 1] = tmp

 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]][p0[1]+1]
 data[p0[0]][p0[1]+1] = t
 p0[1] += 1
 elif xinhao == 1:
 tmp = picture[p0[0]][p0[1]]
 picture[p0[0]][p0[1]] = picture[p0[0] - 1][p0[1]]
 picture[p0[0] - 1][p0[1]] = tmp

 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]-1][p0[1]]
 data[p0[0]-1][p0[1]] = t
 p0[0] -= 1
 elif xinhao == 2:
 tmp = picture[p0[0]][p0[1]]
 picture[p0[0]][p0[1]] = picture[p0[0] + 1][p0[1]]
 picture[p0[0] + 1][p0[1]] = tmp

 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0] + 1][p0[1]]
 data[p0[0] +1][p0[1]] = t
 p0[0] += 1
 #print(data)

def check_events(picture, p0, data, bushu):
 for event in pygame.event.get():
 if event.type == pygame.QUIT:
  sys.exit()
 elif event.type == pygame.KEYDOWN and game_over(data, set, bushu):
  if event.key == pygame.K_DOWN and p0[0] > 0:
  xinhao = 1
  bushu[0] += 1
  updata(xinhao, picture, p0, data)
  elif event.key == pygame.K_UP and p0[0] < 3:
  xinhao = 2
  bushu[0] += 1
  updata(xinhao, picture, p0, data)
  elif event.key == pygame.K_RIGHT and p0[1] > 0:
  xinhao = 3
  bushu[0] += 1
  updata(xinhao, picture, p0, data)
  elif event.key == pygame.K_LEFT and p0[1] < 3:
  xinhao = 4
  bushu[0] += 1
  updata(xinhao, picture, p0, data)

if __name__ == '__main__':
 set = Settings()
 # 初始数据
 data = [[9, 1, 3, 4],
  [2, 16, 14, 8],
  [6, 10, 5, 12],
  [13, 7, 11, 15]]
 p0 = [1, 1]
 caozuoshu = create_caozuoshu()
 data_begin(caozuoshu, p0, data)
 bushu = [0]
 # 创建图片
 picture = [[None, None, None, None],
  [None, None, None, None],
  [None, None, None, None],
  [None, None, None, None]]
 yuantu = Picture(17)
 create_pictures(picture, data, set)
 # 创建窗口
 screen = screen_create(set)

 # 游戏主循环
 while True:
 check_events(picture, p0, data, bushu)
 screen_updata(picture, screen, set, yuantu)

游戏用到的图片,图片位置和文件名要和程序中的一致

python实现拼图小游戏

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python实现dict版图遍历示例
Feb 19 Python
跟老齐学Python之有点简约的元组
Sep 24 Python
Python抓取百度查询结果的方法
Jul 08 Python
python3利用Dlib19.7实现人脸68个特征点标定
Feb 26 Python
Python实现的括号匹配判断功能示例
Aug 25 Python
Python爬虫小技巧之伪造随机的User-Agent
Sep 13 Python
python 基于dlib库的人脸检测的实现
Nov 08 Python
对tensorflow中的strides参数使用详解
Jan 04 Python
Python接口测试get请求过程详解
Feb 28 Python
Python 操作SQLite数据库的示例
Oct 16 Python
Python中的流程控制详解
Feb 18 Python
分享Python获取本机IP地址的几种方法
Mar 17 Python
Python双链表原理与实现方法详解
Feb 22 #Python
Python单链表原理与实现方法详解
Feb 22 #Python
python函数enumerate,operator和Counter使用技巧实例小结
Feb 22 #Python
python通过文本在一个图中画多条线的实例
Feb 21 #Python
python使用html2text库实现从HTML转markdown的方法详解
Feb 21 #Python
python-sys.stdout作为默认函数参数的实现
Feb 21 #Python
pycharm运行程序时看不到任何结果显示的解决
Feb 21 #Python
You might like
有关php运算符的知识大全
2011/11/03 PHP
PHP图片处理之使用imagecopyresampled函数实现图片缩放例子
2014/11/19 PHP
php解析xml方法实例详解
2015/05/12 PHP
PHP实现ASCII码与字符串相互转换的方法
2017/04/29 PHP
Laravel框架中Blade模板的用法示例
2017/08/30 PHP
为Plesk PHP7启用Oracle OCI8扩展方法总结
2019/03/29 PHP
从jQuery.camelCase()学习string.replace() 函数学习
2011/09/13 Javascript
jQuery点击tr实现checkbox选中的方法
2013/03/19 Javascript
js简单的弹出框有关闭按钮
2014/05/05 Javascript
js插件设置innerHTML时在IE8下提示“未知运行时错误”解决方法
2015/04/25 Javascript
jQuery调用WebMethod(PageMethod) NET2.0的方法
2016/04/15 Javascript
Jquery组件easyUi实现选项卡切换示例
2016/08/23 Javascript
angular实现form验证实例代码
2017/01/17 Javascript
javascript事件的传播基础实例讲解(35)
2017/02/14 Javascript
jquery.flot.js简单绘制折线图用法示例
2017/03/13 Javascript
Vue.js实现实例搜索应用功能详细代码
2017/08/24 Javascript
浅谈Vue.js中ref ($refs)用法举例总结
2017/12/19 Javascript
angular 数据绑定之[]和{{}}的区别
2018/09/25 Javascript
JavaScript的查询机制LHS和RHS解析
2019/08/16 Javascript
微信小程序中使用 async/await的方法实例分析
2020/05/06 Javascript
Vue 事件的$event参数=事件的值案例
2021/01/29 Vue.js
[37:23]DOTA2上海特级锦标赛主赛事日 - 3 胜者组第二轮#2Secret VS EG第二局
2016/03/04 DOTA
对Python中type打开文件的方式介绍
2018/04/28 Python
python opencv3实现人脸识别(windows)
2018/05/25 Python
python验证码识别教程之滑动验证码
2018/06/04 Python
WoolOvers澳洲官方网站:英国针织服装公司
2018/05/13 全球购物
英国文具、办公用品和科技商店:Ryman
2018/09/27 全球购物
使用useBean标志初始化BEAN时如何接受初始化参数
2012/02/11 面试题
请写出char *p与"零值"比较的if语句
2014/09/24 面试题
乡镇三项教育实施方案
2014/03/30 职场文书
招股说明书范本
2014/05/06 职场文书
党支部换届选举方案
2014/05/08 职场文书
优秀家长事迹材料
2014/05/17 职场文书
公司离职证明范本(5篇)
2014/09/17 职场文书
2015年控辍保学工作总结
2015/05/18 职场文书
2015年医院后勤工作总结
2015/05/20 职场文书