学习python编程,在新东方少儿编程官网的自由创作平台做的一些作品。
可以在线运行,效果如下(在新页面中打开链接):
代码如下(用到平台自定义的xdf库):
from xdf import *
W = 768
H = 1024
CW = 64
M = int(W / CW)
N = int(H / CW)
cubes = []
data = []
isOver = False
TIME_STEP = 20
time = 0
score = 0
L = [[[1, 0], [1, 1], [1, 2], [0, 2]],
[[0, 0],[0, 1], [1, 1], [2, 1]],
[[0, 0], [1, 0], [0, 1], [0, 2]],
[[0, 0], [1, 0], [2, 0], [2, 1]]];
B = [[[0,1],[0,2],[1,1],[1,2]],
[[0,1],[0,2],[1,1],[1,2]],
[[0,1],[0,2],[1,1],[1,2]],
[[0, 1], [0, 2], [1, 1], [1, 2]]];
J = [[[0,1],[1,1],[2,0],[2,1]],
[[0,0],[1,0],[1,1],[1,2]],
[[0,1],[0,2],[1,1],[2,1]],
[[1, 0], [1, 1], [1, 2], [2, 2]]];
T = [[[0,1],[1,0],[1,1],[1,2]],
[[0,1],[1,1],[1,2],[2,1]],
[[1,0],[1,1],[1,2],[2,1]],
[[0, 1], [1, 0], [1, 1], [2, 1]]];
I = [[[1,0],[1,1],[1,2],[1,3]],
[[0,1],[1,1],[2,1],[3,1]],
[[1,0],[1,1],[1,2],[1,3]],
[[0, 1], [1, 1], [2, 1], [3, 1]]];
Z = [[[0,0],[0,1],[1,1],[1,2]],
[[0,1],[1,0],[1,1],[2,0]],
[[0,0],[0,1],[1,1],[1,2]],
[[0, 1], [1, 0], [1, 1], [2, 0]]];
S = [[[0,0],[1,0],[1,1],[2,1]],
[[0,1],[0,2],[1,0],[1,1]],
[[0,0],[1,0],[1,1],[2,1]],
[[0, 1], [0, 2], [1, 0], [1, 1]]];
tps = [L,B,J,T,I,Z,S]
tp = 0 #L、B、J、T、I、Z、S
rot = 0 #0、1、2、3
blockArr = []
bottom = [int(M/2)-1, -1]
def getTpArr():
return tps[tp]
def ij2xy(i, j):
return [i * CW, j * CW]
def xy2ij(x, y):
return [int(x / CW), int(y / CW)]
for i in range(M):
cbr = []
cubes.append(cbr)
vr = []
data.append(vr)
for j in range(N):
vr.append(0)
xy = ij2xy(i, j)
c = box(xy[0] + 2, xy[1] + 2, CW - 4, CW - 4, 'gray', 'black')
cbr.append(c)
overTxt = text("游戏结束,按空格键开始", 50, 'red', 'center')
scoreTxt = text("得分", 50, 50, 50, 'red', '')
def checkOver():
global blockArr,bottom
for i in range(len(blockArr)):
if bottom[1] + blockArr[i][1] < 0:
gameOver()
return True
return False
def dispear(j):
global data
while j >= 0:
sm = 0
for i in range(M):
v = 0
if j != 0:
v = data[i][j - 1]
data[i][j] = v
sm = sm + v
if sm == 0:
break
j = j - 1
def addScore():
global score
score = score + 100
def checkDispear():
global M,N,data
j = N - 1
while j >= 0:
sm = 0
for i in range(M):
sm = sm + data[i][j]
if sm == 0: #不用再往上查了
break
if sm == M: #可消除
dispear(j)
j = j + 1
addScore()
j = j - 1
def checkItem(i, j):
if j < 0:
return True
global M,N,data
if i < 0 or i >= M or j >= N:
return False
if data[i][j] != 0:
return False
return True
def checkMoveAble(arr, topI, topJ):
for i in range(len(arr)):
ii = arr[i][0]
jj = arr[i][1]
if not checkItem(topI + ii, topJ + jj):
return False
return True
def putBlock():
global blockArr,data,bottom
for i in range(len(blockArr)):
ii = blockArr[i][0] + bottom[0]
jj = blockArr[i][1] + bottom[1]
if jj >= 0:
data[ii][jj] = 1
checkDispear()
def createBlock():
global bottom,tp,tps,rot,blockArr
tp = random(len(tps))-1
rot = random(4) - 1
blockArr = tps[tp][rot]
bottom = [int(M/2)-1, -1]
def getBlockArr(t, rot):
global tps
return tps[t][rot]
def moveLeft():
global bottom, blockArr
if checkMoveAble(blockArr, bottom[0] - 1, bottom[1]):
bottom[0] = bottom[0] - 1
def moveRight():
global bottom,blockArr
if checkMoveAble(blockArr, bottom[0] + 1, bottom[1]):
bottom[0] = bottom[0] + 1
def moveDown():
global bottom,blockArr
if checkMoveAble(blockArr,bottom[0], bottom[1] + 1):
bottom[1] = bottom[1] + 1
else:
if not checkOver():
putBlock()
createBlock()
def rotate():
global bottom,blockArr,rot,tp
r = (rot + 1) % 4
arr = getBlockArr(tp, r)
if checkMoveAble(arr, bottom[0], bottom[1]):
rot = r
blockArr = arr
def keydown(key, keycode):
global isOver,time
if isOver:
if key == 32:
reset()
return
if key == 37: #left
moveLeft()
elif key == 39: #right
moveRight()
elif key == 40: #down
moveDown()
time = 0
elif key == 38: #up
rotate()
#print(key, keycode)
def loop():
global isOver
if isOver:
return
global time,TIME_STEP
time = time + 1
if time >= TIME_STEP:
time = 0
moveDown()
refresh()
def gameOver():
global isOver,overTxt,time
isOver = True
time = 0
overTxt.show()
refresh()
def reset():
global isOver,overTxt, time, data,score
score = 0
isOver = False
time = 0
overTxt.hide()
for i in range(M):
for j in range(N):
data[i][j] = 0 #random(2) - 1
createBlock()
refresh()
def refresh():
global data,cubes,blockArr,bottom, score,scoreTxt
scoreTxt.change("得分:" + str(score))
for i in range(M):
for j in range(N):
if data[i][j] == 0:
cubes[i][j].change('gray')
else:
cubes[i][j].change('black')
for i in range(len(blockArr)):
ii = blockArr[i][0] + bottom[0]
jj = blockArr[i][1] + bottom[1]
if jj >= 0:
cubes[ii][jj].change('black')
reset()
# 加个空格按钮,加个按钮,在触屏设备上也能用
def onClickSpace():
keydown(32, " ")
btnSpace = text("空格键", 768 - 150, 50, 50, 'red', '')
btnSpace.tap = onClickSpace
class MyKeyBoard:
def __init__(self, x, y, w):
hw = w / 2
hhw = w * 0.75
self.arr = [
box(x, y - w, w, w, "transparent", "red"),
text("↑", x + hw, y - w + hhw, w, "center", "red"),
box(x, y, w, w, "transparent", "red"),
text("↓", x + hw, y + hhw, w, "center", "red"),
box(x - w, y, w, w, "transparent", "red"),
text("←", x - hw, y + hhw, w, "center", "red"),
box(x + w, y, w, w, "transparent", "red"),
text("→", x + w + hw, y + hhw, w, "center", "red"),
]
self.arr[0].tap=self.clickUp
self.arr[2].tap=self.clickDown
self.arr[4].tap=self.clickLeft
self.arr[6].tap=self.clickRight
delay(self.loop, 100)
def loop(self):
for i in range(len(self.arr)):
self.arr[i].front()
delay(self.loop, 100)
def onKeydown(self, keyCode, key):
try:
keydown(keyCode, key)
except e:
pass
def clickUp(self):
self.onKeydown(38, "ArrowLeft")
def clickDown(self):
self.onKeydown(40, "ArrowDown")
def clickLeft(self):
self.onKeydown(37, "ArrowLeft")
def clickRight(self):
self.onKeydown(39, "ArrowRight")
MyKeyBoard(768-200, 1024-100, 100)
也可以下载代码,本地python环境运行(用pygame封装了xdf库)。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。