1、python贪吃蛇游戏技术细节_python实现简单贪吃蛇游戏本实例为家分享了python实现贪吃蛇游戏的具体代码,供家参考,具体内容如下代码:from turtle import*from random import randrangefrom time import sleep#定义变量snake=0,0,10,0,20,0,30,0,40,0,50,0apple_x=randrange(-20,20)*10apple_y=randrange(-20,20)*10aim_x=10aim_y=0def squre(x,y,size,color_name):up()goto(x,y)down(
2、)color(color_name)begin_fill()forward(size)left(90)forward(size)left(90)forward(size)left(90)forward(size)left(90)end_fill()#控制蛇def change(x,y):global aim_x,aim_yaim_x=x;aim_y=y;#判断撞墙def inside():#判断是否在画布if-210=snake-10=200 and-210=snake-11=200:return Trueelse:return False#随机成苹果def gameLoop():global
3、 apple_x,apple_y,aim_x,aim_y,snakesnake.append(snake-10+aim_x,snake-11+aim_y)if not inside():sleep(2)#snake=0,0,10,0,20,0,30,0,40,0,50,0#apple_x=randrange(-20,20)*10#apple_y=randrange(-20,20)*10#aim_x=10#aim_y=0return#吃苹果if snake-10!=apple_x or snake-11!=apple_y:snake.pop(0)#加个元素 删除个元素 蛇就往前运了else:ap
4、ple_x=randrange(-20,20)*10apple_y=randrange(-20,20)*10clear()#清楚之前画的痕迹squre(apple_x,apple_y,10,red)#x y 位置 图形 颜for n in range(len(snake):squre(snaken0,snaken1,10,black)ontimer(gameLoop,100)#每100毫秒运次update()#主程序setup(420,420,0,0)hideturtle()#隐藏箭头tracer(False)#不延时listen()#监听onkey(lambda:change(0,10),w)onkey(lambda:change(0,-10),s)onkey(lambda:change(-10,0),a)onkey(lambda:change(10,0),d)gameLoop()done()更多有趣的经典游戏实现专题,分享给家:C+经典游戏汇总python经典游戏汇总python俄罗斯块游戏集合JavaScript经典游戏 玩不停java经典游戏汇总javascript经典游戏汇总以上就是本的全部内容,希望对家的学习有所帮助,也希望家多多持我们。