听说最近python热闹,用python3写了个方块凑凑热闹。
Tofloor
poster avatar
gzw
deepin
2018-04-05 21:30
Author
import sys,select,time,termios,tty,random
from time import sleep
kuang=[65532,32772,32772,32772,32772,32772,32772,32772,32772,32772,32772,32772,32772,65532,0,0]
hctx=kuang[:]
bjtx=kuang[:]
fktx=[4,14,0,0,4,6,4,0,0,14,4,0,4,12,4,0,0,6,6,0,0,6,6,0,0,6,6,0,0,6,6,0,4,4,4,4,0,15,0,0,4,4,4,4,0,15,0,0,4,4,6,0,0,14,8,0,12,4,4,0,2,14,0,0,4,4,12,0,8,14,0,0,6,4,4,0,0,14,2,0,4,12,8,0,12,6,0,0,4,12,8,0,12,6,0,0,8,12,4,0,0,6,12,0,8,12,4,0,0,6,12,0,]
fkh=fkt=fkx=fky=fkxb=fkyb=fkhb=fktb=timex=0
def xinfangkuai():
        global fkh,fkt,fkx,fky,fkhb,fktb,fkxb,fkyb
        fkt=fktb=random.randint(0,6)
        fkh=fkhb=0
        fkyb=fky=8
        fkxb=fkx=1
def pengzhuang():
        global fkh,fkt,fkx,fky,fkxb,fkyb,fkhb,fktb,hctx,bjtx,kuang,fangkuai,fangkuaib
        x=fkx
        for c in fktx[fkt*16+fkh*4:fkt*16+fkh*4+4]:
                if bjtx[x]|(c<<(15-fky))!=bjtx[x]+(c<<(15-fky)):
                        if fky!=fkyb:
                                fkx=fkxb
                                fky=fkyb
                                fkh=fkhb
                                fkt=fktb
                        if fkx!=fkxb:
                                if fkxb==1:hctx=kuang[:]
                                bjtx=kuang[:]
                                hcs=bjs=len(bjtx)-4
                                while hcs>=1:
                                        if hctx[hcs]!=65532:
                                                bjtx[bjs]=hctx[hcs]
                                                bjs=bjs-1
                                        hcs=hcs-1
                                xinfangkuai()                               
                x=x+1
        fkxb=fkx
        fkyb=fky
        fkhb=fkh
        fktb=fkt
def hecheng():
        global fkh,fkt,fkx,fky,fkxb,fkyb,fkhb,fktb,hctx,bjtx,kuang
        x=fkx
        hctx=bjtx[:]
        for c in fktx[fkt*16+fkh*4:fkt*16+fkh*4+4]:
                hctx[x]=bjtx[x]|c<<(15-fky)
                x=x+1
def xianshi():
        print('\33[0;0H \n please input 2 4 6 8 0\n')
        for y in hctx:
                for x in [32768,16384,8192,4096,2048,1024,512,256,128,64,32,16,8,4,2,1]:
                        c=x&y
                        if c==0:print('  ',end='')
                        else:print('▓▓',end='')
                print('')
old_settings = termios.tcgetattr(sys.stdin)
tty.setcbreak(sys.stdin.fileno())
xinfangkuai()
print('\033[2J')
while True:
        sleep(.1)
        pengzhuang()
        hecheng()
        xianshi()
        if timex!=int(time.time()):
                timex=int(time.time())
                fkx=fkx+1
                continue
        if select.select([sys.stdin],[],[],0) == ([sys.stdin],[],[]):
                c=sys.stdin.read(1)
                if c=='0':break
                if c=='4':fky=fky-1
                if c=='6':fky=fky+1
                if c=='8':
                        if fkh<3:fkh=fkh+1
                        else:fkh=0
                if (c=='2'):
                        timex=int(time.time())
                        fkx=fkx+1
        while select.select([sys.stdin],[],[],0) == ([sys.stdin],[],[]):sys.stdin.read(1)
termios.tcsetattr(sys.stdin,termios.TCSADRAIN,old_settings)


Reply Favorite View the author
All Replies
avatar
ritter
deepin
2018-04-05 23:32
#1
你这些变量名都是不合格的,鬼才看得懂这些变量名代表什么东西。。。。。。
Reply View the author
avatar
wtz
deepin
2018-04-05 23:57
#2
https://bbs.deepin.org/post/155013
你这些变量名都是不合格的,鬼才看得懂这些变量名代表什么东西。。。。。。 ...

