How do you get the numerical values in this collision function

42 Views Asked by At

so here is some code. basically, the goal here is for the turtle not to be able to pass any of the blue lines, meaning it can't go inside the shape. The code works perfectly but I don't particularly understand how the code works I will explain in details the lines I don't understand below. The Code:

import turtle 
import random
sam=turtle.Turtle()
score=turtle.Turtle()
screen=turtle.Screen()
PEN_SIZE = 10
CURSOR_SIZE = 20
BOUNDARY = PEN_SIZE/2 + CURSOR_SIZE/2
sam.pensize(PEN_SIZE)
sam.speed('fastest')
sam.penup()
sam.goto(-400,-400)
sam.pendown()
sam.speed(0)
sam.color("blue")
screen.bgcolor("black")
class lshape:
    def __init__(self,turtle):
        self.turtle=turtle
        self.position = None
    def draw(self):
        self.position = self.turtle.position()
        sam.fd(100)
        sam.lt(90)
        sam.fd(50)
        sam.lt(90)
        sam.fd(20)
        sam.lt(90)
        sam.fd(30)
        sam.rt(90)
        sam.fd(80)
        sam.lt(90)
        sam.fd(20)
        sam.lt(90)
    def collision(self, position):
        # outer bounding box = (0, 0, 50, 100)
        # internal safe zone = (0, 0, 30, 80)

        x1, y1 = self.position
        x2, y2 = position

        if x1 - 50 - BOUNDARY < x2 < x1 + BOUNDARY and y1 - BOUNDARY < y2 < y1 + 100 + BOUNDARY:
            # inside outer
            if x2 < x1 - 20 - BOUNDARY and y2 < y1 + 80 - BOUNDARY:
                # inside inner
                return False

            # outside inner
            return True  # collision!

        return False
shapes = []
shape1 = lshape(sam)

sam.penup()
sam.goto(-120, -270)
sam.lt(90)
sam.pendown()
shape1.draw()
shapes.append(shape1)

j = turtle.Turtle()
j.shape('turtle')
j.color('yellow')

j.penup()
j.goto(200, -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.onkey(up, 'Up')
screen.onkey(down, 'Down')
screen.onkey(right, 'Right')
screen.onkey(left, 'Left')
screen.listen()

screen.mainloop()

Okay so the really just focus on the collision method in the lshape class because that is the only thing I dont get. In particular it is the if commands were are running. I understand that x1,y1= the blue turtles starting positon correct if I am wrong. I also undersand that x2,y2=the yellow turtles current position. What I dont not understand is where we are getting the valyes of 50,100,20,80 so far really the only comparison I see is that 3 of them are foward command values. So if someone can please explain to me how it works it would be much appreciated. In conclusion I want someone to explain to me in deep how the two if loops work in reaching our goal. If its easier to explain by showing I have another class you can test it on but it will be a little bit trickier as you have to make the collision function.

#Just substitute the first class with this class and make the collsison function for this one. Code to the second class:

class lshape2:
    def __init__(self, turtle):
        self.turtle = turtle
        self.position = None

    def draw(self):
        self.position = self.turtle.position()
        self.turtle.forward(150)
        self.turtle.left(90)
        self.turtle.forward(20)
        self.turtle.left(90)
        self.turtle.forward(40)
        self.turtle.right(90)
        self.turtle.forward(70)
        self.turtle.left(90)
        self.turtle.forward(20)
        self.turtle.left(90)
        self.turtle.forward(70)
        self.turtle.right(90)
        self.turtle.forward(90)
        self.turtle.left(90)
        self.turtle.forward(20)

0

There are 0 best solutions below