Aug-16-2021, 06:20 PM
Hello! I am trying to build a small project in python turtle module but I am having some issue with how to make a wall (that is not an outside border)
I know how to do it in pygame, but this is the first time I am trying to do it in turtle and I need a little bit of help. I have removed almost everything except the wall I'm having issues with to make it simpler to read.
To explain what I've tried to do below, I've tried to add a collision trigger that checks if I'm close to the object in mind. The way it's setup is to make sure that a certain event happens if I hit the object. I've tested this function on hitting a small circle object and made it so that if I hit it, it dissapears. This is all fine and all but if I try to do this with the line(wall) below, then it only registers the collision on the top of the line, while I want to make it so that it registers it anywhere along the line.
How do I do this?
Had this been a pygame project, I would've just added the line to a class of walls and say something like if player.distance <=20: and wall in wall.wall then player.direction(180)
But since this is turtle. I don't think I can do it that way.
Any help would be appreciated.
I know how to do it in pygame, but this is the first time I am trying to do it in turtle and I need a little bit of help. I have removed almost everything except the wall I'm having issues with to make it simpler to read.
To explain what I've tried to do below, I've tried to add a collision trigger that checks if I'm close to the object in mind. The way it's setup is to make sure that a certain event happens if I hit the object. I've tested this function on hitting a small circle object and made it so that if I hit it, it dissapears. This is all fine and all but if I try to do this with the line(wall) below, then it only registers the collision on the top of the line, while I want to make it so that it registers it anywhere along the line.
How do I do this?
Had this been a pygame project, I would've just added the line to a class of walls and say something like if player.distance <=20: and wall in wall.wall then player.direction(180)
But since this is turtle. I don't think I can do it that way.
Any help would be appreciated.
import turtle import random import math # wn = Window wn = turtle.Screen() wn.bgcolor('black') wn.title('Freetown') class Line(turtle.Turtle): def __init__(self): turtle.Turtle.__init__(self) self.penup() self.hideturtle() self.speed(0) self.color('red') self.pensize(5) def draw_line(self): self.penup() self.goto(-200, -200) self.pendown() self.goto(-200,200) class Player(turtle.Turtle): def __init__(self): turtle.Turtle.__init__(self) self.score = 0 self.penup() self.speed(0) self.shape('triangle') self.color('green') self.speed = 1 def move(self): self.forward(self.speed) def turnleft(self): self.left(30) def turnright(self): self.right(30) def increasespeed(self): self.speed +=1 def decreasespeed(self): self.speed -=1 def isCollision2(t1, t2): a = t1.xcor()-t2.xcor() b = t1.ycor()-t2.ycor() distance = math.sqrt((a ** 2) + (b ** 2)) if distance < 20: player.clear() return True else: return False #Class instance player = Player() line = Line() #Draw the line line.draw_line() #Keyboard bindings turtle.listen() turtle.onkey(player.turnleft, 'a') turtle.onkey(player.turnright, 'd') turtle.onkey(player.increasespeed, 'w') turtle.onkey(player.decreasespeed,'s') while True: player.move() if isCollision2(player,line): player.setheading(180)