对新人还是包容一点好……
非英语国家的人写程序有时也用本国语言,只是看起来比较费劲而已。
还有楼主啊,能不能照顾一下没有小键盘的用户……改成WASD可以么……
Reply View the author
avatar
gzw
deepin
2018-04-06 00:42
#3
https://bbs.deepin.org/post/155013
对新人还是包容一点好……
非英语国家的人写程序有时也用本国语言,只是看起来比较费劲而已。
还有楼主啊 ...

              if c=='0':break
                if c=='4':fky=fky-1
                if c=='6':fky=fky+1
                if c=='8':
                        if fkh<3:fkh=fkh+1
                        else:fkh=0
                if (c=='2'):
把代码尾部里面的相应数字改成WASD就可以了
Reply View the author
avatar
gzw
deepin
2018-04-06 00:44
#4
0退,4左,6右,2下,8转
Reply View the author
avatar
gzw
deepin
2018-04-06 00:47
#5
https://bbs.deepin.org/post/155013
你这些变量名都是不合格的,鬼才看得懂这些变量名代表什么东西。。。。。。 ...

多向他人学习是进步之源,向你学习。
Reply View the author
avatar
gzw
deepin
2018-04-06 00:57
#6
kuang 最初的空白框
hctx 合成图形数据
bjtx 背景图形数据
fktx 方块图形数据
fkh 当前方块的旋转类型
fkt 当前方块图形类型
fkx 当前方块位置行
fky 当前方块位置列
fkxb 历史方块位置行
fkyb=历史方块位置列
fkhb=历史方块旋转类型
fktb 历史方块图形类型
timex 上次自动下落时间戳
Reply View the author
avatar
gzw
deepin
2018-04-06 01:09
#7
def xinfangkuai():新方块产生初始化
def pengzhuang():方块碰撞检测与处理
def hecheng():方块与背景的合成
def xianshi():获取合成数据向屏幕输出
最后一段是键盘处理和调用以上函数
Reply View the author
avatar
gzw
deepin
2018-04-06 01:11
#8
代码在终端窗口运行,需要用python3
Reply View the author
avatar
gzw
deepin
2018-04-06 01:12
#9
以上代码在deepin系统的深度终端测试可以正常运行
Reply View the author
avatar
ritter
deepin
2018-04-06 01:25
#10
https://bbs.deepin.org/post/155013
多向他人学习是进步之源,向你学习。

不敢不敢,互相学习。不过养成良好的编程习惯还是很重要的,写的程序能让别人比较容易看懂是很重要的,毕竟这是个社区时代!
Reply View the author
avatar
字甲达宾
deepin
2018-04-06 18:00
#11
两点建议:

* 1. 在文件头加上两行

#! /usr/bin/env python
# -*- coding: utf-8 -*-

第一行, 加上这行, 这个py就处于了可执行模式下, (当然是针对linux类的操作系统),  
这个hint, 告诉操作系统要使用哪个python解释器来执行这个py.

在linux上执行一下命令 /usr/bin/env python ,就知道这行其实是call一下python解释器.  
这种写法比#! /usr/bin/python要好, 后者是hard coding 了python的路径.

第二行, 是告诉python解释器, 应该以utf-8编码来解释py文件, 对于python 2.6/2.7,
如果程序中包含中文字符, 又没有这一行, 运行将会报错.
但python3.1没有这行, 也会成功运行的.


* 2. 顺便再把变量定义添加到文件里,方便大家更好地理解你的编程意图。
Reply View the author
avatar
gzw
deepin
2018-04-06 18:44
#12
https://bbs.deepin.org/post/155013
两点建议:

* 1. 在文件头加上两行

谢谢你的建议,代码在你的设备上能跑起来吗?
Reply View the author
avatar
151******42
deepin
2018-04-07 19:43
#13
这是什么情况?
Reply View the author
avatar
wolfblood
deepin
2018-04-07 23:30
#14

print的这个end参数好像是python3.0之后的语法,你直接执行python默认启动的应该是python2.7,你可以用python3 文件名.py  来执行试试
Reply View the author
avatar
151******42
deepin
2018-04-08 13:44
#15
https://bbs.deepin.org/post/155013
print的这个end参数好像是python3.0之后的语法,你直接执行python默认启动的应该是python2.7,你可以用py ...

嗯,这样呀,当年学RUBY去了,PYTHON还没学,了解一下,不知道那个好
Reply View the author
avatar
jiuxian
deepin
2018-04-08 19:13
#16
https://bbs.deepin.org/post/155013
嗯,这样呀,当年学RUBY去了,PYTHON还没学,了解一下,不知道那个好

不用想了,肯定是python好,应用范围、框架工具、社区、学习资料 、就业前景都,不管哪样都比ruby不知高到哪里去了。
Reply View the author