How do you make turtle move without using turtle.goto(x,y) but turtle.speed(speed) and turtle.heading(angle)? I need this for a game I am making. Where the mouse is, I want to make it go in that direction. But when I change it, it goes to that place then to my mouse:
import turtle
screen = turtle.Screen()
screen.title("Test")
screen.bgcolor("white")
screen.setup(width=600, height=600)
ship = turtle.Turtle()
ship.speed(1)
ship.shape("triangle")
ship.penup()
ship.goto(0,0)
ship.direction = "stop"
ship.turtlesize(3)
turtle.hideturtle()
def onmove(self, fun, add=None):
if fun is None:
self.cv.unbind('<Motion>')
else:
def eventfun(event):
fun(self.cv.canvasx(event.x) / self.xscale, -self.cv.canvasy(event.y) / self.yscale)
self.cv.bind('<Motion>', eventfun, add)
def goto_handler(x, y):
onmove(turtle.Screen(), None)
ship.setheading(ship.towards(x, y)) #this is where you use the x,y cordinates and I have seat them to got to x,y and set heading
ship.goto(x,y)
onmove(turtle.Screen(), goto_handler)
onmove(screen, goto_handler)
If you only setheading and speed it just turns that way and does not move. If you try this code it works -- it is just that I use ship.goto(x, y) which makes it go to (x, y). But when you change your mouse when it is moving, it first goes to (x, y) then to your new mouse position. I pretty much just want it to just follow the mouse but I can not do that.
I believe the code below gives you the motion you desire. It only uses onmove() to stash the target's position and uses an ontimer() to aim and move the turtle. It also stops when the target has been enveloped:
from turtle import Screen, Turtle, Vec2D
def onmove(self, fun, add=None):
if fun is None:
self.cv.unbind('<Motion>')
else:
def eventfun(event):
fun(Vec2D(self.cv.canvasx(event.x) / self.xscale, -self.cv.canvasy(event.y) / self.yscale))
self.cv.bind('<Motion>', eventfun, add)
def goto_handler(position):
global target
onmove(screen, None)
target = position
onmove(screen, goto_handler)
def move():
if ship.distance(target) > 5:
ship.setheading(ship.towards(target))
ship.forward(5)
screen.ontimer(move, 50)
screen = Screen()
screen.title("Test")
screen.setup(width=600, height=600)
ship = Turtle("triangle")
ship.turtlesize(3)
ship.speed('fast')
ship.penup()
target = (0, 0)
onmove(screen, goto_handler)
move()
screen.mainloop()
Related
I'm creating a simple game with Python using turtle package.
My aim is to have some balloons on the screen running from right to left and then when one of them is clicked to make it disappear.
What I have going wrong is that when I click one balloon, all of them are disappeared!
Here is my code
1- Main
from turtle import Screen
from balloon import Balloon
import time
screen = Screen()
screen.title('Balloons Nightmare')
screen.setup(width=600, height=600)
screen.bgpic(picname='sky-clouds.gif')
screen.tracer(0)
balloons_manager = Balloon()
current_x = 0
current_y = 0
screen.listen()
screen.onclick(fun=balloons_manager.explode_balloon, btn=1)
game_is_on = True
while game_is_on:
time.sleep(0.1)
screen.update()
balloons_manager.create_balloon()
balloons_manager.move_balloon()
screen.exitonclick()
2- balloon module
import random
from turtle import Turtle
COLORS = ["red", "yellow", "green", "blue", "black"]
MOVEMENT_SPEED = 2
class Balloon:
def __init__(self):
self.all_balloons = []
self.balloon_speed = MOVEMENT_SPEED
self.x = 0
self.y = 0
self.hidden = None
def create_balloon(self):
random_choice = random.randint(1, 9)
if random_choice == 1:
new_balloon = Turtle("circle")
new_balloon.penup()
new_balloon.turtlesize(stretch_wid=2, stretch_len=0.75)
new_balloon.color(random.choice(COLORS))
random_y_cor = random.randint(-50, 280)
new_balloon.goto(320, random_y_cor)
self.hidden = new_balloon.isvisible()
self.all_balloons.append(new_balloon)
def move_balloon(self):
for balloon in self.all_balloons:
balloon.backward(self.balloon_speed)
def explode_balloon(self, x, y):
for balloon in range(len(self.all_balloons)):
print(self.all_balloons[balloon].position())
self.all_balloons[balloon].hideturtle()
# for balloon in self.all_balloons:
# balloon.hideturtle()
I tried so many changes but nothing helped me, example of what I tried so far
getting the current x,y coordinates of the balloon so that on click to hide only the one with these coordinates but didn't work for me or I did something wrong with it
Any hints will be appreciated.
Thank you
I fixed the issue by adding an inner function in create_balloon function
def create_balloon(self):
random_choice = random.randint(1, 9)
if random_choice == 1:
new_balloon = Turtle("circle")
new_balloon.penup()
new_balloon.turtlesize(stretch_wid=2, stretch_len=0.75)
new_balloon.color(random.choice(COLORS))
random_y_cor = random.randint(-50, 280)
new_balloon.goto(320, random_y_cor)
def hide_the_balloon(x, y):
return new_balloon.hideturtle()
new_balloon.onclick(hide_the_balloon)
self.all_balloons.append(new_balloon)
Here's your code triggered by the click handler:
def explode_balloon(self, x, y):
for balloon in range(len(self.all_balloons)):
print(self.all_balloons[balloon].position())
self.all_balloons[balloon].hideturtle()
This loops over all balloons and hides them unconditionally.
You probably want to use an if in there to only conditionally trigger the hiding behavior. Compare the x and y coordinates of the click against the current balloon in the loop. Only hide the balloon if the distance is less than a certain amount (say, the radius of the balloon).
Another approach is to use turtle.onclick to add a handler function that will be trigged when the turtle is clicked.
Related:
How to see if a mouse click is on a turtle in python
Python find closest turtle via mouse click
Why is my Python Turtle program slowing down drastically the longer it runs? (since you're never removing turtles and constantly adding new ones, this is a good thread to take a look at)
Python screen displays the coordinates after every click, but it won't break the loop when I reach a specific x,y coordinate. It's important to my game and has to be precise. This is what I've put together so far:
import turtle
from turtle import *
from turtle import Turtle, Screen
import random
import time
screen = Screen()
screen.bgcolor("green")
tur = turtle
screen.setup(width=800, height=600)
x = xcor()
y = ycor()
pen = Turtle()
tur = Turtle()
tur = Turtle(shape="turtle")
tur.color("black")
tur.speed("fastest")
tur.color("black")
tur.goto(x=16, y=-132)
while True:
def get_mouse_click_coor(x, y):
print(x, y)
tur.clear()
tur.write((x, y))
if get_mouse_click_coor() == (16, -132):
print("win")
turtle.onscreenclick(get_mouse_click_coor)
turtle.mainloop()
You should make the while loop stop.
One way to do this, is by defining a global flag. Put the flag in the condition of the while. When the desired coordinates are met, set the flag to False.
I also recommend to define the get_mouse_click_coor function out of the while loop, because at each loop, this function gets defined, however using as it is, does not make any problem.
play = True
def get_mouse_click_coor(x, y):
print(x, y)
tur.clear()
tur.write((x, y))
if get_mouse_click_coor() == (16, -132):
print("win")
play = False
while play:
turtle.onscreenclick(get_mouse_click_coor)
turtle.mainloop()
I've been having trouble with getting my bullet (named asteroid) and my zombie. more specifically, I'm having trouble getting my game to register collision between these two turtles, it gets even weirder when you reverse the lesser than symbol into a greater than symbol. I do not know what is up with my code, any help is appreciated.(I have included the entirety of my code, since I am unsure of the source of the problem, I just know which part isn't working, I would recommend starting there.)
#the bullet that doesn't hit it's target
#Turtle Graphics game
import turtle
import random
import time
#set up screen
wn = turtle.Screen()
wn.bgcolor("grey")
finish= False
def randor1():
rand=random.randint(-280,280)
def randor2():
rand=random.randint(50,280)
def check_target_pos():
#side boundary checking
if zombie.xcor() > 280 or zombie.xcor() <- 280:
zombie.right(180)
#top/bottom boundary checking
if zombie.ycor() > 280 or zombie.ycor() <- 280:
zombie.right(180)
def check_turtle_pos():
#side boundary checking
if asteroid.xcor() > 280 or asteroid.xcor() <- 280:
asteroid.right(180)
#top/bottom boundary checking
if asteroid.ycor() > 280 or asteroid.ycor() <- 280:
asteroid.right(180)
def new_asteroid():# the turtle bullet,will change the name later on
for i in range(50):
asteroid.forward(10)
asteroid.goto(0,0)
def k2():#turn turtle left
asteroid.left(45)
def k3():#turn turtle right
asteroid.right(45)
#Draw border for arena
mypen = turtle.Turtle()
mypen.penup()
mypen.setposition(-300,-300)
mypen.pendown()
mypen.pensize(3)
for side in range(4):
mypen.forward(600)
mypen.left(90)
mypen.hideturtle()
#create turtle turtle, again will change name later
asteroid = turtle.Turtle()
asteroid.color("green")
asteroid.shape("turtle")
asteroid.penup()
asteroid.speed(0)
#create turtle zombie
def zombies():
global zombie
zombie= turtle.Turtle()
zombie.hideturtle()
zombie.color("green")
zombie.shape("circle")
zombie.penup()
zombie.speed(0)
x= random.randint(-280,280)
y= random.randint(50,280)
zombie.goto(x,y)
zombie.showturtle()
zombies()
while (finish!= True):
check_target_pos()
check_turtle_pos()
zombie.forward(1)
def end():
finish==True
wn.bye()
if asteroid.distance(zombie)<40: #problem area
end()
wn.onkey(new_asteroid, "space")#shoot button.
wn.onkey(k2, "Left")#turn left button
wn.onkey(k3, "Right") #turn right button
wn.onkey(end, "e")#exit
wn.listen()#so all the on key functions above work
Your code is somewhat a mess and doesn't facilitate the event-driven nature of turtle. There is no place for while True: loops nor time.sleep() or such in this environment. You also seem to be confusing your gun with your bullet (you're flinging your gun into space to kill the zombie, not its bullet!)
I've rewritten your code below using an event-based model with ontimer(). This is a single bullet implementation (you can't fire again until your bullet hits something or disappears into the distance):
from turtle import Screen, Turtle
from random import randint
def check_bullet_position():
if bullet.distance(turtle) > 240:
bullet.hideturtle()
if bullet.distance(zombie) < 20:
bullet.hideturtle()
reset_zombie()
def shoot_bullet():
if bullet.isvisible():
return
bullet.setheading(turtle.heading())
bullet.setposition(turtle.position())
bullet.forward(15)
bullet.showturtle()
def turn_left():
turtle.left(20)
def turn_right():
turtle.right(20)
def reset_zombie():
while zombie.distance(turtle) < 240:
x = randint(-280, 280)
y = randint(-280, 280)
zombie.goto(x, y)
zombie.setheading(zombie.towards(turtle))
def move():
zombie.forward(1)
if bullet.isvisible():
bullet.forward(2)
check_bullet_position()
screen.update()
if turtle.distance(zombie) > 20:
screen.ontimer(move)
# Set up screen
screen = Screen()
screen.tracer(False)
screen.bgcolor('grey')
# Draw border for arena
pen = Turtle()
pen.hideturtle()
pen.pensize(3)
pen.penup()
pen.setposition(-300, -300)
pen.pendown()
for _ in range(4):
pen.forward(600)
pen.left(90)
# Create turtle turtle
turtle = Turtle()
turtle.shape('turtle')
turtle.color('green')
turtle.penup()
# Create turtle bullet
bullet = Turtle()
bullet.hideturtle()
bullet.shape('circle')
bullet.shapesize(0.5)
bullet.color('yellow')
bullet.penup()
# Create turtle zombie
zombie = Turtle()
zombie.shape('circle')
zombie.color('red')
zombie.penup()
reset_zombie()
screen.onkey(shoot_bullet, 'space')
screen.onkey(turn_left, 'Left')
screen.onkey(turn_right, 'Right')
screen.onkey(screen.bye, 'e') # exit
screen.listen() # enable onkey() functions above
move()
screen.mainloop()
This should give you a starting point for building the game you envision. It is possible to write a multiple bullet implementation, it just take a little more thought and design work.
Your searching for a collision outside of the loop where you shot the projectile.
The easiest way to make your code work is to add the collision detection to the new_asteroid() function. But I don't think that's the right way.
Your technique is what is referred to as blocking. while you're projectile is moving, no other code is running. hence your collision detection is not working. change Line 38 to this.
def new_asteroid():# the turtle bullet,will change the name later on
for i in range(50):
asteroid.forward(10)
if asteroid.distance(zombie) < 40: # problem area
end()
asteroid.goto(0,0)
To create a non-blocking version of this function you would have to divide the for loop up to increments called by your main loop. so that your projectile moves once per iteration, instead of all at once.
Your program also crashes on exit. May I recommend re organizing your code into functions, then execution. it makes it much more readable. Happy gaming.
My Python turtle game obstacle freezes when the player receives input and continues afterward:
# Written in Python 3
# By
# February 4th, 2019
# Purpose: Mimic the no-wifi Google Chrome dinosaur game
# Bonus 6
import turtle # Used for graphics
from turtle import Screen # Used for inputs
import random# Used to generate a random number
import time
# Creates a turtle with the proper size and color
player = turtle.Turtle()
player.shape("square")
player.color("#cc0000")
player.turtlesize(1.5, 1.5)
player.penup()
player.goto(-50, 0)
# Creates the ground
ground = turtle.Turtle()
ground.shape("square")
ground.turtlesize(0.25, 300)
ground.penup()
ground.goto(0, -18)
# This function makes the square jump, unbinds 'Up', return to the ground, then rebinds 'Up'
def jump():
Screen().onkey(null, 'Up')
player.speed(2)
if player.ycor() == 0:
player.goto((player.xcor()), (player.ycor()+100))
print("G")
player.speed(1.5)
player.goto(-50, 0)
Screen().onkey(jump, 'Up')
# Blank function
def null():
n =1
Screen().onkey(jump, 'Up')
Screen().listen()
# Ignore this
x = 3 * random.sample(range(4), 4)
print (x)
print (x[1])
# Creating obstacles (not finished, just moves)
obst1 = turtle.Turtle()
obst1.shape("square")
obst1.turtlesize(3, 2)
obst1.penup()
obst1.goto(300,0)
obst1.speed(1)
obst1.setx(-300)
I want the obstacle to continue moving while I jump. I only have Python 3 and its standard modules. I cannot download PIP or anything else like it, for some reason. I'm trying to mimic the dinosaur game from Google Chrome. I'm new to this sort of thing so please explain any suggestions in as much detail as possible. Thanks a lot!
The way you wrote your code, you can only move one turtle at a time. Below is a rework of your code where the user controls the player, but the obstacle is controlled by a timer so they can move at the same time:
from turtle import Screen, Turtle
# This function unbinds 'Up', makes the square jump, return to the ground, then rebinds 'Up'
def jump():
screen.onkey(None, 'Up')
if player.ycor() == 0:
player.forward(150)
player.backward(150)
screen.onkey(jump, 'Up')
def move():
obstacle.forward(6)
if obstacle.xcor() > -400:
screen.ontimer(move, 100)
# Creates the ground
ground = Turtle('square')
ground.turtlesize(0.25, 45)
ground.penup()
ground.sety(-15)
# Creates a turtle with the proper size and color
player = Turtle('square')
player.color('red')
player.turtlesize(1.5)
player.speed('slowest')
player.penup()
player.setx(-100)
player.setheading(90)
screen = Screen()
screen.onkey(jump, 'Up')
screen.listen()
# Creating obstacles (not finished, just moves)
obstacle = Turtle('square')
obstacle.turtlesize(3, 1.5)
obstacle.speed('fastest')
obstacle.penup()
obstacle.setposition(400, 15)
obstacle.setheading(180)
move()
screen.mainloop()
This should more closely simulate the type of motion you're trying to achieve.
In my code I've written a definition to change the background of the turtle after an action is completed. The definition to me looks ok and I don't see any issues with it but when the action is completed and the background image is suppose to change, the turtle window becomes unresponsive. Here is the definition that I'm talking about:
if default.distance(pickaxe) < 10:
screen.clearscreen()
wn.bgpic('TrumpTowersInside.gif')
And if the rest of the code is needed for whatever reason, here is the rest of the code for my turtle-based game:
from turtle import Screen, Turtle
def get_mouse_click_coor(x, y):
print(x, y)
def drag(x, y):
default.ondrag(None) # disable handler inside handler
default.goto(x, y)
if default.distance(scar) < 40:
default.shape('defaultscar.gif')
scar.hideturtle()
mini.hideturtle()
pickaxe.showturtle()
if default.distance(mini) < 40:
banshee.goto(-200,200)
banshee.showturtle()
banshee.speed(0)
for x in range(200):
banshee.forward(1)
banshee.right(90)
banshee.forward(1)
banshee.left(90)
banshee.shape('banshee.gif')
banshee.left(90)
banshee.forward(50)
scar.hideturtle()
mini.hideturtle()
banshee.shape('bansheescar.gif')
default.shape('defaultdead.gif')
if default.distance(pickaxe) < 10:
screen.clearscreen()
wn.bgpic('TrumpTowersInside.gif')
default.ondrag(drag)
wn = Screen()
wn.setup(500, 500)
wn.bgpic('TrumpTowers.gif')
wn.register_shape('default.gif')
wn.register_shape('scar.gif')
wn.register_shape('defaultscar.gif')
wn.register_shape('mini.gif')
wn.register_shape('defaultgliding.gif')
wn.register_shape('banshee.gif')
wn.register_shape('bansheescar.gif')
wn.register_shape('defaultdead.gif')
wn.register_shape('pickaxe.gif')
scar = Turtle('scar.gif', visible=False)
scar.speed(-1)
scar.color('pink')
scar.penup()
scar.left(90)
scar.forward(50)
scar.showturtle()
mini = Turtle('mini.gif', visible=False)
mini.speed(-1)
mini.color('pink')
mini.penup()
mini.forward(60)
mini.showturtle()
default = Turtle('default.gif', visible=False)
default.shapesize(2)
default.speed(1)
default.penup()
default.left(90)
default.backward(50)
default.showturtle()
default.ondrag(drag)
banshee = Turtle('defaultgliding.gif', visible=False)
banshee.shapesize(2)
banshee.speed(1)
banshee.penup()
# banshee.showturtle()
pickaxe = Turtle('pickaxe.gif', visible=False)
pickaxe.pu()
pickaxe.forward(10)
pickaxe.left(90)
pickaxe.forward(50)
wn.mainloop()
The documentation for clear() is clear on this:
Reset TurtleScreen to its initial state: white background,
no backgroundimage, no eventbindings and tracing on.
All of your event bindings (i.e. ondrag()) are undone by the clear() so you have redo them.
UPDATE
The screen's clear() method (aka clearscreen() fuction) is more severe than the documentation might lead one to believe. It seems to destroy all user created turtles and resets the default turtle to its initial state.
The screen's .reset() method isn't much better -- you get to keep your turtles but they lose all the attributes you set.
As an alternative to clearing or resetting the screen, I suggest you ask the turtles to clear() to clean up any drawing if the pen was down and then move them to new locations or home().
from turtle import Screen, Turtle
def drag(x, y):
default.ondrag(None) # disable handler inside handler
default.goto(x, y)
if default.distance(pickaxe) < 10:
wn.bgpic('TrumpTowersInside.gif')
pickaxe.hideturtle() # should move it elsewhere
default.ondrag(drag)
wn = Screen()
wn.setup(500, 500)
wn.bgpic('TrumpTowers.gif')
default = Turtle('turtle', visible=False)
default.color('red')
default.shapesize(2)
default.penup()
default.left(90)
default.backward(50)
default.showturtle()
default.ondrag(drag)
pickaxe = Turtle('turtle', visible=False)
pickaxe.color('green')
pickaxe.penup()
pickaxe.forward(10)
pickaxe.left(90)
pickaxe.forward(50)
pickaxe.showturtle()
wn.mainloop()