Python培训
400-996-5531
各位python大神,你们都用python开发过哪个游戏?飞船有没有?射击有没有?2048有没有?俄罗斯方块有没有?没有吗?你想玩拼图?python编程也是可以实现的哦,不信你来瞧好了:
一、效果的演示
小编用了91步才完成这拼图...你会用多少步呢?
二、 游戏的玩法
思路:先设置一个棋盘,棋盘里面有我们的图像,在图像里面我们有一个个的小方块,通过这些错乱的小方块拼接图板。拼接的过程就是鼠标点击事件的一个过程
三、具体的实现步骤
1.设置图像
2.定义一个图像块的类
3.定义一个方法开始拼接图板
4.重置游戏
5.绘制游戏界面各元素
6.定义鼠标的点击事件
7.创建框架
8.注册鼠标事件
9.初始化游戏
10.启动框架
环境:Python 3.6 + Windows
IDE: sublime txt3
使用到的模块:Simpleguitk
安装模块:pip install simpleguitk
#!/usr/bin/env python # -*- coding: utf-8 -*- import simpleguitk as simplegui import random byamax = simplegui.load_image('https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523374883465&di=d0545c2c8adb05310f4f56a35e2c6976&imgtype=0&src=http%3A%2F%#%2F160625%2F235106-1606250Q05845.jpg') WIDTH = 600 HEIGHT = WIDTH+100 IMAGE_SIZE = WIDTH/3 all_coordinates = [[IMAGE_SIZE*0.5, IMAGE_SIZE*0.5], [IMAGE_SIZE*1.5, IMAGE_SIZE*0.5], [IMAGE_SIZE*2.5, IMAGE_SIZE*0.5], [IMAGE_SIZE*0.5, IMAGE_SIZE*1.5], [IMAGE_SIZE*1.5, IMAGE_SIZE*1.5], [IMAGE_SIZE*2.5, IMAGE_SIZE*1.5], [IMAGE_SIZE*0.5, IMAGE_SIZE*2.5], [IMAGE_SIZE*1.5, IMAGE_SIZE*2.5], None] ROWS = 3 COLS = 3 steps = 0 board = [[None,None,None],[None,None,None],[None,None,None]] class Square: def __init__(self,coordinage): self.center = coordinage def draw(self,canvas,board_pos): canvas.draw_image(byamax,self.center,[IMAGE_SIZE,IMAGE_SIZE], [(board_pos[1]+0.5)*IMAGE_SIZE,(board_pos[0]+0.5)*IMAGE_SIZE],[IMAGE_SIZE,IMAGE_SIZE]) def init_board(): random.shuffle(all_coordinates) for i in range(ROWS): for j in range(COLS): idx = i * ROWS + j square_center = all_coordinates[idx] if square_center is None: board[i][j] = None else: board[i][j] = Square(square_center) def play_game(): global steps steps = 0 init_board() def draw(canvas): canvas.draw_image(byamax,[WIDTH/2,WIDTH/2],[WIDTH,WIDTH],[50,WIDTH+50],[98,98]) canvas.draw_text('步数:'+str(steps),[400,680],22,'White') for i in range(ROWS): for j in range(COLS): if board[i][j] is not None: board[i][j].draw(canvas,[i,j]) def mouseclick(pos): global steps r = int(pos[1]//IMAGE_SIZE) c = int(pos[0]//IMAGE_SIZE) if r<3 and c<3: if board[r][c] is None: return else: current_square = board[r][c] if r - 1 >= 0 and board[r - 1][c] is None: # 判断上面 board[r][c] = None board[r - 1][c] = current_square steps += 1 elif c + 1 <= 2 and board[r][c + 1] is None: # 判断右面 board[r][c] = None board[r][c + 1] = current_square steps += 1 elif r + 1 <= 2 and board[r + 1][c] is None: # 判断下面 board[r][c] = None board[r + 1][c] = current_square steps += 1 elif c - 1 >= 0 and board[r][c - 1] is None: # 判断左面 board[r][c] = None board[r][c - 1] = current_square steps += 1 frame = simplegui.create_frame('拼图',WIDTH,HEIGHT) frame.set_canvas_background('Black') frame.set_draw_handler(draw) frame.add_button('重新开始',play_game,60) frame.set_mouseclick_handler(mouseclick) play_game() frame.start()
感谢您的阅读,用python编程开发拼图游戏,你学会了吗?还想了解更多用python编程实现的游戏?继续关注达内python培训就对了!
免责声明:内容和图片源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。
填写下面表单即可预约申请免费试听! 怕学不会?助教全程陪读,随时解惑!担心就业?一地学习,可全国推荐就业!
Copyright © 京ICP备08000853号-56 京公网安备 11010802029508号 达内时代科技集团有限公司 版权所有
Tedu.cn All Rights Reserved