-1

I'm currently making a snake game in python (IDLE 3.6.0) and it keeps coming up with an error called 'break' outside loop. What does that mean? What am I doing wrong. This is the first time I've ever come accross this error.

Here's my code:

# SNAKES GAME
# Use ARROW KEYS to play, SPACE BAR for pausing/resuming and Esc Key for exiting

import curses
from curses import KEY_RIGHT, KEY_LEFT, KEY_UP, KEY_DOWN
from random import randint


curses.initscr()
win = curses.newwin(20, 60, 0, 0)
win.keypad(1)
curses.noecho()
curses.cur_set(0)
win.border(0)
win.nodelay(1)

key = KEY_RIGHT                                          # Initalizing Values
score = 0

snake = [[4,10], [4,9], [4,8]]                          # Initial snake co-ordinates
food = [10,20]                                          # First food co-ordinates                         

win.addc(food[0], food[1], '*')                             # Prints the food

while key != 27:
    win.border(0)
    win.addstr(0, 2, 'Score :' + str(score) + ' ')          # Printing 'Score' and      
    win.addstr(0, 27, ' SNAKE ')                            # 'SNAKE' strings
    win.timeout(150 - (len(snake)/5 + len(snake)/10)%120)

    prevKey = key                                           # Previous key pressed
    event = win.getch
    key = key if event == -1 else event


    if key == ord(' '):                                     # If SPACE BAR is pressed, wait for another
        key = -1                                            # one (Pause/Resume)
        while key != ord(' '):
            key = win.getch()
    key = prevKey
    continue

if key not in [KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN, 27]:  # If an invalid key is pressed
    key = prevKey

    # Calculates the new coordinates of the head of the snake. NOTE: len(snake) increases.
    # This is taken care of later at [1].
    snake.insert(0, [snake[0][0] + (key == KEY_DOWN and 1) + (key == KEY_UP and -1), snake[0][1] + (key == KEY_LEFT and -1) + (key == KEY_RIGHT and 1)])

    # If snake crosses the boundaries, make it enter from the other side                
    if snake[0][0] == 0: snake[0][0] = 18
    if snake[0][1] == 0: snake[0][1] = 58
    if snake[0][0] == 19: snake[0][0] = 1
    if snake[0][1] == 59: snake[0][1] = 1

    # Exit if snake crosses the boundaries (Uncomment to enable)
    # if snake[0][0] == 0 or snake[0][0] == 19 or snake[0][1] == 0 or snake[0][1] == 59: break

    # If snake runs over itself
    if snake[0]in snake[1:]:  break


    if snake[0] == food:
        food = []                                                                                                                           
        score += 1
        while food == []:
            food = [randint(1, 18), randint(1, 58)]
            if food in snake: food = []
        win.addch(food[0], food[1], '*')
    else:                                                                                                                       
        last = snake.pop()
        win.addch(last[0], last[1], ' ')
    win.addch(snake[0][0], snake[0][1], '#')                                                                                                                               

curses.erdwin()
print("\nScore - " + str(score))
print("http://bitemelater.in\n")                                                                                                                               

I'd be glad if you could help! Thanks!

R.M.R
  • 193
  • 1
  • 2
  • 9
  • You have a `break` outside a loop. The error message should point to it; it's probably pointing to `if snake[0]in snake[1:]: break`. What did you think that would do? – user2357112 Feb 01 '17 at 20:04
  • Your `break` is outside your loop. What are you intending to accomplish with the large block beginning with `if key not in [KEY_LEFT...`? – BrenBarn Feb 01 '17 at 20:04
  • I thought that snake[0]in snake[1:]: break would kill the snake if it touched itself. – R.M.R Feb 01 '17 at 20:12

1 Answers1

0

The line that says if snake[0]in snake[1:]: break is where your error is stemming from. You probably want to use some kind of game ending function to display points and high scores and such:

def end_game():
    #do your game ending things here

Then you would call that function here:

if snake[0]in snake[1:]:  end_game()

EDIT:

I found this in relation to your curses error: Error no module named curses. It seems as if curses doesn't support Windows machines. Try installing UniCurses or this binary.

Community
  • 1
  • 1
Steampunkery
  • 3,839
  • 2
  • 19
  • 28
  • Thank you, that worked but now this error is showing up Traceback (most recent call last): File "C:\Users\sony\AppData\Local\Programs\Python\Python36-32\Python Snake Game 3.6.py", line 4, in import curses File "C:\Users\sony\AppData\Local\Programs\Python\Python36-32\lib\curses\__init__.py", line 13, in from _curses import * ModuleNotFoundError: No module named '_curses' – R.M.R Feb 01 '17 at 20:22
  • Ohhhh... Sorry, my mistake! – R.M.R Feb 01 '17 at 20:30
  • yes. curses works on Linux systems. If you have many questions about curses and such, try google or a new question instead of the comments section. – Steampunkery Feb 01 '17 at 20:36
  • Um, okay. It's just that I'm going to study in a computer class with pupils who are 2 years older than me at school. I don't want to make a fool out of myself in front of them. Sorry if I'm bothering you though. – R.M.R Feb 01 '17 at 20:41
  • You're not bothering me, it's just that the comments section isn't the best place. I am about to add a new link to a windows version of curses (unofficial) if any of the things in my answer work, then accept it and post a new question if you have more to ask. – Steampunkery Feb 01 '17 at 20:47
  • can you help me on this question: http://stackoverflow.com/questions/42008560/futher-problems-with-python-snake-game?noredirect=1#comment71192970_42008560 – R.M.R Feb 02 '17 at 18:19