Python Turtle Collision Help and Updating Score/Lives - python

I am working on a scrolling game that uses multiple turtles. The player turtle moves on the Y-Axis on key commands. While the Harm and Benefits move across the X-Axis then looping around and changing Y-Axis position. I have tried defining a function called colliding() that can test if the player and benefit turtles collide. Can someone help with the collision function? And I also have a question on how, after detecting collision, to change the value of the player score? I think I have figured this out: I used player['lives']+=1. But this doesn't change because -- is something wrong with my collision function or loop?
import turtle
import random
import math
#The list of the turtles being used
t = turtle.Turtle()
Harm1 = turtle.Turtle()
Harm2 = turtle.Turtle()
Ben = turtle.Turtle()
player1 = turtle.Turtle()
#Screen Specifications
screen = turtle.Screen()
screen.setup(500,500)
screen.bgcolor('darkgray')
Harm1.tracer(0)
Harm2.tracer(0)
Ben.tracer(0)
player1.tracer(0)
#Character dictionary
player ={"type":'Player',"x" : -200,"y" : 0,"size" : 20,"speed" : 10,
"color" : "green","Lives":3,"Score":0}
harm = {"type":'Harm',"x" : -200,"y" : 0,"size" : 30,"speed" : 6,
"color" : "red",}
benefit = {"type":'Benefit',"x" : -200,"y" : 0,"size" : 15,"speed":6,
"color" : "yellow",}
#These can change when collision happens
lives = player['Lives']
score = player['Score']
#These are the keys that let the player move up/down
def move_up():
player['y']+=player['speed']
return
def move_down():
player['y']-= player['speed']
#Player gets info from dictionary
def draw_player():
player1.clear()
player1.penup()
player1.goto(player["x"], player["y"])
player1.pendown()
player1.color(player["color"])
player1.begin_fill()
player1.circle(player["size"])
player1.end_fill()
#if player1(player['y']) > 250 or player1(player['y']) < -250:
#player1.sety(player['y'])
player1.update()
player1.hideturtle()
screen.listen()
screen.onkey(move_up, 'up')
screen.onkey(move_down, 'down')
def draw_harms():
Harm1.clear()
Harm1.color(harm['color'])
Harm1.begin_fill()
Harm1.circle(harm['size'])
Harm1.end_fill()
Harm1.update()
Harm1.setx(Harm1.xcor()-(harm['speed']))
if (Harm1.xcor() < -260 ): #This part of the code makes the object comeback.
Harm1.setx(260)
Harm1.sety(random.randint(-160,160)) #This makes the object change Y cor
Harm1.hideturtle()
Harm2.clear()
Harm2.color(harm['color'])
Harm2.begin_fill()
Harm2.circle(harm['size'])
Harm2.end_fill()
Harm2.update()
Harm2.setx(Harm2.xcor()-(harm['speed']))
if (Harm2.xcor() < -260 ): #This part of the code makes the object comeback.
Harm2.setx(220)
Harm2.sety(random.randint(-160,160)) #This makes the object change Y cor
Harm2.hideturtle()
def draw_benefit():
Ben.clear()
Ben.color(benefit['color'])
Ben.begin_fill()
Ben.circle(benefit['size'])
Ben.end_fill()
Ben.update()
Ben.setx(Ben.xcor()-(benefit['speed']))
if (Ben.xcor() < -260 ): #This part of the code makes the object comeback.
Ben.setx(220)
Ben.sety(random.randint(-160,160)) #This makes the object change Y cor
Ben.hideturtle()
#This Keeps the score and Lives
def draw_title_name(): #This writes the title on the screen
t.penup()
t.goto(-210,-200)
t.pendown()
t.write(("Score:", score),font=("Arial", 18, "normal"))
t.penup()
t.goto(-210,-223)
t.pendown()
t.write(('Lives:',lives),font=('Arial',18,'normal'))
t.hideturtle()
return
def colliding(player,benefit):
collision_detected = False;
var_dx = player['x'] - benefit['x']
var_dy = player['y'] - benefit['y']
distance = math.sqrt(var_dx * var_dx + var_dy * var_dy)
if (distance < player['radius']+ benefit['radius']):
collision_detected = True;
return collision_detected
while lives > 0: #ANIMATION LOOP
draw_player()
draw_harms()
draw_benefit()
draw_title_name()
if colliding == True:
player['lives'] += 1 This changes the lives in the player Dict
if lives == 0:
clearscreen
#Finish with a gameover screen!

