论如何用ybc_game做一个文字迷宫(表情包版本)

大家好,我是于翱睿,这个作品我们一起来看一下如何使用python的ybc_game来做一个文字迷宫,文字迷宫其实很简单,尤其是地图更新,0代表着空地,1代表着墙2代表着门,3代表的是我,4代表的是猫(gui)5代表的是刀,用来保护自己。

还有,本文字迷宫是表情包版本,如果想改的话,记得自己探索。

以下是文字迷宫的代码,如果有新的地图的创意,欢迎私信留言。如果合理,我们一定更新。

最后一点:本文字迷宫游戏使用了ybc_game和pgzero第三方库,如果没下载,记得先下载后运行。

import ybc_game
import pgzero
import math
from collections import deque

ybc_game.mobile_type(2)
#ybc_game.play_sound('Sea.mp3',-1)

ybc_game.size(800, 800)
ybc_game.title('逃离文字迷宫-逐梦工作室精品')

# ybc_game.play_sound('bgm', -1)
me = ybc_game.actor('board')
ghost = ybc_game.actor('board')
weapon = ybc_game.actor('board')

cube_size = 60
level = -1
# 0:空地,1:墙,2:门,3:我,4:鬼,5:符
walls_str = [
'''
222
212
222
''',
'''
0000000000
114111111111111
100000000011111
111111011111111
000011301111111
000011101111111
000011001111111 
000011011111111
111111001111111
111111101111111
111111001111111
111111211111111
''',
'''
11111111111
10000000001
10111110111
10010000001
11010111111
10010010121
10111010101
13010010101
11010110101
10010000101
10111511101
10000000041
11111111111
''',
'''
14111111
10111111
10111111
10111111
10011111
11011111
10011101
13000001
11111101
10000001
10111111
10000001
11111151
10000001
10111111
12111111
''',
'''
111111111111111111111
100013001000100010001
101010101010101010101
101000100010001000101
101111111111111100001
101000000000000000111
101111210111101111111
101000410111101000001
101011010100000011101
100011010110101000101
111000010000001110101
101011110110101000101
101000000111111101101
101111110000001100001
100000000000001101111
111111111111111111111
''',
'''
000000000000000000000
111111111111111111111
111400000000000111111
111111111111110111111
110000000000000111111
111101111111111111111
111101113111111111111
111100000111111111111
111111110111110021111
111111110111110011111
111111115111110011111
111111110111110011111
111111110111110011111
111111110000000011111
111111111111111111111
''',
'''
000000000000000000000
111111111111111111111
111111111100111111111
111111111104111111111
111111111111111111111
111111111111311111111
111111110000000000111
111111110000000000111
111111110000000000111
122222222222222222221
122222222222222222221
122222222222222222221
111111111111111111111
''',
'''
111111111111111111111
111111111411111111111
111111111011111111111
111111111011111111111
111111111011111111111
111111111011111111111
111111111011111111111
1111111110000000000001
1111111111111111111101
1000000003000000000001
101111111111111111101
101111111111111111101
101111111111111111101
101111111111111111101
101111111111111111101
101111111111111111101
101111111111111111101
100000000511111111101
111111111111111111101
100000000000000000001
101111111111111111111
101111111111111111111
100000001111111111111
111111101111111111111
111000001111111111111
111011111111111111111
111011111111111111111
111000000000000000001
111111111111111111101
111111111111111111101
111111111110000000001
111111111110111111111
111111111112111111111
111111111111111111111
''',
'''
11111
01310
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01010
01210
01110
00000
''',
'''
11111111111
13000000001
11111011111
00101010000
00001011111
00001000021
01200111001
''',
'''
11111111111
10000000001
10111110111
10010000001
11010111111
22222222222
10111010101
13010010101
11010110101
22222222101
10111011101
10000000041
11111111111
''',
'''
11444444
10000001
10111101
10011001
01241001
15011001
10121141
13111121
11111111
''',
'''
111111111111111111111
100444401000100010001
101010101010101010101
101000100010001000101
101111111111111111101
101002222222000000101
101111110000001110101
101000010111111000101
142424010100000011101
101012010110101000101
101010010000001110101
101042420110101000101
101000000111111101101
222424222000001100001
140000000000051101101
111111111111111111111
''',
'''
121
232
121
''',
'''
00002222122220000
22220000300002222
00002222122220000
''',
'''
111010001001000001
010010023200100010
010010020200010100
111011101000001000
''',
'''
11111111111111111111
13000001222222222222
11222100000000000001
11110000000012222222
44442222222222222222
''',
'''
1111111111111111111111111111111111111
1000000000000000000000000000000000001
1000000000000000000000000000000000001
1000000000000000000000000000000000001
1000000000000000000000000000000000001
1000000000000000000000000000000000001
1000000000000000000000000000000000001
1000000000000000000000000000000000001
1000000030000000000000000020000000001
1000000000000000000000000000000000001
1000000000000000000000000000000000001
1000000000000000000000000000000000001
1000000000000000000000000000000000001
1000000000000000000000000000000000001
1000000000000000000000000000000000001
1000000000000000000000000000000000001
1111111111111111111111111111111111111
''',
'''
1111111111112111111111111
1111111111112222221111111
1111111122222200000000111
1111122222211111131111111
1111111222222220000000011
1000000011111111111100002
''',
'''
1111122222
1130011111
1001122211
1010001001
1001110101
1100000001
1111011112
1000000001
1000000001
1222222221
''',
'''
00001222210000
00010000001000
00100000000100
00100000000100
00010000001000
11111000011111
13000000000011
11111111111122
22222222222222
''',
'''
1111121111111
1222100112221
1100010010011
1311101001001
1001100111101
1100010011001
1001111000011
1000000000001
1111111111111
''',
'''
2111111111112
1000000000001
1100003000011
1211110111121
1222100012221
1110000000111
1111112111111
''',
'''
1111111111111111110000000000000
1222222222222222211111100000000
1111111100000000000011111110000
1111111111110000000000001111111
1111100000000000000000000011311
1111100000000000000000000000001
1111111111111111111111111111111
''',
'''
1111222222222
2222111111111
1111000003111
0001111110001
2222200000011
2222222011111
''',
'''
00011110111100000
00100001000010000
00010003000100000
00001000001000000
00000100010000000
00000010100000000
00000002000000000
''',
'''
111111111111111111111111
100000000000000000000001
100000000000000000000001
100000000000000000020001
100000000000000000000001
100000000000000000000001
100000000000000000000001
100000000000300000000001
100000000000000000000001
100000000000000000000001
100000000000000000000001
100000000000000000000001
100000000000000000000001
111111111111111111111111
''',
'''
111111111111111111111111
100000011100000011100001
100000111110000111110201
100011111111101111111111
100000111111111111110001
100000011111111111100001
100000000000111000000001
100000000003010000000001
111111111111111111111111
''',
'''
111111111
100000041
101111111
101111111
101111111
1300111111
1110111121
1110111101
1110005001
1111111111
''',
'''
1111211111
1001010001
1000010111
1311114111
1000000111
1111111111
''',
'''
111111111111
400000000352
111111111111 
''',
'''
1111111111
1000103002
1410001001
1111111111
''',
'''
11111111111111111
10000000000000001
10000000000000001
10000000030000001
10000000000000001
10000000000000001
10000000000000001
12000000000000001
11111111111111111
''',
'''
44444444444444444
43500000000000004
41111111111111154
41111111111111144
11111111111111124
'''
]
walls = []
state = '准备'
time = 4
me.speed = 15
me.dir = ''
me.new_dir = ''
ghost.pause_time = 16
ghost.speed = 30
ghost.dir = ''
ghost.timer = ghost.pause_time
ghost.energy = 0


