教你从零开始实现贪吃蛇Python小游戏

贪吃蛇python小游戏(源码+注释+粘贴即用) 这款贪吃蛇游戏十分简便,规避使用难以载包的pygame,下面是运行图:

-1

-2

文章中部是游戏原理及代码讲解,文末是完整代码。

游戏代码实现

1.绘制图像

在绘制游戏图像仿麦呢,我们采用的是数据库中较之pygame更为轻便的Turtle Graphics

构建数据库(gamebase.py)

  1. from turtle import * # “*”是引用所有函数
  2.  
  3. def square(x, y, size, color_name):
  4.      up()
  5.      goto(x, y)
  6.      down()
  7.      color(color_name)
  8.      begin_fill()
  9.  
  10.      forward(size)
  11.      left(90)
  12.      forward(size)
  13.      left(90)
  14.      forward(size)
  15.      left(90)
  16.      forward(size)
  17.      left(90)
  18.  
  19.      end_fill()
  20.  

上面是通过乌龟制图,前进单位距离后旋转90度,然后再旋转90度直至绘制出一个小方块

绘制苹果(snake.py)

引用刚刚我们绘图的数据库

  1. from turtle import *
  2. from gamebase import square
  3. from random import randrange

然后定义画布的大小

  1. setup(420,420,0,0)
  2. //隐藏乌龟头 emoj.emoj.
  3. hideturtle
  4. //隐藏轨迹
  5. tracer(False)
  6.  
  7. //绘制
  8. done()

定义游戏程序循环(相当于Java中的loop线程)

  1. def gameLoop():
  2.      //随机生成苹果
  3.      apple_x = randrange(-200, 200)
  4.      apple_y = randrange(-200, 200)
  5.      //绘制苹果
  6.      square(apple_x, apple_y, 10, “red”)
  7.      //刷新画布
  8.      update()

绘制贪吃蛇(snake.py)