Your code is a disaster, there are any number of bits of logic in it that will keep it from running. To address your question, turtle has it's own distance() method to measure the distance between turtles or turtles and positions. Using that, your colliding() method could be as simple as:
def colliding(player, benefit):
return player.distance(benefit) < player_dict['size'] + benefit_dict['size']
if you were to actually call your colliding() method, which you don't, as #Hoog points out. Other showstoppers in this code:
if (distance < player['radius']+ benefit['radius']):
The radius properties are never defined.
var_dx = player['x'] - benefit['x']
Although player's x position is updated in the dictionary, benefit's isn't, so this will never work.
player['lives'] += 1 This changes the lives in the player Dict
Missing comment character.
clearscreen
What is this? Probably should be screen.clearscreen().
collision_detected = False;
...
collision_detected = True;
semicolons in Python are usually a sign that things aren't going well.
Harm1 = turtle.Turtle()
Harm2 = turtle.Turtle()
Ben = turtle.Turtle()
player1 = turtle.Turtle()
...
Harm1.tracer(0)
Harm2.tracer(0)
Ben.tracer(0)
player1.tracer(0)
Turtle instances don't respond to the tracer() method. Screen instances do.
player1.update()
Harm1.update()
Harm2.update()
Ben.update()
Ditto for update(). And so forth.
Below is my rework of your code to make it basically run:
from turtle import Screen, Turtle
from random import randint
FONT = ('Arial', 18, 'normal')
CURSOR_SIZE = 20
# Character dictionaries
player_dict = {'type': 'Player', 'x': -200, 'y': 0, 'radius': 20, 'speed':10, 'color': 'green', 'lives': 3, 'score': 0}
harm_dict = {'type': 'Harm', 'x': 0, 'y': 0, 'radius' : 30, 'speed': 6, 'color': 'red'}
benefit_dict = {'type': 'Benefit', 'x': 0, 'y': 0, 'radius': 15, 'speed': 6, 'color': 'yellow'}
# These are the keys that let the player move up/down
def move_up():
player_dict['y'] += player_dict['speed']
def move_down():
player_dict['y'] -= player_dict['speed']
# Player gets info from dictionary
def draw_player():
player1.sety(player_dict['y'])
def draw_harms():
harm1.forward(harm_dict['speed'])
if harm1.xcor() < -250 - harm_dict['radius']: # This part of the code makes the object come back.
harm1.hideturtle()
harm1.setx(250 + harm_dict['radius'])
harm1.sety(randint(-160, 160)) # This makes the object change Y coordinate
harm1.showturtle()
harm2.forward(harm_dict['speed'])
if harm2.xcor() < -250 - harm_dict['radius']: # This part of the code makes the object comeback.
harm2.hideturtle()
harm2.setx(250 + harm_dict['radius'])
harm2.sety(randint(-160, 160)) # This makes the object change Y coordinate
harm2.showturtle()
def draw_benefit():
ben.forward(benefit_dict['speed'])
if ben.xcor() < -250 - benefit_dict['radius']: # This part of the code makes the object comeback.
ben.hideturtle()
ben.setx(250 + benefit_dict['radius'])
ben.sety(randint(-160, 160)) # This makes the object change Y coordinate
ben.showturtle()
# This Keeps the score and Lives
def draw_lives():
lives.undo()
lives.write("Lives: {}".format(player_dict['lives']), font=FONT)
def draw_score():
score.undo()
score.write("Score: {}".format(player_dict['score']), font=FONT)
def colliding(player, benefit):
return player.distance(benefit) < player_dict['radius'] + benefit_dict['radius']
# Screen Specifications
screen = Screen()
screen.setup(500, 500)
screen.bgcolor('darkgray')
# The list of the turtles being used
t = Turtle(visible=False)
t.penup()
harm1 = Turtle('circle', visible=False)
harm1.color(harm_dict['color'])
harm1.shapesize(harm_dict['radius'] * 2 / CURSOR_SIZE)
harm1.penup()
harm1.setx(250 + harm_dict['radius'])
harm1.sety(randint(-160, 160))
harm1.setheading(180)
harm1.showturtle()
harm2 = Turtle('circle', visible=False)
harm2.color(harm_dict['color'])
harm2.shapesize(harm_dict['radius'] * 2 / CURSOR_SIZE)
harm2.penup()
harm2.setx(250 + harm_dict['radius'])
harm2.sety(randint(-160, 160))
harm2.setheading(180)
harm2.showturtle()
ben = Turtle('circle', visible=False)
ben.color(benefit_dict['color'])
ben.shapesize(benefit_dict['radius'] * 2 / CURSOR_SIZE)
ben.penup()
ben.setx(250 + benefit_dict['radius'])
ben.sety(randint(-160, 160))
ben.setheading(180)
ben.showturtle()
player1 = Turtle('circle', visible=False)
player1.color(player_dict['color'])
player1.shapesize(player_dict['radius'] * 2 / CURSOR_SIZE)
player1.penup()
player1.goto(player_dict['x'], player_dict['y'])
player1.showturtle()
score = Turtle(visible=False)
score.penup()
score.goto(-210, -200)
score.write("Score: {}".format(player_dict['score']), font=FONT)
lives = Turtle(visible=False)
lives.penup()
lives.goto(-210, -223)
lives.write("Lives: {}".format(player_dict['lives']), font=FONT)
screen.onkey(move_up, 'Up')
screen.onkey(move_down, 'Down')
screen.listen()
def move():
draw_player()
draw_harms()
draw_benefit()
# draw_score()
if colliding(player1, ben):
player_dict['lives'] += 1 # This increases the lives in the player dictionary
ben.hideturtle()
ben.setx(250 + benefit_dict['radius'])
ben.sety(randint(-160, 160))
ben.showturtle()
draw_lives()
if colliding(player1, harm1):
player_dict['lives'] -= 1 # This decreases the lives in the player dictionary
harm1.hideturtle()
harm1.setx(250 + harm_dict['radius'])
harm1.sety(randint(-160, 160))
harm1.showturtle()
draw_lives()
if colliding(player1, harm2):
player_dict['lives'] -= 1
harm2.hideturtle()
harm2.setx(250 + harm_dict['radius'])
harm2.sety(randint(-160, 160))
harm2.showturtle()
draw_lives()
if player_dict['lives'] == 0:
screen.clearscreen()
# Finish with a gameover screen!
return
if player_dict['lives'] > 0:
screen.ontimer(move, 75)
move()
screen.mainloop()
It still needs a lot of work, however.

