Help with reset a int[] and multiple screens

#1

Hey, guys i got an question about my code i want to reset the int [] hinder so that the objects arre resettet when i make back=true. Is it possible to make processing by my android things multiple screens because on my phone is it perfect but on my tablet not.

Thanks.

PImage Hintergrund, figur1, figur2, figur3, Rakete, GameOverScreen, Gewonnen, Gewonnen2, arrow;
int hg;
boolean starten, knopf, figur1_ak, figur2_ak, figur3_ak;
int [] hinder;
int [] hinderspeed;
int [] speed;
boolean [] sichtbar;
int springen;
int figurx;
int figury;
int SprSpeed;
boolean springt = false;
int counter;
int speedcounter;
boolean einstellungen;
boolean GameOver;
boolean spiel_laeuft;
boolean gewonnen;
boolean back;
int [] timer;

int timer1;

void setup()
{
size(displayWidth, displayHeight);
orientation(LANDSCAPE);
//size(1600,800);
hinder = new int[100];
sichtbar = new boolean [100];
hinderspeed = new int [30];
timer= new int[30];
speed = new int [30];

figurx = 95;
figury = 800;
speedcounter=0;
timer1=0;
counter=0;
hg=0;

figur1=loadImage(“mario.png”);
figur2=loadImage(“figur2.png”);
figur3=loadImage(“figur3.png”);
Rakete=loadImage(“rakete.png”);
GameOverScreen=loadImage(“goscreen.jpg”);
arrow=loadImage(“arrow.png”);
Hintergrund = loadImage(“hintergrund.png”);

spiel_laeuft=false;
einstellungen=false;
GameOver=false;
gewonnen=false;
knopf=false;
starten=false;
figur1_ak=true;
figur2_ak=false;
figur3_ak=false;
back=false;
for (int i=0; i<20; i=i+1)
{
hinder[i]=2000;
hinderspeed[i]=0;
sichtbar[i]=false;
}

speed[0]= 17;
speed[1]= 14;
speed[2]= 25;
speed[3]= 20;
speed[4]= 35;
speed[5]= 35;
speed[6]= 30;
speed[7]= 25;
speed[8]= 20;
speed[9]= 35;
speed[10]= 30;
speed[11]= 20;
speed[12]= 35;
speed[13]= 20;
speed[14]= 45;
speed[15]= 40;
speed[16]= 34;
speed[17]= 22;
speed[18]= 25;
speed[19]= 35;
speed[20]= 35;
speed[21]= 43;
speed[22]= 31;
speed[23]= 25;
speed[24]= 33;
speed[25]= 35;
speed[26]= 35;
speed[27]= 29;
speed[28]= 15;
speed[29]= 3;

timer[0]=10;
timer[1]=25;
timer[2]=40;
timer[3]=35;
timer[4]=35;
timer[5]=35;
timer[6]=35;
timer[7]=25;
timer[8]=20;
timer[9]=30;
timer[10]=25;
timer[11]=15;
timer[12]=25;
timer[13]=10;
timer[14]=40;
timer[15]=20;
timer[16]=25;
timer[17]=30;
timer[18]=40;
timer[19]=50;
timer[20]=10;
timer[21]=20;
timer[22]=25;
timer[23]=10;
timer[24]=30;
timer[25]=10;
timer[26]=40;
timer[27]=50;
timer[28]=20;
timer[29]=50;
}

