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)

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

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

(0)

相关推荐

  • Python加pyGame实现的简单拼图游戏实例

    本文实例讲述了Python加pyGame实现的简单拼图游戏.分享给大家供大家参考.具体实现方法如下: import pygame, sys, random from pygame.locals import * # 一些常量 WINDOWWIDTH = 500 WINDOWHEIGHT = 500 BACKGROUNDCOLOR = (255, 255, 255) BLUE = (0, 0, 255) BLACK = (0, 0, 0) FPS = 40 VHNUMS = 3 CELLNUMS

  • 利用python制作拼图小游戏的全过程

    开发工具 Python版本:3.6.4 相关模块: pygame模块: 以及一些Python自带的模块 关注公众号:Python学习指南,回复"拼图"即可获取源码 环境搭建 安装Python并添加到环境变量,pip安装需要的相关模块即可. 原理介绍 游戏简介: 将图像分为m×n个矩形块,并将图像右下角的矩形块替换为空白块后,将这些矩形块随机摆放成原图像的形状.游戏目标为通过移动非空白块将随机摆放获得的图像恢复成原图像的模样,且规定移动操作仅存在于非空白块移动到空白块. 例如下图所示:

  • OpenCV Python实现拼图小游戏

    基于OpenCV实现拼图版小游戏,供大家参考,具体内容如下 效果展示 实现 思路 1.对图像进行分割,分割成m*n个子图 2.打乱子图的顺序 3.将子图重新组成一幅新的图片并显示 4.添加鼠标点击响应动作,交换鼠标依次点击的两张图的位置 5.每次交换后,判断是否与原图是否一致 python代码 import cv2 as cv import numpy import random import math src = cv.imread("D:\\CvPic\\1.jpg") print

  • python实现拼图小游戏

    Python小白一只,正在成长,程序自己设计,很多不足,算法很多地方能优化.欢迎大佬来指教. 游戏效果 创建设置类,储存游戏基础数据 可以不使用这个类,在程序中直接使用相应的数据.但是使用这个类更便于程序阅读和修改基础数据. class Settings: def __init__(self): self.picture_num = 4 # 每行图片数 self.screen_width = 408 # 窗口宽度 self.screen_length = 809 # 窗口长度 self.pict

  • 使用vue.js编写蓝色拼图小游戏

    之前在网上看到<蓝色拼图>这款小游戏,作者是用jquery写的.于是便考虑能不能用vue.js优雅简单的编写出来呢? Later equals never!说干就干.首先理解游戏的规则:第一关为1*1的方块,第二关为2*2以此类推 该图为第三关3*3的方块.点击一个小方块,该方块和它相邻的方块的的颜色会从黄色变为蓝色,全部变为蓝色就过关了. 现在规则清楚了,开动吧! /*style*/ .game_bg{ background: #333; width: 600px; height: 600p

  • jQuery实现拼图小游戏(实例讲解)

    小熊维尼拼图 jQuery代码实现拼图小游戏,鼠标选中拼块,用上下左右键移动拼块. html代码 <div id="box-div"> <!--走不通时的提示!--> <div id="tips"> <p>\(╯-╰)/ 哎呦,走不通啦!</p> </div> <div id="container"> <div class="row"&g

  • 使用Python写一个小游戏

    引言 最近python语言大火,除了在科学计算领域python有用武之地之外,在游戏.后台等方面,python也大放异彩,本篇博文将按照正规的项目开发流程,手把手教大家写个python小游戏,来感受下其中的有趣之处.本次开发的游戏叫做alien invasion. 安装pygame并创建能左右移动的飞船 安装pygame 本人电脑是windows 10.python3.6,pygame下载地址: 传送门 请自行下载对应python版本的pygame 运行以下命令 $ pip install wh

  • Android实现拼图小游戏

    本文实例为大家分享了Android实现拼图小游戏的具体代码,供大家参考,具体内容如下 目标效果: 1.activity_main.xml页面: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schem

  • python实现猜拳小游戏

    用python实现猜拳小游戏,供大家参考,具体内容如下 本练习旨在养成良好的编码习惯和练习逻辑思考. 1.使用python版本: 3.7.3: 2.代码内容实现如下 #!/usr/bin/env python # -*- coding: utf-8 -*- """ 简单实现猜拳小游戏,默认每回合 五局 Version: 0.1 Author: smartbabble Date: 2018-03-12 """ from random import

  • python实现五子棋小游戏

    本文实例为大家分享了python实现五子棋小游戏的具体代码,供大家参考,具体内容如下 暑假学了十几天python,然后用pygame模块写了一个五子棋的小游戏,代码跟有缘人分享一下. import numpy as np import pygame import sys import traceback import copy from pygame.locals import * pygame.init() pygame.mixer.init() #颜色 background=(201,202

  • Android自定义View实现拼图小游戏

    本文实例为大家分享了Android拼图小游戏的具体代码,供大家参考,具体内容如下 1.效果图: 运行时: 结束时: 2.PuzzleLayoutView: public class PuzzleLayoutView extends RelativeLayout implements View.OnClickListener { //表示将其切成2*2拼图(默认4块) private int mColumn = 2; //容器的内边距 private int mPadding; //每个块块的边距

随机推荐