Formulas in MakeCode

So here is my thought. I am middle school teacher. My students are starting equations and I want them to see how different equations create different patterns. Contra was great at this. You get a power up and then your gun that was shooting straight lines now shoots at angles (slope equation) (or it shoots a circle forward or a wave) whatever. The right equation and you can make any crazy path.

Here is what I have so far.
Game play is left to right so projectile moves horizontal across the screen
When A is pressed generate a projectile at Player Sprite
(Currently I can only set a velocity, I want just the XY coordinate)
Start a timer (I think I can do this by cycling a value but would like an actual timer) I
change the X based upon the timer value a loop and increase
Change the Y based upon the X in the equation
I kind of envision something like:

def on_a_pressed():
global projectile
projectile = sprites.create_projectile_AT_sprite(img(β€œβ€"
β€œβ€"), mySprite)
IF PROJX <= 160
SET PROJECTILE X TO PROJX
SET PROJECTILE Y TO PROJX*(a slope value)
PROJX + 1
ELSE
PROJX = 0

It feels like something that should be achievable but I just can’t figure it out. Thanks.

3 Likes

Welcome, @jquinn !

Here is a project with some ideas in it:

The interesting code is in the movement functions. I split the movement code out of the loop; you easily could merge the two functions into one.

def move_projectile(sprite: Sprite, kind: number):
    if kind == 0:
        # plasma_ball moves in a zig-zag motion up the screen
        sprite.vx = -sprite.vx
    if kind == 1:
        # laser_flash follows a sinusoidal path
        # Change the divisor here to change the amplitude
        # Larger values increase the amplitude
        sprite.vx = Math.sin(game.runtime() / 250) * 30
    if kind == 2:
        # photon_torpedo moves in a circular path with an added vertical velocity
        # i.e., it moves in a spiral
        sprite.vx = Math.sin(game.runtime() / 1000 % (2 * Math.PI)) * 20
        sprite.vy = Math.cos(game.runtime() / 1000 % (2 * Math.PI)) * 20 - 10

def move_projectiles(kind: number):
    projectiles: List[Sprite] = None
    if kind == 0:
        projectiles = sprites.all_of_kind(SpriteKind.plasma_ball)
    if kind == 1:
        projectiles = sprites.all_of_kind(SpriteKind.laser_flash)
    if kind == 2:
        projectiles = sprites.all_of_kind(SpriteKind.photon_torpedo)
    if projectiles != None and projectiles.length > 0:
        for sprite in projectiles:
            move_projectile(sprite, kind)

You could make all sorts of patterns with some clever arithmetic.

Have fun!

6 Likes

This is great. Thank you. I read your code and am reminded of how much more I have to learn. Why did you use an array? How did it make it easier? Could you do without an array?

Thanks again! This is great.

2 Likes

Hi, @jquinn !

I assumed that you wanted all sprites that are the same kind to move in the same way. To find all sprites that are of a certain kind, sprites.all_of_kind() returns an array. There might not be any sprites of a particular kind (in which case, the array will be empty), there may be just one sprite, or there may be more than one. An array can handle all of those scenarios.