How do I make a game with code?

I saw a Wick Editor Jam on Newgrounds. I download Super Mario Bros. Sprite Sheets because I just test the game but I don’t know how to use Codes?

Hi, I would say that I’m pretty good with javascript.
Functions are little things you say in the code that do much bigger things. function(), put specifications in the parentheses.
Variables store info in your code that can be read and written. Making a variable is as easy as “var i = 94”
To read a variable, just use the name of the variable, where you would put a number or word.
If you need to increase a variable by 1, type “i += 1”
:smile: hope this helps!
Edit: w3schools.com has more on js

Yeah, thanks but how about Controls for Game, not Interactive.

controls.wick (16.5 KB)

Not so great but you need Arrow Keys for Game.

  1. Its just a POC
  2. Easily changeable

okay, I’ll try.

I tried. This code sucks, well… I just made it up.

I sent ya a PM on Newgrounds including a very basic code for moving around an object with the arrow keys in the Alpha version. Here’s one that includes some ways to prevent people getting that speed boost from pressing multiple keys that I mentioned in that message. Not going to lie, this is probably a pretty goofy way to fix that problem, and there is likely a better way to do it than this. This is just the first solution that came to mind for me, but I’d encourage you to try to find a way to do the same thing in fewer lines of code, probably a good coding exercise anyway!

Put this code in the “Keydown” tab of the object you want to move in Alpha version:

//set a variable to control the speed
//this allows you to change the speeds in the code all at once instead of trying to edit each one
//also allows speed changes at run time, so you can have “stats” or “power-ups”
var speed = 5;

//check key presses
//for each frame the key is held down, increase or decrease position by speed accordingly
//first four are right, left, up, down
//the “!” basically means “not”, I included the “not key ‘up’ and not key ‘down’”
//this is to help avoid speed doubling when pressing multiple keys
//honestly there is probably a better way to do this
//this is just the first solution that came to mind, feel free to experiment with other ideas
if(isKeyDown(“right”) && !isKeyDown(“up”) && !isKeyDown(“down”)){
this.x += speed;
}
if(isKeyDown(“left”) && !isKeyDown(“up”) && !isKeyDown(“down”)){
this.x -= speed;
}
if(isKeyDown(“up”) && !isKeyDown(“right”) && !isKeyDown(“left”)){
//note: -y is up, +y is down
this.y -= speed;
}
if(isKeyDown(“down”) && !isKeyDown(“right”) && !isKeyDown(“left”)){
//note: -y is up, +y is down
this.y += speed;
}

//second four are diagonals
//for these, I move the character in the appropriate direction, but at half the speed for each
if(isKeyDown(“right”) && isKeyDown(“up”)){
this.x += speed/2;
this.y -= speed/2;
}
if(isKeyDown(“right”) && isKeyDown(“down”)){
this.x += speed/2;
this.y += speed/2;
}
if(isKeyDown(“left”) && isKeyDown(“up”)){
this.x -= speed/2;
this.y -= speed/2;
}
if(isKeyDown(“left”) && isKeyDown(“down”)){
this.x -= speed/2;
this.y += speed/2;
}

//last part
//This just cancels out the speed boost that you get from pressing 3 keys
if(isKeyDown(“up”) && isKeyDown(“right”) && isKeyDown(“left”)){
//add half speed in the opposite direction:
this.y += speed/2;
}
if(isKeyDown(“down”) && isKeyDown(“right”) && isKeyDown(“left”)){
this.y -= speed/2;
}
if(isKeyDown(“right”) && isKeyDown(“up”) && isKeyDown(“down”)){
this.x -= speed/2;
}
if(isKeyDown(“left”) && isKeyDown(“up”) && isKeyDown(“down”)){
this.x += speed/2;
}

1 Like

If you still want to do it in the live version, you can use this instead. Just put this in the script for your player character, though you’ll need to figure out some way to stop the speed from doubling if the player is pressing more than one key. The doubling occurs because the character is moving at “speed” (5) to the right and “speed” up when pressing both right and up. You want some way to move the character at (speed divided by 2) in both directions when pressing those two keys.

//set a speed variable so you can edit all speeds at once and also change speed at run time
var speed = 5;

