We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
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()So where would I add x=x+speedX; and y=y+speedY;?
**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
continued elsewhere
closing this