void draw()
{

background(250);
fill(250, 0, 0);
rect(800, 200, 400, 250);

if (starten==false);
{
fill(0, 250, 0);
textSize(35);
text(“Klicke zum Starten!”, 835, 330);
fill(250, 0, 250);
textSize(20);
text(“created by Nick and Marvin”, 10, 1070);
fill(0, 0, 250);
rect(800, 600, 400, 100);
textSize(40);
fill(250, 0, 0);
text(“Einstellungen”, 870, 660);
if (einstellungen==true)
{
background(250);
fill(0, 0, 250);
textSize(50);
text(“Wähle eine Figur!”, 750, 70);
fill(0, 0, 250);
rect(100, 250, 400, 600);
rect(750, 250, 400, 600);
rect(1450, 250, 400, 600);
rect(10, 950, 250, 80);
if (figur1_ak==true && figur2_ak==false && figur3_ak==false)
{
fill(250, 0, 0);
rect(100, 250, 400, 600);
}

  if (figur2_ak==true  && figur1_ak==false && figur3_ak==false)
  {
    fill(250, 0, 0);
    rect(750, 250, 400, 600);
  }

  if (figur3_ak==true  && figur2_ak==false && figur1_ak==false)
  {
    fill(250, 0, 0);
    rect(1450, 250, 400, 600);
  }

  image(figur1, 100, 250, 400, 600);
  image(figur2, 700, 250, 500, 600);
  image(figur3, 1450, 250, 400, 600);

  fill(250, 0, 0);
  text("Zum Start", 10, 1000);
}
if (GameOver==true)
{
  image(GameOverScreen, 0, 0, 1950, 1100);
  fill(0);
  textSize(100);
  text("Punkte:"+ counter, 800, 100);
  fill(0, 0, 250);
  rect(10, 950, 250, 80);
  fill(250,0,0);
  textSize(60);
  text("Neustart",10,1000);
}

}

// SPIEL

if (starten==true && spiel_laeuft==true)
{
//background(Hintergrund);
image(Hintergrund, hg, 0, 1600, 1200);
image(Hintergrund, hg+1600, 0, 1600, 1200);
image(Hintergrund, hg+3200, 0, 1600, 1200);
if (figur1_ak==true)
{
figur2_ak=false;
figur3_ak=false;
image(figur1, figurx, figury, 150, 150);
}

if (figur2_ak==true)
{
  figur1_ak=false;
  figur3_ak=false;
  image(figur2, figurx, figury, 150, 150);
}

if (figur3_ak==true)
{
  figur1_ak=false;
  figur2_ak=false;
  image(figur3, figurx, figury, 150, 150);
}
hg=hg-5;

fill(250, 0, 0);     


noFill();
rect(1600,150,220,220);//PFEIL
image(arrow, 1600, 150, 220, 220);

// SPRINGEN

if (springt == true)
{
  figury = figury - SprSpeed;
  SprSpeed = SprSpeed -3;

  if (figury>800)
  {
    springt=false;
    figury = 800;
  }
}

if (knopf==true && springt == false)
{
  springt = true;
  SprSpeed = 35;
  knopf=false;
}



// HINDERNISSE



hinder[counter]=2000;
fill(0);
textSize(40);
text(counter, 20, 40);


timer1=timer1+1;

if (timer1>timer[speedcounter])
{
  timer1=0;
  sichtbar[counter]=true;
  hinderspeed[counter]= speed[speedcounter];
  hinder[counter]=2000;
  counter= counter+1;
  speedcounter= speedcounter+1;
}




if (hg<-1600)
{
  hg=0;
}



// Hindernisse

for (int i=0; i<100; i=i+1)
{
  if (sichtbar[i]==true)
  {
    hinder[i]=hinder[i]-hinderspeed[i];
    image(Rakete, hinder[i], 870, 120, 80);
    if (hinder[i]<-100)
    {
      sichtbar[i]=false;
    }
  }
  if (hinder[i]< figurx+155 && hinder[i] > figurx - 70 && figury > 760)
  {
    GameOver=true;
    starten=false;
  }
}

}
if (counter == 29)
{
gewonnen=true;
}
if (gewonnen==true)
{
background(250);
image(Gewonnen, 880, 370, 800, 800);
image(Gewonnen2, -400, 400, 800, 800);
starten=false;
textSize(70);
text(“Du hast gewonnen!”, 370, 230);
}
if (back==true)
{
einstellungen=false;
GameOver=false;
counter=0;
hinder[counter]=2000;
back=false;

}
}

