学习python编程,在新东方少儿编程官网的自由创作平台做的一些作品。
可以在线运行,效果如下(在新页面中打开链接):
代码如下(用到平台自定义的xdf库):
from xdf import *
road = [
line(0, 0, 0, 1024, 'red'),
line(768, 0, 768, 1024, 'red')
]
lines = []
objects = []
for i in range(9):
lines.append({
'x': 768 / 2,
'y': i * 128,
'line': line(768/2, i * 128, 768/2, i * 128 + 96)
})
for i in range(5):
a = random(2)
if a != 1:
a = -1
objects.append({
'x': 768/2 + a * 768 / 4,
'y': i * 2 * 128 - 1024,
'sp': box(0, 0, 300, 60, 'red')
})
hero = {
'x': 768 / 2,
'y': 1024 - 200,
'v': 20,
'frames': [
box(0, 0, 100, 100, 'black')
]
}
state = {
's': 0,
'v': 10,
'over': False
}
overText = text("Game Over", 100, 'center', 'green')
overText.hide()
sText = text("", 10, 60, 40, 'blue')
def loop():
sText.change(str(state['s'] // 20) + "米")
if state['over']:
return
if hero['x'] < 50:
hero['x'] = 50
elif hero['x'] > 768 - 50:
hero['x'] = 768 - 50
hero['frames'][0].move(hero['x'], hero['y'])
state['s'] = state['s'] + state['v']
n = len(lines)
for i in range(n):
l = lines[i]
l['y'] = l['y'] + state['v']
if l['y'] > 1024:
l['y'] = l['y'] - 128 * n
l['line'].move(l['x'], l['y'], l['x'], l['y'] + 96)
n = len(objects)
for i in range(n):
o = objects[i]
o['y'] = o['y'] + state['v']
if o['y'] > 1024:
a = random(2)
if a != 1:
a = -1
o['x'] = 768/2 + a * 768 / 4
o['y'] = o['y'] - 2 * 128 * n
o['sp'].move(o['x'], o['y'])
for i in range(n):
dx = abs(hero['x'] - objects[i]['x'])
dy = abs(hero['y'] - objects[i]['y'])
if dx < 50 + 150 and dy < 50 + 30:
state['over'] = True
overText.show()
break
def reStart():
overText.hide()
state['over'] = False
state['s'] = 0
hero['x'] = 768 / 2
for i in range(len(lines)):
l = lines[i]
l['y'] = i * 128
for i in range(len(objects)):
o = objects[i]
o['y'] = i * 2 * 128 - 1024
def moveLeft():
hero['x'] = hero['x'] - hero['v']
def moveRight():
hero['x'] = hero['x'] + hero['v']
def keydown(keyCode, key):
if keyCode == 37:
moveLeft()
elif keyCode == 39:
moveRight()
elif keyCode == 32:
if state['over']:
reStart()
def tap():
if state['over']:
reStart()
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库)。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。