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 › Mazegame, enemys, different controls, pls help
Page Index Toggle Pages: 1
Mazegame, enemys, different controls, pls help (Read 504 times)
Mazegame, enemys, different controls, pls help
Feb 10th, 2010, 6:13am
 
Hello,
i am programming a simple mazegame at the moment and nearly got it.
now i need a little help, i already got 1 enemy walking randomly threw the labyrinth, but i want to have 3 with different starting positions.
beyond that i programmed 1 version with key control and one with sudden-motion-sensor control.
so i want to get it into one, switching from key control to suddenmotion with a key, for example i press s than suddenmotion is activated and keys  are off and i press k so keys are on again and suddenmotion is of,
i think i could do it with boolean but i am not sure how.

First the enemy thin :
Here the storylines, i have to lvls., i need to put in 2 more enemys in storyline 2 and 5. :

void draw() {
if(storyline==1){////////////ANFANG(PROLOG)
image(menu, 0, 0);
 if (keyCode =='1') {
 storyline=2;
 println("hier1");
   }
     if (keyCode =='2') {
 storyline=5;
 println("hier2");
   }
}
if(storyline==2){////////////LVL(SPIEL)
 // background(0);
 image(level, 0, 0);
 
 fill(255, 0, 0);
 noStroke();
    rectangleX += (x-rectangleX) * 0.2;
 rectangleY += (y-rectangleY) * 0.2;
 rect(rectangleX, rectangleY, 10, 10);

 enemy();
 if(dist(x, y, posxe, posye)< 10){ //hitdetection
  x = 50;
 y = 750;
 
 

 }
}
if(storyline==3){///////////endVictory
image(victory, 0, 0);
 if (keyCode == '8') {
background(0);
   storyline=1;
   x = 50;
 y = 750;
 
   }
}

if(storyline==4){///////////GameOver
image(end, 0, 0);
if(keyCode == '9'){
 storyline=1;
}
}

if(storyline==5){
image(level2, 0, 0);
fill(255, 0, 0);
 noStroke();
   rectangleX += (x-rectangleX) * 0.1;
 rectangleY += (y-rectangleY) * 0.1;
 rect(rectangleX, rectangleY, 10, 10);

 enemy();
 if(dist(x, y, posxe, posye)< 10){ // hitdetection
storyline=4;
  x = 50;
 y = 750;
 

}
}

}


::::: This is the enemy function :::::

void enemy(){


   if (ye==-4) {

     geschwindigkeit = geschwindigkeit * -1;

   }

   if (ye==+4) {

     geschwindigkeit = geschwindigkeit * -1;

   }
 
PImage levelCheck = new PImage();
 if(storyline == 2) {
   levelCheck = level;
 } else if(storyline == 5) {
   levelCheck = level2;
 }
   color hoche = levelCheck.get (posxe, posye-1*speede);
   color runtere = levelCheck.get (posxe, posye +1*speede);
   color rechtse = levelCheck.get (posxe+1*speede, posye);
   color linkse = levelCheck.get (posxe-1*speede, posye);
   //println("hoch: " + hoche +"runter: " + runtere +"links: " + linkse +"rechts: " + rechtse );
   //println(richtung);


   if( hoche>=-2.0 && richtung==1){       //Bewegung nur wenn Ziel weiße farbe ist (-10 ist mit toleranz -1 würde eigtl auch reichen aber sicher ist sicher
     posye-=1 *speede;    

   }
   else if(runtere>=-2.0&& richtung==2){
     posye+=1 *speede;

   }
   else if (linkse>=-2.0 && richtung==3){
     posxe-=1 *speede;

   }
   else if (rechtse>=-2.0 && richtung==4){
     posxe+=1 *speede;

   }  
   else {
     re = random(1,4);
     richtung = round(re);

   }
   fill(0,0,255);
   ellipse(posxe,posye,12,12);  
   
    //image(schirm, x-2400, y-1600);

 }

now the control problem :

this is for key control :

