I am trying to make a Turtle Eater Game and I'm running into a minor bug(kind of)

37 views Asked by At

The objective of this project is to make a game in which the turtle eats all of the dots. I thought that this code would be an intuitive way of going about the problem as opposed to manually checking each dot. However, this code only works for the first dot and none of the rest. I think it's because I'm using a while loop so the variable will always stay at 0 but I'm not too sure how to fix this error and keep the code simple without making it too long. If anyone can help me find a way to fix this, it would be extremely helpful.

import turtle
screen=turtle.Screen()
class Dots:
    def __init__(self,shape, color):
        self.t = turtle.Turtle()
        self.shape = shape
        self.color = color
        self.t.color(self.color)
        self.t.shape(self.shape)
    
    def xcor(self):
        return self.t.xcor()
    
    def ycor(self):
        return self.t.ycor()
    
    def goto(self, x, y):
        self.t.goto(x, y)
    
    def penup(self):
        self.t.penup()
    
    def pendown(self):
        self.t.pendown()
    def collide(self, other):
        if self.t.distance(other) <= 15:
            return True
        return False # Set up the screen
    def hideturtle(self):
      self.t.hideturtle()

x = -200
screen = turtle.Screen()
circles=[]
for i in range(14):
    my_dots = Dots("circle", "white")
    my_dots.penup()
    my_dots.goto(x, -370)
    circles.append(my_dots)
    x += 44
y=-330
for i in range(2):
    dot = turtle.Turtle()
    my_dots = Dots("circle", "white")
    my_dots.penup()
    my_dots.goto(-200, y)
    circles.append(my_dots)
    y += 40
    
y=-330
for i in range(2):
    dot = turtle.Turtle()
    my_dots = Dots("circle", "white")
    my_dots.penup()
    my_dots.goto(370, y)
    circles.append(my_dots)
    y += 40

x=-160
for i in range(3):
    dot = turtle.Turtle()
    my_dots = Dots("circle", "white")
    my_dots.penup()
    my_dots.goto(x,-290)
    circles.append(my_dots)
    x += 40

x=330
for i in range(2):
    dot = turtle.Turtle()
    my_dots = Dots("circle", "white")
    my_dots.penup()
    my_dots.goto(x,-290)
    circles.append(my_dots)
    x -= 40

y=-290
for i in range(3):
    dot = turtle.Turtle()
    my_dots = Dots("circle", "white")
    my_dots.penup()
    my_dots.goto(-80,y)
    circles.append(my_dots)
    y += 40
j = turtle.Turtle()
j.shape('turtle')
j.color('yellow')

j.penup()
j.goto(400, -370)

def right():
    j.rt(90)

def left():
    j.left(90)

def up():
    j.forward(10)

    position = j.position()

    for shape in shapes:
        if shape.collision(position):
            j.undo()
            return

def down():
    j.bk(10)

    position = j.position()

    for shape in shapes:
        if shape.collision(position):
            j.undo()
            return
screen.listen()
screen.onkeypress(up, 'Up')
screen.onkeypress(down, 'Down')
screen.onkeypress(right, 'Right')
screen.onkeypress(left, 'Left')
while True:
    screen.update() # Check for collision with the dot
    for i in range(len(circles)):
      if circles[i].collide(j):
        circles[i].hideturtle()
        screen.mainloop()
0

There are 0 answers