-1

I was working on fixing up some code to make it look better and when I finished copying the code over and finishing fixing it, I ran it and nothing happened. Two things loaded then everything clears. I can't find the problem.

Original code works

import turtle
from random import randint
import time
"""-------------"""
t=turtle.Turtle()
s=turtle.Screen()
cube=turtle.Turtle()
title=turtle.Turtle()
"""--------------"""
WIDTH, HEIGHT=300, 300
LENGTH=(5)
"""--------------"""
x1 = randint(cube.xcor()/2 - WIDTH/2, WIDTH/2 - cube.xcor()/2)
y1 = randint(cube.ycor()/2 - HEIGHT/2, HEIGHT/2 - cube.ycor()/2)
"""------------"""
s.setup(WIDTH,HEIGHT)
"""------------"""
t.width(1)
s.bgcolor("dark green")
"""-----------------"""
#Title
title.color("Red")
title.hideturtle()
title.penup()
title.goto(0,130)
title.pendown()
title.write("SNAK3",align="center", font=("Lobster", 20, "Bold"))
"""-------------"""
class Food():
    def block():
        cube.hideturtle()
        cube.color("red")
        cube.penup()
        cube.goto(x1,y1)    
        cube.speed(0)
        cube.begin_fill()
        for i in range(4):
            cube.pendown()
            cube.forward(10)
            cube.right(90)
        cube.end_fill()
"""---------------"""
t.shape("square")
class Player():
    def move_up():
        player=False
        while player==False:
            for i in range(10):
                t.forward(LENGTH)
                t.delay(25)
            t.clear()
            x, y = t.position()
            if not -WIDTH / 2 < x < WIDTH / 2 or not -HEIGHT / 2 < y < HEIGHT / 2:
                player=True
                t.hideturtle()
                cube.clear()
                title.clear()
                title.penup()
                title.goto(0,0)
                title.pendown()
                title.write("GAMEOVER", align="center", font=("lobster", 18, "bold"))
    def move_left():
        t.speed(0)
        t.left(90)
        t.speed(3)
    def move_right():
        t.speed(0)
        t.right(90)
        t.speed(3)
"""------------"""
food=Food.block()
player1=Player()
"""----------------"""
s.onkey(Player.move_up,"up")
s.onkey(Player.move_left,"left")
s.onkey(Player.move_right,"right")
s.listen()
"""--------------"""
collision=Collision_check.check()
"""--------------------"""
t1=t.xcor(),t.ycor()
cube1=cube.xcor(),cube.ycor()
class Collision_check():
    def check():
        crash=True
        if t1.xcor()>=cube.xcor() and t1.xcor()<=cube.xcor()+10 and t1.ycor>=cube.ycor() and t1.ycor()<=cube.ycor+10:
            cube.clear()
            cube.goto(x1,y1)
            LENGTH=+1
        elif t1!=cube1:
            crash=False
        return crash

Fixed code with slight changes

import turtle
from random import randint

t=turtle.Turtle()
s=turtle.Screen()
cube=turtle.Turtle()
title=turtle.Turtle()

t.width(1)
s.bgcolor("dark green")

#Title
title.color("red")
title.hideturtle()
title.penup()
title.goto(0,130)
title.write("SNAK3",align="center",font=("lobster",20,"Bold"))

WIDTH, HEIGHT=300, 300
LENGTH=[5]

s.setup(WIDTH,HEIGHT)

#Random cords
x1=randint(cube.xcor()/2-WIDTH/2, WIDTH/2-cube.xcor()/2)
y1=randint(cube.ycor()/2-HEIGHT/2, HEIGHT/2-cube.ycor()/2)

class Food():
    def block():
        cube.hideturtle()
        cube.color("red")
        cube.penup()
        cube.goto(x1,y1)
        cube.speed(0)
        cube.begin_fill()
        cube.pendown()
        for i in range(4):
            cube.forward(10)
            cube.right(90)
        cube.end_fill()

t.shape("square")
class Player():
    def forward():
        player=False
        while player==False:
            for i in range(10):
                t.forward(LENGTH)
                t.delay(25)
            t.clear
            x, y=t.position()
            if not -WIDTH / 2 < x < WIDTH / 2 or not -HEIGHT / 2 < y < HEIGHT/2:
                player=True
                cube.clear()
                t.hideturtle()
                title.clear()
                title.penup()
                title.goto(0,0)
                title.pendown()
                title.write("GAMEOVER", align="center", font=("lobster",18,"bold"))
def left():
        t.speed(0)
        t.left(90)
        t.speed(3)
    def right():
        t.speed(0)
        t.right(90)
        t.speed(3)

food=Food.block()
player1=Player()

s.onkey(player1.forward,("up"))
s.onkey(player1.left,("left"))
s.onkey(player1.left,("right"))

class Collision_check():
    def check():
        crash=True
        if t.xcor()>=cube.xcor() and t.xcor()<=cube.xcor()+10 and t.ycor()>=cube.ycor() and t.ycor()<=cube.ycor()+10:
            cube.clear()
            cube.goto(x1,y1)
            LENGTH+=1
            if cube.xcor()>=t.xcor() and cube.xcor()<=t.xcor()+10 and cube.ycor()>=t.ycor() and cube.ycor()<=t.ycor()+10:
            cube.clear()
            cube.goto(x1,y1)
            LENGTH+=1
        else:
            crash=False
        return crash
