user17518258
user17518258

Reputation:

Python Turtle - Why does this program not work?

So, I was making a snake game.
I have the following code:

import turtle as tur;
import random, sys, time;

wn = tur.Screen(); #Screen
wn.setup(width=640, height=320);
wn.bgcolor("#000000")

"""

Instructions

"""
boundary = tur.Turtle();
boundary.hideturtle();
writingInstructions = ("Times New Roman", 30, "bold");
writing = ("Times New Roman", 20, "bold");
time.sleep(2);
boundary.write("Instructions:", font=writingInstructions, align="center");
time.sleep(2);
boundary.write("1. You need to eat the apples.", font=writing, align="center");
time.sleep(2);
boundary.write("2. You must not touch the boundary.", font=writing, align="center");
time.sleep(2);
boundary.write("Press 's' to start...");
def no():
    wn.clear();
wn.onkey(no, 's');
wn.listen();

"""

Code for game

"""
shapeSnake = ((-10, 0), (0, 10), (10, 0), (-10, -10)); 
#Initial shape of the snake declared
playArea = ((-200, 200), (200, 200), (200, -200), (-200, -200)); #Play area declared
tur.register_shape('snake', shapeSnake); #Reigster shape for snake
tur.register_shape('play', playArea); #Register shape for play area
t = tur.Turtle(shape='snake'); #Create snake shape
t.up();
a = tur.Turtle(shape='circle'); #Create apple
a.up();
a.goto(randint(-199, 199), randint(-199, 199));
screen = tur.Turtle(shape='play'); #Create play area
score = 0; #score initialisation

def travel(snakeSpeed): #For snake to move
    t.fd(snakeSpeed);

snakeSpeed = 2;

travel(snakeSpeed)
wn.onkey(t.seth(0), 'Right'); #For "right" button
wn.onkey(t.seth(180), 'Left'); #For "left" button
wn.onkey(t.seth(270), 'Down'); #For "down" button
wn.onkey(t.seth(90), 'Up'); #For "up" button

tx, ty = t.pos();
ax, ay = a.pos();
if (tx == ax) and (tx == ax): #Check if apple is eaten
    a.goto(randint(-199, 199), randint(-199, 199));
    score += 1;
    snakeSpeed += 2;
else:
    if (tx >= 200) or (tx <= -200): #Check if outside boundary in X-axis
        sys.exit();
    elif (ty >= 200) or (ty <= -200): #Check if outside boundary in Y-axis
        sys.exit();

wn.listen();
travel(snakeSpeed);
wn.mainloop();

What it does is that just a Turtle window with a black background pops up and stays blank.
If I press s, the background colour changes to white and that's all.

I have no idea why this doesn't work.
Any help is greatly appreciated.

Pls: I need to give this a birthday present tomorrow. If someone could give an immediate response, I would be extremely grateful.

Thanks in advance.

Upvotes: 0

Views: 72

Answers (1)

cdlane
cdlane

Reputation: 41925

I have no idea why this doesn't work.

Let's enumerate the posibilities. Your consistent use of semicolons (;) indicates a lack of understanding of Python syntax. This sequence:

boundary.write("Press 's' to start...");
def no():
    wn.clear();
wn.onkey(no, 's');
wn.listen();

"""
Code for game
"""
shapeSnake = ((-10, 0), (0, 10), (10, 0), (-10, -10)); 

implies you expect onkey() to wait for the 's' key to be pressed before the program goes on. This is not the case, it goes on immediately. Here you're passing the result of a call to t.seth(), i.e. None, instead of a function to be called later:

wn.onkey(t.seth(0), 'Right'); #For "right" button
wn.onkey(t.seth(180), 'Left'); #For "left" button
wn.onkey(t.seth(270), 'Down'); #For "down" button
wn.onkey(t.seth(90), 'Up'); #For "up" button

This is a misunderstanding of how to set up event handlers. What we expect is something more like:

wn.onkey(lambda: t.seth(90), 'Up')  # For "up" button

This scoring and boundary checking code appears at the top level of the file and is only executed once:

tx, ty = t.pos();
ax, ay = a.pos();
if (tx == ax) and (tx == ax): #Check if apple is eaten
    a.goto(randint(-199, 199), randint(-199, 199));
    score += 1;
    snakeSpeed += 2;
else:
    if (tx >= 200) or (tx <= -200): #Check if outside boundary in X-axis
        sys.exit();
    elif (ty >= 200) or (ty <= -200): #Check if outside boundary in Y-axis
        sys.exit();

We'd expect this code to be executed after every move, not just once before the snake really gets moving.

There is no code to keep the snake moving in a forward direction. It moves forward four pixels, in two spurts, at the start of the program and never again.

Upvotes: 1

Related Questions