python推箱子小游戏

推箱子小游戏

本次小游戏学习视频:https://www.bilibili.com/video/BV1gz411B71H
相关素材:点击这里

在这里插入图片描述

import turtle
import level

ms = turtle.Screen()
ms.setup(900, 650, 200, 100)
# ms.bgcolor('#CC99CC')
ms.bgpic('bc1.gif')
ms.title('推箱子小游戏')

ms.register_shape('wall.gif')  # 墙
ms.register_shape('o.gif')  # 贵鬼
ms.register_shape('p.gif')  # 人
ms.register_shape('box.gif')  # 箱子
ms.register_shape('win_box.gif')  # 胜利箱子
ms.register_shape('bc1.gif')
ms.register_shape('bc2.gif')
ms.register_shape('bc3.gif')
ms.register_shape('bc4.gif')
ms.register_shape('bc5.gif')

ms.tracer(0)

levels = level.level_list()


class Pen(turtle.Turtle):
    def __init__(self, pic):
        super().__init__()
        self.shape(pic)
        self.penup()

    def move(self, x, y, px, py):
        gox, goy = x + px, y + py
        if (gox, goy) in go_space:
            self.goto(gox, goy)
        if (gox + px, goy + py) in go_space and (gox, goy) in box_space:
            for i in box_list:
                if i.pos() == (gox, goy):
                    go_space.append(i.pos())
                    box_space.remove(i.pos())
                    i.goto(gox + px, goy + py)
                    self.goto(gox, goy)
                    go_space.remove(i.pos())
                    box_space.append(i.pos())

                    if i.pos() in correct_box_space:
                        i.shape('win_box.gif')
                    else:
                        i.shape('box.gif')
                    if set(box_space) == set(correct_box_space):
                        text.show_win()

    def go_up(self):
        self.move(self.xcor(), self.ycor(), 0, 50)

    def go_down(self):
        self.move(self.xcor(), self.ycor(), 0, -50)

    def go_left(self):
        self.move(self.xcor(), self.ycor(), -50, 0)

    def go_right(self):
        self.move(self.xcor(), self.ycor(), 50, 0)


# wall = turtle.Turtle()  # 墙
# wall.shape('wall.gif')
# wall.penup()
# correct_box = turtle.Turtle()  # 鬼
# correct_box.shape('o.gif')
# correct_box.penup()
# player = turtle.Turtle()  # 人
# player.shape('p.gif')
# player.penup()
# box = turtle.Turtle()  # 箱子
# box.shape('box.gif')
# box.penup()


class Game():
    def paint(self):
        i_date = len(levels[num - 1])
        j_date = len(levels[num - 1][0])
        for i in range(i_date):
            for j in range(j_date):
                x = -j_date * 25 + 25 + j * 50 + sister_x
                y = i_date * 25 - 25 - i * 50
                if levels[num - 1][i][j] == ' ':  # 人
                    player.goto(x, y)
                    go_space.append((x, y))
                if levels[num - 1][i][j] == 'X':  # 墙
                    wall.goto(x, y)
                    wall.stamp()
                if levels[num - 1][i][j] == 'O':  # 鬼
                    correct_box.goto(x, y)
                    correct_box.stamp()
                    go_space.append((x, y))
                    correct_box_space.append((x, y))
                if levels[num - 1][i][j] == 'P':  # 人
                    player.goto(x, y)
                    go_space.append((x, y))
                if levels[num - 1][i][j] == 'B':  # 箱子
                    box = Pen('box.gif')
                    box.goto(x, y)
                    box_space.append((x, y))
                    box_list.append(box)


class ShowMessage(turtle.Turtle):
    def __init__(self):
        # super(ShowMessage, self).__init__()
        super().__init__()
        self.penup()
        self.pencolor('blue')
        self.ht()

    def message(self):
        self.goto(0+sister_x, 290)
        self.write(f'第{num}关', align='center', font=('正楷', 20, 'bold'))
        self.goto(0+sister_x, 270)
        self.write(f'重新开始请按回车键', align='center', font=('正楷', 15, 'bold'))
        self.goto(0+sister_x, 250)
        self.write(f'选择关请按Q', align='center', font=('正楷', 15, 'bold'))

    def show_win(self):
        global num
        if num == len(levels):
            num = 1
            self.goto(0, 0)
            self.write('你已经全部过关', align='center', font=('仿宋', 20, 'bold'))
            self.goto(0, -50)
            self.write('返回第一关按空格键', align='center', font=('仿宋', 15, 'bold'))
        else:
            num += 1
            self.goto(0, 0)
            self.write('恭喜过关', align='center', font=('仿宋', 20, 'bold'))
            self.goto(0, -25)
            self.write('进入下一关按空格键', align='center', font=('仿宋', 15, 'bold'))


