猫史档案馆


【Python作品分享】深渊历险记【作品秀】

用户:晴天rctQ晴天rctQ查看:0 回复:1 评论:0 创建时间:2023-08-12T19:36:23


【作品展示】

center_image

 

【作品介绍】

深渊历险记

游戏规则:

运行程序后,玩家通过左右键移动角色,让角色获得分数,玩家碰到尖刺踏板或舞台下边缘游戏失败

 

【作品源代码】

import pgzrun
import random

WIDTH = 500
HEIGHT = 700

dudu = Actor('嘟嘟')

score = 0

music.play('背景音乐')

direction = 3

bricks = []
for i in range(5):
    n = random.randint(1, 4)
    b = Actor('踏板' + str(n))
    min_x = b.width // 2
    max_x = WIDTH - b.width // 2
    b.x = random.randint(min_x, max_x)
    b.y = 140 * (i + 1)
    bricks.append(b)
    if i == 2:
        dudu.x = b.x
        dudu.bottom = b.top
        b.image = '踏板1'

def draw():
    global score
    screen.blit('背景', [0, 0])
    dudu.draw()
    for brick in bricks:
        brick.draw()
    screen.draw.text(str(score), (20, 10))

def update():
    global score, direction
    if dudu.image == '嘟嘟':
        score += 1
        on_brick = 0
        for b in bricks:
            b.y -= 3
            if b.image == '踏板2':
                b.x += direction
                if b.right >= WIDTH:
                    direction = -3
                elif b.left <= 0:
                    direction = 3
            if b.y < 0:
                n = random.randint(1, 4)
                b.image = '踏板' + str(n)
                b.y = HEIGHT
                min_x = b.width // 2
                max_x = WIDTH - b.width // 2
                b.x = random.randint(min_x, max_x)
        for b in bricks:
            if dudu.colliderect(b) and dudu.bottom < b.bottom:
                dudu.bottom = b.top
                on_brick = 1
                if b.image == '踏板4':
                    dudu.image = '嘟嘟哭'
                if b.image == '踏板2':
                    dudu.x += direction
                    
        if on_brick == 0:
            dudu.y += 8
        if dudu.bottom > HEIGHT:
            dudu.image = '嘟嘟哭'
        if keyboard.left:
            if dudu.left > 0:
                dudu.x -= 5
        if keyboard.right:
            if dudu.right < WIDTH:
                dudu.x += 5
    if dudu.image == '嘟嘟哭':
        music.stop()

def on_key_down(key):
    if key == keys.SPACE and dudu.image == '嘟嘟哭':
        global score, bricks
        music.play('背景音乐')
        dudu.image = '嘟嘟'
        bricks = []
        score = 0
        for i in range(5):
            n = random.randint(1, 4)
            b = Actor('踏板' + str(n))
            min_x = b.width // 2
            max_x = WIDTH - b.width // 2
            b.x = random.randint(min_x, max_x)
            b.y = 140 * (i + 1)
            bricks.append(b)
            if i == 2:
                b.image = '踏板1'
                dudu.x = b.x
                dudu.bottom = b.top

pgzrun.go()

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
春桥渡又回来力春桥渡又回来力

请问在客户端窗口打开之后迅速关闭是什么问题

 

点赞0


评论