Python Game Programming: Simple Cookie Clicker
Вставка
- Опубліковано 8 лис 2024
- In this video I show you how to make a simple Cookie Clicker clone in just 22 lines of code!
Download the Code and Image Here: github.com/wyn...
📖 DOWNLOAD my FREE Intro to Python eBook here: drive.google.c...
📖 DOWNLOAD my FREE Intro to Java eBook here: drive.google.c...
NEED HELP?
🆘 If you need help, please copy and paste your code into a comment along with any error messages. I'll take a look when I'm able and get back to you.
LINKS
🗄️ GITHUB: github.com/wyn...
💬 Follow me on Twitter: / tokyoedtech
📝 Check out my Blog: christianthomp...
⬇️ Download Geany Editor: www.geany.org
LEARN MORE PYTHON
➡️Space Invaders: • Python Game Programmin...
➡️Snake Game: • Python Game Programmin...
➡️Pong: • Python Game Programmin...
➡️Space War: • Python Game Programmin...
➡️Intro to Python (for Java Coders): • Intro to Python for Ja...
➡️Space Arena - The Ultimate Python Turtle Graphics Game Tutorial: • Ultimate Python Turtle...
LEARN MORE JAVA
➡️Basic Java for Beginners: • Basic Java 1&2: Commen...
➡️Intro to AP Computer Science A: • AP Computer Science Un...
#Python #Game #Tutorial - Наука та технологія
Best coding teacher on UA-cam! I nailed this tutorial and was surprised I was able to keep up. He doesn't go too fast like everyone else. Perfect speed and explanations!
Thanks - I try! Keep on codin'!
You are so much better than the other tutors around, some I can't even listen to... I hope you make lots more content for beginners like me.
Thanks - I try. Keep on codin'!
Thanks I had to make a game for school but i added more stuff like grandma's and cursors thanks to you!
That's great!
you can by upgrades so you can get more cookies a click, and you can get an auto clicker where it gives you cookies automatically. It costs cookies to buy these upgrades btw
Cool - I didn't know that. It's a good exercise for you to try.
Wow, awesome man! Simply explained and it works. Thanks i wish you well and good luck in the new year
Thanks - glad you liked it. Keep on codin'!
Thank you so much for this helpful video! I'm looking for inspiration for a school project and this feels like something I can work with!
You're quite welcome. Good luck with your project and keep on codin'!
Got it to work on Windows 10. Thanks for the clear tutorial! It took me a little bit to figure out the pathing on windows for the cookie picture, and how to reposition the clicks text so it was within the window. Solution to the first problem is to make sure the slashes are / instead of \ that windows provides when looking at the location of the file in the properties. the solution to the second problem was the change pen.goto(0, 400) to pen.goto(0, 200). Simple, I know, but I'm new to python and I didn't know what I didn't know. Thanks again!
You're quite welcome - glad to hear you got it working! Keep on codin'!
Thanks this was really helpful for me. Do you know why changing 400 to 200 makes a difference? I couldn't figure it out
@@gigisme7 The default window size varies from Python version to Python version.
This was a great tutorial im very VERY new to python and this was super fun to see something visual.
Thanks - really glad I could help!
this was really helpful and I expanded and added a shop with upgrades!
Awesome - that is how you learn to code! Learn the basics then experiment with your own ideas. Keep on codin'!
Hello, I've been watching your videos and so far I managed to get the Pong and Snake games going, it felt awesome to see myself creating something so cool with your help.
Now I see this new function "register shape" and I was wondering if I could use it on those other games to improve some visuals.
Thank you very much, I really appreciate the time you take to make these tutorials and keep up with the comments section!
Glad I could help! Yep - definitely can use it on the other games - for example, you could make better looking paddles and ball for pong.
Thank u this was my first python game ever u explained it well and After trial and error i made it
You're welcome - glad to hear it. Keep on codin'!
This is very good code. You would be great at making gameboy games. Because they have almost no memory & you need to make a lot of shortcuts! They run a z80 microprocessor (its very similar) there is almost no practical use for learning z80... But you can still have loads of fun!
Thanks - I really miss the old days. Check out my 6502 assembly intro video; you might find it interesting.
This is the best video ever! Thank you so much for showing me how to make a simple game in python.
Thanks - much appreciated! Keep on codin'!
This question is a little off-topic, but how much Japanese do you know?
You've said that you live in Tokyo, and I find that you often use terms like "yosh/yoshi".
I'm pretty good in day to day conversation, but can't read or write all that well.
I thought mine wasn't working but I just had to make my window bigger haha
It's the little things that get you!
Hello, I saw your video recently and I liked it. I'm doing it for a school project in "replit" but I can't get the image to show, what can I do?
Sorry, I'm not sure about repl.it - I recommend coding on your computer. It's helpful to know how files and folders are organized.
The pen didn't work for me (using Windows 10) so I replaced all instances of the pen.write() with print(f"Clicks: {clicks}"). Other than that, amazing tutorial! You just earned a sub.
Try moving the pen down a bit - pen.write might be drawing it off the visible part of the screen.
Try pen.goto(0, 100) instead of 250.
AHH I LOVE YOUR CONTENT SO MUCH😭
Thanks! Keep on codin'!
181st comment , keep up the good work bro!!
Thanks - doing my best!
Nice video! :D
I was wondering if you have any videos or could do one on making a sprite in python or the basics of a customizing game?
That's an interesting one, but I'm not sure how to go about it...
@@TokyoEdTech Oh ok, that's fine, you have other very helpful tutorials already :)
Thanks lol , i’ve been trying to do this by myself for a bit and couldn’t get it and i was able to get the first parts done before watching tutorial but then i just wasn’t able to do the last bit
Glad I could help - keep on codin'!
Great
Which video editor do use
I don't edit my videos - I just record using OBS studio.
@@TokyoEdTech Ok Great
Can I use this in VS Code? I wanted to do this for a school project
Sure - no problem.
Finally... A worthy UA-cam tutorial...
Your day is gonna be lucky if you saw this ;)
Cheers!
@@TokyoEdTech Cheers!
no its not i dont use linux it dosent even work on windows
It should work just fine on Windows. You may have to include the full path for the image though.
@@TokyoEdTech ok i will try
This is great! Thanks man
Glad you liked it!
first time evr its worked! i popped my cookie here! thanks! ill never forget....sob sob...
Glad to hear it - keep on codin'!
Why doesnt the clicks show for me im using jupyter notebook
I'm not sure - I haven't tried this on Jupyter notebook.
Hello, I did everything and almost everything works, but I don't see the clicks?
I'll need to see the code to help.
This looks easy.
But I have to try it
That's the idea!
haha its been 2 years and you still replying to the comments! W rokyo
Thanks - I try to keep an eye on things when I can...keep on codin'!
Thank you so so much for this, very simple and fun
You're welcome... glad to hear it! Keep on codin'!
Great video, works well. Can you please make a video on how to add sounds and animations to it?
Thanks - would be happy to when I have time.
Its guys like Kevin that keep us going
Thanks, Kevin!
OMG I am sooo proud of myself :).
THANKYOU SO MUCCHH!!! :D
You're welcome!
Can You make videos about adding upgrades to click, or something else that would fit in Clicker Game?
Possible someday - if you shoot me a reminder in summer I might have time then!
Hey I'm wondering something, is making programs like this for personal use legal? Sorry I'm new
It's good to ask those types of questions. Yes, for personal use you can do pretty much what you want - it's called fair use.
help, when I try to add the cookie sprite, it says "couldn't open "cookie.gif": no such file or directory"
here's my code:
import turtle
wn = turtle.Screen()
wn.title("Cookie Clicker by @yoshihome")
wn.bgcolor("black")
wn.register_shape("cookie.gif")
cookie = turtle.Turtle()
cookie.shape("cookie.gif")
cookie.speed(0)
wn.mainloop()
9th comment, I'm a ultra legend.......😅😅😅
Nice!
Bro I pasted the exact same code you wrote but,, the pen doesnt show anything.. im using python 3.9
import turtle
import time
wn = turtle.Screen()
wn.title("Cookie Clicker")
wn.bgcolor("black")
wn.register_shape("cc.gif")
cookie = turtle.Turtle()
cookie.shape("cc.gif")
cookie.speed(0)
clicks = 0
pen = turtle.Turtle()
pen.hideturtle()
pen.color("white")
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"))
cookie.onclick(clicked)
wn.mainloop()
Can you share the code here? And any error messages?
@@TokyoEdTech I dont get any error messages but when I try turtle.write instead of pen it works /
@@messi-arg1 There is likely a problem with your code - if you share it, I can take a look.
how would i change the picture after a certain amount of clicks?
It's pretty straightforward - do something LIKE the following:
if num_of_clicks == 10:
cookie.shape("image.png")
I saw your video and I tried doing it but the picture of the cookie doesn't appear I am doing it on mac and I have it in the same folder as the program but it still doesn't work
Can you share your code here? Copy and paste it along with any error messages you're getting.
@@TokyoEdTech I played around with the files and it worked but thanks for replying your python coding tutorials are really awesome and easy to follow love your content btw
@@EpicGM100 Glad you got it sorted. Thanks for the kinds words and keep on codin'!
Has anyone ever told you, that you very much sound like The 8 Bit Guy :D, amazing tutorial though ;)
Thanks - that is quite the compliment!
Hey man , really nice tutorial , thank your for it !
Thanks! Glad you liked it! Keep on codin'!
Dope video 🔥🔥🔥
Thanks 💯
40th comment! 🎂🥳
Congrats...I think!
should be obv but check your spelling and capitilazation if you ron windows the screen will tell you what line you messed up
Good advice - thanks!
hey why is it that when i get to a certain amount of clicks the "clicks" message moves to the left
Because the pen is is aligned in the center of its coords (0, 400).
Its like in MS Word text aligner option that makes text go on the left, middle, or right.
The middle of the characters in the pen's text aligns to its coords. This means when the text gets longer, (i.e. number of digits going up) the center character of the text changes, and it re-aligns the text back to the center.
TLDR;
The "clicks" message isn't moving to the left, both ends of the text are just getting longer when the digit increases.
Thanks for jumping in there!
Type less...
Do more...
Productivity = Python
✅✅✅✅✅✅✅✅
Truth!
@@TokyoEdTech
Indeed!
Yay!
:)
Hi, could you please make another tutorial where there is a shop system and clicks per second, just like Cookie Clicker?
Hi! I get this request a lot - I'll think about it. I've never really played the real cookie clicker though - will have to check this out.
I am getting an error:
Traceback (most recent call last):
File "/Users/***/Desktop/CookieClick/Gui", line 11, in
wn.title("Cookie Click")
AttributeError: 'function' object has no attribute 'title'
Can you share the code? Copy and paste is fine.
I don't know why but mine doesn't work, I did it on Trinket with Phyton (not 3) and I get the message that I have an bad input in line 25 :/
I'm not sure this will work on Trinket.
@@TokyoEdTech Hey man, when I do this with VS Code, everything works, but... the text doesn't write on the screen at all, I have copied and pasted it from your GitHub and it still doesn't work :( Can you help me fix this? I have a feeling it has something to do with the fact that cookie = turtle.Turtle() and pen use the same thing after, possibly putting the text as cookie. It's that or the pen.hideturtle() line
@@TokyoEdTech never mind, i fixed it, it was going off the screen and the window needed to be expanded for you to see it.
@@PhoenixVids123 Glad you got it sorted - great job. Keep on codin'!
you need bacrownd music!😁😁
I agree!
Tried this, it says syntax error: position argument follows keyword argument
You need to share your code and the exact error message.
Why do you tend to use turtle rather than pygame?
Good question - I teach middle and high school students. Pygame is definitely more powerful than the turtle module, but it is also much more complicated. For that reason, I tend to use the turtle module instead.
@@TokyoEdTech I see, yeah for kids turtle is probably a better module to teach.
code:
import turtle
wn = turtle.Screen()
wn.title("Cookie Clicker")
wn.bgcolor("Black")
wn.register_shape("cookie.gif")
cookie = turtle.Turtle()
cookie.shape("cookie.gif")
cookie.speed(0)
clicks = 0
pen = turtle.Turtle()
pen.hideturtle()
pen.color("white")
pen.pendown()
pen.goto(0, 270)
pen.clear()
pen.write(f"Clicks: {clicks}", align="center", font=("Courier New", 50, "normal"))
def clicked(x,y):
global clicks
clicks += 1
pen.clear()
pen.write(f"Clicks: {clicks}", align="center", font=("Courier New", 50, "normal"))
cookie.onclick(clicked)
wn.mainloop()
Thanks for sharing!
339th comment lets go!
claim your before 1000 comments
👇
Go for it!
thx for make this video , i love it :)
You're welcome - keep on codin'!
it says something about unicode at where it says wn.register_shape, im also on windows so i put the full path
nevermind, i found my problem and fixed it
@@lockedgaming6081 Good to hear!
@@TokyoEdTech thank you! im now experimenting with upgrades!
@@lockedgaming6081 How did you fix it??
when i click the cookie the number goes away, whats the solution here? if anyone can tell me thx :D
If you share your code here I will take a look.
@@TokyoEdTech ok so, i followed the tut, did everything right according to me. (not very accurate) so here the code i wrote according to your video:
import turtle
wn = turtle.Screen()
wn.title("cookieclicker thingy by commander molotov")
wn.bgcolor("Black")
wn.register_shape("249534.gif")
cookie = turtle.Turtle()
cookie.shape("249534.gif")
cookie.speed(0)
clicks = 0
pen = turtle.Turtle()
pen.hideturtle()
pen.color("white")
pen.penup()
pen.goto(0, 300)
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"))
cookie.onclick(clicked)
wn.mainloop()
@@molotov5000 In your clicked method you need to indent the pen.write line.
@@TokyoEdTech ohhhh thanks
@@molotov5000 Sure thing - keep on codin'!
Hi, I dont have any error but when i click it does not count
Hmmm...if you share your code I can take a look.
@@TokyoEdTech Thank you for the reply but i already figured it out and its working, thank you so much for the video :)
@@rabiasonmez2329 You're quite welcome - glad you got it sorted. Keep on codin'!
it doesnt work when i tried to print the picture it just shows a syntax error and theres no cookie in the screen
heres the code:
import turtle
wn = turtle.Screen()
wn.title("Cookie Clicker")
wn.bgcolor("black")
wn.register_shape("cookie.gif")
cookie = turtle.Turtle()
cookie.shape("cookie.gif")
cookie.speed(0)
wn.mainloop()
and this was since 5:41 minutes into your video
Heya - what was the syntax error? Can you copy and paste it here?
Make sure it is above wn.mainloop()
can you help me too the cookie is not coming into pycharm
Sure. You probably need the full path to the image.
Otherwise, share your code here and any error message.
I can't get the cookie to load in.
I'm on Windows btw
You probably need the full path to the image.
Something like C:\Users\jameszhu\....\cookie.gif
If you right click and choose properties you can see the full path, I believe...sorry, not a Windows user.
ubuntu.com/
@@TokyoEdTech Ok thx I'll try it!
when i run it it has the error "self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "cookie.gif": no such file or directory" even though i have the .py and .gif files in the same folder
Are you sure it is a gif image? On windows you night need the complete path to the file
@@TokyoEdTech yeah its a gif, the issue fixed itself when i turned my pc off and on (that trick seems to fix everything)
It never fails!
What is the program you are using?
It's called Geany. I made a short video about it here: ua-cam.com/video/quemIgUn4C8/v-deo.html
Hi! This is the first video I've watched from you and I am so glad I found you because this video was so good! It has everything what begginer needs. But I have a little problem...
When I execute the game I can't see the text Clicks: 0.
I checked the code and it was written correctly and I have Python version 3.8 so that shouldn't be problem neither sooo... I don't know.
I would really appriciate your help. :D Thanks.
Thanks! Try moving the text lower on the screen. It's probably there but off the screen.
All the code lines and everything ids the same but the wind that opes only shows the cookie
The text is probably off screen. Try moving the pen down.
Hello, this isn't working for me unfortunately - i'm using PyCharm via MacOS and when I run the code (copied exactly from your link with the image in the correct folder etc) the window opens really small with the scroll bars glitching out (no image or custom title either, it says "Python Turtle Graphics" on the top bar). When I then close the window i get a bunch of errors all referencing this file: "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py" and this: _tkinter.TclError: can't invoke "bind" command: application has been destroyed. Any ideas?
Can you copy and paste your code here?
@@TokyoEdTech
import turtle
wn = turtle.Screen()
wn.title("Cookie Clicker by @TokyoEdtech")
wn.bgcolor("black")
wn.register_shape("cookie.gif")
cookie = turtle.Turtle()
cookie.shape("cookie.gif")
cookie.speed(0)
clicks = 0
pen = turtle.Turtle()
pen.hideturtle()
pen.color("white")
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"))
cookie.onclick(clicked)
wn.mainloop()
@@TokyoEdTech It's straight from your github. Not sure what the issue is...thanks in advance for your help :)
After wn.bgcolor...try adding this line:
wn.setup(600, 600)
@@joec23 No worries. Some computers are set up differently. It happens.
I can't understand which line of code makes it so the counter doesnt go up when you click on anything other than the cookie itself?
The click is bound to the turtle that has the cookie as it's image so clicking on the cookie is what triggers the clicking event.
thanks! @@TokyoEdTech
You're welcome. Keep on coding!
@@icakabg0468 You're welcome!
so i made a clicker game without any game library i only used tkiter and i am about to add a small data base to kame it store the score and the upgrades (i don't know wy i comented )
Glad to hear that - thanks for sharing! Keep on codin'!
Cool bro!!! Tks for this
You're welcome - keep on codin'!
everything works but the text wont show up
Can you share your code here and I'll take a look.
Same happens to me. Try changing the coordinates to (0, 200)
@@TheCubingChronicles Thanks for jumping in there!
probably up there, make the turtle screen bigger. Happend to me
@@treborydt Thanks - I should have addressed that in the video, but didn't.
wn.setup(600, 600)
600x600 is the default for a lot of systems, but apparently not all.
It dont show me the clicks...
Is it because of version of python?
Mine is 3.9.0
There is a link in the description labeled “NEED HELP?” Check that out and get back to me. Keep on codin’!
this is actually kinda works but the problem is that i use idle python 3.5.1 and when i do the f"Clicks: {clicks}" it showes me an error and if i delete the f neither way the text won't appear
You should update to the latest Python version so you can use f strings - very convenient!
The text wont appear did you find the solution?
@@urosradosavljevic3047 There is a link in the description labeled “NEED HELP?” Check that out and get back to me. Keep on codin’!
it workes but when i click the image the clickes dont go up do you have a fix?
I need to see the code. Can you paste it here?
@@TokyoEdTech heres the code. import turtle
wn = turtle.Screen()
wn.title("Cookie Clicker by meeee")
wn.bgcolor("blue")
wn.register_shape("cookie.gif")
cookie = turtle.Turtle()
cookie.shape("cookie.gif")
cookie.speed(0)
clicks = 0
pen = turtle.Turtle()
pen.hideturtle()
pen.color("white")
pen.penup()
pen.goto(0, 400)
pen.write(f"Clicks: {clicks}", align="center", font=("Courier New", 32, "normal"))
def clicked():
global clicks
clicks +=1
pen.clear()
pen.write(f"Clicks: {clicks}", align="center", font=("Courier New", 32, "normal"))
cookie.onclick(clicked)
wn.mainloop()
Does anyone know why when i run the script the window that i opened gets closed after half a second?
You have an error in your code. Check your error messages.
do you have the wn.mainloop()
How can i add this upgrade that when the clicks count reaches the factor of 100 the cookie picture changes
You need to add something like:
wn.register_shape("cookie2.gif")
And
if clicks>100:
cookie.shape("cookie2.gif")
How can I add :-
Another game mode that number of clicks are divided by given amount of time , so we clicks per second ?
@@baldavsingh4596 That's not as simple - you need to get the start time then calculate the clicks per second as you play. It's not super complicated, but requires a bit more knowledge then I would expect a beginner to have.
do you have a tutorial for mac aswell?
It should work on all platforms. The only the ng that really differs is making sounds
GOOD INFO
Thanks
NIce vide i really like it thanks :)
I'm glad you like it!
hey i am a German guy and i like your tutorial and i am new to python can you help me to learn how to get a upgrade shop and a automatic generate cookie ? i dont know how to code that
I could probably figure that out, but it would be better for you to try to figure it out on your own...you'll learn better that way.
@@TokyoEdTech Jeah Sure But where can i learn it i have no idea where i can watch. Or what i Need and ty for the fast answer
It's not a simple thing that can be explained ina comment. Keep learning and one day you'll be able to do it in your own!
Hey can u make a upgrading system in code?
Hiya - I get this request from time to time - I might try it someday. I'm not really familiar with the game though to know what kind of upgrades can be done.
@@TokyoEdTech like click upgrade or auto click
Gimp... The free Photoshop
Yep - I've been using it for years!
I tried to make it with a different .gif and everything works eccept it doesnt show how many times i clicked, not even the text. I even copy & pasted to make sure it wanst my fault but it didnt show the text still. Im on Windows 10. Is it an OS problem?
You may have 2 problems:
1. On Windows you probably need the full path to the image, something like C:\Users\...\cookie.gif
2. You might have to move the text down the screen it might be outside the visible window.
@@TokyoEdTech thanks this also worked for me!
Why not just use pillow module instead of turtle?
The pillow module is completely different and doesn't do what the turtle module does.
On line 12 wn.register_shape(“cookie.gif”) I’m getting an error that it can’t open the file. I’m on Mac and I have the file in the project folder what should I do?
Are you sure the file is a gif? Or did you just rename it? It must be a gif.
Just had the same problem as you, if you're on Windows (like I am) then instead of the file name write r'C:\Users\(your user name)\Desktop\cookie.gif' or you can copy the file location, add the file name at the end and an 'r' before the file location as shown in my example. I hope it works. Good luck with your game.
PS. You'll have to add that long string when declaring the shape too
I changed the file to a gif it must be something small wrong with my code I’ll check it
@@GlitchedGD How did you change it to a gif? What program did you use? Try a different file - some images don't work for some reason.
180th comment don't let the ball stop keep it a rollin
Solid life advice!
@@TokyoEdTech thanks
Does it work with Visual studio code because I am getting a lot of error
Yes - it should. What is the exact error message?
The clicks text not showing pls help, thanks
There is a link in the description labeled “NEED HELP?” Check that out and get back to me. Keep on codin’!
nvm i just installed geany and it worked. I also installed python version 3.10.
Good to hear - keep on codin'!
HELP I did everything good but does anyone know to make the counter actually go up when I click the cookie. I manually drew the cookie out by myself, so I can't do the same thing he did. But still, does anyone know how to make the counter go up when i click the cookie?
Share your code.
@@TokyoEdTech here:
from turtle import *
sc = Screen()
sc.bgcolor('black')
hideturtle()
speed(0)
up()
goto(0,-100)
down()
fillcolor('darkgoldenrod')
begin_fill()
c = circle(100)
cookie = Turtle()
cookie.shape(c)
end_fill()
fillcolor('saddlebrown')
begin_fill()
up()
goto(-35, 35)
down()
circle(20)
end_fill()
begin_fill()
up()
goto(-30, -75)
down()
circle(20)
end_fill()
begin_fill()
up()
goto(59, 10)
down()
circle(22)
end_fill()
begin_fill()
up()
goto(50, -50)
down()
circle(17)
end_fill()
begin_fill()
up()
goto(0, -10)
down()
circle(18)
end_fill()
begin_fill()
up()
goto(-65, -18)
down()
circle(13)
end_fill()
clicks = 0
up()
goto(0, 150)
pencolor('white')
write('Clicks: '+str(clicks), align='center', font = ('Arial', 20, 'normal'))
def clicked(x,y):
global clicks
clicks += 1
clear()
write('Clicks: '+str(clicks), align='center', font = ('Arial', 20, 'normal'))
cookie.onclick(clicked)
done()
I know where I got it wrong but I don't know how to fix it
@@therealrxyang sorry i didn't find this earlier but here is your corrected code:
from turtle import *
# Setup the screen
sc = Screen()
sc.bgcolor('black')
speed(0)
# Draw the cookie
def draw_cookie():
# Draw the cookie body
up()
goto(0, -100) # Move to the center-bottom
down()
fillcolor('darkgoldenrod')
begin_fill()
circle(100) # Draw the cookie
end_fill()
# Draw the chocolate chips
fillcolor('saddlebrown')
begin_fill()
up()
goto(-35, 35)
down()
circle(20) # Draw first chocolate chip
end_fill()
begin_fill()
up()
goto(-30, -75)
down()
circle(20) # Draw second chocolate chip
end_fill()
begin_fill()
up()
goto(59, 10)
down()
circle(22) # Draw third chocolate chip
end_fill()
begin_fill()
up()
goto(50, -50)
down()
circle(17) # Draw fourth chocolate chip
end_fill()
begin_fill()
up()
goto(0, -10)
down()
circle(18) # Draw fifth chocolate chip
end_fill()
begin_fill()
up()
goto(-65, -18)
down()
circle(13) # Draw sixth chocolate chip
end_fill()
# Initialize clicks
clicks = 0
# Draw initial state
draw_cookie()
# Display click count
up()
goto(0, 150)
pencolor('white')
write('Clicks: ' + str(clicks), align='center', font=('Arial', 20, 'normal'))
# Click handler function
def clicked(x, y):
global clicks
clicks += 1
clear() # Clear the drawing
draw_cookie() # Redraw the cookie
up()
goto(0, 150) # Move to the click count position
write('Clicks: ' + str(clicks), align='center', font=('Arial', 20, 'normal'))
# Set up the cookie turtle to register clicks
cookie = Turtle()
cookie.hideturtle() # Hide the turtle
cookie.speed(0) # Fastest drawing speed
cookie.shape("circle") # Set the shape to a circle
cookie.shapesize(stretch_wid=10, stretch_len=10) # Make the turtle larger
cookie.color('darkgoldenrod') # Set the turtle color to match the cookie
cookie.onclick(clicked) # Register the click event
done()
i added hashtags to make it easier for you my improvements were: Function for Drawing the Cookie: I encapsulated the drawing logic inside the draw_cookie() function to keep the code organized and reusable.
Clickable Cookie: Created a separate Turtle instance named cookie for handling click events. This turtle is set to a larger size and is positioned at the center to simulate clicking on the cookie.
Redraw on Click: The clicked() function now clears the screen, redraws the cookie, and updates the click count each time it is clicked.
Initialization: The initial cookie is drawn once before any clicks, so it appears immediately on the screen.
"couldn't open "cookie.gif": no such file or directory" that happened when I launched the script, even if my file is on my desktop. here is my script, but i dont think the problem is here :
import turtle
wn = turtle.Screen()
wn.title("Cookie Clicker")
wn.bgcolor("black")
wn.register_shape("cookie.gif")
cookie = turtle.Turtle()
cookie.shape("cookie.gif")
cookie.speed(0)
wn.mainloop()
thanks for helping me in advance.
You've probably got one of 3 likely problems:
1) The file is in one place, but the python program file is not
2) Especially on Windows, you need the full path. Something like C:\Users\....\filename.gif
3) The file is not a gif. Renaming a file from .png to .gif doesn't make it a gif - you need to use a program to convert it.
can u pls update this game cause its pretty fun and pls add more stuff to buy
If you want to learn to code, you gotta code - see if you can figure it out!
The image and the code is in the same file, but for some reason it doesn't work. the image is a gif file, and followed your steps, but it seems to not work.
If you are on Windows you may need the full path to your file. C:\Users....
I had the same issue and it was because the wn.mainloop () was above that section of code and it must be at the bottom. Hope that helps
the text is not showing me , what to do now? please help
Try watching closely. Also, try making the window bigger.
If that doesn't help, paste your code here and any error messages.
@@TokyoEdTech pen.write(f"Clicks: {clicks}", align="center", font=("Courier New", 32, "normal"))
@@user-ib1tk8vg8t If you need help, please share all the code and describe the problem in detail.
scratch that i figured it out
@@user-ib1tk8vg8t how did you? I'm facing the same issue
I don't know how, but my text overlaps every time I click the cookie
Can you share your code and I'll take a look?
@@TokyoEdTech okay, but in a few hours
You have to clear the screen when you reprint.
Heey, nice video... But actually you missed the biggest part with all the upgrades that you can buy and will generate cookies for you automatically 😅😂
Thanks. Yeah - I only ever played the really simple version. I wanted this to be an easy tutorial for my students.
@@TokyoEdTech still a good video, maybe that could be something for a part two:D
@@Thomate1375 Possibly! But, it would be a good idea for you to do this on your own to develop your skills.
@@TokyoEdTech actually thats exactly what I'm doing right now 😂😅
But tbh im kind of struggling with python... I'm pretty good at c and c#, but I have just started to learn python 😂😅
can you please make a part 2 covering animations
Maybe someday!