void mousePressed()
{
//println(mouseX, mouseY);
if (starten==false && GameOver==true)
{
if (mouseX>10 && mouseX<260 && mouseY>950 && mouseY<1030)
{
back=true;
}
}
if (mouseX>800 && mouseX<1200 && mouseY>600 && mouseY<700 && starten==false)
{
einstellungen=true;
}

if (mouseX>800 && mouseX<1200 && mouseY>200 && mouseY<450 && einstellungen==false)
{
starten=true;
spiel_laeuft=true;
}

if (starten==true && spiel_laeuft==true)
{
if (mouseX>1600 && mouseX<1820 && mouseY>150 && mouseY<370)
{
knopf=true;
}
}

if (einstellungen==true)
{
if (mouseX>100 && mouseX<500 && mouseY>250 && mouseY<850)
{
figur1_ak=true;
figur2_ak=false;
figur3_ak=false;
}
if (mouseX>750 && mouseX<1150 && mouseY>250 && mouseY<850)
{
figur1_ak=false;
figur2_ak=true;
figur3_ak=false;
}
if (mouseX>1450 && mouseX<1850 && mouseY>250 && mouseY<850)
{
figur1_ak=false;
figur2_ak=false;
figur3_ak=true;
}
if (mouseX>10 && mouseX<260 && mouseY>950 && mouseY<1030)
{
back=true;
}
}
}

#2

-a- pls format your code
-1- in the processing IDE with [ctrl][t]
-2- in the forum editor using

</> code tag

and paste your code in-between the
```
your code
```

-b- your code should run from the IDE?
is it a question about android?
ok, for test run would need the pictures.
or
if the pictures are not needed for testing what your question is about,
show some code about the problem without need of extra files…

-c-
you ask about your array

int [] hinder;
void setup() {
  hinder = new int[100];
  for (int i=0; i<20; i=i+1)
  {
    hinder[i]=2000;
  }
}

void draw() {
// ...
    hinder[counter]=2000;
// ...
    hinder[i]=hinder[i]-hinderspeed[i];

}

so, where? you want to do what? with that array? pls more details

1 Like
#3

