猫史档案馆


【Python作品分享】flappybird【作品秀】

用户:Kei8DOlKei8DOl查看:0 回复:1 评论:0 创建时间:2020-01-17T23:58:03


【作品展示】

center_image

 

【作品介绍】

Yet another flappybird copy...

have fun...

 

【作品源代码】

import pgzrun
import math
import random

WIDTH = 400
HEIGHT = 喵0

highscore = 0
score = 0
dead = True
played = False

# GLOBAL
gravity = - HEIGHT / 5
acc = - gravity * 7.5
vol = 0
tubevol = WIDTH * 0.2
tubewidth = WIDTH / 10
tubegap = tubewidth * 3
tubeholdheiht = tubewidth * 2.5

# BRID INFO
bridRadius = math.floor(tubewidth / 4)
bridHeight = HEIGHT - HEIGHT * 2 / 3

tubeArray = []
def drawTube():
    for tube in tubeArray:
        BOX = Rect((tube["x"], 0), (tubewidth, HEIGHT))
        screen.draw.filled_rect(BOX, (0, 255, 0))
        HOLE = Rect((tube["x"] - 5, tube["hole"]),
                    (tubewidth + 10, tubeholdheiht))
        screen.draw.filled_rect(HOLE, (0, 0, 0))

def drawBrid():
    screen.draw.filled_circle((WIDTH*0.5, bridHeight),
                              bridRadius, (100, 255, 255))


def drawScore():
    screen.draw.text("score:"+str(score), (WIDTH*0.4, HEIGHT*0.1))

def drawRun():
    drawTube()
    drawBrid()
    drawScore()

def drawStart():
    global played
    screen.draw.text("heightest score: "+str(highscore),
                     (WIDTH*0.3, HEIGHT*0.1))
    if played:
        screen.draw.text("GameOver", (WIDTH*0.35, HEIGHT*0.4))
    screen.draw.text("Tap to start", (WIDTH*0.35, HEIGHT*0.5))


def flap():
    global vol
    vol = acc

def bridUpdate(dt):
    global bridHeight, vol
    vol = max(vol + gravity, gravity)
    bridHeight = bridHeight - vol * dt
    posY = HEIGHT - bridHeight
    


def tubeUpdate(dt):
    for tube in tubeArray:
        tube["x"] = tube["x"] - tubevol * dt
        if tube["x"] + tubewidth + tubegap < 0:
            tube["x"] = WIDTH
            tube["score"] = False

def checkCollision():
    global dead
    x = WIDTH*0.5
    y = bridHeight
    for tube in tubeArray:
        if tube["x"] - bridRadius < x and tube["x"] + tubewidth + bridRadius > x :
            if not (tube["hole"] + bridRadius < y and tube["hole"] + tubeholdheiht - bridRadius > y):
                dead = True


def checkPoint():
    global score
    for tube in tubeArray:
        if not tube["score"]:
            if tube["x"] + tubewidth / 2 < WIDTH / 2:
                tube["score"] = True
                score = score + 1

def setup():
    global score
    global bridHeight
    global tubeArray
    global dead
    score = 0
    bridHeight = HEIGHT - HEIGHT * 2 / 3

    tubeArray = []
    for i in range(1, math.floor((WIDTH) / (tubewidth + tubegap)) + 2):
        tubeArray.append({
            "x": WIDTH + (i - 1) * (tubewidth + tubegap),
            "hole": random.uniform(0.2, 0.8) * HEIGHT,
            "score": False,
        })
    dead = False

def on_mouse_down():
    global dead
    global played
    if not dead:
        flap()
    else:
        setup()
        played = True

def update(dt):
    global highscore, dead
    if not dead:
        bridUpdate(dt)
        tubeUpdate(dt)
        checkCollision()
        checkPoint()
        if bridHeight > HEIGHT or bridHeight < 0:
            dead = True
        highscore = max(highscore, score)


def draw():
    screen.fill((0, 0, 0))
    if dead:
        drawStart()
    else:
        drawRun()



pgzrun.go()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
昊宸呦西昊宸呦西

厉害厉害

 

点赞0


评论