//when a key is down, check what the key is
//then adjust position accordingly
//this code effectively doubles speed when pressing two directions like up and right at the same time
function keyDown(){
if(keyIsDown(“UP”)){
this.y -= speed;
}
if(keyIsDown(“DOWN”)){
this.y += speed;
}
if(keyIsDown(“RIGHT”)){
this.x += speed;
}
if(keyIsDown(“LEFT”)){
this.x -= speed;
}
}

1 Like

@GoodL

Just my two cents, as I find this way a bit easier.
(The code below is just the concept.)

//Make a var that holds a modifiable speed value.
this.speed =7;
//Make a var that holds a facing direction value.(This is defaulted to down.)
this.facing =‘down’;
//Make a var that detects if whatever object should be moving.(Defaulted to false.)
this.isMoving =false;

//Make conditionals for all the possible directions you want…

//Face up and begin moving.
if(key === ‘UP’){this.facing =‘up’; this.isMoving =true;}
//Face down and begin moving.
else if (key === ‘DOWN’) {this.facing =‘down’; this.isMoving =true;}
//Face left and begin moving.
else if (key === ‘LEFT’) {this.facing =‘left’; this.isMoving =true;}
//Face right and begin moving.
else if (key === ‘RIGHT’) {this.facing =‘right’; this.isMoving =true;}

//Make character move.
//If the user does not continue to hold the key down, movement stops.
//Move up.
if(this.isMoving && this.facing ===‘up’){this.y-=this.speed; this.isMoving =false;}
//Move down.
else if(this.isMoving && this.facing ===‘down’){this.y+=this.speed; this.isMoving=false;}
//Move left.
else if(this.isMoving && this.facing ===‘left’){this.x-=this.speed; this.isMoving =false;}
//Move right.
else if(this.isMoving && this.facing ===‘right’){this.x+=this.speed; this.isMoving =false;}

This also prevents the multi direction thing.
Let me know you think.

2 Likes

@colorsCrimsonTears This is a Default or Keydown?

Gave your code a try, to get it working (in Alpha) I had to change

if(key === ‘KEY’))

to

if(isKeyDown(“key”))

at which point it started working. The issue I have here is that this just completely disallows movement in the diagonals, and even then, holding down two directions (say, right and up) does still give you a speed boost. It prefers vertical movement (I assume this is because of the order they appear in the script), so holding down left and down will move you down at twice the normal speed.

What I do really like about your script though is that it adds in a nice way to keep track of which way your character is facing, so you could rotate your character art with only a few extra lines of code. :slight_smile:

@AngryDuck16 I got this script working in Alpha by putting it in the Keydown tab.

3 Likes

Great catch GoodL.

The trick to understand here is that the Keydown tab will run once, for each key pressed down, per tick.

Here are what I would consider the most basic examples for keyboard movement. They both do the same thing, but one uses the multi-tab system, the other uses the single default tab system. If you need a reference, these might be good!

default_keydown.wick (2.9 KB)

tab_keydown.wick (2.9 KB)

1 Like

So uh… These work great. I don’t really know what happened when I was testing the very simplest version of my code before I added all that extra stuff but apparently it was all entirely unnecessary! These will do the trick perfectly!

1 Like

I noticed in your screenshot you tried to add a “jump” key, so I suspect you might be trying to make a platformer, and since all the stuff we’ve been talking about above is more for top-down games, I figured I’d make a platformer example for you so you could get an idea for how a jump might work.

Note: this could be done better! The way I’m doing it here moves the player up instantly during a jump, but it would be better if you made it smoothly move the player up, which could be done by using the update function, just play around with it! This is just supposed to get you thinking about how to do it.

PlatformerExample.wick (6.4 KB)

I tried again but It’s still not working.

Did you make the groundCheck object? I don’t see it underneath your player. If you did make it, can you show me the code on it?

That code wasn’t meant to be used, it was just to explain the concept.
It was my intention for more directions to be added as needed, this is based on movement from older games. (Like nes zelda.)

And wait, I don’t understand how you would still get speed boosted?
If you’re still on, is it alright for me to send you a test program that fleshes out what I
meant?

Sure no problem! I’m no expert so it’s totally possible I just did something goofy. I had the same problem when I was just trying to make a simple code like Luxapodular’s so it 100% could be something on my end! Feel free to demonstrate what you had in mind :slight_smile:

1 Like