""" 推箱子游戏1.py """ from sprites import * def moveobject(obj): """周边没有墙则移动对象""" x,y = obj.pos() x = x + bug.dx y = y + bug.dy for wall in wall_group: if wall.distance(x,y)<1: return obj.move(bug.dx,bug.dy) return True def existbox(x,y): for box in box_group: if box.distance(x,y)<1: return box def existotherbox(self,x,y): """查找x,y坐标是否有箱子""" for other in box_group: if self == other:continue if other.distance(x,y)<1: return other screen = Screen() bug = Sprite() box_group = Group(tag='box') square1 = Sprite('res/blue_square.png',pos=(100,0),tag='box') square2 = Sprite('res/blue_square.png',pos=(100,100),tag='box') square3 = Sprite('res/blue_square.png',pos=(-100,0),tag='box') wall_group = Group(tag='wall') wall1 = Sprite('res/brown_square.png',pos=(100,150),tag='wall') wall2 = Sprite('res/brown_square.png',pos=(-100,100),tag='wall') wall3 = Sprite('res/brown_square.png',pos=(-150,100),tag='wall') def movethebug(): x = bug.xcor() + bug.dx y = bug.ycor() + bug.dy box = existbox(x,y) # 检测在周边是否有箱子 if box: print(box) x1 = box.xcor() + bug.dx y1 = box.ycor() + bug.dy # 下面检测此箱子周边是否还有箱子 other = existotherbox(box,x1,y1) if other == None: # 如果没有,并没有墙的阻挡则向周边移动箱子 moved = moveobject(box) if moved:bug.move(bug.dx,bug.dy) else: moveobject(bug) screen.listen() keyup = Key('Up') keydown = Key('Down') keyleft = Key('Left') keyright = Key('Right') clock = Clock() 以下代码省略......
下载完整源代码与素材 。此压缩包有两个版本的推箱子游戏!
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)