@Nick1 ===
-as for the code i have the same questions than @kll

  • but reading your question i think that the main problem is not here but about adapting this code to the various phones & tablets sizes
  • which is not at all easy with P5, knowing that there are 18 cases (density *size) and that you have to do all this stuff by code (it is possible)
  • Anyway first thing to do is to make a function which returns the size of the phone and conclude : is or is not a tablet???
  • knowing that (and btw the height and width of the phone) instead of hardcoding all your x and y values you have to adapt them and : if(thisIsATablet() && if (width > 500&&width<600) this is a little tablet but >600 this a big tablet (Samsung eg) so you can claculate proportionnaly your values.
    -you have also to know the density in order to adapt your images and textSize(): which means to create 6 (4 at least) versions of your images and choose according density and size. Example code below::::

import android.app.Activity;
import android.util.DisplayMetrics;
import android.content.Context;
/////////////////////////////////////////////
public static double  screenDiagonal= 0f;
public static Context context;
public static Activity activity;
public boolean phone = false;
/////////////////////////////////////////////

void setup(){
  
size(displayWidth, displayHeight);
context = this.getActivity().getApplicationContext();
activity = this.getActivity();
if (isTablet(context)){
  println("big tablet==="+ screenDiagonal);
}else{
  println("little tablet===="+ screenDiagonal);
}
if(screenDiagonal<=5){
  phone = true;
}
}

void draw(){
  
}

public static boolean isTablet(Context context) {
  
        DisplayMetrics metrics = new DisplayMetrics();
        
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

        double wInches = metrics.widthPixels / (double) metrics.densityDpi;
        double hInches = metrics.heightPixels / (double) metrics.densityDpi;

        screenDiagonal = Math.sqrt(Math.pow(wInches, 2) + Math.pow(hInches, 2));
        System.out.println("diagonale=======" + screenDiagonal);
        if(screenDiagonal<=5){
          
          return(screenDiagonal<=5);
        }
        if (screenDiagonal >= 6.80) {
            return true;
        }
        return (false);
    }
2 Likes
#4

Thank you. Sorry but, its my first time on a forum and i am an german so my english is very bad. I want to bring the array hinder when back = true is to the x from the array hinder 2000 but i dont know how to do this. I make another code where you dont need the pictures.

#5

Thanks for you answer. I want to play the “game” on different screen sizes: tablets, phones for any phone and tablet is there no easier way to make that because id dont understand your code an i am not so far to understand the things with “public”. I send in a few moment a versions without any pictures.

#6
if (back==true)
{
einstellungen=false;
GameOver=false;
counter=0;
hinder[counter]=2000;
back=false;

}

might be

if (back==true) {
  einstellungen=false;
  GameOver=false;
  counter=0;
  for ( int i=0; i<100; i++) hinder[i]=2000;
  back=false;
}

just a idea ( without understanding the context )

#7

@Nick1 ===
Sorry but with P5 i dont think that there is another solution; it s much more easy to do with android Studio but of course it’s android studio…Have you tried to run my code (which is very simple to understand: width, height+ Pythagore…)

#8

thanks you its works. they are a little bit faster but its ok when you want to try it here is the code:

int hg;
boolean starten, knopf, figur1_ak, figur2_ak, figur3_ak;
int [] hinder;
int [] hinderspeed;
int [] speed;
boolean [] sichtbar;
int springen;
int figurx;
int figury;
int SprSpeed;
boolean springt = false;
int counter;
int speedcounter;
boolean einstellungen;
boolean GameOver;
boolean spiel_laeuft;
boolean gewonnen;
boolean back;
int [] timer;

int timer1;

void setup()
{
size(displayWidth, displayHeight);
orientation(LANDSCAPE);
//size(1600,800);
hinder = new int[100];
sichtbar = new boolean [100];
hinderspeed = new int [30];
timer= new int[30];
speed = new int [30];

figurx = 95;
figury = 800;
speedcounter=0;
timer1=0;
counter=0;
hg=0;

spiel_laeuft=false;
einstellungen=false;
GameOver=false;
gewonnen=false;
knopf=false;
starten=false;
figur1_ak=true;
figur2_ak=false;
figur3_ak=false;
back=false;
for (int i=0; i<20; i=i+1)
{
hinder[i]=2000;
hinderspeed[i]=0;
sichtbar[i]=false;
}

speed[0]= 17;
speed[1]= 14;
speed[2]= 25;
speed[3]= 20;
speed[4]= 35;
speed[5]= 35;
speed[6]= 30;
speed[7]= 25;
speed[8]= 20;
speed[9]= 35;
speed[10]= 30;
speed[11]= 20;
speed[12]= 35;
speed[13]= 20;
speed[14]= 45;
speed[15]= 40;
speed[16]= 34;
speed[17]= 22;
speed[18]= 25;
speed[19]= 35;
speed[20]= 35;
speed[21]= 43;
speed[22]= 31;
speed[23]= 25;
speed[24]= 33;
speed[25]= 35;
speed[26]= 35;
speed[27]= 29;
speed[28]= 15;
speed[29]= 3;

timer[0]=10;
timer[1]=25;
timer[2]=40;
timer[3]=35;
timer[4]=35;
timer[5]=35;
timer[6]=35;
timer[7]=25;
timer[8]=20;
timer[9]=30;
timer[10]=25;
timer[11]=15;
timer[12]=25;
timer[13]=10;
timer[14]=40;
timer[15]=20;
timer[16]=25;
timer[17]=30;
timer[18]=40;
timer[19]=50;
timer[20]=10;
timer[21]=20;
timer[22]=25;
timer[23]=10;
timer[24]=30;
timer[25]=10;
timer[26]=40;
timer[27]=50;
timer[28]=20;
timer[29]=50;
}

void draw()
{

background(250);
fill(250, 0, 0);
rect(800, 200, 400, 250);

if (starten==false);
{
fill(0, 250, 0);
textSize(35);
text(“Press to start!”, 835, 330);
fill(250, 0, 250);
textSize(20);
text(“created by Nick and Marvin”, 10, 1070);
fill(0, 0, 250);
rect(800, 600, 400, 100);
textSize(40);
fill(250, 0, 0);
text(“Options”, 870, 660);
if (einstellungen==true)
{
background(250);
fill(0, 0, 250);
textSize(50);
text(“Choose a figure(out of function)!”, 750, 70);
fill(0, 0, 250);
rect(100, 250, 400, 600);
rect(750, 250, 400, 600);
rect(1450, 250, 400, 600);
rect(10, 950, 250, 80);
if (figur1_ak==true && figur2_ak==false && figur3_ak==false)
{
fill(250, 0, 0);
rect(100, 250, 400, 600);
}

  if (figur2_ak==true  && figur1_ak==false && figur3_ak==false)
  {
    fill(250, 0, 0);
    rect(750, 250, 400, 600);
  }

  if (figur3_ak==true  && figur2_ak==false && figur1_ak==false)
  {
    fill(250, 0, 0);
    rect(1450, 250, 400, 600);
  }

  rect(100, 250, 400, 600);
  rect(700, 250, 500, 600);
  rect(1450, 250, 400, 600);

  fill(250, 0, 0);
  text("Start", 10, 1000);
}
if (GameOver==true)
{
  background(0);
  fill(250);
  textSize(100);
  text("dead", 800, 400);
  text("Points:"+ counter, 800, 100);
  fill(0, 0, 250);
  rect(10, 950, 250, 80);
  fill(250, 0, 0);
  textSize(60);
  text("Restart", 10, 1000);
}

}

// SPIEL

if (starten==true && spiel_laeuft==true)
{
//background(Hintergrund);
background(0);
if (figur1_ak==true)
{
figur2_ak=false;
figur3_ak=false;
rect( figurx, figury, 150, 150);
}

if (figur2_ak==true)
{
  figur1_ak=false;
  figur3_ak=false;
  rect( figurx, figury, 150, 150);
}

if (figur3_ak==true)
{
  figur1_ak=false;
  figur2_ak=false;
  rect( figurx, figury, 150, 150);
}
hg=hg-5;

fill(250, 0, 0);     


noFill();
rect(1600, 150, 220, 220);//PFEIL
textSize(50);
fill(0,0,250);
text("Jump", 1650, 200);
 fill(250,0,0);
rect( 1600, 150, 220, 220);

// SPRINGEN

if (springt == true)
{
  figury = figury - SprSpeed;
  SprSpeed = SprSpeed -3;

  if (figury>800)
  {
    springt=false;
    figury = 800;
  }
}

if (knopf==true && springt == false)
{
  springt = true;
  SprSpeed = 35;
  knopf=false;
}



// HINDERNISSE



hinder[counter]=2000;
fill(0);
textSize(40);
text(counter, 20, 40);


timer1=timer1+1;

if (timer1>timer[speedcounter])
{
  timer1=0;
  sichtbar[counter]=true;
  hinderspeed[counter]= speed[speedcounter];
  hinder[counter]=2000;
  counter= counter+1;
  speedcounter= speedcounter+1;
}




if (hg<-1600)
{
  hg=0;
}



// Hindernisse

for (int i=0; i<100; i=i+1)
{
  if (sichtbar[i]==true)
  {
    hinder[i]=hinder[i]-hinderspeed[i];
    fill(250, 0, 0);
    rect( hinder[i], 870, 120, 80);
    if (hinder[i]<-100)
    {
      sichtbar[i]=false;
    }
  }
  if (hinder[i]< figurx+155 && hinder[i] > figurx - 70 && figury > 760)
  {
    GameOver=true;
    starten=false;
  }
}

}
if (counter == 29)
{
gewonnen=true;
}
if (gewonnen==true)
{
background(250);
starten=false;
textSize(70);
text(“Du hast gewonnen!”, 370, 230);
}
if (back==true)
{
einstellungen=false;
GameOver=false;
counter=0;
for ( int i=0;i<100;i++) hinder[i]=2000;
back=false;
}

}

void mousePressed()
{
//println(mouseX, mouseY);
if (starten==false && GameOver==true)
{
if (mouseX>10 && mouseX<260 && mouseY>950 && mouseY<1030)
{
back=true;
}
}
if (mouseX>800 && mouseX<1200 && mouseY>600 && mouseY<700 && starten==false)
{
einstellungen=true;
}

if (mouseX>800 && mouseX<1200 && mouseY>200 && mouseY<450 && einstellungen==false)
{
starten=true;
spiel_laeuft=true;
}

if (starten==true && spiel_laeuft==true)
{
if (mouseX>1600 && mouseX<1820 && mouseY>150 && mouseY<370)
{
knopf=true;
}
}

if (einstellungen==true)
{
if (mouseX>100 && mouseX<500 && mouseY>250 && mouseY<850)
{
figur1_ak=true;
figur2_ak=false;
figur3_ak=false;
}
if (mouseX>750 && mouseX<1150 && mouseY>250 && mouseY<850)
{
figur1_ak=false;
figur2_ak=true;
figur3_ak=false;
}
if (mouseX>1450 && mouseX<1850 && mouseY>250 && mouseY<850)
{
figur1_ak=false;
figur2_ak=false;
figur3_ak=true;
}
if (mouseX>10 && mouseX<260 && mouseY>950 && mouseY<1030)
{
back=true;
}
}
}

#9

I have seen something on youtube to do a processing project in android studio. Is it then possible?

#10

@Nick1 ===
Yes it’s possible : you export your app as android with P5 then import it with Android studio…

#11

sorry, again you post your code WRONG
Help with reset a int[] and multiple screens here
i show you how to do
and i need your to edit / repair both code postings

we not only talk about beauty,
when you post code like that it is damaged,
so when we try to copy paste into IDE and try to run it complains about
" wrong / * missing.

#12

I did it already but i dont know and i dont find any video on youtube what i have to do.

#13

Sorry but i dont know how i have to do it explain it and i do it good ok?

#14

@Nick1 ===
Supposing that you have imported your app in AS the first thing is to look at the res folders and inside them put a) your launcher icons into mipmap folders b) inside “drawable” your images with 4, 5 or 6 dimensions and res dpi, from mediumdpi (160) to Xdpi (320dpi) to XX dpi or XXXdpi. Of course you have to give them name easy to understand and not using any forbidden (android) char: so only lowercase from a, b,c, d…to z + numbers (int.from 0 to 9) - Finally, inside your code you call this image or this other one: Anyway at the very beginning, in the main Activity, you will have to test the device dims and dpi using the code i have put. Yet be sure that this is NOT the android standard way. With AS you create all the xml layout put your images into imageView and android chooses by itself the image required by the phone…

#15

thank you for your help i try it.

#16

Here i got this and my icons are already there (i did it before). But how can i know set the other screen resolutions i dont find it.
Thanks for help.

#17

@Nick1===
as for beginning you put your images inside the drawable; then you can create a folder for each image and inside it put the differents dpi (exactly as it appears in your screen capture for the icon folder) - and dont forget to put minSdk targetted in the build graddle file module app, AS is not OK with the Manifest way…

#18

thanks for your help but i think its ´not to difficult for me.
Thanks alot.