ForumsProgramming ForumNew Programmer

128 44383
Tookman
offline
Tookman
87 posts
Nomad

I am Tookman, a high school student that doesn't have much experience coding games, but I have experience with just general coding, can someone give me the coding to a simple game so that I can study it to understand more about game coding?

  • 128 Replies
Tookman
offline
Tookman
87 posts
Nomad

current (working) code:

package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard
public class Main extends Sprite
{
private var aDown:Boolean = false;
private var dDown:Boolean = false;
private var sDown:Boolean = false;
private var wDown:Boolean = false;
private var char : player;

private var gDown:Boolean = false;
private var jDown:Boolean = false;
private var hDown:Boolean = false;
private var yDown:Boolean = false;
private var par : shark;

private var bar : Back;

private var sar : Speed;

private var randomX:int = Math.floor(Math.random()*12)+1;

public function Main():void
{

bar = new Back();
bar.x = -300;
bar.y = 0;
addChild(bar);

sar = new Speed();
if(randomX == 1)
{
sar.x = -100;
sar.y = 400;
}
else if(randomX == 2)
{
sar.x = 275;
sar.y = 350;
}
else if(randomX == 3)
{
sar.x = 150;
sar.y = 200;
}
else if(randomX == 4)
{
sar.x = 700;
sar.y = 50
}
else if(randomX == 5)
{
sar.x = -50;
sar.y = 300;
}
else if(randomX == 6)
{
sar.x = 100;
sar.y = 400;
}
else if(randomX == 7)
{
sar.x = 750;
sar.y = 50;
}
else if(randomX == 8)
{
sar.x = 800;
sar.y = 400;
}
else if(randomX == 9)
{
sar.x = 350;
sar.y = 400;
}
else if(randomX == 10)
{
sar.x = 650;
sar.y = 450
}
else if(randomX == 11)
{
sar.x = 150;
sar.y = 500;
}
else if(randomX == 12)
{
sar.x = 700;
sar.y = 200;
}
addChild(sar);

char = new player();
char.x = 500;
char.y = 300;
addChild(char);

par = new shark();
par.x = 600;
par.y = 400;
addChild(par);

stage.addEventListener(Event.ENTER_FRAME, keys);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keysdown);
stage.addEventListener(KeyboardEvent.KEY_UP, keysup);
}

public function keys(e:Event):void
{
if (aDown == true)
char.x -= 10;
if (dDown == true)
char.x += 10;
if (sDown == true)
char.y += 10;
if (wDown == true)
char.y -= 10;

if (gDown == true)
par.x -= 10;
if (jDown == true)
par.x += 10;
if (hDown == true)
par.y += 10;
if (yDown == true)
par.y -= 10;
}
public function keysdown(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case Keyboard.A:
{
aDown = true;
break;
}
case Keyboard.D:
{
dDown = true;
break;
}
case Keyboard.S:
{
sDown = true;
break;
}
case Keyboard.W:
{
wDown = true;
break;
}

case Keyboard.G:
{
gDown = true;
break;
}
case Keyboard.J:
{
jDown = true;
break;
}
case Keyboard.H:
{
hDown = true;
break;
}
case Keyboard.Y:
{
yDown = true;
break;
}
}
}
public function keysup(e:KeyboardEvent):void
{
switch (e.keyCode)

{

case Keyboard.A:
{
aDown = false;
break;
}
case Keyboard.D:
{
dDown = false;
break;
}
case Keyboard.S:
{
sDown = false;
break;
}
case Keyboard.W:
{
wDown = false;
break;
}

case Keyboard.G:
{
gDown = false;
break;
}
case Keyboard.J:
{
jDown = false;
break;
}

case Keyboard.H:
{
hDown = false;
break;
}
case Keyboard.Y:
{
yDown = false;
break;
}
}
}
}
}

now I need three more things before my code is complete;
1) The speed boost to add speed that goes away in 5 seconds to Bob when he comes near, causing the speed boost to respawn and the score to go up one.
2) When the shark comes near, I want it to display "game over" and the recently earned score as well as the best score scored on the console.
3) I also need to use the blitting tool to make the image I created for the speed block have a strobe effect.

Tookman
offline
Tookman
87 posts
Nomad

Tookman
offline
Tookman
87 posts
Nomad

... Um how do you link a picture?

Tookman
offline
Tookman
87 posts
Nomad

Oh yeah, instead of the shark being controlled, I need him to always be traveling in the direction of Bob

weirdlike
offline
weirdlike
1,299 posts
Prince

Are you using flash or flashDevelop?

Tookman
offline
Tookman
87 posts
Nomad

Yes indeed.

weirdlike
offline
weirdlike
1,299 posts
Prince

Hey no problem...

Is your img in a folder? Or is it there with your program files?

Tookman
offline
Tookman
87 posts
Nomad

Oh I mean to this website not to flash

weirdlike
offline
weirdlike
1,299 posts
Prince

:O

I don't know what you mean.

Tookman
offline
Tookman
87 posts
Nomad

how do you put a picture on armorgames?

weirdlike
offline
weirdlike
1,299 posts
Prince

one way, is to take your img and upload it to a hosting site. imgur is a good site to use, among the many.

Once your img is uploaded to the img hosting site, you then take the link that is associated with the img and paste it here using the img button.

Tookman
offline
Tookman
87 posts
Nomad

k thanks. Now will you help me make the speed boost actually boost the speed?

weirdlike
offline
weirdlike
1,299 posts
Prince

you will need to create a variable containing the speed I am assuming you will want it be speed boost for a short amount of time then you will need to create another variable which will be the duration of the speed and after it counts down the speed will be back to normal.

in your loop it will be changed as so

then when in your collision add

in your loop

var speed:int = 10;
var speedDuration:int = 30;
Tookman
offline
Tookman
87 posts
Nomad

Okay, now how do I add in the collision, I've done
[quote]var speed:int = 10;
var speedDuration:int = 30;

in your loop it will be changed as so

char.x -= speed;[quote]

weirdlike
offline
weirdlike
1,299 posts
Prince

If you haven't added the collision then changing the speed is irrelevant.

You can take a look at part 3 of my tutorial at the very bottom is the collision. if it is too confusing then you can try googling as3 hitTestObject or read up on adobe's display object on the method. Once you get the collision to register then you can add whatever it is that you are trying to achieve, in this case it is speed boost.

Showing 91-105 of 128