How to move my objects

I am working on a project where I have to make an asteroid game. In it, you have to fire shots from the left and right turrets and hit the moving asteroid. The problem is, I can't move my shots or the asteroid itself down the screen. Here's my code:

int drawShot=0;
color c;
int xShotfired = 0;
int drawAsteroid;
int asX;
int asY;
int asSpeed;

boolean collision = false;

void setup(){
 size(600,600);
 background(0);
 asX=300;
 asSpeed =10;
}

void draw(){
 drawTurret(10,color(255,0,0),true);
 drawTurret(540,color(0,0,255),true);
 drawShot = drawShot(60,300,true);
 drawShot = drawShot(540,300,true);
 asY = drawAsteroid(asX,asY,asSpeed); 
}


void drawTurret(int x, color cVal, boolean rightTurret){
  fill(cVal);
  rect(x,10,50,580);
}

int drawShot(int xShot, int yShot, boolean rightShot){
fill(255,0,0);
ellipse(xShot,yShot,20,20);
return drawShot = drawShot - 4;

}


int drawAsteroid(int x, int y, int speed){
  fill(255);
  ellipse(x,y,100,100);
  return speed = y * speed;

}

//boolean collision(int xAsteroid, int yAsteroid, int xBolt, int yBolt){

//}

Answers

  • to move something, store x and y position in a variable and add a speed to it

    x=x+speedX;
    y=y+speedY;
    rect(x,y,20,20); 
    

    you need to do the same for all moving things separately

    now when you have multiple asteroids, you might want to look into arrays or ArrayList. Also, since one asteroid consists of x,y,speedX, speedY, color you might want to turn to a thing called object oriented programming which allows you to pack all those data into one packet (the class). You can have then objects from that class and hold them in an ArrayList

  • here is your code

    line 35 in your code above was wrong and you forgot background(0); in draw()

    int drawShot=0;
    color c;
    int xShotfired = 0;
    int drawAsteroid;
    int asX;
    int asY;
    int asSpeed;
    
    boolean collision = false;
    
    void setup() {
      size(600, 600);
      background(0);
      asX=300;
      asSpeed =10;
      frameRate(44);
    }
    
    void draw() {
      background(0); 
      drawTurret(10, color(255, 0, 0), true);
      drawTurret(540, color(0, 0, 255), true);
      drawShot = drawShot(60, 300, true);
      drawShot = drawShot(540, 300, true);
      asY = drawAsteroid(asX, asY, asSpeed);
    }
    
    
    void drawTurret(int x, color cVal, boolean rightTurret) {
      fill(cVal);
      rect(x, 10, 50, 580);
    }
    
    int drawShot(int xShot, int yShot, boolean rightShot) {
      fill(255, 0, 0);
      ellipse(xShot, yShot, 20, 20);
      return drawShot = drawShot - 4;
    }
    
    
    int drawAsteroid(int x, int y, int speed) {
      fill(255);
      ellipse(x, y, 100, 100);
      return y + speed;
    }
    
    //boolean collision(int xAsteroid, int yAsteroid, int xBolt, int yBolt){
    
    //}
    //
    
  • So where would I add x=x+speedX; and y=y+speedY;?

  • Answer ✓

    **well at the moment you do the y=y+speedY; in line 44 (making a return value) plus in line 25 where you assign the return value to asY.

    you could as well say in line 44 asY = asY + speedY;

    without using or having a return value in the first place....

    the same goes for the shots / bullets

    maybe look here

    http://www.openprocessing.org/sketch/77863

  • int drawShot=0;
    
    color c;
    
    int xShotfired = 0;
    
    int drawAsteroid;
    
    int asX;
    int asY;
    int asSpeed;
    
    int shot1X=60;
    int shot1Y=300;
    int shot1SpeedX=1;
    int shot1SpeedY=-1;
    
    boolean collision = false;
    
    void setup() {
      size(600, 600);
      background(0);
      asX=300;
      asSpeed =10;
      frameRate(44);
    }
    
    void draw() {
      background(0); 
    
      drawTurret(10, color(255, 0, 0), true);
      drawTurret(540, color(0, 0, 255), true);
    
      drawShot(shot1X, shot1Y, true);
      drawShot(540, 300, true);
    
      shot1X+=shot1SpeedX;
      shot1Y+=shot1SpeedY;
    
      drawAsteroid(asX, asY, asSpeed);
    }
    
    
    void drawTurret(int x, color cVal, boolean rightTurret) {
      fill(cVal);
      rect(x, 10, 50, 580);
    }
    
    void drawShot(int xShot, int yShot, boolean rightShot) {
      fill(255, 0, 0);
      ellipse(xShot, yShot, 20, 20);
      // return drawShot = drawShot - 4;
    }
    
    
    void drawAsteroid(int x, int y, int speed) {
      fill(255);
      ellipse(x, y, 100, 100);
      asY= asY + speed;
    }
    
    boolean collision(int xAsteroid, int yAsteroid, 
      int xBolt, int yBolt) {
      return false;
    }
    //
    
  • continued elsewhere

    closing this

Sign In or Register to comment.