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.
Related
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.
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()
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:
i am currently trying to make a game like snake in python using turtle graphics and have encountered a game breaking bug when you turn the turtle using a and d keys if you are in line with any previous turn. it appears to be executing code out of order but i have no real idea whats happening.
the whole code is below
import turtle
import random
import math
x = 400
y = 400
global speed
global points
points = 0
speed = 2
posList = ['']
# turns your character left
def left():
global speed
global posList
key = True
char.fd(speed)
char.left(90)
char.fd(speed)
# turns your character left
def right():
global speed
global posList
key = True
char.fd(speed)
char.right(90)
char.fd(speed)
# adds one box to the point counter
def point():
global points
points += 1
wall.speed(0)
wall.pendown()
wall.forward(50)
wall.seth(90)
wall.forward(10)
wall.seth(180)
wall.forward(50)
wall.seth(270)
wall.forward(10)
wall.penup()
wall.seth(90)
wall.forward(12)
wall.seth(0)
dot.setx(random.randint(-200,200))
dot.sety(random.randint(-200,200))
print(points)
# checks if curren posisition is anywhere you have ever been
def checkBracktrack(pos, poslist):
found = False
for thing in posList:
if thing == pos:
found=True
return found
# creates the box that the game occurs in
turtle.colormode(255)
screen = turtle.Screen()
dot = turtle.Turtle()
dot.penup()
dot.speed(0)
dot.shape('turtle')
dot.setx(random.randint(-200,200))
dot.sety(random.randint(-200,200))
wall = turtle.Turtle()
wall.speed(0)
wall.penup()
wall.goto(x/2,y/2)
wall.pendown()
wall.seth(180)
wall.forward(400)
wall.seth(270)
wall.forward(400)
wall.seth(0)
wall.forward(400)
wall.seth(90)
wall.forward(400)
wall.seth(270)
wall.forward(400)
wall.seth(0)
wall.penup()
wall.forward(100)
char = turtle.Turtle()
x = 0
y = 0
# updates the position of the player turtle
while True:
screen.onkey(left,"a")
screen.onkey(right,"d")
char.hideturtle()
char.forward(speed)
char.speed(0)
turtle.listen(xdummy=None, ydummy=None)
print(char.pos())
print(posList[(len(posList)-1)])
# checks if current position is the same as any position it has ever been in !this is the bit that is having problems!
if checkBracktrack(char.pos(),posList):
speed = 0
break
# checks if it is close enough to a point marker to
if char.ycor() in range(dot.ycor()-10,dot.ycor()+10) and char.xcor() in range(dot.xcor()-10,dot.xcor()+10):
point()
# checks if in the box
if char.ycor() not in range(-200,200) or char.xcor() not in range(-200,200):
speed = 0
# adds current location to the list
posList.append(char.pos())
char.fd(speed)
print('you travelled',len(posList),'pixels')
print('collided with yourself')
print(char.pos())
print(posList)
name = input('quit')
screen.mainloop()
There are many little issues with your code: you need to reread about when to use global; your checkBracktrack() function takes poslist as an argument but operates on the global posList instead (case typo); your pixels travelled distance calculation is incorrect because of extra fd() calls and a speed greater than 1; your proximity test can be greatly simplified using turtle's .distance() method; your code to display points on the game board doesn't work at all; you call onkey() over and over again in a loop when you only need to call it once for each key; your checkBracktrack() function has an unnecessary loop.
The biggest issue I have with the code is the while True: which shouldn't happen in event-based code. I've rewritten, and simplified your code below, addressing the above issues as well as others:
from turtle import Turtle, Screen
from random import randint
FONT = ('Arial', 24, 'normal')
WIDTH, HEIGHT = 400, 400
SPEED = 1
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(points, font=FONT)
dot.setpos(randint(-WIDTH/2, WIDTH/2), randint(-HEIGHT/2, HEIGHT/2))
def checkBracktrack(pos, poslist):
""" checks if current posiition is anywhere you have ever been """
return pos in poslist
def move_char():
""" updates the position of the player turtle """
over = 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
# adds current location to the list
posList.append(char.pos())
# checks if it is close enough to a point marker
if char.distance(dot) < 20:
point()
screen.ontimer(move_char, 10)
points = 0
posList = []
# 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)
wall.speed('fastest')
wall.penup()
wall.goto(WIDTH/2, HEIGHT/2)
wall.pendown()
for _ in range(4):
wall.right(90)
wall.forward(400)
wall.penup()
wall.forward(100)
wall.write("0", font=FONT)
char = Turtle(visible=False)
char.speed('fastest')
move_char()
screen.mainloop()
My belief is that the problem that prompted your original question got fixed in the process of reworking the code.
So I'm playing around with a Pygame platformer. Right now I have a player character, a bumper, and background. The player can run around the floor of the level or the bumper and the screen can screen when the character runs against the edge.
What I'd like though is a way to have multiple bumpers without having to create each individually. Here is some relevant code.
This is creating the bumper before the game's main loop...
bumper = pygame.image.load("bumper.png") #load bumper pic
bumperbounds = bumper.get_clip() #create boundaries for bumper
bumperbounds = bumperbounds.move (200, 250) #move the bumper to starting location
This is the code that makes the bumper stop the player from falling through. It just sets the landed value to 1...
if playerbounds.left <= bumperbounds.right and playerbounds.right >= bumperbounds.left and playerbounds.bottom <= bumperbounds.top + 30 and playerbounds.bottom >= bumperbounds.top and gravity >=0: #if player lands on top of the bumper
landed = 1 #set player to landed
This is some code so that the player lands on the platform correctly. Basically it slows the player down at the last loop iteration from falling through the bumper so its exact falling speed lands the bottom of the player on the top of the bumper.
if playerbounds.left <= bumperbounds.right and playerbounds.right >= bumperbounds.left and playerbounds.bottom <= bumperbounds.top and gravity > bumperbounds.top-playerbounds.bottom: #to land exactly on the top of the bumper
gravity = bumperbounds.top-playerbounds.bottom
This is to move the bumper when the screen is scrolling. This is just the right direction, the left one works basically the same.
if xacc > 0:#if player is moving right
if playerx >= width * .8 and screenx < levelwidth - playerbounds.width:#if the screen is scrolling
screenx = screenx + xacc
bumperbounds = bumperbounds.move(-xacc, 0) #bumper moves on the screen
skybounds = skybounds.move(-xacc / 10, 0)
groundbounds = groundbounds.move(-xacc * 2, 0)
else:
playerbounds = playerbounds.move(xacc, 0)
playerx += xacc
xacc -= 1
This just shows the bump on the screen at the end of the loop.
screen.blit(bumper,bumperbounds) #displays the bumper on the screen
Any help can be useful?!
You need create a class for the object you are creating.
For example:
class Leaf:
def __init__(self):
self.leafimage = pygame.image.load('fallingleaf.jpg').convert()
self.leafrect = self.leafimage.get_rect()
xpos = random.randint(0, 640)
self.leafrect.midtop = (xpos, 0)
def move(self):
self.leafrect = self.leafrect.move([0, 1])
To create objects at the same time,
leaves = []
for i in range(5):
leaves.append(Leaf())
With changing xpos value:
class Leaf:
def __init__(self,xpos):
self.leafimage = pygame.image.load('fallingleaf.jpg').convert()
self.leafrect = self.leafimage.get_rect()
self.leafrect.midtop = (xpos, 0)
def move(self):
self.leafrect = self.leafrect.move([0, 1])
To create objects at the same time,
leaves = []
xpos= [20 30 40 50 60]
for i in range(5):
leaves.append(Leaf(xpos[i]))