猫史档案馆


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

用户:癫才喵癫才喵查看:0 回复:1 评论:0 创建时间:2024-08-17T11:31:04


【作品展示】

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 dudu.right >= WIDTH:
                        direction = -3
                    elif dudu.left <= 0:
                        direction = 3

        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下一页
code猫的code猫的

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(f'Score: {score}', (20, 10))
    if dudu.image == '嘟嘟哭':
        screen.draw.text('Game Over! Press SPACE to restart.', (WIDTH // 2 - 120, HEIGHT // 2), color='red')

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 dudu.right >= WIDTH:
                        direction = -3
                    elif dudu.left <= 0:
                        direction = 3

        if on_brick == 0:
            dudu.y += 8
        if dudu.bottom > HEIGHT:
            dudu.image = '嘟嘟哭'
        if keyboard.left and dudu.left > 0:
            dudu.x -= 5
        if keyboard.right and dudu.right < WIDTH:
            dudu.x += 5
    if dudu.image == '嘟嘟哭':
        music.stop()

def on_key_down(key):
    if key == keys.SPACE and dudu.image == '嘟嘟哭':
        restart_game()

def restart_game():
    global score, bricks
    music.play('背景音乐')
    dudu.image = '嘟嘟'
    dudu.y = HEIGHT // 2
    score = 0
    bricks.clear()
    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'

pgzrun.go()
修改内容:
  1. 游戏结束提示: 当“嘟嘟”掉落时,屏幕上会显示“Game Over! Press SPACE to restart.”,并且分数会显示为“Score: {score}”。
  2. 游戏重启: 当按下空格键时,游戏会重启,重新设置分数和踏板位置。
  3. 代码优化: 合并了一些重复代码和减少了全局变量的使用,使代码更整洁。
游戏体验建议:

点赞0


评论