Pygame has a built in collision detection algorithm rect.colliderect() which might help you out.
For your code the issue seems to be this line if colliding == True: you never set the variable colliding, perhaps you meant to call the function with something like: if colliding(Player1,Ben1) == True:

Related

I have problem with my game, when the ball goes over the block, it is not deleting from my screen

Someone can help me please?
I spent many hours to understand the problem.. (Without success 🤣)
I tried to built a 80s hit game.
The problem is that when the ball goes on one of the stones, I catch the event (shows me in the console) and update the list of blocks but the block is not deleted from the screen as if nothing happened and I did not update any list
this is my code:
import time
from turtle import Screen, Turtle
import random
screen = Screen()
screen.bgcolor("black")
screen.setup(width=800, height=600)
screen.tracer(0)
player = Turtle("square")
player.shapesize(1.0, 4.0, 0)
player.color("blue")
player.penup()
player.goto(0, -250)
screen.tracer(0)
blue_turtle = []
blue_positions = []
for i in range(7):
turtle = Turtle("square")
turtle.shapesize(2.0 ,4.0, 0)
turtle.color("blue")
turtle.penup()
blue_positions.append((-300 + i * 100, 200))
blue_turtle.append(turtle)
def renderBlocks(b):
for i in range(len(b) - 1):
if b[i] == None:
print("Skipped")
continue
else:
x, y= blue_positions[i]
b[i].goto(x=x, y=y)
# total += 100
ball = Turtle("circle")
ball.color("white")
game_on = True
def move():
x = ball.xcor() + 10
y = ball.ycor()+ 10
ball.goto(x=x, y=y)
while game_on:
move()
renderBlocks(blue_turtle)
for i in range(len(blue_turtle) - 1):
if (blue_turtle[i] != None) and ball.distance(blue_turtle[i]) < 20:
blue_turtle[i] = (None)
time.sleep(0.1)
screen.update()
screen.exitonclick()
I believe the problem is here:
blue_turtle[i] = (None)
You tell your code that the turtle has been eliminated but you don't tell turtle graphics that it should be removed. Instead consider:
blue_turtle[i].hideturtle()
blue_turtle[i] = None
My rework of your code addressing this issue and some others:
from turtle import Screen, Turtle
def renderBlocks(b):
for i in range(len(b) - 1):
if b[i]:
x, y = blue_positions[i]
b[i].goto(x, y)
def move():
if game_on:
x = ball.xcor() + 10
y = ball.ycor() + 10
ball.goto(x, y)
renderBlocks(blue_turtles)
for i in range(len(blue_turtles) - 1):
if blue_turtles[i] and ball.distance(blue_turtles[i]) < 20:
blue_turtles[i].hideturtle()
blue_turtles[i] = None
screen.update()
screen.ontimer(move, 100) # milliseconds
screen = Screen()
screen.setup(width=800, height=600)
screen.bgcolor('black')
screen.tracer(0)
player = Turtle('square')
player.shapesize(1, 4, 0)
player.color('blue')
player.penup()
player.sety(-250)
blue_turtles = []
blue_positions = []
for i in range(7):
turtle = Turtle('square')
turtle.shapesize(2, 4, 0)
turtle.color('blue')
turtle.penup()
blue_positions.append((-300 + i * 100, 200))
blue_turtles.append(turtle)
ball = Turtle('circle')
ball.color('white')
game_on = True
move()
screen.exitonclick()

