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