Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 04-02-2009, 11:24 PM
Damix2 Damix2 is offline
RED SQUARE CLUB
Join Date: Nov 2003
Location: NY-what's better?
Posts: 3,577
Damix2 will become famous soon enough
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];
    }
  } 
__________________
Reply With Quote
  #2  
Old 04-03-2009, 08:38 PM
GULTHEX GULTHEX is offline
Registered User
Join Date: Jul 2008
Posts: 148
GULTHEX can only hope to improve
good job Damix

its realy good
Reply With Quote
  #3  
Old 04-03-2009, 08:59 PM
fragman85 fragman85 is offline
Banned
Join Date: Mar 2009
Location: Switzerland
Posts: 261
fragman85 is on a distinguished road
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;
  }

Reply With Quote
  #4  
Old 04-03-2009, 09:07 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Excellent!
__________________
Reply With Quote
  #5  
Old 04-03-2009, 09:10 PM
xXziroXx xXziroXx is offline
Malorian
xXziroXx's Avatar
Join Date: May 2004
Posts: 5,289
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
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.
__________________
Follow my work on social media post-Graal:Updated august 2025.
Reply With Quote
  #6  
Old 04-03-2009, 11:45 PM
Damix2 Damix2 is offline
RED SQUARE CLUB
Join Date: Nov 2003
Location: NY-what's better?
Posts: 3,577
Damix2 will become famous soon enough
Quote:
Originally Posted by fragman85 View Post
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 View Post
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.
__________________
Reply With Quote
  #7  
Old 04-04-2009, 12:05 AM
xXziroXx xXziroXx is offline
Malorian
xXziroXx's Avatar
Join Date: May 2004
Posts: 5,289
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Quote:
Originally Posted by Damix2 View Post
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.
__________________
Follow my work on social media post-Graal:Updated august 2025.
Reply With Quote
  #8  
Old 04-04-2009, 12:13 AM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Quote:
Originally Posted by xXziroXx View Post
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?
__________________
Reply With Quote
  #9  
Old 04-04-2009, 12:18 AM
xXziroXx xXziroXx is offline
Malorian
xXziroXx's Avatar
Join Date: May 2004
Posts: 5,289
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Quote:
Originally Posted by Chompy View Post
In graal, any value not equal to 0 is true :o?
I never stated that.
__________________
Follow my work on social media post-Graal:Updated august 2025.
Reply With Quote
  #10  
Old 04-04-2009, 12:43 AM
LoneAngelIbesu LoneAngelIbesu is offline
master of infinite loops
LoneAngelIbesu's Avatar
Join Date: May 2007
Location: Toldeo, Ohio
Posts: 1,049
LoneAngelIbesu has a spectacular aura aboutLoneAngelIbesu has a spectacular aura about
Send a message via AIM to LoneAngelIbesu
Quote:
Originally Posted by xXziroXx View Post
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.
__________________
"We are all in the gutter, but some of us are looking at the stars."
— Oscar Wilde, Lady Windermere's Fan
Reply With Quote
  #11  
Old 04-04-2009, 01:00 AM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Quote:
Originally Posted by xXziroXx View Post
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
__________________
Reply With Quote
  #12  
Old 04-04-2009, 08:25 AM
Gambet Gambet is offline
Registered User
Join Date: Oct 2003
Posts: 2,712
Gambet is on a distinguished road
Quote:
Originally Posted by xXziroXx View Post
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
Reply With Quote
  #13  
Old 04-04-2009, 08:56 AM
The_Blue_Cracka The_Blue_Cracka is offline
Blonk
The_Blue_Cracka's Avatar
Join Date: Nov 2004
Location: Pennsylvania
Posts: 147
The_Blue_Cracka is on a distinguished road
Send a message via AIM to The_Blue_Cracka
Quote:
Originally Posted by xXziroXx View Post
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.
__________________
2 kewl 4 an sigantrur!1
Reply With Quote
  #14  
Old 04-04-2009, 09:25 AM
Damix2 Damix2 is offline
RED SQUARE CLUB
Join Date: Nov 2003
Location: NY-what's better?
Posts: 3,577
Damix2 will become famous soon enough
Quote:
Originally Posted by xXziroXx View Post
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.
__________________
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 05:10 AM.


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