def init():
    text.clear()
    wall.clear()
    correct_box.clear()
    for i in box_list:
        i.ht()
        del(i)
    box_list.clear()
    box_space.clear()
    go_space.clear()
    correct_box_space.clear()
    play_game.paint()
    text.message()
    ms.bgpic(f'bc{num}.gif')


def choose():
    global num
    a = ms.numinput('选择关卡', '请输入你的选择(输入1-5):')
    if a is None and a not in [1, 5]:
        return a == num
    num = int(a)
    init()
    ms.listen()


if __name__ == '__main__':

    num = 1
    sister_x = 225
    correct_box_space = []  # 正确箱子的地址
    box_list = []  # 箱子海龟的列表
    box_space = []  # 箱子所在位置坐标的列表
    go_space = []  # 人要去的位置坐标

    wall = Pen('wall.gif')
    correct_box = Pen('o.gif')
    player = Pen('p.gif')

    play_game = Game()
    play_game.paint()

    text = ShowMessage()
    text.message()

    ms.listen()  # 监听
    ms.onkey(player.go_up, "Up")  # 绑定键盘按钮
    ms.onkey(player.go_down, "Down")
    ms.onkey(player.go_left, "Left")
    ms.onkey(player.go_right, "Right")
    ms.onkey(init, "Return")
    ms.onkey(init, "space")
    ms.onkey(choose, "Q")

    while True:
        ms.update()
    ms.mainloop()

  • 3
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里给你一个简单的推箱子小游戏Python 代码,有兴趣的话可以自己扩展功能: ```python # 定义地图和箱子位置 MAP = [ "##########", "# #", "# OO #", "# O #", "# O #", "# #", "# #", "# ###", "# # #", "####### #", ] BOXES = [(3, 2), (4, 3), (4, 4), (4, 5)] # 定义人物和目标位置 PLAYER = (2, 2) GOAL = (2, 8) # 游戏主循环 while True: # 打印地图 for i in range(len(MAP)): for j in range(len(MAP[i])): if (i, j) in BOXES: print("O", end="") elif (i, j) == PLAYER: print("P", end="") elif (i, j) == GOAL: print("G", end="") else: print(MAP[i][j], end="") print() # 判断是否获胜 if all(box == GOAL for box in BOXES): print("You win!") break # 获取用户输入 move = input("Move (u/d/l/r): ") # 移动人物 if move == "u": newpos = (PLAYER[0] - 1, PLAYER[1]) elif move == "d": newpos = (PLAYER[0] + 1, PLAYER[1]) elif move == "l": newpos = (PLAYER[0], PLAYER[1] - 1) elif move == "r": newpos = (PLAYER[0], PLAYER[1] + 1) else: continue if MAP[newpos[0]][newpos[1]] == " ": PLAYER = newpos # 推动箱子 if newpos in BOXES: newboxpos = (newpos[0] + (newpos[0] - PLAYER[0]), newpos[1] + (newpos[1] - PLAYER[1])) if MAP[newboxpos[0]][newboxpos[1]] == " " and newboxpos not in BOXES: BOXES.remove(newpos) BOXES.append(newboxpos) # 清屏 print("\n" * 50) ``` 在这个代码中,我们通过定义地图和箱子位置、人物和目标位置来实现游戏的基本功能。在游戏主循环中,我们首先打印出地图和游戏元素的位置,然后判断是否已经获胜,如果是则退出游戏。接下来获取用户输入并移动人物,如果移动的位置是空地,则直接移动人物;如果移动的位置是箱子,则判断能否推动箱子,并更新箱子位置和人物位置。最后清屏并进入下一轮循环。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值