-1

I think it must have something to do with where I do or don't call the global variables, but I can't figure out how to solve this error. (This is a part of the snake game I'm trying in Python). Thank you so much for any tips or suggestions.

here is my code:

import snakelib
from snakelib import SnakeUserInterface

width = 0  # initialized in play_animation
height = 0  # initialized in play_snake
ui = None  # initialized in play_animation
SPEED = 10
keep_running = True
WIDTH = 9
HEIGHT = 9
x = width
y = height
position = (width, height)
direction = "r"
color = None


def draw():
    # global color,x,y
    ui.clear()
    ui.place(x, y, color)
    ui.show()

def move():
    global x, width
    if x <= width:
        x += 1


  def process_event(event):
     if event.name == 'alarm' and event.data == "refresh":
        process_alarm()
        draw()
     elif event.name == 'other' and event.data == "space":
        process_space()


 def process_alarm():
    global x, y, WIDTH, HEIGHT
    move()
    if x > WIDTH:
        y += 1
        x = 0
    if y > HEIGHT:
        x = 0
        y = 0


def process_space():
    global color
    if color == ui.SNAKE:
        color = ui.FOOD
    else:
        color = ui.SNAKE


def play_animation(init_ui):
    global width, height, ui, keep_running, color
    ui = init_ui
    width, height = ui.board_size()
    color = ui.SNAKE
    draw()

    while keep_running:
        event = ui.get_event()
        process_event(event)
       
        if event.name == "quit":
            keep_running = False
alex
  • 10,249
  • 13
  • 68
  • 93

0 Answers0