summarymit-scratch

Is there a way to write summation using Scratch?


So I have recently been creating Cookie Clicker in Scratch. While I have been adding buildings to the game and a buy 10 function for those buildings, I honestly also want to add a Buy 100 function to the game but I don't want to have the code as x+(x*1.15)+(x*1.15^2)+...+(x*1.15^99), where x is defined as the number of that building that the player currently has, and I honestly don't want to have to keep having to add 200+ blocks of code to the "Buy 10 of y" codes. (y in this case what building is being bought 10 times) 4 difficulties that are keeping me from solving this myself:

  1. Scratch sadly doesn't have blocks to create less/greater than or equal to functions
  2. Scratch doesn't have summation blocks
  3. I honestly am terrible at coding, so I honestly just want a custom block that makes this easier.
  4. Regular exponentiation doesn't exist in Scratch, but exponential notation (x*10^y) does.

As mentioned above (I've just been copying and pasting code at this point), I have to write it out as

if z (z being the number of the cookies the player has attained) = x+(x*1.15)+(x*1.15*1.15)+(x*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15*1.15*1.15*1.15*1.15) or if z > x+(x*1.15)+(x*1.15*1.15)+(x*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15*1.15*1.15*1.15*1.15)

then set z to z - x+(x*1.15)+(x*1.15*1.15)+(x*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15*1.15*1.15*1.15*1.15)

Change cps (cookies per second) by 10 * a (the amount of cookies per second that is being changed by multiplied by 10. I usually just add an extra 0 to the end of the number for simplicity)

Set b(y) (the cost of the building that is being bought) by (x*1.15*1.15*1.15*1.15*1.15*1.15*1.15*1.15*1.15)

Change y by 10

else

broadcast Can't Buy!

However, I want to write as a cumulative price, similar to the formula used to determine how many cookies you would need to buy x buildings when you have y amount of the building such as bΣ(b=a+1) ((c)(1.15^b))/1.15 where c is the base cost of the type building you want to buy, b is the number of the type of building you want to have, and a is the number of the type of building you have currently.

To clear up any confusion, if you want to take a look at the editor, here is the link.


Solution

  • I used that C program link I sent you, for how to compute powers, to write an equivalent function in Scratch. Works with both positive and negative exponents. I'm rusty with Scratch so there might be a better way to return from the function, but it seems like it works great.

    This should be all you need in order to get going, but if you still run into issues I can try to help out:

    Power Function

    Result:

    enter image description here

    Proof:

    enter image description here