//Asteroids game //Player that moves in zero g //asteroids that do the same //player dies if collide with any asteroid, using dist for circular style detection //Position and Speed variables for the Player float px, py; float sx, sy; //Position and Speed variables for the Fixed Array asteroids int numAst = 5; float[] ax, ay, sax, say; //Position and Sped ofr the ArrayList version of the asteroids ArrayList Ax, Ay, sAx, sAy; void setup() { size(640, 480); //Set the screen Size px = width / 2; //Initial position of the player py = height / 2; sx = 0; //Initial Speed of the player, see movement in keyPresses sy = 0; //Initialize the Arrays, just gets the memory ready, values come after ax = new float[numAst]; ay = new float[numAst]; sax = new float[numAst]; say = new float[numAst]; //Putting values into the blank entries of the Arrays for (int i=0; i < numAst; i++) { ax[i] = randInt(100, width - 100); ay[i] = randInt(100, width - 100); sax[i] = randInt(-2, 2); say[i] = randInt(-2, 2); } //Initialize the ArrayList versions of the asteriods //After this they are ready to use, but current'y have no data Ax = new ArrayList(); Ay = new ArrayList(); sAx = new ArrayList(); sAy = new ArrayList(); //Adding 1 asteriod to the lists //For more, you can use a forloop, of go see keyPressed where I made more with the SPACEBAR Ax.add(randInt(100, width - 100)); Ay.add(randInt(100, width - 100)); sAx.add(randInt(-2, 2)); sAy.add(randInt(-2, 2)); } //Gives me random numbers on command! float randInt(int s, int e) { return random(e-s) + s; } void draw() { background(0); //Makes the game look like outer space //Do the movement "physics" for the player px += sx; //newPosition = Position + speed py += sy; //Teleport the player if they move off the edges of the screen if (py < 0) py = height; else if (py > height) py = 0; if (px < 0) px = width; else if (px > width) px = 0; //Draw the player fill(255); ellipse(px, py, 20, 20); //Asteroid code goes here //This code is conceptually the same as the player. //Move, teleport, draw //Make these asteroids RED fill(255, 0, 0); for (int i=0; i < numAst; i++) {//For all the asteriods in the fixed sized arrays //Move ax[i] += sax[i]; ay[i] += say[i]; //Teleport if (ay[i] < 0) ay[i] = height; else if (ay[i] > height) ay[i] = 0; if (ax[i] < 0) ax[i] = width; else if (ax[i] > width) ax[i] = 0; //Draw //radius is 25: needed for collision detection later ellipse(ax[i], ay[i], 50, 50); //Collision with player //if(distance between player and asteroid is less then radiusPlayer + radiusAsteroid if(dist(px,py, ax[i], ay[i]) < 10 + 25){ println("you are now dead from rigid thinking"); } } //Same as above, but with ArrayLists instead of Arrays //I made these ones GREEN.. because outer space. fill(0, 255, 0); for (int i=0; i < Ax.size(); i++) { //x += sx; This is what I am doing with movement. newPosition = currentPosition + speed //Long form // CurrentPos + Speed float newValue = Ax.get(i) + sAx.get(i); Ax.set(i, newValue);//Update the array value //Compact form //You can do the same as above in one line if you prefer Ay.set(i, Ay.get(i) + sAy.get(i)); //Teleport //This is structurally identical to the two other teleports I did above. //.get(i) to get a copy of the value so I can check it //.set(i, to update the position value with a new one. TA DAAA is teleport. if (Ay.get(i) < 0) Ay.set(i, (float)height); else if (Ay.get(i) > height) Ay.set(i, 0.0); if (Ax.get(i) < 0) Ax.set(i, (float)width); else if (Ax.get(i)> width) Ax.set(i, 0.0); //Draw the asteroid ellipse(Ax.get(i), Ay.get(i), 30, 30); //Exact same distance calc for collision detection if(dist(px,py, Ax.get(i), Ay.get(i)) < 15 + 10){ println("you are now dead from arraylist syntax"); //I decided to remove any ArrayList asteroid I hit, becuase why not //Just use .remove(index) if you need to remove something from an ArrayList //Remember, fixed size arrays [] can't do this at all, they have so few options Ax.remove(i); Ay.remove(i); sAx.remove(i); sAy.remove(i); } } //Super crazy bonus type extra marks feature //Its asteriods colliding with each other. Write this to build character, its not on the test. for(int i=0 ; i < numAst; i++){ for(int j=0; j < numAst; j++){ if(i != j && dist(ax[i], ay[i], ax[j], ay[j]) < 15 + 25){ sax[i] *= -1; say[i] *= -1; break; } } } } void keyPressed() { if (key == 'w') {//Up //sy = -2; //Just sets the speed value, its not smooth acceleration style sy -= 0.1; //But slowly changing the speed, we get a smooth accelleration style of movement //Relax its not on the test. } else if (key == 's') {//Down // sy = 2; sy += 0.1; } else if (key == 'd') {//Right // sx = 2; sx += 0.1; } else if (key == 'a') {//Left // sx = -2; //Regular simple movement sx -= 0.1; //Super fun times accel movement } else if (key == ' ') {//SPACEBAR //An asteroid is 4 values. position x & y, speed for x & y Ax.add(randInt(100, width - 100)); Ay.add(randInt(100, width - 100)); sAx.add(randInt(-2, 2)); sAy.add(randInt(-2, 2)); } }