1、过年的时候在手机上下载了2048玩了几天,心血来潮决定用py写一个,刚开始的时候想用QT实现,发现依赖有点大。正好看到graphics.py是基于tkinter做的封装就拿来练手,并借用了CSDN一位朋友封装的model.py(2048逻辑部分)

2、由于是练手的所以不免有写的不好的地方请大家喷的轻点。

先看看演示图片

附上源码:

2048主程

#-*-coding:utf-8-*-

#python3.3.5

from graphics import*

from tkinter.messagebox import askquestion

from tkinter.messagebox import showinfo

import time,random,model,configparser

import GUI_2048 as g

class Application():

'''

初始化应用程序

'''

def __init__(self):

self.matrix = model.init()

self.win = g.init()

self.create_r_2048(self.win)

self.show_matrix(self.matrix)

self.win.master.bind("", self.bind_key)

while 1:

update()

'''

创建网格上的16个方格、最佳成绩、当前分数

'''

def create_r_2048(self,win):

p = Point(10, 190)

n = 4

self.rt =  [0 for row in range(n*n)]

for i in range(n):

for a in range(n):

_p = Point(p.x + 60*i, p.y + 60*a)

self.rt[i+4*a] = g.rectangle_2048(win,_p)

#最佳成绩

self.zjcj = g._text(win,Point(135, 60 + 30),Point(135 + 115, 60 + 30 + 30),self.getMaxScore())

#当前分数

self.dqjf = g._text(win,Point(135, 120 + 30),Point(135 + 115, 120 + 30 + 30),'0')

'''

从配置文件中获取最佳成绩

'''

def getMaxScore(self):

config = configparser.ConfigParser()

config.read('config.ini')

maxScore = config.get("Score", "maxScore")

return maxScore

'''

把最佳成绩写入配置文件

'''

def setMaxScore(self,score):

config = configparser.ConfigParser()

config.optionxform = str

config.read('config.ini')

config.set("Score", "maxScore",str(score))

config.write(open("config.ini", "w"))

'''

初始化数据和界面,在游戏结束后调用

'''

def my_init(self):

maxScore = self.getMaxScore()

if int(maxScore) < model.getScore():

self.setMaxScore(model.getScore())

self.zjcj.setText(model.getScore())

matrix = model.init()

self.dqjf.setText(model.getScore())

return matrix

'''

绑定键盘事件 捕获上下左右和Q键

'''

def bind_key(self, event):

'''

key event

'''

if model.is_over(self.matrix):

if askquestion("GAME OVER","GAME OVER!\nDo you want to init it?") == 'yes':

self.matrix = self.my_init()

self.show_matrix(self.matrix)

return

else:

self.win.close()

else:

if event.keysym.lower() == "q":

self.win.close()

elif event.keysym == "Left":

self.matrix = model.move_left(self.matrix)

elif event.keysym == "Right":

self.matrix = model.move_right(self.matrix)

elif event.keysym == "Up":

self.matrix = model.move_up(self.matrix)

elif event.keysym == "Down":

self.matrix = model.move_down(self.matrix)

if event.keysym in ["q", "Left", "Right", "Up", "Down"]:

try:

self.matrix = model.insert(self.matrix)

self.dqjf.setText(model.getScore())

self.show_matrix(self.matrix)

except:

pass

if model.is_win(self.matrix):

if askquestion("WIN","You win the game!\nDo you want to init it?") == 'yes':

self.matrix = self.my_init()

self.show_matrix(self.matrix)

return

else:

self.win.close()

'''

从二维数组中获取结果数据并展示在16方格中

'''

def show_matrix(self, matrix):

for i in range(16):

