Reputation: 11
I am having some game breaking issues with a simple game that is similar to snake.
As you can see in the picture the snake is overlapping itself but has not collided like it is supposed to.
The collision with self does work some of the time as you can see but it is not reliable.
I have tried everything I can think of to fix this.
from turtle import Turtle, Screen
from random import randint
FONT = ('Arial', 24, 'normal')
WIDTH, HEIGHT = 400, 400
SPEED = 1
points = 0
posList = []
def left():
## turns your character left
char.left(90)
def right():
## turns your character right
char.right(90)
def point():
## adds one box to the point counter
global points
points += 1
wall.undo()
wall.write(str(points) + ' score', font=FONT)
dot.setpos(randint(-WIDTH/2, WIDTH/2), randint(-HEIGHT/2, HEIGHT/2))
dot.seth(randint(0,360))
def checkBracktrack(pos, poslist):
## checks if current posiition is anywhere you have ever been
return pos in poslist
def moveChar():
## updates the position of the player turtle
over = False
change = False
char.forward(SPEED)
# checks if current position is the same as any position it has ever been at
if checkBracktrack(char.pos(), posList):
over = True
# checks if in the box
elif not (-200 <= char.ycor() <= 200 and -200 <= char.xcor() <= 200):
over = True
if over:
print('you travelled', len(posList), 'pixels')
return
tempPos = char.pos()
# adds current location to the list
posList.append(tempPos)
# checks if it is close enough to a point marker
if char.distance(dot) < 20:
point()
# calls the moveChar function again
screen.ontimer(moveChar, 1)
# creates the box in which the game occurs
screen = Screen()
screen.onkey(left, "a")
screen.onkey(right, "d")
screen.listen()
dot = Turtle('turtle')
dot.speed('fastest')
dot.penup()
dot.setpos(randint(-WIDTH/2, WIDTH/2), randint(-HEIGHT/2, HEIGHT/2))
wall = Turtle(visible=False)
score = Turtle(visible=False)
wall.speed('fastest')
wall.penup()
wall.goto(WIDTH/2, HEIGHT/2)
wall.pendown()
for _ in range(4):
wall.right(90)
wall.forward(400)
try:
with open('score.txt','r') as file:
highScore = int(file.read())
except:
with open('score.txt','w') as file:
file.write('0')
highScore = 0
wall.penup()
wall.forward(50)
wall.write("0" + ' score', font=FONT)
score.penup()
score.setpos(wall.pos())
score.seth(270)
score.fd(30)
score.write(str(highScore) + ' high score', font=FONT)
score.right(180)
score.fd(30)
char = Turtle(visible=False)
char.speed('fastest')
moveChar()
screen.mainloop()
if points > highScore:
with open('score.txt','w') as file:
file.write(str(points))
print('new high score of ' + str(score) + ' saved')
print(posList)
Upvotes: 0
Views: 386
Reputation: 41872
I can't reproduce your error but let's try something. Since the turtle coordinate system is floating point, let's assume that small errors are creeping into the fractional portion of your positions that are preventing this test from working:
return pos in poslist
So, let's change that line to:
return (int(pos[0]), int(pos[1])) in poslist
And change the matching line in moveChar()
:
# adds current location to the list
posList.append((int(tempPos[0]), int(tempPos[1])))
Now we're only storing and testing the integer portion of the position so any fractional component will be ignored. Try this and let us know what happens.
Upvotes: 1