推箱子小游戏
本次小游戏学习视频: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()