num = matrix[i//4][i%4]

print(num)

if num == 0:

num = ''

self.rectangle_2048(i,num)

'''

对16个方格做颜色和数字变更

'''

def rectangle_2048(self,i,num):

c = color_rgb(200, 190, 180)

if num == 2:

c = color_rgb(240, 230, 220)

elif num == 4:

c = color_rgb(240, 220, 200)

elif num == 8:

c = color_rgb(240, 180, 120)

elif num == 16:

c = color_rgb(240, 140, 90)

elif num == 32:

c = color_rgb(240, 120, 90)

elif num == 64:

c = color_rgb(240, 90, 60)

elif num == 128:

c = color_rgb(240, 90, 50)

elif num == 256:

c = color_rgb(240, 200, 70)

elif num == 512:

c = color_rgb(240, 200, 70)

elif num == 1024:

c = color_rgb(0, 130, 0)

elif num == 2048:

c = color_rgb(0, 130, 0)

'''

循环设置颜色和数字

'''

self.rt[i][0].setFill(c)

self.rt[i][1].setText(num)

#main

Application()

2048gui部分

#-*-coding:utf-8-*-

#python3.3.5

from graphics import*

#初始化并构建2048界面

def init():

win = GraphWin("2048", 260, 450)

win.master.geometry('+400+150')  #屏幕位置

c = color_rgb(206, 194, 180)

win.setBackground(c)

hint(win)

_title(win)

_grid(win)

maxScore(win)

curScore(win)

return win

#2048方格

def rectangle_2048(win, p1 = Point(10, 10),txt='',c = color_rgb(206, 194, 180)):

p2 = Point(p1.x + 60, p1.y + 60)

r = _rectangle(win,p1,p2,c)

t = _text(win,p1,p2,txt)

return r,t

#挂牌

def hint(win,p1 = Point(10, 10)):

p2 = Point(p1.x + 240, p1.y + 40)

c = color_rgb(187, 173, 164)

_rectangle(win,p1,p2,c)

t = _text(win,p1,p2,'真英雄 挑战2048~')

t.setTextColor(color_rgb(238, 231, 221))

return t

#标题logo

def _title(win,p1 = Point(10, 60)):

p2 = Point(p1.x + 120, p1.y + 120)

c = color_rgb(228, 184, 0)

_rectangle(win,p1,p2,c)

t = Text(Point((p2.x + p1.x) / 2, (p2.y + p1.y) / 2), '2048')

t.setSize(35)

t.setStyle('bold')

t.setTextColor('white')

t.draw(win)

#画正方形

def _rectangle(win,p1,p2,c):

r = Rectangle(p1, p2)

r.setFill(c)

r.setOutline(color_rgb(198, 186, 174))

r.draw(win)

return r

#写文字

def _text(win,p1,p2,txt):

t = Text(Point((p2.x + p1.x) / 2, (p2.y + p1.y) / 2), txt)

t.draw(win)

return t

#网格

def _grid(win,p1 = Point(10, 190)):

#上面

p_u_1 = Point(p1.x + 60, p1.y)

p_u_2 = Point(p1.x + 120, p1.y)

p_u_3 = Point(p1.x + 180, p1.y)

p_u_4 = Point(p1.x + 240, p1.y)

#左面

p_l_1 = Point(p1.x, p1.y + 60)

p_l_2 = Point(p1.x, p1.y + 120)

p_l_3 = Point(p1.x , p1.y + 180)

p_l_4 = Point(p1.x , p1.y + 240)

#右面

p_r_1 = Point(p1.x + 240, p1.y + 60)

p_r_2 = Point(p1.x + 240, p1.y + 120)

p_r_3 = Point(p1.x + 240, p1.y + 180)

p_r_4 = Point(p1.x + 240, p1.y + 240)

#下面

p_d_1 = Point(p1.x + 60 , p1.y + 240)

p_d_2 = Point(p1.x + 120 , p1.y + 240)

p_d_3 = Point(p1.x + 180 , p1.y + 240)

p_d_4 = Point(p1.x + 240 , p1.y + 240)

c = color_rgb(198, 186, 174)

#画横线

l_W_1 = Line(p1, p_u_4)

l_W_2 = Line(p_l_1, p_r_1)

l_W_3 = Line(p_l_2, p_r_2)

l_W_4 = Line(p_l_3, p_r_3)

l_W_5 = Line(p_l_4, p_r_4)

l_W_1.setFill(c)

l_W_2.setFill(c)

l_W_3.setFill(c)

l_W_4.setFill(c)

l_W_5.setFill(c)

l_W_1.draw(win)

l_W_2.draw(win)

l_W_3.draw(win)

l_W_4.draw(win)

l_W_5.draw(win)

#画竖线

l_H_1 = Line(p1, p_l_4)

l_H_2 = Line(p_u_1, p_d_1)

l_H_3 = Line(p_u_2, p_d_2)

l_H_4 = Line(p_u_3, p_d_3)

l_H_5 = Line(p_u_4, p_d_4)

l_H_1.setFill(c)

l_H_2.setFill(c)

l_H_3.setFill(c)

l_H_4.setFill(c)

l_H_5.setFill(c)

l_H_1.draw(win)

l_H_2.draw(win)

l_H_3.draw(win)

l_H_4.draw(win)

l_H_5.draw(win)

#最佳成绩

def maxScore(win,p1 = Point(135, 60)):

p2 = Point(p1.x + 115, p1.y + 30)

c = color_rgb(187, 173, 164)

_rectangle(win,p1,p2,c)

_text(win,p1,p2,'最佳成绩:')

#当前分数

def curScore(win,p1 = Point(135, 120)):

p2 = Point(p1.x + 115, p1.y + 30)

c = color_rgb(187, 173, 164)

_rectangle(win,p1,p2,c)

_text(win,p1,p2,'当前分数:')

以上就是本文的全部内容了,希望大家能够喜欢。

python设计2048小游戏_使用graphics.py实现2048小游戏相关推荐

  1. python里graphics的使用_使用graphics.py实现2048小游戏

    1.过年的时候在手机上下载了2048玩了几天,心血来潮决定用py写一个,刚开始的时候想用QT实现,发现依赖有点大.正好看到graphics.py是基于tkinter做的封装就拿来练手,并借用了CSDN ...

  2. python 贴吧盖楼_ es6 + canvas 开源 盖楼小游戏 完整代码注释 从零教你做游戏(一)...

    盖楼游戏 一个基于 Canvas 的盖楼游戏 Demo 预览 手机设备可以扫描下方二维码 github Game Rule 游戏规则 以下为默认游戏规则,也可参照下节自定义游戏参数 每局游戏生命值为3 ...

  3. 可以用python实现一些小发明_【kimol君的无聊小发明】—用python写图片格式批量处理工具...

    前言某个夜深人静的夜晚,夜微凉风微扬,月光照进我的书房~ 当我打开文件夹以回顾往事之余,惊现许多看似杂乱的无聊代码.我拍腿正坐,一个想法油然而生:"生活已然很无聊,不如再无聊些叭" ...

  4. python摄像头看图识字_《看图识字》小程序开发小结

    我做的一个小程序<看图识字>在4月27日上线.在线上运行二十三天了,这里做个小结.总结一下为何要做这个.做这个遇到的一些问题以及做一个产品(姑且算一个产品吧)的感悟. 为何要做这个小程序? ...

  5. 用记事本编写小游戏_记事本3分钟编写放置小游戏(v0.4 土豪情趣屋与大型灵石盾构)...

    ------------------------- v0.4新增问山村的土豪情趣屋(大量生产人口),新增灵石盾构(提升灵石产量)! ------------------------- 说明: 本教程无 ...

  6. python为text添加滚动条_在Tkinter中向一组小部件添加滚动条

    概述 您只能将滚动条与一些小部件关联起来,根小部件和Frame不是那组小部件的一部分. 最常见的解决方案是创建一个画布小部件,并将滚动条与该小部件关联起来.然后,将包含标签小部件的框架嵌入到画布中.确 ...

  7. mpvue 微信小程序_使用Vue.js开发微信小程序:开源框架mpvue解析

    戳蓝字"CSDN云计算"关注我们哦! 作者 | 成全 责编 | 阿秃 转自 | 美团技术团队企业博客 前言 mpvue是一款使用Vue.js开发微信小程序的前端框架.使用此框架,开 ...

  8. 新版微信不停跳转到小程序_如何设置跳转微信小程序

    一.功能效果 手机网站常用模块:文本.图片.按钮支持设置点击跳转微信小程序. 可实现手机微信端下,打开手机网站可与微信小程序的实现相互跳转. 二.注意事项 [版本]展示中级版及以上版本支持开通. [条 ...

  9. 2个人怎么一起玩军旗游戏_让我们一起玩:建立游戏社区

    2个人怎么一起玩军旗游戏 Sharing Is Like Alchemy 分享就像炼金术 One of the biggest pleasures of my career has been the ...

最新文章

  1. sqlserver循环
  2. 手把手教你如何建立自己的Linux系统(二)
  3. 【每天学一点linux】后台进程不打印日志
  4. java amqp_AMQP协议
  5. error:use of undeclared identifier
  6. 查看Linux上程序或进程用到的库
  7. PHPCMS V9 按浏览次数排行调用文章
  8. 【光说不练假把式】今天说一说Kubernetes 在有赞的实践
  9. Python进制转换(利用栈)
  10. 计算机科学与技术素材,计算机科学与技术ppt素材
  11. leetcode刷题日记- 超级次方
  12. linux——文件操作
  13. 如何利用3Dslicer将mhd格式三维图像迅速转换为tif单张图片
  14. SpringIOC控制反转之XML配置
  15. 人生无捷径「一万小时定律·正篇」
  16. Android 仿QQ动态背景登录
  17. NIOS II 内核使用 之 代码保存FLASH(EPCSX芯片)
  18. 关于sdcard读写速率慢的问题排查
  19. csdn竟然还有这种神器!后悔没有早点知道!超好用的csdn插件,别再犹豫了,赶快入手吧!
  20. 一文搞懂晶振,晶振的作用和原理?

热门文章

  1. APK 签名轮替方案 v3
  2. Linux课程特点与目标
  3. Vue3 企业级项目实战:项目须知与课程约定
  4. bbsy计算机组成原理,2017年第二军医大学训练部408计算机学科专业基础综合之计算机组成原理考研题库...
  5. 多次迟到、旷工?员工将百度告上法院,获赔12万!
  6. BIGEMAP如何使用高程DEM建立三维地图模型(Arcgis ArcScene)
  7. win10中eclipse的ctrl+alt+down快捷键与系统冲突解决办法
  8. win10的计算机服务器,Tomcat服务器怎么在Win10系统上搭建-电脑自学网
  9. 导入SVN版本库,提示svnadmin: E000002: Can't open file
  10. PDF屏蔽打印,隐藏工具栏和菜单栏