We are about to switch to a new forum software. Until then we have removed the registration on this forum.
//NOT PROPERLY WORKING ARRAY
int cols= 3;
int rows=2;
//int cols_select;
//int rows_select;
Alien[][] aliens = new Alien[cols][rows];
PImage background;
int y=0;
int bulletX;
int bulletY;
int score=0;
int alienx;
int alieny;
final int NORMAL=0;
final int CRASHED=1;
int gameMode=NORMAL;
//calling the constractors
//!ALIENS!
//Alien alien1=new Alien(200,30);
//Alien alien2=new Alien(400,30);
//Alien alien3=new Alien(600,30);
//Alien alien4=new Alien(200,130);
//Alien alien5=new Alien(400,130);
//Alien alien6=new Alien(600,130);
//!DEFENDER!
Defender defender1;
//!BULLET!
bullet bullet1;
void setup()
{
bulletX=0;
bulletY=-100;
size(1000,800);
background=loadImage("bg1.jpg");
image(background,0,0);
background.resize(width,height);//resize the image to the size of my background
// bullets=new ArrayList();
//NOT PROPERLY WORKING ARRAY
//creating the array
for (int i=0; i< cols; i++){
for (int j=0; j<rows; j++){
aliens[i][j] = new Alien(alienx,alieny);
alienx=alienx+70;
alieny=alieny+20;
}
}
bullet1=new bullet(bulletX,bulletY);
defender1=new Defender(320,450);
}//end of setup
void draw()
{
if (gameMode==NORMAL)
{
background(255);
image(background,0,y);
image(background,0,y+height);
y=y-10;
if (y==-background.height)
{
y=0;
}
// for (int i=0; i<bullets.size(); i++)
//{
//bullet.update();
//}
//alien1.update();
//alien2.update();
//alien3.update();
//alien4.update();
//alien5.update();
//alien6.update();
//NOT WORKING PROPERLY ARRAY
for (int i=0; i< cols; i++){
for(int j=0; j<rows; j++){
aliens[i][j].update();
}
}
// boolean AlienOnScreen;
// AlienOnScreen=alien1.update();
//AlienOnScreen=alien2.update();
//AlienOnScreen=alien3.update();
// if (AlienOnScreen==false)
//{
//alien1=new Alien(200,30);
//alien2=new Alien(400,30);
//alien3=new Alien(600,30);
//}
defender1.render();
bullet1.update();
textSize(30);
fill(255);
text("SCORE:"+score,30,30);
if (defender1.crash()||(bullet1.crash())==true)
{
//fill(255);
//text("YOUR SCORE IS:",score,220,420);
fill(255,0,0);
textSize(30);
text("GAME OVER PRESS R TO TRY AGAIN",240,400);
gameMode=CRASHED;
}
if (defender1.crash()||(bullet1.crash())==true)
{
score=score+50;
}
}
}
void keyPressed()
{
if (key==CODED)
{
if (keyCode==UP && defender1.y>=400)
{
defender1.y=defender1.y-10;
}
else if (keyCode==DOWN && defender1.y<=height-260)
{
defender1.y=defender1.y+10;
}
else if (keyCode==RIGHT && defender1.x<width-130)
{
defender1.x=defender1.x+20;
}
else if (keyCode==LEFT && defender1.x>15)
{
defender1.x=defender1.x-20;
}
}
if (key=='r' && gameMode==CRASHED)
{
gameMode=NORMAL;
// alien1=new Alien(200,30);
// alien2=new Alien(400,30);
// alien3=new Alien(600,30);
//alien4=new Alien(200,130);
//alien5=new Alien(400,130);
//alien6=new Alien(600,130);
//aliens[i][j].update();
bullet1=new bullet(bulletX+1000,bulletY+1000);
defender1=new Defender(320,450);
}
if (key==' ')
{
bulletX=defender1.x+75;
bulletY=defender1.y;
bullet1=new bullet(bulletX, bulletY);
// bullet2=new bullet(bulletX, bulletY);
}
}
Here is my alien class:
class Alien
{
int y=100;
int x=10;
int v=5;
Alien (int x ,int y)
{
this.x=x;
this.y=y;
}
//boolean move()
//{
//y=y+1;
//x=x;
//if (y>=0)
//{
//return true;
//}
//else
// {
//return false;
//}
//}//end of the boolean
void move ()
{
x=x+v; // which direction the alien is moving
if(x> width - 25)
{
v = -5;
y = y+30;
}
if (x==25)
{
v =+5;
y= y+30;
}
}//end of move
void create()
{
fill(255,255,255);
ellipse(x,y,45,50);
fill(255,0,0);
ellipse(x,y,60,25);
}
void update()
{
create();
move();
//y=y+50;
}
}//end of class
i want to get 3 columns with 2 rows of aliens in fixed place with a simple moving pattern for some reason tho i cant get them to display properly with are going diagonally and the other problem that i have is when i try to press "r" and restart the game and set the aliens in the same place where they start it wont work they just keep going from where they left
Any help would be appreciated thanks in advance can put all the code if someone wants to check it
Answers
excuse the horrible preview its my first time posting here
edit post, highlight code, hit ctrl-o.
thanks for the tip :)
creating the array
you need to reset alienx
here you want to do the double for-loop above in keyPressed :
if (key=='r'...
and, yes, in future please post the entire code. We can't run it otherwise.
omg thank you very much chrissir there is one more thing aliens for some reason keep kinda going diagonally and when i press the r it resets them but lower everytime?
Thanks in advance
are we talking about these lines:
nothing (no alien) gets resetted here
did you insert the double for-loop as I told you to?
yes ive done this
you need to start with
alieny = 0;
in line 11yeah that fixed the issue thanks do you happen to know why aliens go diagonally?
because you tell them to??
(or do you mean the layout of the grid right after the start? )
that is a pattern that they follow once they hit the width of the screen to go diagonally 40 pixels down but before hitting it i want them to be in a straight line (right at the start)
this calculates the alienx and alieny from i and j; so you get better values
110 is distance from screen border, 70 and 120 the distance from each other
it worked you are amazing but how? haha i keep reading the code wont understand why it worked thank you very much anyways
Ohh alright thank you thank you very much you just saved me from a sleepless night :)
well, you have rows and columns
in your version with
the x-position AND the y-position was increased EVERY alien. So it becomes diagonal.
In my version :
the value of alienx and alieny are calculated from the i and j.
This means, alieny is changed when the inner for-loop is closed and we re-enter the for j loop with a new i-value. And alienx is this situation gets the same value as in the last column
also please hit ctrl-t often in processing; it gives you auto-indent in processing
and some house-cleaning: size should be first line in setup()
you have an image you use as background. Good. Better name it backgroundImg
but just say
background(backgroundImg);
instead of using the commandimage()
here (or usingbackground(255);
), pleasedelete lines that are not in use (which start with
//
) and make comments where needed pleaseswap line 42 and 43 above (in the first post); replace the command image() with the command
background(backgroundImg);
as mentioned[EDITED]
Great explanation thanks for your valuable time!! Gotta work on the bullets array now and hitting and making the aliens disappear
shall i post all the program here without background and defender image?
not now
only when you have more questions
and then the entire program without remarks in between ("Here is my alien class:")
;-)
Alien class:
bullet class:
Defender class:
ohh ok sorry i misunderstood you then thanks for the tip you are a very helpful man thank you thank you so much for your time i never thought id get a help from a forum i was always ignored thanks again!
oh what I meant was:
you still need the background image, you just can use it with the command background
so please restore line 5 etc. ....
also kill line 17 and 72