def start():
    global state, time, walls, level
    if time > 1:
        time -= 1
        ybc_game.delay(start, 1)
    else:
        level += 1
        state = '运行'
        walls = []
        lines = walls_str[level].split()
        ghost.enable = False
        weapon.enable = False
        weapon.follow_me = False
        if level == 3:
            ghost.pause_time = 8
        for y in range(len(lines)):
            walls.append([])
            for x in range(len(lines[y])):
                n = int(lines[y][x])
                if n in (0, 1, 2):
                    walls[y].append(n)
                else:
                    if n == 3:
                        me.x = x * cube_size
                        me.y = y * cube_size
                        me.dir = me.new_dir = ''
                    if n == 4:
                        ghost.x = x * cube_size
                        ghost.y = y * cube_size
                        ghost.enable = True
                    if n == 5:
                        weapon.x = x * cube_size
                        weapon.y = y * cube_size
                        weapon.enable = True
                    walls[y].append(0)

ybc_game.delay(start, 2)


def update():
    if state == '运行':
        move()
        if weapon.enable and weapon.follow_me:
            weapon.x = me.x
            weapon.y = me.y
        if ghost.enable:
            ghost_move()
        collide()
    draw()


def draw():
    screen.fill('black')
    if state in ('运行'):
        for i in range(len(walls)):
            for j in range(len(walls[i])):
                if walls[i][j] == 1:
                    if abs(j * cube_size - me.x) < 440 and abs(i * cube_size - me.y) < 440:
                        screen.draw.rect((j * cube_size + 402 - me.x - cube_size / 2,
                                          i * cube_size + 402 - me.y - cube_size / 2, cube_size - 4, cube_size - 4),
                                         'white')
                        ybc_game.text('🏙️', [j * cube_size + 400 - me.x, i * cube_size + 400 - me.y], 'white', 28)
                if walls[i][j] == 2:
                    screen.draw.rect((j * cube_size + 402 - me.x - cube_size / 2,
                                      i * cube_size + 402 - me.y - cube_size / 2, cube_size - 4, cube_size - 4),'blue')
                    ybc_game.text('🚪', [j * cube_size + 400 - me.x, i * cube_size + 400 - me.y], 'green', 28)
        screen.draw.rect((402 - cube_size / 2, 402 - cube_size / 2, cube_size - 4, cube_size - 4), 'brown')
        ybc_game.text('😀', [400, 400], 'green', 28)
        if ghost.enable:
            screen.draw.rect((ghost.x - me.x + 402 - cube_size / 2, ghost.y - me.y + 402 - cube_size / 2, cube_size - 4,cube_size - 4), 'orangered')
            ybc_game.text('🐱', [ghost.x - me.x + 400, ghost.y - me.y + 400], 'orange', 28)
        if weapon.enable:
            screen.draw.rect((weapon.x - me.x + 402 - cube_size / 2, weapon.y - me.y + 402 - cube_size / 2, cube_size - 4,
                              cube_size - 4), 'yellow')
            ybc_game.text('🔪', [weapon.x - me.x + 400, weapon.y - me.y + 400], 'yellow', 28)

    if state == '胜利':
        ybc_game.text('恭喜胜利', [400, 400], 'yellow', 50)
        ybc_game.text('点个赞吧, 欢迎写信给我提供新关卡的创意', [400, 460], 'yellow', 20)
    if state == '失败':
        ybc_game.text('你倒是给点力啊!\n不要让我被猫抓住了\n真服你失败了\n点个关注八!\n逐梦精品', [400, 400], 'red', 60)
    if state == '准备':
        ybc_game.text('加油!\n点一下屏幕,然后用方向键控制\n(稍微等一会)\n(没有倒计时)\n(正在加载游戏地图)\n小贴士:\n每一关猫的速度都有可能不一样哦', [400, 400], 'white', 40)
    


