Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Snake (https://forums.graalonline.com/forums/showthread.php?t=84972)

Damix2 04-02-2009 11:24 PM

Snake
 
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.


PHP Code:

//#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(299this.startx+10,this.starty-20"Arial""c"this.score);
       
changeimgvis(2995);    
       
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+10,this.candyx+10,this.candyy+10,this.candyx+10,this.candyy});
    
changeimgvis(2036);
    
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.startx+500+this.border,this.starty+300+this.border,this.startx+500+this.border,this.starty-this.border});
    
changeimgvis(2004);
    
changeimgcolors(200,0,0,0,.99);
    
showpoly(201, {this.startx,this.starty,this.startx,this.starty+300,this.startx+500,this.starty+300,this.startx+500,this.starty});
    
changeimgvis(2015);
    
changeimgcolors(201,1,1,1,.99);
    
showtext(299this.startx+10,this.starty-20"Arial""c"this.score);
      
changeimgvis(2995);      
    
this.x1[0] = screenwidth/250;
    
this.y1[0] = screenheight/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 (
0this.chaini++) {        
        
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+i6);
        
changeimgcolors(500+i,0,0,0,.99);        
      }             
    }
    else {
      
this.on 0;
    }
    for (
1this.chaini++) {
      if ((
this.x1[0] == this.x1[i]) && (this.y1[0] == this.y1[i])) {
        
this.on 0;
      }  
    }
  }
  
  function 
newCoord() {
    for (
this.chain 10i--) {
      
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 08:38 PM

good job Damix

its realy good

fragman85 04-03-2009 08: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:

PHP Code:

      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:

PHP Code:

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



cbk1994 04-03-2009 09:07 PM

Excellent!

xXziroXx 04-03-2009 09:10 PM

Instead of doing...

PHP Code:

this.on 1
this.on 0

... I'd suggest doing ...

PHP Code:

this.on true
this.on false

It just looks cleaner to me.

Damix2 04-03-2009 11:45 PM

Quote:

Originally Posted by fragman85 (Post 1480709)
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:

PHP Code:

      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:

PHP Code:

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



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

Quote:

Originally Posted by xXziroXx (Post 1480713)
Instead of doing...

PHP Code:

this.on 1
this.on 0

... I'd suggest doing ...

PHP Code:

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 12:05 AM

Quote:

Originally Posted by Damix2 (Post 1480765)
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 12:13 AM

Quote:

Originally Posted by xXziroXx (Post 1480769)
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 12:18 AM

Quote:

Originally Posted by Chompy (Post 1480771)
In graal, any value not equal to 0 is true :o?

I never stated that.

LoneAngelIbesu 04-04-2009 12:43 AM

Quote:

Originally Posted by xXziroXx (Post 1480769)
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 01:00 AM

Quote:

Originally Posted by xXziroXx (Post 1480772)
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 08:25 AM

Quote:

Originally Posted by xXziroXx (Post 1480713)
Instead of doing...

PHP Code:

this.on 1
this.on 0

... I'd suggest doing ...

PHP Code:

this.on true
this.on false

It just looks cleaner to me.


Hmm...

PHP Code:

this.on = !this.on


The_Blue_Cracka 04-04-2009 08:56 AM

Quote:

Originally Posted by xXziroXx (Post 1480769)
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 09:25 AM

Quote:

Originally Posted by xXziroXx (Post 1480769)
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.


All times are GMT +2. The time now is 01:20 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.