Need help detecting collision in python

I’ve tried multiple things to try to detect collision between the player turtle and the apple turtle and nothing happens. What I wanted it to do is that when the turtles get too close, the player turtle disappears. If the apple gets to low below the player, it resets back up top.
Here is the code I’ve done. I’m new to code so I’m not very good.
import turtle as trtl
import random as rand
ground_height = -200
# lists for the turtle to use
colors = ["blue", "darkorange", "cyan", "green", "black"]
shapes = [ "triangle", "turtle", "classic"]
sizes = [0.5,1,1.25,1.5,2]
#creates the turtles
player = trtl.Turtle(shape = rand.choice(shapes)) #gives the circle its shape
player.turtlesize(1.5)
counter = trtl.Turtle()
apple = trtl.Turtle(shape = "circle")
apple.color("red")
apple.turtlesize(rand.choice(sizes))
#gives turtles their colors
player.color(rand.choice(colors))
player.penup()
apple.penup()
apple.setx(rand.randint(0,200))
apple.sety(rand.randint(0,200))
#sets up timer
font_setup = ("Arial", 20, "normal")
timer = 0
counter_interval = 1000
timer_up = False
counter.hideturtle()
counter.penup()
counter.goto(-200,160)
#gives player movement
def move_player():
player.forward(10)
def player_left():
player.left(180)
def player_right():
player.right(180)
apple.right(90)
#lets the timer start and end
def countdown():
global timer, timer_up
counter.clear()
if timer <= -1:
counter.write("Time's Up", font=font_setup)
timer_up = True
else:
counter.write("Timer: " + str(timer), font=font_setup)
timer += 1
apple.forward(10)
counter.getscreen().ontimer(countdown, counter_interval)
countdown()
# lets the player move on key press
wn = trtl.Screen()
wn.listen()
wn.onkeypress(move_player, "w")
wn.onkeypress(player_left,"a")
wn.onkeypress(player_right,"d")
wn.mainloop()
Testing for collision can be as simple as adding a clause to the if statement in the countdown() function:
elif apple.distance(player) < 15:
counter.write("Collision!", font=FONT)
Below is my rework of your code that incorporates this change as well addresses various style and coding issues I noticed:
from turtle import Screen, Turtle
from random import choice, randint
# lists for the turtle to use
COLORS = ['blue', 'dark orange', 'cyan', 'green', 'black']
SHAPES = ['triangle', 'turtle', 'classic']
SIZES = [0.5, 1, 1.25, 1.5, 2]
FONT = ('Arial', 20, 'normal')
COUNTER_INTERVAL = 1000
# give player movement
def move_player():
player.forward(10)
def player_left():
player.left(180)
def player_right():
player.right(180)
# set up timer
timer = 0
def countdown():
global timer
counter.clear()
if timer <= -1:
counter.write("Time's Up", font=FONT)
elif apple.distance(player) < 15:
counter.write("Collision!", font=FONT)
else:
counter.write("Timer: " + str(timer), font=FONT)
timer += 1
apple.forward(10)
screen.ontimer(countdown, COUNTER_INTERVAL)
# create turtles
player = Turtle(shape=choice(SHAPES))
player.turtlesize(1.5)
player.color(choice(COLORS))
player.penup()
counter = Turtle()
counter.hideturtle()
counter.penup()
counter.goto(-200, 160)
apple = Turtle(shape='circle')
apple.color('red')
apple.turtlesize(choice(SIZES))
apple.penup()
apple.setposition(randint(0, 200), randint(0, 200))
apple.setheading(270)
screen = Screen()
# let player move on key press
screen.onkeypress(move_player, 'w')
screen.onkeypress(player_left, 'a')
screen.onkeypress(player_right, 'd')
screen.listen()
countdown()
screen.mainloop()
What I wanted it to do is that when the turtles get too close, the
player turtle disappears. If the apple gets to low below the player,
it resets back up top
My fix above just detects and announces the collision, you'll need to augment it to include these additional actions.

