Python 如何使我的蛇更长,我的蛇游戏上的覆盆子皮SenseHat

Python 如何使我的蛇更长,我的蛇游戏上的覆盆子皮SenseHat,python,raspberry-pi,Python,Raspberry Pi,我正在SenseHat上做一个蛇游戏,我学习的网站告诉我,我需要使用mod操作符,以便让我的蛇在每次吃食物时都长得更长。但我的蛇却开始永远长高,尽管它什么也没吃 以下是我一直使用的网站,该链接直接将您发送到我所停留的部分: 虽然我已经看了一段时间了,但我不知道我需要在脚本中的什么地方以及如何使用它 这是我的整个游戏脚本 import random from random import randint import time from sense_hat import SenseHat sens

我正在SenseHat上做一个蛇游戏,我学习的网站告诉我,我需要使用mod操作符,以便让我的蛇在每次吃食物时都长得更长。但我的蛇却开始永远长高,尽管它什么也没吃

以下是我一直使用的网站,该链接直接将您发送到我所停留的部分:

虽然我已经看了一段时间了,但我不知道我需要在脚本中的什么地方以及如何使用它

这是我的整个游戏脚本

import random
from random import randint
import time
from sense_hat import SenseHat
sense = SenseHat()


vegetables = []
direction = "right"
colx = randint(0,255)
coly = randint(0,255)
colz = randint(0,255)
food_col = (colx, coly, colz)
blank = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
snake =[ [2,4], [3,4], [4,4] ]
score = (0)

def move():
  global score
  last = snake[-1]
  first = snake[0]
  next = list(last)     
  remove = True

  if direction == "right":
    if last[0] + 1 == 8:
     next[0] = 0
    else:
     next[0] = last[0] + 1

  elif direction == "left":
    if last[0] - 1 == -1: 
      next[0] = 7
    else:
      next[0] = last[0] - 1

  elif direction == "down":
    if last[1] + 1 == 8: 
      next[1] = 0
    else:
      next[1] = last[1] + 1

  elif direction == "up":
    if last[1] - 1 == -1: 
      next[1] = 7
    else: 
      next[1] = last[1] - 1

  if next in vegetables:
    vegetables.remove(next)
    score == 1

  if next in vegetables:
    if remove == True:
      sense.set_pixel(first[0], first[1], blank)
      snake.remove(first)
    if score % 5 == 0:
      remove = False
  snake.append(next)
  sense.set_pixel(next[0], next[1], green)
def draw_snake():
  for segment in snake: 
    sense.set_pixel(segment[0], segment[1], green)

def joystick_moved(event):
  global direction
  direction = event.direction

def make_food():
  new = snake[0]
  while new in snake:
    x = randint(0,7)
    y = randint(0,7)
    new = [x,y]
    for segment in new: 
      sense.set_pixel(x, y, food_col)
  vegetables.append(new)



sense.clear()
draw_snake()
while True: 
  if len(vegetables) < 3: 
    make_food()
  move()
  time.sleep(0.5)
  sense.stick.direction_any = joystick_moved

我想让我的蛇在每次吃东西的时候都能长出来,但它只是从一开始就永远长出来。如果您想在sense帽子上尝试我的脚本,这里有一个模拟器


谢谢你

斯内克。下一步行动中的附件不在状态。此外,在蔬菜条件下还有两个if-next,第一个从列表中删除,这意味着第二个将始终为false。您可能希望将两者合并。

考虑一下,就像您希望在特定事件中将一个元素附加到列表中一样。尝试将您的问题简化为这种情况,并从中成长。snake.appendnext不是条件是什么意思?您想执行snake.appendnext当蛇成长时,您的snake.appendnext当前处于检查蛇是否应该成长的条件之后,而不是在其中。当我缩进snake.appendnext使其处于remove=False或if remove==true下时:snake不移动您是否合并了这两个if-next?第一个使第二个无法运行。是的。我在蔬菜中合并了两个if-next:我是否也应该缩进感觉。设置_pixelnext[0],next[1],绿色?