Reputation: 1
I am making a snake game on the SenseHat and the website i am learning from tells me that i need to use the mod operator in order to make my snake grow longer every time it eats a food. But my snake just starts growing for ever even though it did not eat anything
the following is the website that i have been using (the link sends you directly to the part that i am stuck on): https://projects.raspberrypi.org/en/projects/slug/9
Although i have been looking at this for a while, i have no idea where and how i need to put this in my script
This is my entire script for the game.
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
I am wanting to make it so that my snake will grow every time it eats a food but instead it just grows forever from the beginning. Here is a emulator if you want to try my script on a sense hat https://trinket.io/sense-hat
Thank You
Upvotes: 0
Views: 217
Reputation: 719
snake.append(next)
in move
is not in a condition. Also you have two if next in vegetables
conditions and the first removes it from the list which means the second will always be false. You probably want to merge the two.
Upvotes: 1