Trying to create space invaders kind of game.

LATLAT
edited March 2018 in Questions about Code

Trying to make one from scratch with limited knowledge on coding. I have been able to get it to move left to right, but I can't seem to get the bullets to stop moving along with the turret or to disappear once it hits the top of the canvas.

float gunX;
float bulletY;
float SPEED = 2.0;
float STALL = 0.0;
int N=0;
final int BLACK = #000000;
final int WHITE = #FFFFFF;
final float gunBodySize = 50;
final float barrelParameterOne=25;
final float barrelParameterTwo=75;
final float bulletDIAM = 5;
final float SPACING = 20;
boolean hitLeftEdge, hitRightEdge,hitTop;
void setup(){
  size(500,500);
  gunX=width/2-barrelParameterOne;
}
void draw(){
  background(150);
  moveBullet();
  drawBullet();
  moveGun();
  drawGun();
}
void moveGun(){
  hitLeftEdge();
  hitRightEdge();
  if (key == CODED) {
    if (hitLeftEdge){
    gunX=0;
    }
    else if (keyPressed && keyCode == LEFT) {
      gunX+=-SPEED;
    }
    if (hitRightEdge){
      gunX=width-gunBodySize;
    }
    else if (keyPressed && keyCode == RIGHT) {
      gunX+=SPEED;
    } 
  }
}
void drawGun(){
  fill(WHITE);
  rect(gunX,height, gunBodySize,-gunBodySize);
  triangle(gunX,height-gunBodySize,gunX+barrelParameterOne,height-barrelParameterTwo,gunX+gunBodySize,height-gunBodySize);
}
void hitLeftEdge(){
  if (gunX<0){
    hitLeftEdge =  true;
  }
  else {
    hitLeftEdge = false;
  }
}
void hitRightEdge(){
  if (gunX>width-gunBodySize){
    hitRightEdge = true;
  }
  else {
    hitRightEdge = false;
  }
}
void drawBullet(){
  fill(BLACK);
  if (mousePressed){
    int i = 1;
    while (i<=5){
      bulletY=height-(gunBodySize+SPEED*N)+SPACING*i;
      ellipse(gunX+barrelParameterOne,bulletY,bulletDIAM,bulletDIAM);
      i++;
    }
  }  
}
void moveBullet(){
  checkBullet();
  if(mousePressed && !hitTop){
    N++;
  }
  else if(mousePressed && hitTop){
    N=0;
  }
}
void checkBullet(){
  if (bulletY<=0){
    hitTop = true;
  }
  else {
    hitTop = false;
  }
}
Tagged:

Answers

  • edited March 2018

    Please note that the back apostrophe (``) is use to mark inline code for example final float barrelParameterOne=25;. To format a code block make sure that you have a blank line before and after the block. Then highlight the code block and click on the C button, this will indent the code by four spaces.

    Go back and edit your post above.

  • The problem is the line

    ellipse(gunX+barrelParameterOne,bulletY,bulletDIAM,bulletDIAM);

    in the drawBullet method. The x position of the bullet is linked to the barrel position. What you need to do is create a new variable which remembers the barrel X position when the gun is fired and then uses this variable when drawing the bullet.

  • Thanks. I will try that and see if it fixes my problem.

  • I tried to set it on void mousePressed() to save posX=gunX+barrelParameterOne,, but it just stops firing once I add anything to mousePressed.

  • My solution will not work easily with your code. The problem is that you need

    1) record the gun's current x position when the bullet is fired then
    2) use this x position (not the gun's position) as the bullet moves up the screen.

    The problem with your code is that the gun fires AND the bullet moves on the same event, the mousePressed event.

    The real problem is that you don't record the {x,y] position of the bullets rather use some maths to calculate the y position each frame.

    To create a space invaders type game you need to be able to store data about multiple bullets and multiple aliens. You can't do that unless you use arrays or some other Java collection.

    Imagine the real game, you press and hold the fire button and move the gun to the right. You will get a stream of bullets like this.

    p1

    You need to manage a collection of bullets with each bullet having it's own [x,y] position.

    I suggest that you look at the tutorials on arrays. I also suggest using the PVector class to store positions.

  • Thanks. Never learned how to do arrays yet, so I'll try it out and get back to you. Sort of nice how there is always something to learn. Thought I just needed if statements, and loops.

  • LATLAT
    edited March 2018

    This is what I got up to after a bit of cleaning and renaming. I tried storing the value of the gun's muzzle at the time mouse is pressed, but it seems like it either requires me to hold down mouse to work, or it simply does not fire.

    float posX,posY;
    float bulletX, bulletY;
    float SPEED = 2.0;
    float bulletSpeed=0.1;
    float STALL = 0.0;
    int N=0;
    final int BLACK = #000000;
    final int WHITE = #FFFFFF;
    final float gunBodySize = 50;
    final float barrelParameterOne=25;
    final float barrelParameterTwo=75;
    final float bulletDIAM = 5;
    final float SPACING = 20;
    boolean hitTop=false;
    boolean bulletFired=false;
    void setup(){
      size(500,500);
      posX=width/2-barrelParameterOne;
      posY=height-barrelParameterTwo;
    }
    void draw(){
      background(150);
      moveBullet();
      drawBullet();
      moveGun();
      drawGun();
    }
    void moveGun(){
      if (posX>0 && keyCode == LEFT) {
          posX+=-SPEED;
      }
      if ((posX+gunBodySize<width) && (keyCode == RIGHT)) {
          posX+=SPEED;
      } 
    }
    
    void drawGun(){
      fill(WHITE);
      rect(posX,height, gunBodySize,-gunBodySize);
      triangle(posX,height-gunBodySize,posX+barrelParameterOne,posY,posX+gunBodySize,height-gunBodySize);
    }
    
    // use special void mousePressed(); for getting fixed X coord.  And can use for getting it to keep running till bullet Y hits the top.
    void drawBullet(){
      fill(BLACK);
      int i = 1;
      while (i<=5 && mousePressed && !hitTop && bulletFired){
        bulletY=bulletY+SPACING*i;
        ellipse(posX+barrelParameterOne,bulletY,bulletDIAM,bulletDIAM);
        i++;
      }
    }  
    
    
    void moveBullet(){
      checkBullet();
      if(mousePressed && !hitTop){
        bulletY+=-bulletSpeed;
      }
      else if(mousePressed && hitTop){
        background(150);
        bulletFired = false;
      }
    }
    void checkBullet(){
      if (bulletY<=0){
        hitTop = true;
      }
      else {
        hitTop = false;
      }
    }
    
    void mousePressed(){
      bulletX=posX+barrelParameterOne;
      bulletFired = true;
    }   
    
Sign In or Register to comment.