如何在Python中编写贪吃蛇游戏?
时间:2023-05-10 10:04
贪吃蛇Python小游戏(源码+注释+粘贴即用) 这款贪吃蛇游戏十分简便,规避使用难以载包的pygame,下面是运行图: 在绘制游戏图像仿麦呢,我们采用的是数据库中较之pygame更为轻便的Turtle Graphics 上面是通过乌龟制图,前进单位距离后旋转90度,然后再旋转90度直至绘制出一个小方块 引用刚刚我们绘图的数据库 然后定义画布的大小 注意,我们可以把贪吃蛇看作一个队列,而这个队列之中的每一个元素都包含两个变量(该元素的横纵坐标) 贪吃蛇运动原理:为了方便使蛇移动,我们要把蛇倒装到队列中,在它移动的时候,我们会抛出蛇队列的第一个元素(pop()),然后,在蛇尾新增一个元素(append()) 然后,我们要加入循环刷新运行时间, 我们要建立键盘监听,这对于python而言是十分简单的 这个也是十分容易,我们只用比较蛇头的横纵坐标是否都等于苹果的横纵坐标(蛇头与苹果重合) 这个原理和上面蛇是否吃到苹果原理类似 以上就是如何在Python中编写贪吃蛇游戏?的详细内容,更多请关注Gxl网其它相关文章!游戏代码实现
1.绘制图像
构建数据库(gamebase.py)
from turtle import * # "*"是引用所有函数def square(x, y, size, color_name): up() goto(x, y) down() color(color_name) begin_fill() forward(size) left(90) forward(size) left(90) forward(size) left(90) forward(size) left(90) end_fill()
绘制苹果(snake.py)
from turtle import *from gamebase import squarefrom random import randrange
setup(420,420,0,0)//隐藏乌龟头 emoj.emoj.hideturtle//隐藏轨迹tracer(False)//绘制done()
定义游戏程序循环(相当于Java中的loop线程)
def gameLoop(): //随机生成苹果 apple_x = randrange(-200, 200) apple_y = randrange(-200, 200) //绘制苹果 square(apple_x, apple_y, 10, "red") //刷新画布 update()
绘制贪吃蛇(snake.py)
def gameLoop(): //随机生成苹果 apple_x = randrange(-200, 200) apple_y = randrange(-200, 200) //绘制蛇 for n in range(len(sanke)): square(snake[n][0],snake[n][1[],10,"black) //绘制苹果 square(apple_x, apple_y, 10, "red") //刷新画布 update()
绘制贪吃蛇的运动
global apple_x, apple_y, snake, aim_x, aim_y #全局变量申请snake.append([ snake[-1][0] + aim_x, snake[-1][1] + aim_y ])snake.pop(0)global apple_x, apple_y, snake, aim_x, aim_y #全局变量申请snake.append([ snake[-1][0] + aim_x, snake[-1][1] + aim_y ])snake.pop(0)
ontimer(gameLoop, 100) # 每100毫秒运行一下gameLoop函数
制作贪吃蛇操作响应
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()
判定蛇是否吃到苹果
if snake[-1][0] != apple_x or snake[-1][1] != apple_y: snake.pop(0) else: apple_x = randrange(-20, 18) * 10 apple_y = randrange(-19, 19) * 10
判定蛇是否咬到自己
def bite(): for n in range(len(snake)-1): if snake[-1][0] == snake[n][0] and snake[-1][1] == snake[n][1]: return True return False
判定蛇是否在界内
def inside(): if -200 <= snake[-1][0] <= 180 and -190 <= snake[-1][1]<=190: return True else : return False
游戏源码
gamebase.py
from turtle import * # "*"是引用所有函数def square(x, y, size, color_name): up() goto(x, y) down() color(color_name) begin_fill() forward(size) left(90) forward(size) left(90) forward(size) left(90) forward(size) left(90) end_fill()
snake.py
from time import sleepapple_x = randrange(-20, 18) * 10apple_y = randrange(-19, 19) * 10snake = [[0, 0], [10, 0], [20, 0], [30, 0], [40, 0], [50, 0]]aim_x = 10aim_y = 0def inside(): if -200 <= snake[-1][0] <= 180 and -190 <= snake[-1][1]<=190: return True else : return Falsedef change(x, y): global aim_x, aim_y aim_x = x aim_y = ydef bite(): for n in range(len(snake)-1): if snake[-1][0] == snake[n][0] and snake[-1][1] == snake[n][1]: return True return Falsedef gameLoop(): global apple_x, apple_y, snake, aim_x, aim_y #全局变量申请 snake.append([ snake[-1][0] + aim_x, snake[-1][1] + aim_y ]) if snake[-1][0] != apple_x or snake[-1][1] != apple_y: snake.pop(0) else: apple_x = randrange(-20, 18) * 10 apple_y = randrange(-19, 19) * 10 if(not inside()) or bite(): square(snake[-1][0], snake[-1][1], 10,"hotpink") update() sleep(2)# 暂停2秒 apple_x = randrange(-20, 18) * 10 apple_y = randrange(-19, 19) * 10 snake = [[0, 0], [10, 0], [20, 0], [30, 0], [40, 0], [50, 0]] aim_x = 10 aim_y = 0 n = 0 clear() square(-210,-200,410,"black") square(-200,-190,390,"white") square(apple_x, apple_y, 10, "red") for n in range(len(snake)): square(snake[n][0], snake[n][1], 10, 'black') ontimer(gameLoop, 100) # 每300毫秒运行一下gameLoop函数 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()