PDA

View Full Version : Snake


Damix2
04-03-2009, 12:24 AM
A few things, this is very basic snake, nothing really fancy about it. I got bored so I made it. My GScript is very limited as you can see, thanks to Tig for answering a few questions along the way.

Since I am very new (this I would say, is probably the 5th thing I've ever really tried to do) I'd love for some constructive criticism.

I'm sure there a a million things wrong (but hey, at least it works!), or inefficient about what I did, and if you could point out 1, 2 or all of them I'd be very happy.

Some of my indenting got screwy when I copied and pasted, sorry.



//#CLIENTSIDE
function onWeaponFired() {
if (this.on == 0) {
setUpBoard();
placeCandy();
this.on = 1;
setTimer(0.1);
}
else {
this.on = 0;
}
}
function onTimeOut() {
if (this.on == 1) {
freezeplayer(1);
if (keydown(1)) {
this.dir = 1;
}
if (keydown(2)) {
this.dir = 2;
}
if (keydown(3)) {
this.dir = 3;
}
if (keydown(0)) {
this.dir = 4;
}
switch (this.dir) {
case 1: {
newCoord();
this.x1[0] = this.x1[0] - 10;
this.x2[0] = this.x1[0] + 10;
break;
}
case 2: {
newCoord();
this.y1[0] = this.y1[0] + 10;
this.y2[0] = this.y1[0] + 10;
break;
}
case 3: {
newCoord();
this.x1[0] = this.x1[0] + 10;
this.x2[0] = this.x1[0] + 10;
break;
}
case 4: {
newCoord();
this.y1[0] = this.y1[0] - 10;
this.y2[0] = this.y1[0] + 10;
break;
}
}
checkCandy();
doMovement();
setTimer(.1);
}
else {
setTimer(0);
hideimgs(200,1000);
freezeplayer(0);
}
}

function checkCandy() {
if ((this.x1[0] == this.candyx) && (this.y1[0] == this.candyy)) {
this.score++;
this.chain++;
showtext(299, this.startx+10,this.starty-20, "Arial", "c", this.score);
changeimgvis(299, 5);
placeCandy();
this.x1[this.chain] = this.x1[this.chain-1]-10;
}
}

function placeCandy() {
this.candyx = rand.randint()%50 * 10 + this.startx;
this.candyy = rand.randint()%30 * 10 + this.starty;
showpoly(203, {this.candyx,this.candyy,this.candyx,this.candyy+1 0,this.candyx+10,this.candyy+10,this.candyx+10,thi s.candyy});
changeimgvis(203, 6);
changeimgcolors(203,1,0,0,.99);
}

function setUpBoard() {
this.x1 = new[1000];
this.y1 = new[1000];
this.x2 = new[1000];
this.y2 = new[1000];
this.border = 4;
this.score = 0;
this.startx = screenwidth/3;
this.starty = screenheight/3;
showpoly(200, {this.startx-this.border,this.starty-this.border,this.startx-this.border,this.starty+300+this.border,this.start x+500+this.border,this.starty+300+this.border,this .startx+500+this.border,this.starty-this.border});
changeimgvis(200, 4);
changeimgcolors(200,0,0,0,.99);
showpoly(201, {this.startx,this.starty,this.startx,this.starty+3 00,this.startx+500,this.starty+300,this.startx+500 ,this.starty});
changeimgvis(201, 5);
changeimgcolors(201,1,1,1,.99);
showtext(299, this.startx+10,this.starty-20, "Arial", "c", this.score);
changeimgvis(299, 5);
this.x1[0] = screenwidth/3 + 250;
this.y1[0] = screenheight/3 + 150;
this.x2[0] = this.x1[0] + 10;
this.y2[0] = this.y1[0] + 10;
this.dir = 0;
this.chain = 1;
rand = new MRandomLCG();
}

function doMovement() {
if ((this.x1[0] >= this.startx) && (this.y1[0] >= this.starty) && (this.y2[0] <= this.starty+300) && (this.x2[0] <= this.startx+500)) {
for (i = 0; i < this.chain; i++) {
showpoly(500+i, {this.x1[i],this.y1[i],this.x1[i],this.y2[i],this.x2[i],this.y2[i],this.x2[i],this.y1[i]});
changeimgvis(500+i, 6);
changeimgcolors(500+i,0,0,0,.99);
}
}
else {
this.on = 0;
}
for (i = 1; i < this.chain; i++) {
if ((this.x1[0] == this.x1[i]) && (this.y1[0] == this.y1[i])) {
this.on = 0;
}
}
}

function newCoord() {
for (i = this.chain - 1; i > 0; i--) {
this.x1[i] = this.x1[i-1];
this.x2[i] = this.x2[i-1];
this.y1[i] = this.y1[i-1];
this.y2[i] = this.y2[i-1];
}
}

GULTHEX
04-03-2009, 09:38 PM
good job Damix

its realy good

fragman85
04-03-2009, 09:59 PM
I just slipped throught it, but here some small tips:

Instead of:

if(blabla == 0) you can use if (!blabla)

Oh and also, instead of:


if (keydown(1)) {
this.dir = 1;
}
if (keydown(2)) {
this.dir = 2;
}
if (keydown(3)) {
this.dir = 3;
}
if (keydown(0)) {
this.dir = 4;
}


you can use:


for (i=0; i<4; i++) {
if (keydown(i)) {
this.dir = i;
}
}

cbk1994
04-03-2009, 10:07 PM
Excellent!

xXziroXx
04-03-2009, 10:10 PM
Instead of doing...


this.on = 1;
this.on = 0;


... I'd suggest doing ...


this.on = true;
this.on = false;


It just looks cleaner to me.

Damix2
04-04-2009, 12:45 AM
I just slipped throught it, but here some small tips:

Instead of:

if(blabla == 0) you can use if (!blabla)

Oh and also, instead of:


if (keydown(1)) {
this.dir = 1;
}
if (keydown(2)) {
this.dir = 2;
}
if (keydown(3)) {
this.dir = 3;
}
if (keydown(0)) {
this.dir = 4;
}


you can use:


for (i=0; i<4; i++) {
if (keydown(i)) {
this.dir = i;
}
}


Ahh, thanks, I'll do that next time.

Instead of doing...


this.on = 1;
this.on = 0;


... I'd suggest doing ...


this.on = true;
this.on = false;


It just looks cleaner to me.

Guess that one is preference, I've always used 0 and 1s instead of booleans where I could. Though it is noted and I appreciate the reply.

xXziroXx
04-04-2009, 01:05 AM
Guess that one is preference, I've always used 0 and 1s instead of booleans where I could. Though it is noted and I appreciate the reply.

You're saying that you prefer using magic numbers instead of true booleans? Using true/false is a widely accepted convention in computer science.


Extraneous information:

There's no real reason why 0 is false and 1 is true. It just happened; that's why it's good practice to use true/false. Apart from better communicating your intention, Stefan might go mad and change the engine so that the number 7 is true and number 3 is false.

Chompy
04-04-2009, 01:13 AM
You're saying that you prefer using magic numbers instead of true booleans? Using true/false is a widely accepted convention in computer science.


Extraneous information:

There's no real reason why 0 is false and 1 is true. It just happened; that's why it's good practice to use true/false. Apart from better communicating your intention, Stefan might go mad and change the engine so that the number 7 is true and number 3 is false.

In graal, any value not equal to 0 is true :o?

xXziroXx
04-04-2009, 01:18 AM
In graal, any value not equal to 0 is true :o?

I never stated that.

LoneAngelIbesu
04-04-2009, 01:43 AM
There's no real reason why 0 is false and 1 is true.I always thought it had something to do with transistors being turned on and off. Using 0 to represent off makes sense. But, I agree with the syntax. I prefer using the words.

Chompy
04-04-2009, 02:00 AM
I never stated that.

I know, I felt like saying it. Damn, I hate being tired. >_<

In graal, there's no such thing as true booleans :(

Gambet
04-04-2009, 09:25 AM
Instead of doing...


this.on = 1;
this.on = 0;


... I'd suggest doing ...


this.on = true;
this.on = false;


It just looks cleaner to me.


Hmm...


this.on = !this.on;

The_Blue_Cracka
04-04-2009, 09:56 AM
You're saying that you prefer using magic numbers instead of true booleans? Using true/false is a widely accepted convention in computer science.


Extraneous information:

There's no real reason why 0 is false and 1 is true. It just happened; that's why it's good practice to use true/false. Apart from better communicating your intention, Stefan might go mad and change the engine so that the number 7 is true and number 3 is false.

In many cases 1 is true and 0 is false. More specifically, 1 is known as "on" and 0 is known as "off". Computers only recognize one main function, and that's whether or not there's electricity flowing. It converts this fast-paced data in form of binary which is the most basic step of humanizing the machine.

0 is 0
1 is 1
10 is 2
11 is 3
etc.

This information is then utilized accordingly to perform specific actions.

Damix2
04-04-2009, 10:25 AM
You're saying that you prefer using magic numbers instead of true booleans? Using true/false is a widely accepted convention in computer science.


Extraneous information:

There's no real reason why 0 is false and 1 is true. It just happened; that's why it's good practice to use true/false. Apart from better communicating your intention, Stefan might go mad and change the engine so that the number 7 is true and number 3 is false.


You are right, there is no reason why I couldn't say 1000 and 5000 are true and false instead, it is just easier for me to say 1 is on and 0 is off then it is to say true is on and false is off. That is the way I learned it. I will admit that true/false makes more sense then 1/0 (because if you are using an integer it could conceivably be set to not 1 or 0,) but what is familiar will always hold true to the programmer.