python编程学习-跳伞
学习python编程,在新东方少儿编程官网的自由创作平台做的一些作品。
可以在线运行,效果如下(在新页面中打开链接):
代码如下(用到平台自定义的xdf库):
from xdf import *
W = 768
H = 1024
groundY = H - 100
fill("#456078")
class Actor:
def __init__(self, color, x=0, y=0):
self.x = x
self.y = y
self.eyeX = 0
self.eyeY = 0
self.speedY = 0
self.aY = 0
self.showBalloon = False
self.state = 0 # 0:未下落,1:下落中,未开伞,2:开伞,3:落地
self.size = 30
self.life = 100
self.score = 0
self.ai = None
size = self.size
self.line1 = line(x, y, x - size, y - size * 1.8, 'black', size * 0.1)
self.line2 = line(x, y, x + size, y - size * 1.8, 'black', size * 0.1)
self.balloon1 = circle(x - size, y - size * 1.8, size * 0.5, 'white')
self.balloon2 = circle(x - size * 0.4, y - size * 2.0, size * 0.5, 'white')
self.balloon3 = circle(x + size * 0.4, y - size * 2.0, size * 0.5, 'white')
self.balloon4 = circle(x + size, y - size * 1.8, size * 0.5, 'white')
self.head = circle(x, y, size, color)
self.eyeL = circle(x - size * 0.32, y, size * 0.4, 'white', 'black')
self.eyeR = circle(x + size * 0.32, y, size * 0.4, 'white', 'black')
self.eyeM = circle(x, y, size * 0.24, 'white')
self.eyeBallL = circle(x - size * 0.32, y, size * 0.1, 'black')
self.eyeBallR = circle(x + size * 0.32, y, size * 0.1, 'black')
def update(self, dt):
if self.ai:
self.ai.update()
self.speedY += self.aY * dt
if self.showBalloon:
k = self.aY * dt / 100
self.speedY -= self.speedY * k
self.y += self.speedY * dt
global groundY
if self.state != 3 and self.y + self.size >= groundY:
self.showBalloon = False
self.state = 3
maxSpeed = 100
self.score = getScore()
if self.speedY > maxSpeed:
self.life -= int((self.speedY - maxSpeed) * 0.5 + 0.5)
if self.life < 0:
self.life = 0
if self.y + self.size >= groundY:
self.y = groundY - self.size
self.speedY *= -0.8
if self.showBalloon:
self.line1.show()
self.line2.show()
self.balloon1.show()
self.balloon2.show()
self.balloon3.show()
self.balloon4.show()
else:
self.line1.hide()
self.line2.hide()
self.balloon1.hide()
self.balloon2.hide()
self.balloon3.hide()
self.balloon4.hide()
self.updatePos()
def updatePos(self):
x = self.x
y = self.y
size = self.size
self.line1.move(x, y, x - size, y - size * 1.8)
self.line2.move(x, y, x + size, y - size * 1.8)
self.balloon1.move(x - size, y - size * 1.8)
self.balloon2.move(x - size * 0.4, y - size * 2.0)
self.balloon3.move(x + size * 0.4, y - size * 2.0)
self.balloon4.move(x + size, y - size * 1.8)
self.head.move(x, y)
self.eyeL.move(x - size * 0.32, y)
self.eyeR.move(x + size * 0.32, y)
self.eyeM.move(x, y)
self.eyeBallL.move(x - size * 0.4 + self.eyeX, y + self.eyeY)
self.eyeBallR.move(x + size * 0.4 + self.eyeX, y + self.eyeY)
def resetTurn(self):
self.aY = 0
self.y = 100
self.speedY = 0
self.showBalloon = False
self.state = 0
self.score = 0
def reset(self):
self.life = 100
self.resetTurn()
def tap(self):
if self.state == 0:
self.aY = 30 * 10
self.state = 1
elif self.state == 1:
self.showBalloon = True
self.state = 2
class LifeBar:
def __init__(self):
self.life = 100
self.bg = line(0, 10, W, 10, "black", 30)
self.bar = line(0, 10, W, 10, "red", 30)
self.tf = text('100', 0, 22, 30, 'green')
def update(self):
self.bar.size(self.life * W / 100)
self.tf.change(str(self.life))
class Ground:
def __init__(self):
self.b = box(0, 0, W, 200, "black")
self.b.move(W/2, groundY + 100)
class ReadyGo:
def __init__(self):
self.tf = text('3', W / 2, H / 2, 200, 'red', 'center')
self.isStart = False
self.count = 0
self.update()
def update(self):
if self.isStart:
self.count -= 1
self.tf.show()
if self.count > 50:
self.tf.change("3")
elif self.count > 30:
self.tf.change("2")
elif self.count > 10:
self.tf.change("1")
else:
self.tf.change("GO")
if self.count <= 0:
self.stop()
self.tf.hide()
else:
self.tf.hide()
def isReady(self):
return self.count <= 10
def stop(self):
self.isStart = False
self.count = 0
def start(self):
self.isStart = True
self.count = 70
class StartPanel:
def __init__(self):
texts = ["游戏规则:",
"1、点击任意区域开始",
"2、倒计时结束,点击任意区域开始下落",
"3、再次点击任意区域,打开降落伞",
"4、根据落地时间先后进行打分",
"5、落地时速度太快会摔伤"
]
self.tfs = []
for i in range(len(texts)):
self.tfs.append(text(texts[i], 100, 300 + 50 * i, 30, 'red'))
def show(self):
for i in range(len(self.tfs)):
self.tfs[i].show()
def hide(self):
for i in range(len(self.tfs)):
self.tfs[i].hide()
class AI:
def __init__(self, actor):
self.actor = actor
def update(self):
global state
if state == 2:
if self.actor.state == 0:
if random(100) > 80:
self.actor.tap()
elif self.actor.state == 1:
if self.actor.y >= H / 2:
if random(100) > 60:
self.actor.tap()
hero = Actor('#13C319', W / 2, 100)
computter1 = Actor('#ff00ff', W / 2 - 300, 100)
computter2 = Actor('#00ffff', W / 2 - 150, 100)
computter3 = Actor('#ffff00', W / 2 + 150, 100)
computter4 = Actor('#0000ff', W / 2 + 300, 100)
computter1.ai = AI(computter1)
computter2.ai = AI(computter2)
computter3.ai = AI(computter3)
computter4.ai = AI(computter4)
actors = [hero, computter1, computter2, computter3, computter4]
bar = LifeBar()
ground = Ground()
go = ReadyGo()
sp = StartPanel()
score = 0
scoreTf = text('得分', 0, 60, 30, 'red')
turnTf = text('点击任意位置开始下一回合', W/2, H/2, 50, 'red', 'center')
overTf = text('点击任意位置重新开始', W/2, H/2, 50, 'red', 'center')
state = 0 # 0:未开始, 1:ready-go, 2:游戏中, 3:回合结束, 4:游戏结束
scoreId = 0
scores = [10, 7, 4, 1, 0]
def getScore():
global scores, scoreId
sc = scores[scoreId]
scoreId += 1
return sc
def resetTurn():
global scoreId
scoreId = 0
for i in range(len(actors)):
actors[i].resetTurn()
def reset():
global sp, hero,turnTf, score, state
state = 0
score = 0
resetTurn()
sp.show()
hero.reset()
def loop():
global actors, bar, ground, go, state, score, scoreTf, turnTf, overTf
for i in range(len(actors)):
actors[i].update(0.05)
bar.life = hero.life
if state == 1:
checkGoEnd()
elif state == 2:
checkTurnEnd()
bar.update()
go.update()
scoreTf.change('得分:' + str(score))
if state == 3:
turnTf.show()
else:
turnTf.hide()
if state == 4:
overTf.show()
else:
overTf.hide()
def oneTurn():
global state, go
go.start()
state = 1
resetTurn()
def checkGoEnd():
global state, go
if go.isReady():
state = 2
def checkTurnEnd():
global state, go, actors, hero, score
isEnd = True
for i in range(len(actors)):
if actors[i].state != 3:
isEnd = False
if isEnd:
score += hero.score
if hero.life > 0:
state = 3
turnOver()
else:
gameOver()
def start():
oneTurn()
sp.hide()
def turnOver():
global state
state = 3
def gameOver():
global state
state = 4
def tap():
global state,hero
if state == 0:
state = 1
start()
elif state == 2:
hero.tap()
elif state == 3:
oneTurn()
elif state == 4:
reset()
reset()
也可以下载代码,本地python环境运行(用pygame封装了xdf库)。