def move():
    # 根据new_dir判断是否要改变方向
    if me.new_dir != '':
        if can_move(me, me.new_dir):
            me.dir = me.new_dir
            me.new_dir = ''
    # 根据dir判断是否可以移动
    if me.dir != '':
        if can_move(me, me.dir):
            if me.dir == 'left':
                me.x -= me.speed
            if me.dir == 'right':
                me.x += me.speed
            if me.dir == 'up':
                me.y -= me.speed
            if me.dir == 'down':
                me.y += me.speed
        else:
            me.dir = ''
            ##########ybc_game.play_sound('hit')


def ghost_move():
    if ghost.dir in ['left', 'right', 'up', 'down']:
        if can_move(ghost, ghost.dir) and ghost.energy > 0:
            if ghost.dir == 'left':
                ghost.x -= ghost.speed
            if ghost.dir == 'right':
                ghost.x += ghost.speed
            if ghost.dir == 'up':
                ghost.y -= ghost.speed
            if ghost.dir == 'down':
                ghost.y += ghost.speed
            ghost.energy -= ghost.speed
        else:
            ghost.dir = ''
    if ghost.dir == '' or ghost.energy == 0:
        ghost.dir = '无'
        ghost.energy = -1
        ghost.timer = ghost.pause_time
    if ghost.timer == 0:
        restart_ghost()
    ghost.timer -= 1


