How to create a premade list of objects that move down and are respawned at the top.
in
Programming Questions
•
6 months ago
I want a large amount of objects that I have already started as an ArrayList and have running on a timer. However I want to make many objects and the method I am using will be long and tedious. Is there a better way to use the code i have to make a lot of objects in specific positions, move down at once?
I am trying to create a game similar to the asteroid levels of radiant.
Here is my code so far...
int gamestate=1;
int lives=3;
int score=0;
float multiplier=1;
int lasts = 0;
int s = 0;
ArrayList<PVector> asteroids = new ArrayList <PVector>();
PVector a;
float[] circleX = new float [39];
float[] circleY = new float [39];
ArrayList<PVector> projectiles = new ArrayList<PVector>();
int last = 0;
int m = 0;
float shipX, shipY;
float sizes;
color colours;
PVector p;
void setup() {
size(800, 600);
smooth();
formShip();
formCircles();
}
void draw() {
background(0);
noStroke();
if (gamestate==1) {
menu();
if (mouseX>300 && mouseX< 500 && mouseY>300 && mouseY< 400 && mousePressed) {
gamestate=2;
}
}
if (gamestate==2) {
drawShips();
updateProjectiles();
updateAsteroids();
// asteroidCollision();
if (moveShip()) {
if (keyPressed) {
if (keyCode == LEFT) {
shipX -=4;
}
if (keyCode == RIGHT) {
shipX +=4;
}
}
}
if (shipX > width-25) {
shipX = width-26;
}
if (shipX < 25) {
shipX = 26;
}
m = millis()-last;
if (millis() > last+400) {
last = millis();
}
if (m>10 && m < 20) {
addProjectile(shipX+45);
}
if (m>210 && m < 220) {
addProjectile(shipX-45);
}
s = millis()-lasts;
if (millis() > lasts+1200) {
lasts = millis();
}
asteroidPlacement();
drawCircle();
moveCircles();
shootingStuff();
textSize(20);
text(lives, 750, 25);
text("Lives =", 675, 25);
text(multiplier, width/2, 25);
text("X", width/2-20, 25);
text(score, 50, 25);
if (lives<=0) {
gamestate=3;
}
}
else if (gamestate==3) {
gameover();
mouseClicked();
lives=3;
shipX=width/2;
}
}
void drawShips() {
drawShip(shipX, shipY, 100, colours);
}
void drawShip(float x, float y, float size, color c) {
float offset=size/4;
rectMode(CENTER);
fill (255);
triangle(x, y-offset, x-size/2, y, x+size/2, y);
triangle(x, y-size+20, x-offset, y-offset+20, x+offset, y-offset+20);
triangle(x-20, y+30, x, y-30, x+20, y+30);
rect(x, y+5, size, 10);
fill(255, 0, 0);
rect(x-size/2+5, y-7.5, 10, 35);
rect(x+size/2-5, y-7.5, 10, 35);
fill(0, 0, 255);
rect(x, y-45, 15, 7);
fill(255, 0, 0);
// rect(x, y-20, 2.5, 20);
// rect(x, y-20, 20, 2.5);
// rect(x-4,y-11,10,2.5);
// rect(x+4,y-29,10,2.5);
// rect(x+9,y-15,2.5,10);
// rect(x-9,y-25,2.5,10);
}
void formShip() {
shipX = width/2;
shipY = 550;
colours = color(random(255), random(255), random(255));
}
boolean moveShip() {
if (shipX > 25 && shipX < width-25) {
return (true);
}
else {
return (false);
}
}
void addProjectile(float x) {
projectiles.add(new PVector(x, shipY-32));
}
void updateProjectiles() {
for (int i=0; i<projectiles.size(); i++) {
PVector p = (PVector)projectiles.get(i);
fill(255, 200, 0);
rect(p.x, p.y, 2.5, 10);
p.y -=6;
if (p.y < 0)
projectiles.remove(p);
}
}
void drawCircle() {
for (int i=0; i<39;i++) {
drawCircles(circleX[i], circleY[i], 30, 255);
}
}
void drawCircles(float x, float y, float size, color c) {
rectMode(CENTER);
fill(255);
ellipse(x, y, 5, 5);
}
void formCircles() {
for (int i=0; i<39; i++) {
circleX[i] = i*20+20;
circleY[i] = (random(-600, 1200));
}
}
void moveCircles() {
for (int i=0; i<39; i++) {
circleY[i] +=10;
if (circleY[i] > 1200) {
circleY[i] = -600;
}
}
}
void addAsteroids(float x, float y) {
asteroids.add(new PVector(x, y));
}
void updateAsteroids() {
for (int i=0;i<asteroids.size();i++) {
PVector a = (PVector)asteroids.get(i);
strokeWeight(2);
stroke(255);
fill(0);
rect(a.x, a.y, 20, 20);
a.y +=10;
if (a.y > 600)
asteroids.remove(a);
if (a.y>shipY-80 && a.y<shipY+30 && a.x>shipX-20 && a.x<shipX+20) {
lives -=1;
asteroids.remove(a);
multiplier=1;
}
else if (a.y>shipY-32 && a.y<shipY && a.x >shipX-55 && a.x < shipX+55) {
lives -=1;
asteroids.remove(a);
multiplier=1;
}
}
}
void asteroidPlacement() {
if (s>10 && s<30)
addAsteroids(width/2, 0);
if (s> 40 && s<60)
addAsteroids(width/4, -100);
if (s>70 && s<90)
addAsteroids(width/2+100, -100);
if (s>0)
score+=2;
}
void menu() {
background(0, 0, 255);
stroke(255, 0, 0);
fill(0, 0, 255);
rect(width/2-150, height/8, 300, 100);
fill(255, 0, 0);
rect(width/2-100, height/2, 200, 100);
fill(0);
textSize(50);
text("Radiant", width/2-100, height/8+68);
textSize(30);
text("Start", width/2-35, height/2+60 );
}
void gameover() {
background(0);
text("GAME OVER", width/2-50, height/2);
score=0;
}
void mouseReleased() {
if (gamestate==3)
gamestate=2;
}
void shootingStuff() {
for (int i=0;i<projectiles.size();i++) {
PVector p=(PVector)projectiles.get(i);
for (int j=0;j<asteroids.size();j++) {
PVector a=(PVector)asteroids.get(j);
if (p.y-5<a.y+10 && p.x+1.25>a.x-10 && p.x-1.25<a.x+10) {
asteroids. remove(a);
projectiles.remove(p);
multiplier +=.1;
score +=100*multiplier;
}
}
}
}
1