注意,我们可以把贪吃蛇看作一个队列,而这个队列之中的每一个元素都包含两个变量(该元素的横纵坐标)

  1. def gameLoop():
  2.      //随机生成苹果
  3.      apple_x = randrange(-200, 200)
  4.      apple_y = randrange(-200, 200)
  5.      //绘制蛇
  6.      for n in range(len(sanke)):
  7.          square(snake[n][0],snake[n][1[],10,“black)
  8.      //绘制苹果
  9.      square(apple_x, apple_y, 10, “red“)
  10.      //刷新画布
  11.      update()

绘制贪吃蛇的运动

贪吃蛇运动原理:为了方便使蛇移动,我们要把蛇倒装到队列中,在它移动的时候,我们会抛出蛇队列的第一个元素(pop()),然后,在蛇尾新增一个元素(append())

  1. 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
  2. #全局变量申请
  3. snake.append([ snake[-1][0] + aim_x, snake[-1][1] + aim_y ])
  4. snake.pop(0)

然后,我们要加入循环刷新运行时间,

  1. ontimer(gameLoop, 100)
  2.      # 每100毫秒运行一下gameLoop函数

制作贪吃蛇操作响应

我们要建立键盘监听,这对于python而言是十分简单的

  1. listen() #监听
  2. onkey(lambda: change(0, 10), “w”)
  3. onkey(lambda: change(0, 10), “s”)
  4. onkey(lambda: change(-10, 0), “a”)
  5. onkey(lambda: change(10, 0), “d”)
  6. gameLoop()

判定蛇是否吃到苹果

这个也是十分容易,我们只用比较蛇头的横纵坐标是否都等于苹果的横纵坐标(蛇头与苹果重合)

  1.      if snake[-1][0] != apple_x or snake[-1][1] != apple_y:
  2.          snake.pop(0)
  3.      else:
  4.          apple_x = randrange(-20, 18) * 10
  5.          apple_y = randrange(-19, 19) * 10

判定蛇是否咬到自己

这个原理和上面蛇是否吃到苹果原理类似

  1. def bite():
  2.      for n in range(len(snake)-1):
  3.          if snake[-1][0] == snake[n][0] and snake[-1][1] == snake[n][1]:
  4.              return True
  5.      return False

判定蛇是否在界内

  1. def inside():
  2.      if 200 <= snake[-1][0] <= 180 and 190 <= snake[-1][1]<=190:
  3.          return True
  4.      else :
  5.          return False

游戏源码

gamebase.py

  1. from turtle import * “*”是引用所有函数
  2.  
  3. def square(x, y, size, color_name):
  4.      up()
  5.      goto(x, y)
  6.      down()
  7.      color(color_name)
  8.      begin_fill()
  9.  
  10.      forward(size)
  11.      left(90)
  12.      forward(size)
  13.      left(90)
  14.      forward(size)
  15.      left(90)
  16.      forward(size)
  17.      left(90)
  18.  
  19.      end_fill()

snake.py

  1. from time import sleep
  2.  
  3. apple_x = randrange(-20, 18) * 10
  4. apple_y = randrange(-19, 19) * 10
  5. snake = [[0, 0], [10, 0], [20, 0], [30, 0], [40, 0], [50, 0]]
  6. aim_x = 10
  7. aim_y = 0
  8.  
  9.  
  10. def inside():
  11.      if 200 <= snake[-1][0] <= 180 and 190 <= snake[-1][1]<=190:
  12.          return True
  13.      else :
  14.          return False
  15.  
  16.  
  17. def change(x, y):
  18.      global aim_x, aim_y
  19.      aim_x = x
  20.      aim_y = y
  21.  
  22.  
  23. def bite():
  24.      for n in range(len(snake)-1):
  25.          if snake[-1][0] == snake[n][0] and snake[-1][1] == snake[n][1]:
  26.              return True
  27.      return False
  28.  
  29.  
  30. def gameLoop():
  31.      global apple_x, apple_y, snake, aim_x, aim_y #全局变量申请
  32.      snake.append([ snake[-1][0] + aim_x, snake[-1][1] + aim_y ])
  33.      if snake[-1][0] != apple_x or snake[-1][1] != apple_y:
  34.          snake.pop(0)
  35.      else:
  36.          apple_x = randrange(-20, 18) * 10
  37.          apple_y = randrange(-19, 19) * 10
  38.  
  39.      if(not inside()) or bite():
  40.          square(snake[-1][0], snake[-1][1], 10,“hotpink”)
  41.          update()
  42.          sleep(2)# 暂停2
  43.          apple_x = randrange(-20, 18) * 10
  44.          apple_y = randrange(-19, 19) * 10
  45.          snake = [[0, 0], [10, 0], [20, 0], [30, 0], [40, 0], [50, 0]]
  46.          aim_x = 10
  47.          aim_y = 0
  48.      n = 0
  49.      clear()
  50.      square(-210,-200,410,“black”)
  51.      square(-200,-190,390,“white”)
  52.      square(apple_x, apple_y, 10, “red”)
  53.      for n in range(len(snake)):
  54.          square(snake[n][0], snake[n][1], 10, ‘black’)
  55.      ontimer(gameLoop, 100) # 每300毫秒运行一下gameLoop函数
  56.      update()
  57. #注意:代码的前后顺序会给游戏带来不同的体感
  58.  
  59. setup(420, 420, 0, 0)
  60. hideturtle()
  61. tracer(False)
  62. listen() #监听
  63. onkey(lambda: change(0, 10), “w”)
  64. onkey(lambda: change(0, 10), “s”)
  65. onkey(lambda: change(-10, 0), “a”)
  66. onkey(lambda: change(10, 0), “d”)
  67. gameLoop()
  68. done()

到此这篇关于教你从零开始实现贪吃蛇Python小游戏的文章就介绍到这了,更多相关Python贪吃蛇小游戏内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

标签

发表评论