def restart_ghost():
    path = bfs_maze_solver(walls)
    if len(path) < 2:
        return
    if path[1][0] > path[0][0]:
        ghost.dir = 'down'
    if path[1][0] < path[0][0]:
        ghost.dir = 'up'
    if path[1][1] > path[0][1]:
        ghost.dir = 'right'
    if path[1][1] < path[0][1]:
        ghost.dir = 'left'
    ghost.energy = -2 * cube_size
    for y, x in path:
        ghost.energy += cube_size
        if y != path[0][0] and x != path[0][1]:
            break
    else:
        ghost.energy = 1000 * cube_size


def can_move(obj, direct):
    x = obj.x
    y = obj.y
    if direct == 'left':
        x -= obj.speed
        x2 = x
        y2 = y + cube_size - 1
    if direct == 'right':
        x += obj.speed + cube_size - 1
        x2 = x
        y2 = y + cube_size - 1
    if direct == 'up':
        y -= obj.speed
        x2 = x + cube_size - 1
        y2 = y
    if direct == 'down':
        y += obj.speed + cube_size - 1
        x2 = x + cube_size - 1
        y2 = y

    return walls[int(y / cube_size)][int(x / cube_size)] != 1 and walls[int(y2 / cube_size)][int(x2 / cube_size)] != 1


def collide():
    global state, time
    x = me.x + cube_size / 2
    y = me.y + cube_size / 2
    if walls[int(y / cube_size)][int(x / cube_size)] == 2:
        if level < len(walls_str) - 1:
            state = '准备'
            time = 1
            ybc_game.delay(start, 1)
            #####ybc_game.play_sound('win')
        if level == len(walls_str) - 1:
            state = '胜利'
            #ybc_game.play_sound('win')
    if weapon.enable and abs(me.x - weapon.x) < cube_size and abs(me.y - weapon.y) < cube_size:
        weapon.follow_me = True
    if weapon.enable and weapon.follow_me and abs(ghost.x - weapon.x) <= cube_size and abs(ghost.y - weapon.y) <= cube_size:
        ghost.enable = False
        weapon.enable = False
    if ghost.enable and abs(me.x - ghost.x) < cube_size-1 and abs(me.y - ghost.y) < cube_size-1:
        state = '失败'


def on_key_down(key):
    if key in ['left', 'right', 'up', 'down']:
        me.new_dir = key


def bfs_maze_solver(maze):
    # 定义移动方向(上,下,左,右)
    directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    start_x = int((ghost.x + cube_size / 2) / cube_size)
    start_y = int((ghost.y + cube_size / 2) / cube_size)
    # 队列用来存储在探索中待访问的位置
    queue = deque([(start_y, start_x, [])])  # 存储坐标和到该点的路径
    visited = set([(start_y, start_x)])  # 存储已访问的点
    x = int((me.x + cube_size / 2) / cube_size)
    y = int((me.y + cube_size / 2) / cube_size)
    # BFS循环
    while queue:
        row, col, path = queue.popleft()
        # 当到达迷宫出口
        if row == y and col == x:
            return path + [(row, col)]
        # 探索当前点四周的点
        for dr, dc in directions:
            r, c = row + dr, col + dc
            if 0 <= r < len(maze) and 0 <= c < len(maze[0]) and maze[r][c] == 0 and (r, c) not in visited:
                queue.append((r, c, path + [(row, col)]))
                visited.add((r, c))
    # 如果没有找到路径,则返回空列表
    return []


ybc_game.go()

该代码运行前先要创建一个“images”文件夹,然后在文件夹里面添加一个png格式的透明图片

这样运行不会报错。

以上就是表情包版本文字迷宫的全部内容,这个项目作者鸽了好长时间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值