Can someone help me make borders for my pacman game so the turtle can't touch the blue? I'm running into trouble with that feature. Here is my code:
import turtle
import random
sam=turtle.Turtle()
score=turtle.Turtle()
screen=turtle.Screen()
sam.pensize(10)
sam.speed(0)
sam.penup()
sam.goto(-400,-400)
sam.pendown()
sam.speed(0)
sam.color("blue")
screen.bgcolor("black")
sam.penup()
sam.fd(200)
sam.pendown()
sam.fd(600)
sam.lt(90)
sam.fd(130)
sam.lt(90)
sam.fd(30)
sam.rt(90)
sam.fd(20)
sam.rt(90)
sam.fd(30)
sam.lt(90)
sam.fd(130)
sam.lt(90)
sam.fd(75)
sam.rt(90)
sam.fd(75)
sam.rt(90)
sam.fd(200)
sam.lt(90)
sam.fd(30)
sam.lt(90)
sam.fd(200)
sam.rt(90)
sam.fd(100)
sam.rt(90)
sam.fd(75)
sam.lt(90)
sam.fd(200)
sam.lt(90)
sam.fd(300)
sam.lt(90)
sam.fd(75)
sam.rt(90)
sam.fd(20)
sam.rt(90)
sam.fd(75)
sam.lt(90)
sam.fd(300)
sam.lt(90)
sam.fd(200)
sam.lt(90)
sam.fd(75)
sam.rt(90)
sam.fd(100)
sam.rt(90)
sam.fd(200)
sam.lt(90)
sam.fd(30)
sam.lt(90)
sam.fd(200)
sam.rt(90)
sam.fd(75)
sam.rt(90)
sam.fd(75)
sam.lt(90)
sam.fd(130)
sam.lt(90)
sam.fd(30)
sam.rt(90)
sam.fd(20)
sam.rt(90)
sam.fd(30)
sam.lt(90)
sam.fd(130)
sam.lt(90)
sam.fd(30)
class lshape:
def __init__(self,turtle):
self.turtle=turtle
def draw(self):
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)
class lshape2:
def __init__(self,turtle):
self.turtle=turtle
def draw(self):
self.turtle.fd(150)
self.turtle.lt(90)
self.turtle.fd(20)
self.turtle.lt(90)
self.turtle.fd(40)
self.turtle.rt(90)
self.turtle.fd(70)
self.turtle.lt(90)
self.turtle.fd(20)
self.turtle.lt(90)
self.turtle.fd(70)
self.turtle.rt(90)
self.turtle.fd(90)
self.turtle.lt(90)
self.turtle.fd(20)
class lshape3:
def __init__(self,turtle):
self.turtle=turtle
def draw(self):
self.turtle.fd(150)
self.turtle.rt(90)
self.turtle.fd(20)
self.turtle.rt(90)
self.turtle.fd(65)
self.turtle.lt(90)
self.turtle.fd(70)
self.turtle.rt(90)
self.turtle.fd(20)
self.turtle.rt(90)
self.turtle.fd(70)
self.turtle.lt(90)
self.turtle.fd(65)
self.turtle.rt(90)
self.turtle.fd(20)
class lshape4:
def __init__(self,turtle):
self.turtle=turtle
def draw(self):
for i in range(2):
self.turtle.fd(100)
self.turtle.rt(90)
self.turtle.fd(20)
self.turtle.rt(90)
class lshape5:
def __init__(self,turtle,l):
self.l=l
self.turtle=turtle
def draw(self):
for i in range(2):
self.turtle.fd(self.l)
self.turtle.rt(90)
self.turtle.fd(40)
self.turtle.rt(90)
class Wshape:
def __init__(self,turtle):
self.turtle=turtle
def draw(self):
self.turtle.fd(150)
self.turtle.lt(90)
self.turtle.fd(20)
self.turtle.lt(90)
self.turtle.fd(90)
self.turtle.rt(90)
self.turtle.fd(70)
self.turtle.lt(90)
self.turtle.fd(20)
self.turtle.lt(90)
self.turtle.fd(70)
self.turtle.rt(90)
self.turtle.fd(40)
self.turtle.lt(90)
self.turtle.fd(20)
class Wshape2:
def __init__(self,turtle):
self.turtle=turtle
def draw(self):
sam.fd(100)
sam.rt(90)
sam.fd(50)
sam.rt(90)
sam.fd(20)
sam.rt(90)
sam.fd(30)
sam.lt(90)
sam.fd(80)
sam.rt(90)
sam.fd(20)
sam.lt(90)
class Box:
def __init__(self,turtle,x,y):
self.turtle=turtle
self.x=x
self.y=y
def draw(self):
for i in range(2):
self.turtle.fd(200)
self.turtle.lt(90)
self.turtle.fd(100)
self.turtle.lt(90)
self.turtle.penup()
self.turtle.goto(self.x-5,self.y-5)
self.turtle.pendown()
for i in range(2):
self.turtle.fd(210)
self.turtle.lt(90)
self.turtle.fd(110)
self.turtle.lt(90)
shape=lshape(sam)
shape2=lshape2(sam)
shape3=lshape3(sam)
shape4=lshape4(sam)
shape5=lshape5(sam,100)
wshape=Wshape(sam)
wshape2=Wshape2(sam)
box=Box(sam,-5,-80)
sam.penup()
sam.goto(-120,-270)
sam.lt(90)
sam.pendown()
shape.draw()
sam.penup()
sam.goto(-140,-350)
sam.rt(90)
sam.pendown()
shape2.draw()
sam.penup()
sam.goto(25,-260)
sam.lt(90)
sam.pendown()
shape3.draw()
sam.penup()
sam.goto(190,-350)
sam.rt(90)
sam.pendown()
wshape.draw()
sam.penup()
sam.goto(310,-270)
sam.rt(180)
sam.pendown()
wshape2.draw()
sam.penup()
sam.goto(150,-200)
sam.lt(90)
sam.pendown()
shape4.draw()
sam.penup()
sam.goto(-50,-200)
sam.pendown()
shape4.draw()
sam.penup()
sam.goto(25,-120)
sam.pendown()
shape3.draw()
sam.penup()
sam.goto(-100,-120)
sam.pendown()
shape4.draw()
sam.penup()
sam.goto(265,-120)
sam.pendown()
shape4.draw()
sam.penup()
sam.goto(-5,-80)
sam.pendown()
sam.rt(90)
box.draw()
sam.penup()
sam.goto(-100,20)
sam.pendown()
sam.lt(90)
shape3.draw()
sam.penup()
sam.goto(20,170)
sam.pendown()
sam.rt(180)
shape3.draw()
sam.penup()
sam.goto(280,170)
sam.pendown()
sam.lt(180)
shape3.draw()
sam.penup()
sam.goto(150,250)
sam.pendown()
shape5.draw()
shape5=lshape5(sam,70)
sam.penup()
sam.goto(300,250)
sam.pendown()
shape5.draw()
shape5=lshape5(sam,100)
sam.penup()
sam.goto(-70,250)
sam.pendown()
shape5.draw()
shape5=lshape5(sam,70)
sam.penup()
sam.goto(-190,250)
sam.pendown()
shape5.draw()
j=turtle.Turtle()
j.shape("turtle")
j.color("yellow")
j.penup()
j.goto(200,-370)
j.pendown()
def right():
j.rt(90)
def left():
j.lt(90)
def up():
j.fd(10)
def down():
j.bk(10)
j.penup()
screen.onkey(up,"Up")
screen.onkey(down,"Down")
screen.onkey(right,"Right")
screen.onkey(left,"Left")
screen.listen()
##Im trying to make it so that if the yellow turtle cant go through any of the blue borders. Also I cant use any packages other than turtle.
for i in range(-220,420,1):
x=int(j.xcor())
y=int(j.ycor())
if x-(i)<=5 and y-(-400)<=5:
j.hideturlte()
screen.update()
I tried to find the measure of the absolute value by misusing the value of the turtle's xcor to the xcor of the borders. But it doesn't work and I was wondering if there is a quicker way to go about this. If anyone can give me a block of code that can help me solve this problem it would be much appreciated.
Drawing your boundaries makes it hard to handle collisions. You could use
begin_poly()andend_poly()around your drawing and then save the result ofget_poly(). You'd then need a routine to determine if a point (the turtle's position) is inside any of your polygons. (Such routines are available on the web.) Another approach is to build with simpler shapes, say using a wider pen and fewer lines or by stamping squares of a uniform size instead of drawing.To keep your basic program in tact, we could add a
collision()method to each of yourlshape*objects that determines if the shape has been hit by the turtle. You'd need to save the original position where the shape was drawn. It'd look something like this:Run it to see if this is the kind of functionality you desire. This approach would be most difficult with the maze's outer boundary.
Code suggestions to make things easier for you: rename
lshapeto belshape1and ditto related variables; encapsulate the outer boundary as object classlshape0so you can use similar logic on it as the inner boundaries.