我正在SenseHAT上创建蛇游戏,并且试图使蛇在触碰到自己时死亡。
我变了一个死变量并使它变假,以便当它为假时它使我的蛇停止运动。但这是行不通的。
以下是我的整个代码,因为我不太确定是什么导致了问题
import random
from random import randint
import time
from sense_hat import SenseHat
sense = SenseHat()
dead = False
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)
pause = (0.5)
def draw_snake():
for segment in snake:
sense.set_pixel(segment[0], segment[1], green)
def move():
global score, pause
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
dead = True
snake.append(next)
sense.set_pixel(next[0], next[1], green)
if next in vegetables:
vegetables.remove(next)
score += 1
if score % 1 == 0:
remove = False
if score % 1 == 0:
pause = pause * 0.9
if remove == True:
sense.set_pixel(first[0], first[1], blank)
snake.remove(first)
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()
sense.stick.direction_any = joystick_moved
while not dead:
if len(vegetables) < 1:
make_food()
move()
time.sleep(pause)
我一直在寻找同一个蛇游戏的其他人的脚本以寻求帮助,但我真的看不出有什么区别(https://trinket.io/python/b9e8a05f5b)
谢谢