Python Pong Game not increasing point after 1

So I've tried to make a Pong game in Python with turtle, everything is working except one thing. When the player_score reach 1 point its not increasing anymore. And an annoying thing, does anybody know why is the ball slow down when i move the racket?
Here is my code:
I think this part of the code is okay.
from turtle import *
# Creating screen
court = Screen()
court.title("Bricket Pong v 0.2")
court.setup(width=800, height=600)
court.bgcolor('black')
court.tracer(0)
# Creating ball
ball = Turtle()
ball.shape("circle")
ball.color("green")
ball.penup()
ball.setpos(0, 0)
# Creating ball movement speed
def init():
global ball, want_continue
ball.step_x = 0.5
ball.step_y = 0.5
ball.setpos(0, 0)
want_continue = True
def on_quit():
global want_continue
want_continue = False
court.onkey(on_quit, "q")
court.listen()
# Creating point screen
point = Turtle()
point.speed(0)
point.color('blue')
point.penup()
point.hideturtle()
point.goto(0,260)
point.write("Player: 0 ",align="center",font=('Monaco',24,"normal"))
racket = Turtle()
racket.hideturtle()
racket.shape("square")
racket.color("white")
racket.penup()
racket.goto(0, -285)
racket.shapesize(1, 3)
racket.showturtle()
# Creating arrows to move the racket
def racket_left():
x =racket.xcor()
x = x - 15
racket.setx(x)
def racket_right():
x = racket.xcor()
x = x + 15
racket.setx(x)
court.listen()
court.onkeypress(racket_left, "Left")
court.onkeypress(racket_right, "Right")
The problem is must be here in move_ball def.
# Creating borders to the ball
def move_ball():
global ball
player_score = 0
if ball.xcor() > 390 or ball.xcor() < -390:
ball.step_x *= -1
if ball.ycor() > 290:
ball.step_y *= -1
if ball.ycor() < -290:
ball.setpos(0, 0)
ball.step_y *= -1
player_score= 0
point.clear()
point.write("Player: {} ".format(player_score), align="center", font=('Monaco', 24, "normal"))
ball.setx(ball.xcor() + ball.step_x)
ball.sety(ball.ycor() + ball.step_y)
#Racket ball border
if (ball.ycor() < - 265) and ball.ycor() > - 275 \
and (racket.xcor() + 30 > ball.xcor() > racket.xcor() - 30) :
ball.step_y = ball.step_y * -1
player_score += 1
point.clear()
point.write("Player: {}".format(player_score),align="center",font=('Monaco',24,"normal"))
def run():
global ball, want_continue
while want_continue:
move_ball()
court.update()
#
init()
run()
court.bye()
To solve your problem you can define e.g score_a = 0 and score_b = 0 before wherever the collision function is and then update the score every time ball collides.
And you can display the score with formatted strings as print(f"Player A: {score_a} Player B: {score_b}") or print("Player A: {} Player B: {}".format)
The problem of ball slowing down on collision is due to the way of code. The movement of the ball is done by adding some int value to ball's current (x,y) coords. This value is added every time the screen updates. The loop updates the position as many times as it can in one frame. But due to varying CPU speed every second the ball seems to move inconsistently.
To get a more consistent behavior you could add a long enough pause into your loop to offset the variability of the computing times and to avoid drawing useless frames. You can do this using time
import time
# Initialization
...
while True:
time.sleep(1 / 60)
# The rest of your game logic
...
You're continually setting the player_score to zero, at the top of the move_ball() function (so, every cycle through the while loop in run()) -- you'll have to initialize it somewhere else and increment it without resetting it.

