We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › tron style game help
Page Index Toggle Pages: 1
tron style game help (Read 697 times)
tron style game help
Nov 3rd, 2005, 6:31pm
 
Hi, still fairly new to processing or any code for that matter. im trying to eventually recreate the tron game (two motorcycles with a wall that forms behind them). as of now though i cant even seem to really get the "wall" to work. so far i have this:

<code>
int speed = 1;
int lines = 10000;
Move [] move;
float[] ax = new float[lines];
float [] ay = new float[lines];

void setup(){
 size(400, 400);
 framerate(30);
 
 move = new Move[2];
 for(int i=0; i<move.length; i++){
   move[i] = new Move(0, 0, 0, 0, width/2, height/2, width/2, height/2);
   }
 for(int i=0; i<lines; i++){
   ax[i] = width/2;
   ay[i] = height/2;
 }    
     
}

void draw (){
 background(100);
 
   for(int i=0; i<move.length; i++){
     move[i].keys();    
     move[i].update();
   }

 
 for(int i=0; i<move.length; i++){
   move[i].make();    
 }  
}

class Move{
 float moveleft, moveright, moveup, movedown, xpos, ypos, oldx, oldy;
 
 Move(float moveleft, float moveright, float moveup, float movedown, float xpos, float ypos, float oldx, float oldy){
   this.moveleft = moveleft;
   this.moveright = moveright;
   this.moveup = moveup;
   this.movedown = movedown;
   this.xpos = xpos;
   this.ypos = ypos;
   this.oldx = oldx;
   this.oldy = oldy;
   }  
       
 void make(){
   for(int i=1; i<move.length; i++){
       oldx = xpos;
       oldy = ypos;
     stroke(255);
     point(xpos, ypos);
   }  
 }
   
 void update(){
   xpos = xpos + moveright - moveleft;
   ypos = ypos - moveup + movedown;
   ax[lines-1] = oldx;
   ay[lines-1] = oldy;
   }
   
 void keys(){
   for(int i=0; i<move.length; i++){
   if(key == 'w' || key == 'W' || keyCode == UP){
       moveup = speed;
       movedown = 0;
       moveleft = 0;
       moveright = 0;
       //println("W was pressed");
       }
   if(key == 's' || key == 'S' || keyCode == DOWN){
       moveup = 0;
       movedown = speed;
       moveleft = 0;
       moveright = 0;
       }
     if(key == 'a' || key == 'A' || keyCode == LEFT){
       moveup = 0;
       movedown = 0;
       moveleft = speed;
       moveright = 0;
       }
     if(key == 'd' || key == 'D' || keyCode == RIGHT){
       moveup = 0;
       movedown = 0;
       moveleft = 0;
       moveright = speed;
       }
   }
     
   for(int i=1; i<lines; i++){
     //if(keyPressed == true){
     // ax[i] = ax[i+1];
     // ay[i] = ay[i+1];
     //}
    line(ax[i-1], ay[i-1], ax[i], ay[i]);  
   }                
 }                                                              
}
</code>

i get a line from the original starting point and where the point is then moved to. ive been workin on this far too long and am quite frustrated because this seems to be the only result i can get and i have completly confused myself through the process. so sorry if the code is a little longwinded and excessive(which im sure it is). any help would be great. thanks

randy
Re: tron style game help
Reply #1 - Nov 3rd, 2005, 7:29pm
 
I've made snake for mobiles and a tron based image filter with Processing, so this is an old favourite of mine.

What you need is something to store the state of the playing field. The wall you should draw by not updating the area where the bikes are. You can still have stats and stuff by just painting a black rect over that one area. The array which has the ground in will tell you when bikes are in danger of crashing.

You also need a great little function called switch(). It's faster than a lot of if statements in a row because the compiler builds a table. It also helps you think of your program as a state machine, each case is a different state. Which will help when you have your title screen and the game playing mode.

Here's what I getting at in code:
Code:

Cycle cycle;
boolean [] ground;
int wide = 400;
int high = 400;
void setup(){
size(400,400);
ground = new boolean[wide*high];
cycle = new Cycle(200,200);
background(0);
}
void draw(){
cycle.update();
cycle.draw();
//turbo!
if(keyPressed && key == ' '){
cycle.update();
cycle.draw();
}
}
void keyPressed(){
switch(key){
case 'd':
case 'D':
cycle.d = 0;
break;
case 's':
case 'S':
cycle.d = 1;
break;
case 'a':
case 'A':
cycle.d = 2;
break;
case 'w':
case 'W':
cycle.d = 3;
break;
}
}
class Cycle{
int x,y,d;
boolean alive;
Cycle(int x, int y){
this.x = x;
this.y = y;
d = 0;
alive = true;
}
void update(){
if(alive){
switch(d){
case 0:
x++;
break;
case 1:
y++;
break;
case 2:
x--;
break;
case 3:
y--;
break;
}
}
//has he crashed?
if(x < wide && x > -1 && y < high && y > -1){
if(ground[x + y * wide]){
alive = false;
println("crash1");
}
}
else{
alive = false;
println("crash2");
}
if(alive)
ground[x + y * width] = true;
}
void draw(){
stroke(0,230,230);
point(x,y);
}
}
Page Index Toggle Pages: 1