Hi guys,
I’m making a fun game in which you have to get a rocket as high as possible. I have made the first ‘level’ and I now want to add more levels.
The transition from level 1 to level 2 works fine, but backwards is a bit edgy. Besides that, I don’t know whats happening with the translate function when going to a different level. The landing stuff should stay at the same place, but you can clearly see it isn’t.
Is there someone who could help me?
Kind regards,
Koen.
Rocket r;
Landing l;
Level2 level2;
float a =250;
ArrayList<Fire> f;
Cloud[]clouds = new Cloud[10];
float y;
void setup(){
size (840,840);
r= new Rocket();
l= new Landing();
level2 = new Level2();
for (int i=0; i<clouds.length; i++){
clouds[i] = new Cloud();
}
f = new ArrayList<Fire>();
y=height-100;
}
void draw(){
background(135,206,250);
//grass
fill(124,252,0);
rectMode(CORNER);
noStroke();
rect(0,y,width,100);
//
//engine burn
if (mousePressed == true){
f.add(new Fire());
for (int i =0; i<f.size(); i++){
Fire fireball =f.get(i);
fireball.burn(r);
PVector distance = new PVector (0,abs(r.location.y+fireball.location.y+125-y));
if (distance.y<=50){
fireball.location.x+=random(-3,3);
}
}}
if(f.size()>=20){
f.remove(0);
}
//
//Landing stuff
l.display(r);
l.move1(r);
//
//Rocket
r.move();
//
//Clouds
for (int i=0; i<clouds.length; i++){
clouds[i].display();
clouds[i].move();
}
//
//Fuel bar
fill(0);
textSize(30);
text("FUEL:",712.5,50);
fill(255,0,0);
rectMode(CORNER);
noStroke();
rect(750,350,50,-a);
noFill();
stroke(1);
rect(750,350,50,-250);
if (mousePressed == true){
a-=0.5;
}
if (a<=0){
a=0;
}
//
//New Level
if (r.location.y<=0){
level2.newLevel();
}
//
}
class Cloud {
float x;
float y;
float w;
float h;
float xspeed = random(-3,3);
Cloud(){
x=random(width);
y=random(0,300);
w=random(100,110);
h=random(40,50);
}
void display(){
fill(255);
noStroke();
rect(x,y,w,h);
arc(x+w/2,y,h,h,-PI/2,PI/2);
arc(x-w/2,y,h,h,PI/2,1.5*PI);
arc(x-w/4,y-h/2,w/2,h,PI,2*PI);
arc(x+w/4,y-h/2,w/2,h,PI,2*PI);
arc(x+w/4,y+h/2,w/2,h,0,PI);
arc(x-w/4,y+h/2,w/2,h,0,PI);
}
void move(){
x+=xspeed;
if (x>=width+100|| x<=-100){
if(xspeed<0){
x=width;
}
if(xspeed>0){
x=0;
}
}
}
}
class Fire{
PVector location;
PVector velocity;
PVector acceleration;
Fire(){
location = new PVector (0,0);
velocity = new PVector (random(-2,2),random(3));
acceleration = new PVector (0,random(0.1));
}
void burn(Rocket other){
fill(255,69,0);
ellipse(other.location.x+location.x,other.location.y+location.y+125,25,25);
location.add(velocity);
}
}
class Landing{
float angle;
float angle1;
boolean doOnce;
Landing(){
angle=-0.75;
doOnce = false;
}
void display(Rocket other){
noStroke();
fill(128,128,128);
rect(other.location.x+12.5,other.location.y+50,25,100);
rect(other.location.x+5,other.location.y+150,40,10);
rect(other.location.x-37.5,other.location.y+50,25,100);
rect(other.location.x-45,other.location.y+150,40,10);
}
void move1(Rocket other){
noStroke();
pushMatrix();
translate(other.location.x+12.5,other.location.y+50);
rotate(angle);
fill(128,128,128);
rect(0,0,25,100);
popMatrix();
if (mousePressed == true){
doOnce=true;
}
if (doOnce == true){
if (angle >=-0.75 && other.velocity.y <0){
angle+=0.01;
if (angle>=0){
angle-=0.01;
}}
if (other.velocity.y >0 && other.location.y>=200){
angle-=0.01;
}
if (angle<=-0.75){
angle+=0.01;
}
println(angle);
}
pushMatrix();
translate(other.location.x-37.5,other.location.y+50);
rotate(-angle);
fill(128,128,128);
rect(0,0,25,85);
popMatrix();
println(mouseX,mouseY);
}
// void move2(Rocket other){
// pushMatrix();
// translate(other.location.x-12.5,other.location.y+50);
// rotate(-angle);
// fill(128,128,128);
// rect(0,0,25,100);
// }
}
class Level2{
Stars[]stars= new Stars[50];
Rocket r;
Landing l;
ArrayList<Fire> f;
Level2(){
for (int i=0; i<stars.length; i++){
stars[i]= new Stars();
}
r= new Rocket();
l = new Landing();
f = new ArrayList<Fire>();
}
void newLevel(){
//Stars
background(0);
for (int i=0; i<stars.length; i++){
stars[i].display();
}
//
//Engine burn
noStroke();
if (mousePressed == true){
f.add(new Fire());
for (int i =0; i<f.size(); i++){
Fire fireball =f.get(i);
fireball.burn(r);
}
}
if(f.size()>=20){
f.remove(0);
}
//
//Rocket
r.move();
//
//Landing stuff
l.display(r);
l.move1(r);
//
}
}
class Rocket{
PVector location;
PVector velocity;
PVector acceleration;
PVector gravity;
float a;
Rocket(){
location = new PVector (width/2,height-125);
velocity = new PVector (0,0);
acceleration = new PVector (0,-0.5);
gravity = new PVector (0,0.4);
a=0;
}
void move(){
noStroke();
rectMode(CENTER);
// fill(128,128,128);
// rect(location.x+25,location.y+100,25,100);
// rect(location.x+25,location.y+150,40,10);
// rect(location.x-25,location.y+100,25,100);
// rect(location.x-25,location.y+150,40,10);
fill(255);
rect(location.x,location.y,100,250);
arc(location.x,location.y-125,100,100,PI,2*PI,CHORD);
location.add(velocity);
velocity.add(gravity);
if (mousePressed == true){
velocity.add(acceleration);
//location.add(velocity);
//velocity.add(acceleration);
}
if (location.y >=height-125){
location.y = height-125;
velocity.mult(0);
}
if (velocity.y==0 && mousePressed == true){
velocity.add(acceleration);
}
}
}
class Stars{
float x;
float y;
float r;
Stars(){
x=random(0,width);
y=random(0,height);
r=random(5,15);
}
void display(){
fill(255);
ellipse(x,y,r,r);
}
}