Update the score when two turtles collide

I'm doing an assignment where I have to write a small game. When a turtle collides with a dot (bug) on the screen, it will add one point to the score value in the top left and teleport the bug to another random spot. I'm having trouble getting the score to update when they collide.
I tried to put the score update within the game loop but that did not work as it kept telling me that the value is not defined. I tried solving that with a global value, but that didn't do anything:
import turtle
import math
import random
#Set up the constants for the game.
WINDOW_HEIGHT = 300
WINDOW_WIDTH = 300
FORWARD_STEP = 10 #how much does the turtle move forward
TURN_STEP = 30 #how much does the turtle turn (in degrees)
SHRINK_FACTOR = 0.95 #how much does the turtle shrink when it moves
DEATH_WIDTH = 0.05 #the size at which you stop the game because the user lost
COLLISION_THRESHOLD = 10;#we say that two turtles collided if they are this much away
#from each other
#Define functions
def game_setup():
'''set up the window for the game, a bug and the player turtle '''
#create the screen
wn = turtle.Screen()
wn.screensize(WINDOW_HEIGHT,WINDOW_WIDTH)
wn.bgcolor("light green")
#Create player turtle
player = turtle.Turtle()
player.color("blue")
player.shape("turtle")
player.penup()
player.setpos (random.randrange(1,301), random.randrange(1,301))
#create a bug
bug1 = turtle.Turtle()
bug1.color("black")
bug1.shape("circle")
bug1.shapesize(stretch_wid=0.2, stretch_len=0.2)
bug1.penup()
bug1.speed(0) #the bug is not moving
bug1.setposition(-200, 200)
#create score turtle
score_keeper = turtle.Turtle()
score_keeper.hideturtle()
score_keeper.penup()
score_keeper.setposition (-400,360)
score = 0
scorestring = "Score: %s" %score
score_keeper.write(scorestring, False, align="left", font=("Arial",14, "normal"))
return (wn,player,bug1,score_keeper)
def is_collision (player, bug1):
distance = (math.sqrt((player.xcor()-bug1.xcor())**2 + (player.ycor() - bug1.ycor())**2))
if distance < COLLISION_THRESHOLD:
return True
else:
return False
def main():
#set up the window, player turtle and the bug
(wn,player,bug1,score_keeper) = game_setup()
#make the arrow keys move the player turtle
bindKeyboard(player)
#Set this veriableto True inside the loop below if you want the game to end.
game_over = False
player_width = get_width(player)
#This is the main game loop - while the game is not over and the turtle is large enough print the width of the turtle
#on the screen.
while not game_over and player_width > DEATH_WIDTH:
#your collision detection should go here
if is_collision (player, bug1):
bug1.setpos (random.randrange(1,301), random.randrange(1,301))
player.shapesize(stretch_wid=1, stretch_len=1)
player_width = get_width(player)
player.showturtle()
print(player_width)
print("Done")
wn.exitonclick()
main()
This is most of the code. All I want it to do is when the is_collision() function happens, it adds 1 to the value of score and the score_keeper turtle then prints that value in the window.
Im havign trouble getting the score to update when they collide.
I've done a stripped down rework of your code below (substuting for methods you left out) to show how to update the score on the screen. Code like this shouldn't have an explicit main loop as it's all event driven and should instead call turtle's main event loop:
from turtle import Screen, Turtle
from random import randrange
from functools import partial
# Set up the constants for the game.
WINDOW_WIDTH, WINDOW_HEIGHT = 500, 500
FORWARD_STEP = 10 # how much does the turtle move forward
TURN_STEP = 30 # how much does the turtle turn (in degrees)
COLLISION_THRESHOLD = 10 # we say that two turtles collided if they are this much away from each other
CURSOR_SIZE = 20
FONT = ('Arial', 14, 'normal')
# Define functions
def is_collision(player, bug):
return player.distance(bug) < COLLISION_THRESHOLD
def random_position(turtle):
scale, _, _ = turtle.shapesize()
radius = CURSOR_SIZE * scale
return randrange(radius - WINDOW_WIDTH/2, WINDOW_WIDTH/2 - radius), randrange(radius - WINDOW_HEIGHT/2, WINDOW_HEIGHT/2 - radius)
def forward():
global score
player.forward(FORWARD_STEP)
if is_collision(player, bug):
bug.setposition(random_position(bug))
score += 1
score_keeper.clear()
score_keeper.write(f"Score: {score}", font=FONT)
# Set up the window for the game, a bug and the player turtle.
# Create the screen
screen = Screen()
screen.setup(WINDOW_WIDTH, WINDOW_HEIGHT)
screen.bgcolor('light green')
# Create score turtle
score_keeper = Turtle(visible=False)
score_keeper.penup()
score_keeper.setposition(-230, 230)
score = 0
score_keeper.write(f"Score: {score}", font=FONT)
# Create a bug
bug = Turtle('circle', visible=False)
bug.shapesize(4 / CURSOR_SIZE)
bug.penup()
bug.setposition(random_position(bug))
bug.showturtle()
# Create player turtle
player = Turtle('turtle', visible=False)
player.color('blue')
player.speed('fastest')
player.penup()
player.setposition(random_position(player))
player.showturtle()
# make the arrow keys move the player turtle
screen.onkey(partial(player.left, TURN_STEP), 'Left')
screen.onkey(partial(player.right, TURN_STEP), 'Right')
screen.onkey(forward, 'Up')
screen.listen()
screen.mainloop()

Python - Turtle coordinates not working properly in snake style game

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)
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.

Categories

Resources