Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I’m not sure how to get objects to fly out another object every time it is pressed. It’s just like Cookie Clicker. This is Pygame code(turtle).
This is all my code so far:

import turtle

wn = turtle.Screen()
wn.title("Cake Clicker by @.......")
wn.bgpic("backg for app.gif")

wn.register_shape("cake2.gif")

cake2 = turtle.Turtle()
cake2.shape("cake2.gif")
cake2.speed(0)

clicks = 0

pen = turtle.Turtle()
pen.hideturtle()
pen.color("black")
pen.penup()
pen.goto(0, 400)
pen.write(f"Clicks: {clicks}", align="center", font=("Courier New", 32, "normal"))


def clicked(x, y):
global clicks
clicks += 1
pen.clear()
pen.write(f"Clicks: {clicks}", align="center", font=("Courier New", 32, "normal"))


cake2.onclick(clicked)

wn.mainloop()

If you could respond to me with some help or the correct code, that would be great.
All I need is to have a small cake picture jump out of the larger cake in the center when it is clicked (pressed).

What I have tried:

I have tried researching on YouTube and on Google and I have found no answer.
Posted
Updated 7-Jan-22 5:56am

1 solution

Use the turtle.clone[^] function to create the secondary image. You can then use the various positioning functions to make it move. There is some useful sample code at The Beginner's Guide to Python Turtle – Real Python[^].

This is not too difficult to do. First you need a second image to fly out of the first. I used a square shape as the primary and an oval as the flyer.
Python
def flyout(x, y):
    oval = cake2.clone()
    oval.shape("egg.gif")
    oval.penup()
    oval.setpos(x, y)
    oval.left(80) # point it upwards
    for x in range(30):
        oval.forward(10) # move the oval
        oval.right(5)    # adjust the angle to make it move in an arc

In the clicked function I call flyout(0, 20) to set the start position of the oval to just above the square. It then traverses an arc from that position ...

The above code is just a rough sample, you should modify it as necessary for your own requirements.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900