Python Forum
Make a wall in turtle module
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Make a wall in turtle module
#1
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.

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)
Reply
#2
t2.ycor() will only return one end of your line. You need to test if the collision has taken place anywhere along the length of the wall. see if this is what you're looking for.

import turtle
import random
 
# 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):
		if abs (t1.xcor () - t2.xcor ()) < 20 :
			a = t1.ycor ()
			b = t2.ycor ()
			if a < b and a > b - 400 :
				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(360 - (player.heading () + 180))
Jan_97 likes this post
Reply
#3
Thank you! That was exactly what I was looking for.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Rect object penetrates wall on one side Jan_97 4 2,535 Dec-30-2021, 11:08 PM
Last Post: Jan_97
  Dugeon maze game with turtle module. Newbie1114 3 3,339 Jun-15-2021, 03:11 AM
Last Post: stephen645
  [Turtle]How to make smooth moving and add gravity? ZakHacks 2 6,440 Aug-25-2019, 02:27 AM
Last Post: SheeppOSU
  Multiple wall collision in pacman rustyjoe 4 4,101 Aug-11-2019, 08:08 AM
Last Post: rustyjoe
  Can't make the turtle walk.. Turry 3 4,238 Nov-26-2017, 05:05 PM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020