Python - 布尔测试,而循环永远不会结束

时间:2017-04-03 18:58:38

标签: python-3.x while-loop

您好我正在尝试编程一个覆盆子pi Sense帽子,使用accelorometer在Matrix周围移动一个像素。想法是到红色区域获胜。然而,如果它击中绿色黄色或蓝色,游戏将以亏损告终。当它找到其中一种颜色时,可以将布尔变量e(game_over)更改为True,从而打破循环以停止游戏。

问题是当它读取颜色被点击时它将变量game_over更改为True,但是当它返回到while循环时它会变回False。我已经使用print语句对此进行了测试,看看会发生什么。

from sense_hat import SenseHat
from time import sleep

sense = SenseHat()
sense.clear()

r = [150,0,0]
g = [0,150,0]
j = [150,150,0]
b = [0,0,150]
e = [0,0,0]
w = [255,255,255]
v = [125,150,175]

x = 4
y = 4

maze = [[r,r,r,r,r,r,r,b],
        [g,r,r,r,r,r,b,b],
        [g,g,e,e,e,e,b,b],
        [g,g,e,e,e,e,b,b],
        [g,g,e,e,e,e,b,b],
        [g,g,e,e,e,e,b,b],
        [g,g,j,j,j,j,j,b],
        [g,j,j,j,j,j,j,j]]

false = [
    r,e,e,e,e,e,e,r,
    e,r,e,e,e,e,r,e,
    e,e,r,e,e,r,e,e,
    e,e,e,r,r,e,e,e,
    e,e,e,r,r,e,e,e,
    e,e,r,e,e,r,e,e,
    e,r,e,e,e,e,r,e,
    r,e,e,e,e,e,e,r]


def move_marble(pitch,roll,x,y):
    new_x = x
    new_y = y
    if 1 < pitch < 179 and x != 0:
        new_x -= 1
    elif 359 > pitch > 179 and x != 7 :
        new_x += 1
    if 1 < roll < 179 and y != 7:
        new_y += 1
    elif 359 > roll > 179 and y != 0 :
        new_y -= 1
    x,y = check_wall(x,y,new_x,new_y)
    return x,y

def check_wall(x,y,new_x,new_y):
    if maze[new_y][new_x] != v:
        return new_x, new_y
    elif maze[new_y][x] != v:
        return x, new_y
    elif maze[y][new_x] != v:
        return new_x, y
    return x,y

game_over = False

def check_win(x,y):
    sleep(0.25)
    if maze[y][x] == r:
        sense.show_message('Vous avez gagner', scroll_speed=0.08)
        sleep(1)
        game_over = True
    elif maze[y][x] == j: 
        sense.set_pixels(false)
        game_over = True
        sleep(1)
        sense.show_message('Vous avez perdu', scroll_speed=0.08)

    elif maze[y][x] == b:
        sense.set_pixels(false)
        game_over = True
        sleep(1)
        sense.show_message('Vous avez perdu', scroll_speed=0.08)

    elif maze[y][x] == g:
        sense.set_pixels(false)
        game_over = True
        sleep(1)
        sense.show_message('Vous avez perdu', scroll_speed=0.08)


while game_over == False:
    pitch = sense.get_orientation()['pitch']
    roll = sense.get_orientation()['roll']
    x,y = move_marble(pitch,roll,x,y)
    check_win(x,y)
    maze[y][x] = w
    sense.set_pixels(sum(maze,[]))
    sleep(0.1)
    maze[y][x] = e
sense.clear()

我确定它很小,我似乎无法找到它。

0 个答案:

没有答案