Discuss Scratch

bongolol
Scratcher
6 posts

Cookie Clicker Type Game

Im trying to code a type of golden cookies system in my game but idk how to make it so they just appear randomly
PhiPhenomenon
Scratcher
500+ posts

Cookie Clicker Type Game

Here's some base code that you can use:
hide
forever
wait (pick random (30) to (60)) secs
go to [random position v]
show
wait until <<mouse down?> and <touching [mouse-pointer v] ?>>
hide
change [cookies v] by (1000)
end
The golden cookie appears 30 to 60 seconds at a time. Then it waits until the player clicks it and awards a certain number of cookies.

Last edited by PhiPhenomenon (Jan. 4, 2024 23:39:00)

EDawg2011
Scratcher
1000+ posts

Cookie Clicker Type Game

Is this good?

when green flag clicked
forever
wait (pick random (60) to (240)) secs
show
set [random v] to (pick random (-100) to (100))
go to x: (random) y: (180)
glide (3) secs to x: (random) y: (-180)
hide
end
PhiPhenomenon
Scratcher
500+ posts

Cookie Clicker Type Game

EDawg2011 wrote:

Is this good?

when green flag clicked
forever
wait (pick random (60) to (240)) secs
show
set [random v] to (pick random (-100) to (100))
go to x: (random) y: (180)
glide (3) secs to x: (random) y: (-180)
hide
end
This solution does not detect if the golden cookie has been clicked, nor does it add cookies to the total count.
p-p-p-p-p-p-p-p-p-p-
Scratcher
1000+ posts

Cookie Clicker Type Game

PhiPhenomenon wrote:

This solution does not detect if the golden cookie has been clicked, nor does it add cookies to the total count.
The method you provided actually also does not detect is the golden cookie has been clicked - it detects whether the mouse was down and touching it. Therefor, if a player had their mouse down somewhere, and the cookie fell on it, it would be triggered, even though the mouse did not actually click the cookie.

EDawg2011 wrote:

Is this good?

when green flag clicked
forever
wait (pick random (60) to (240)) secs
show
set [random v] to (pick random (-100) to (100))
go to x: (random) y: (180)
glide (3) secs to x: (random) y: (-180)
hide
end
This also does not help because now, the user can click the cookie multiple times.
a truly perfect script for a golden cookie would be something like this:
when gf clicked
forever
hide
wait (pick random [x] to [y]) secs
go to [random position v]
set [clicked? v] to (0) // Preferably a variable set to "This sprite only", to reduce clutter, but it does not matter.
broadcast [start animation v]
reset timer
show
wait until <<(timer)>[10]>or<(clicked) = [1]>> // waits 10 seconds to be clicked before hiding
end

when this sprite clicked
set [clicked? v] to [1]
perform golden cookie action here :: sensing

when I receive [start animation v]
repeat until <<(timer)>[10]>or<(clicked) = [1]>>
perform animation here ::sensing
At least, that's how I would approach the problem.

Last edited by p-p-p-p-p-p-p-p-p-p- (Jan. 5, 2024 00:09:56)

PhiPhenomenon
Scratcher
500+ posts

Cookie Clicker Type Game

p-p-p-p-p-p-p-p-p-p- wrote:

The method you provided actually also does not detect is the golden cookie has been clicked - it detects whether the mouse was down and touching it. Therefor, if a player had their mouse down somewhere, and the cookie fell on it, it would be triggered, even though the mouse did not actually click the cookie.
That issue is relatively minor due to the low chance of that happening. Anyway, it can be easily fixed by adding a single block:
hide
forever
wait (pick random (30) to (60)) secs
go to [random position v]
show
wait until <not<<mouse down?> and <touching [mouse-pointer v] ?>>>
wait until <<mouse down?> and <touching [mouse-pointer v] ?>>
hide
change [cookies v] by (1000)
end
p-p-p-p-p-p-p-p-p-p-
Scratcher
1000+ posts

Cookie Clicker Type Game

PhiPhenomenon wrote:

p-p-p-p-p-p-p-p-p-p- wrote:

The method you provided actually also does not detect is the golden cookie has been clicked - it detects whether the mouse was down and touching it. Therefor, if a player had their mouse down somewhere, and the cookie fell on it, it would be triggered, even though the mouse did not actually click the cookie.
That issue is relatively minor due to the low chance of that happening. Anyway, it can be easily fixed by adding a single block:
hide
forever
wait (pick random (30) to (60)) secs
go to [random position v]
show
wait until <not<<mouse down?> and <touching [mouse-pointer v] ?>>>
wait until <<mouse down?> and <touching [mouse-pointer v] ?>>
hide
change [cookies v] by (1000)
end
That still has the same problem. The reason I know this is because I coded SWEATFit, which is a very complicated project with a lot of different assets, and one of the things I tried doing was adding buttons like this. But you can swipe them.
I edited my original post with a foolproof solution, with no edge cases.
bongolol
Scratcher
6 posts

Cookie Clicker Type Game

Thank you so much everyone! All this really helped!

Powered by DjangoBB