猫史档案馆


【Python作品分享】7 猫仔跨栏_玩法升级【教程贴】

用户:天真的疾风雀AzD8天真的疾风雀AzD8查看:0 回复:1 评论:0 创建时间:2022-06-18T19:47:01


【作品展示】

center_image

 

【作品介绍】

一个游戏按教程做出他的

 

【作品源代码】

import pgzrun
from random import randint

WIDTH = 喵0
HEIGHT = 360
score = 0
game_state = 'start'
move_speed = 5
count = 0
jump = False
jump_state = 'up'
coin_collected = 0

coin = Actor("coin")
coin.pos = 1000, 300
cat = Actor("mao_walk1", (100, HEIGHT - 58))
box = Rect(WIDTH + 200, HEIGHT - 50, 40, 50)


def draw():
    if game_state == 'start':
        screen.blit('rainbow', (0, 0))
        cat.draw()
        screen.draw.text("Mao Jump", color="dodgerblue",
                         center=(WIDTH / 2, 150), fontsize=70)
        screen.draw.text("Press any key to start",
                         color="dodgerblue",
                         center=(WIDTH / 2, 210), fontsize=40)
    else:
        screen.blit('rainbow', (0, 0))
        screen.draw.filled_rect(box, 'orange')
        cat.draw()
        coin.draw()
        screen.draw.text("Coin: " + str(coin_collected), color="dodgerblue",
                         topright=(WIDTH - 10, 10), fontsize=40)
        screen.draw.text("Score: " + str(score),
                         color="dodgerblue",
                         topleft=(10, 10), fontsize=40)
        if game_state == 'game over':
            screen.draw.text("Game Over", color="dodgerblue",
                             center=(WIDTH / 2, HEIGHT / 2),
                             fontsize=100)


def on_key_down():
    global game_state
    if game_state == 'start':
        game_state = 'play'


def on_mouse_down():
    global jump, jump_speed
    if game_state == 'play':
        jump = True


def cat_walk():
    global count
    if count == 9:
        count = 0
        if cat.image == "mao_walk1":
            cat.image = "mao_walk2"
        else:
            cat.image = "mao_walk1"
    else:
        count += 1


def cat_jump():
    global jump, jump_state
    cat.image = "mao_jump"
    if jump_state == 'up':
        cat.y -= 5
        if cat.y <= HEIGHT / 2 - 10:
            jump_state = 'down'
    elif jump_state == 'down':
        cat.y += 5
        if cat.y == HEIGHT - 58:
            jump = False
            jump_state = 'up'


def update():
    global score, game_state, move_speed, coin_collected, box
    if game_state == 'play':
        if jump:
            cat_jump()
        else:
            cat_walk()
        box.x -= move_speed
        if box.x < -30:
            height = randint(40, 80)
            box = Rect(randint(700, 1000), HEIGHT - height,
                       randint(20, 60), height)
            box.x = randint(700, 1000)
            score += 1
            move_speed += 0.2
        if box.collidepoint(cat.x + 30, cat.y + 55) \
                or box.collidepoint(cat.x - 23, cat.y + 40):
            game_state = 'game over'
            cat.image = 'mao_end'

        coin.x -= move_speed
        if cat.colliderect(coin) or coin.x < 0:
            if coin.x > 0:
                coin_collected += 1
                sounds.coin.play()
            coin.x = randint(800, 3000)
            coin.y = randint(150, 320)

pgzrun.go()

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
匿名的鸭匿名的鸭

imgsrc="https://static.codemao.cn/emoji/codemao/%E7%BC%96%E7%A8%8B%E7%8C%AB_%E6%90%93%E5%A4%B4.gif"alt="emotion_编程猫_搓头"报错了

点赞0


评论