void keyPressed() {
 
 /////Weganalyse////////////////////////
 PImage levelCheck = new PImage();
 if(storyline == 2) {
   levelCheck = level;
 } else if(storyline == 5) {
   levelCheck = level2;
 }
 color hoch = levelCheck.get (x, y-1*speed);
 color runter = levelCheck.get (x, y +1*speed);
 color rechts = levelCheck.get (x+1*speed, y);
 color links = levelCheck.get (x-1*speed, y);//jeweils um zu sehen ob der nächste Schritt ins weiße geht oder nicht
 color pos = levelCheck.get (x,y);
 //println(round(pos));
 // println("rechts"+round(rechts));
 //  println("hoch"+round(hoch));
 

 if ( links >=-2.0 && keyCode == LEFT) {
  x-=1*speed;
   }

 if ( rechts >=-2.0 && keyCode == RIGHT) {
  x+=1*speed;
   }
     if ( hoch >=-2.0 && keyCode == UP) {
  y-=1*speed;
   }
     if ( runter >=-2.0 && keyCode == DOWN) {
  y+=1*speed;

   }
   
if(x> 1100 && y < 80){
storyline=3;
}    
}

this is suddenmotion :
its in the storyline :

if(storyline==2){////////////LVL(SPIEL)
   // background(0);
   ////weganalyse//
   //int[] vals = Unimotion.getSMSArray();
   speedx = int(-1 * (Unimotion.getSMSY() / 270.0) * 10);
   speedy = int(Unimotion.getSMSX() / 270.0 * 10);
   
   println(speedx + " " + speedy);



pls help, i can post the whole code if its more helpful, but at the moment i think its better to get it this way because the code is quite long jet Smiley

Re: Mazegame, enemys, different controls, pls help
Reply #1 - Feb 10th, 2010, 8:14am
 
Post the whole code as it appears in your processing window.
That way we can copy and paste and run it easily on our own machines.
Re: Mazegame, enemys, different controls, pls help
Reply #2 - Feb 10th, 2010, 8:58am
 
ok :

KeyControl :

int x;
int y;
float rectangleX;
float rectangleY;
int s;
int half_size;
int speed;
int storyline=1;

PImage level;
PImage level2;
PImage schirm;
PImage victory;
PImage end;
PImage menu;

////////////////////enemy

 float xe=0;

 float ye=0;
 float yye=0;



 int posxe=322, posye=120;      

 int speede=2;
 int richtung =1;

 float re;

 float geschwindigkeit = -1;
 float geschwindigkeit2 = 1;

boolean[][] collisionMap;

void setup() {
 
 size(1200, 800);
frameRate(25);
smooth();
 background(0);
 rectMode(CENTER);
   schirm = loadImage ("schirm.png");

 x = 50;
 y = 750;
 
   rectangleX = 50;
 rectangleY = 750;

 s = 10;
 half_size = s / 2;
 speed = 12;
 
 menu = loadImage("Menu.gif");
 level = loadImage("level.gif");
   level2 = loadImage("level2.gif");
   victory = loadImage("endv.gif");
       end = loadImage("end.gif");
 PImage colMapImage = loadImage("collisionMap.gif");
 collisionMap = new boolean[colMapImage.width][colMapImage.height];
 
 color black = color(0);
 color white = color(255);
 

 for (int i = 0; i < colMapImage.width; i++) {
   for (int j = 0; j < colMapImage.height; j++) {
     
     color c = colMapImage.get(i, j);
     if (c == black) {
       collisionMap[i][j] = false;
     }

     else if (c == white) {
       collisionMap[i][j] = true;
     }
     else {
       println("Whoops! We shouldn't have colors other than black and white in our collision map!");
     }

   }
 }
}
////////////////////////////////////////////////////////////////////////////////

void draw() {
if(storyline==1){////////////ANFANG(PROLOG)
image(menu, 0, 0);
 if (keyCode =='1') {
 storyline=2;
 println("hier1");
   }
     if (keyCode =='2') {
 storyline=5;
 println("hier2");
   }
}
if(storyline==2){////////////LVL(SPIEL)
 // background(0);
 image(level, 0, 0);
 
 fill(255, 0, 0);
 noStroke();
    rectangleX += (x-rectangleX) * 0.2;
 rectangleY += (y-rectangleY) * 0.2;
 rect(rectangleX, rectangleY, 10, 10);

 enemy();
 if(dist(x, y, posxe, posye)< 10){ //hitdetection
  x = 50;
 y = 750;
 
 

 }
}
if(storyline==3){///////////endVictory
image(victory, 0, 0);
 if (keyCode == '8') {
background(0);
   storyline=1;
   x = 50;
 y = 750;
 
   }
}

if(storyline==4){///////////GameOver
image(end, 0, 0);
if(keyCode == '9'){
 storyline=1;
}
}

if(storyline==5){
image(level2, 0, 0);
fill(255, 0, 0);
 noStroke();
   rectangleX += (x-rectangleX) * 0.1;
 rectangleY += (y-rectangleY) * 0.1;
 rect(rectangleX, rectangleY, 10, 10);

 enemy();
 if(dist(x, y, posxe, posye)< 10){ // hitdetection
storyline=4;
  x = 50;
 y = 750;
 
 
 

}
}

}

void keyPressed() {
 
 /////Weganalyse////////////////////////
 PImage levelCheck = new PImage();
 if(storyline == 2) {
   levelCheck = level;
 } else if(storyline == 5) {
   levelCheck = level2;
 }
 color hoch = levelCheck.get (x, y-1*speed);
 color runter = levelCheck.get (x, y +1*speed);
 color rechts = levelCheck.get (x+1*speed, y);
 color links = levelCheck.get (x-1*speed, y);//jeweils um zu sehen ob der nächste Schritt ins weiße geht oder nicht
 color pos = levelCheck.get (x,y);
 //println(round(pos));
 // println("rechts"+round(rechts));
 //  println("hoch"+round(hoch));
 

 if ( links >=-2.0 && keyCode == LEFT) {
  x-=1*speed;
   }

 if ( rechts >=-2.0 && keyCode == RIGHT) {
  x+=1*speed;
   }
     if ( hoch >=-2.0 && keyCode == UP) {
  y-=1*speed;
   }
     if ( runter >=-2.0 && keyCode == DOWN) {
  y+=1*speed;

   }
   
if(x> 1100 && y < 80){
storyline=3;
}    
}

///////Gegner
void enemy(){


   if (ye==-4) {

     geschwindigkeit = geschwindigkeit * -1;

   }

   if (ye==+4) {

     geschwindigkeit = geschwindigkeit * -1;

   }
 
PImage levelCheck = new PImage();
 if(storyline == 2) {
   levelCheck = level;
 } else if(storyline == 5) {
   levelCheck = level2;
 }
   color hoche = levelCheck.get (posxe, posye-1*speede);
   color runtere = levelCheck.get (posxe, posye +1*speede);
   color rechtse = levelCheck.get (posxe+1*speede, posye);
   color linkse = levelCheck.get (posxe-1*speede, posye);
   //println("hoch: " + hoche +"runter: " + runtere +"links: " + linkse +"rechts: " + rechtse );
   //println(richtung);


   if( hoche>=-2.0 && richtung==1){       //Bewegung nur wenn Ziel weiße farbe ist (-10 ist mit toleranz -1 würde eigtl auch reichen aber sicher ist sicher
     posye-=1 *speede;    

   }
   else if(runtere>=-2.0&& richtung==2){
     posye+=1 *speede;

   }
   else if (linkse>=-2.0 && richtung==3){
     posxe-=1 *speede;

   }
   else if (rechtse>=-2.0 && richtung==4){
     posxe+=1 *speede;

   }  
   else {
     re = random(1,4);
     richtung = round(re);

   }
   fill(0,0,255);
   ellipse(posxe,posye,12,12);  
   
    image(schirm, x-2400, y-1600);

 }
Re: Mazegame, enemys, different controls, pls help
Reply #3 - Feb 10th, 2010, 9:01am
 
suddenmotion control

int x;
int y;
//float rectangleX;
//float rectangleY;
int s;
int half_size;
int speed;
int speedx;
int speedy;
int storyline=1;

PImage level;
PImage schirm;
PImage victory;
PImage end;
import sms.*;
////////////////////enemy

float xe=0;

float ye=0;
float yye=0;



int posxe=322, posye=120;      

int speede=2;
int richtung =1;  // Diameter of rect

float re;

float geschwindigkeit = -1;
float geschwindigkeit2 = 1;

boolean[][] collisionMap;

void setup() {

 size(1200, 800);
 frameRate(25);
 smooth();
 background(0);
 rectMode(CENTER);
 schirm = loadImage ("schirm.png");

 x = 50;
 y = 750;

// rectangleX = 50;
// rectangleY = 750;

 s = 10;
 half_size = s / 2;
 speed = 12;
 speedx = 0;
 speedy = 0;


 level = loadImage("level.gif");
 victory = loadImage("endv.gif");
 end = loadImage("end.gif");
 PImage colMapImage = loadImage("collisionMap.gif");
 collisionMap = new boolean[colMapImage.width][colMapImage.height];

 color black = color(0);
 color white = color(255);


 for (int i = 0; i < colMapImage.width; i++) {
   for (int j = 0; j < colMapImage.height; j++) {

     color c = colMapImage.get(i, j);
     if (c == black) {
       collisionMap[i][j] = false;
     }

     else if (c == white) {
       collisionMap[i][j] = true;
     }
     else {
       println("Whoops! We shouldn't have colors other than black and white in our collision map!");
     }

   }
 }
}
////////////////////////////////////////////////////////////////////////////////



void draw() {
 if(storyline==1){////////////ANFANG(PROLOG)
   //image(victory, 0, 0);
   if (keyCode =='2') {
     storyline=2;
     println("hier");
   }
 }
 if(storyline==2){////////////LVL(SPIEL)
   // background(0);
   ////weganalyse//
   //int[] vals = Unimotion.getSMSArray();
   speedx = int(-1 * (Unimotion.getSMSY() / 270.0) * 10);
   speedy = int(Unimotion.getSMSX() / 270.0 * 10);
   
   println(speedx + " " + speedy);

   image(level, 0, 0);

   color vertikal = level.get (x, y-1*speedy);
   // color runter = level.get (x, y +1*speedy);
   color seitlich = level.get (x-1*speedx, y);
   // color links = level.get (x-1*speedx, y);//jeweils um zu sehen ob der nächste Schritt ins weiße geht oder nicht
   color pos = level.get (x,y);
fill(0, 0, 255);
rect(x+1*speedx, y, 10, 10);
   //println(x+1*speedx);

   if ( seitlich >=-2.0) {
     x-=1*speedx;
   }
   /*
 if ( rechts >=-2.0) {
    x+=1*speedx;
    }
    */
   if ( vertikal >=-2.0) {
     y-=1*speedy;
   }
   /*if ( runter >=-2.0) {
    y+=1*speedy;
    }
    */
   ///////////

   //x = x+ vals[0];
   fill(255, 0, 0);
   noStroke();

   //rectangleX += (x-rectangleX) * 0.2;
   //rectangleY += (y-rectangleY) * 0.2;

   rect(x, y, 10, 10);

   enemy();
   if(dist(x, y, posxe, posye)< 10){ // berührung gegner
     storyline=4;
     x = 50;
     y = 750;
   }
 }
 if(storyline==3){///////////endVictory
   image(victory, 0, 0);
   if (keyCode == '1') {
     background(0);
     storyline=1;
     x = 50;
     y = 750;

   }
 }

 if(storyline==4){///////////GameOver
   image(end, 0, 0);
   if(keyCode == '1'){
     storyline=1;
   }
 }
}


///////Gegner
void enemy(){


 if (ye==-4) {

   geschwindigkeit = geschwindigkeit * -1;

 }

 if (ye==+4) {

   geschwindigkeit = geschwindigkeit * -1;

 }


 color hoche = level.get (posxe, posye-1*speede);
 color runtere = level.get (posxe, posye +1*speede);
 color rechtse = level.get (posxe+1*speede, posye);
 color linkse = level.get (posxe-1*speede, posye);
 //println("hoch: " + hoche +"runter: " + runtere +"links: " + linkse +"rechts: " + rechtse );
 //println(richtung);


 if( hoche>=-2.0 && richtung==1){       //Bewegung nur wenn Ziel weiße farbe ist (-10 ist mit toleranz -1 würde eigtl auch reichen aber sicher ist sicher
   posye-=1 *speede;    

 }
 else if(runtere>=-2.0&& richtung==2){
   posye+=1 *speede;

 }
 else if (linkse>=-2.0 && richtung==3){
   posxe-=1 *speede;

 }
 else if (rechtse>=-2.0 && richtung==4){
   posxe+=1 *speede;

 }  
 else {
   re = random(1,4);
   richtung = round(re);

 }
 fill(200,0,0);
 ellipse(posxe,posye,12,12);  

  image(schirm, x-2400, y-1600);

}


As i said i want suddenmotion and keycontrol in one program, and i want to have several enemys starting at different positions, thats all.

level and level 2 are black and white maze images where you can go on white color, and cant on black. schirm is a big black screen with a hole at the heros position, so that the player sees just a part of the maze.
Page Index Toggle Pages: 1