collision=Collision_check.check()

Everything in the code above is nearly exactly the same as the original. Ive gone over the code several times and can not find the problem. I've ruled out that the collision_check is not the problem, all the classes are in order and aligned. The only other thing I could think of is that i have spelled something wrong.

tobias_k
  • 81,265
  • 12
  • 120
  • 179
geek2001
  • 67
  • 1
  • 3
  • 7
  • I suggest that you learn to use a debugger. https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems and https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ have some great tips to help you start. – Code-Apprentice Jan 10 '18 at 15:12
  • Those where just to separate each line from each other. I later did use black lines when I copied the code to fix it. – geek2001 Jan 10 '18 at 15:17
  • Please highlight what those "slight changes" are. – tobias_k Jan 10 '18 at 15:17
  • 1
    What's your question? – Psytho Jan 10 '18 at 15:20
  • Ok, so, I am trying to make a game like Snake for my final. I do not have pygame so I'm trying to make it on this. My problem is when I run the revised version of the code(bottom code), It will load one or two things (ex. the player and back round), and then the screen clears and then ends the code. BTW* I'm using a website called repl.it, I do not have python installed. – geek2001 Jan 10 '18 at 15:27

1 Answers1

0

Let's first address some basic facts:

  • Neither version of this code works

  • The slight changes to make it look better don't make it look better and aren't slight, there are significant differences

  • Neither version of the code uses Python classes correctly

  • repl.it provides its own implementation of turtle which is generally a subset of the original

A program is like a story, you first explain the background of events and then you proceed to tell the story in an order that makes logical sense.

Here's my rework of your code to try to tell a better story, but it still doesn't work completely as there are parts of the story missing:

from turtle import Turtle, Screen
from random import randint

WIDTH, HEIGHT = 300, 300

class Food():
    def block():
        cube.penup()
        cube.goto(x1, y1)
        cube.pendown()

        cube.begin_fill()
        for _ in range(4):
            cube.forward(10)
            cube.right(90)
        cube.end_fill()

class Player():
    def forward():

        for _ in range(10):
            t.forward(LENGTH)
            # t.delay(25)  # no such turtle method, perhaps repl.it addition
        t.clear()

        x, y = t.position()

        if not -WIDTH / 2 < x < WIDTH / 2 or not -HEIGHT / 2 < y < HEIGHT/2:
            cube.clear()
            t.hideturtle()
            title.clear()
            title.penup()
            title.goto(0, 0)
            title.pendown()
            title.write("GAMEOVER", align='center', font=('lobster', 18, 'bold'))

    def left():
        t.speed(0)
        t.left(90)
        t.speed(3)

    def right():
        t.speed(0)
        t.right(90)
        t.speed(3)

class Collision_check():
    def check():
        global LENGTH
        crash = True

        if t.xcor() >= cube.xcor() and t.xcor() <= cube.xcor() + 10 and t.ycor() >= cube.ycor() and t.ycor() <= cube.ycor() + 10:
            cube.clear()
            cube.goto(x1, y1)
            LENGTH += 1
            if cube.xcor() >= t.xcor() and cube.xcor() <= t.xcor()+10 and cube.ycor() >= t.ycor() and cube.ycor() <= t.ycor()+10:
                cube.clear()
                cube.goto(x1, y1)
                LENGTH += 1
        else:
            crash = False

        return crash

s = Screen()
s.setup(WIDTH, HEIGHT)
s.bgcolor('dark green')

t = Turtle()
t.shape('square')
t.width(1)

cube = Turtle()
cube.hideturtle()
cube.color('red')
cube.speed('fastest')

# Title
title = Turtle()
title.hideturtle()
title.color('red')
title.penup()
title.goto(0, 130)
title.write("SNAK3", align='center', font=('lobster', 20, 'bold'))

LENGTH = 5

# Random cords
x1 = randint(cube.xcor()/2 - WIDTH/2, WIDTH/2 - cube.xcor()/2)
y1 = randint(cube.ycor()/2 - HEIGHT/2, HEIGHT/2 - cube.ycor()/2)

food = Food.block()
player1 = Player()

s.onkey(Player.forward, 'Up')
s.onkey(Player.left, 'Left')
s.onkey(Player.right, 'Right')
s.listen()

# this makes no sense here, needs to happen on a regular basis after moves
collision = Collision_check.check()

s.mainloop()
cdlane
  • 40,441
  • 5
  • 32
  • 81
  • If you put everything after the classes, how is python suppose to know what's in the classes is before it's told what it is? If it's like you said "a story", the story has a climax before there is an intro to the story. Also you did fix the loading problem but now the "player" can't move. – geek2001 Jan 11 '18 at 14:14
  • @geek2001, you broke motion as part of your *slight changes* when you went from invoking a class method `s.onkey(Player.move_up,"up")` to invoking an instance method `s.onkey(player1.forward,("up"))` as your methods lacked a `self` argument as they are class methods. I've fixed this in my example code. As far as the location of the classes in the code, what I did is correct. Background story before current events. – cdlane Jan 11 '18 at 20:00