Skip to content

Instantly share code, notes, and snippets.

@criptography
Created December 20, 2018 16:01
Show Gist options
  • Save criptography/ee60753da79b3ff43205dac5bd6c4e37 to your computer and use it in GitHub Desktop.
Save criptography/ee60753da79b3ff43205dac5bd6c4e37 to your computer and use it in GitHub Desktop.
Space Invaders
class Ship {
//properties
float shipPosX;
float shipPosY;
//constructor
Ship (float shipPosX, float shipPosY) {
this.shipPosX = shipPosX;
this.shipPosY = shipPosY;
}
//returns if the ship is close to the edge
boolean isCloseToEdge (int spacingLeft) {
if (shipPosX >= width - spacingLeft || shipPosX <= spacingLeft) {
return true;
} else return false;
}
//shifts the ship into another row
void incrementRow (int spacingTop) {
shipPosY += spacingTop;
}
//moves the ship to the left / right
void moveShip (float calculatedSpeed) {
shipPosX += calculatedSpeed;
}
//draws the ship
void displayShip () {
strokeWeight(3);
stroke (255, 255, 255);
//individual points that form the ship
float tx1 = shipPosX - 10;
float ty1 = shipPosY - 5;
float tx2 = shipPosX + 10;
float ty2 = shipPosY - 5;
float tx3 = shipPosX;
float ty3 = shipPosY + 15;
//construct a triangle from the points
triangle (tx1, ty1, tx2, ty2, tx3, ty3);
point (shipPosX, shipPosY); // draw center of the ship
}
}
/* ------------------ SPACE INVADERS --------------------*/
/* -------------- Programming Task RC 2018 ------------- */
int shipCount = 55; //total number of ships
ArrayList<Ship> ships = new ArrayList<Ship>(); //array list for ships
int distBetweenShips = 40; //distance between individual ships
int spacingLeft = 40, spacingTop = 40;
float direction = 1;
float speed = 0.3f; //speed in which the ships move
color playerColor = color (200, 10, 0); //color of the player's ship
float playerMovement; //pos for the player's movement
String t = "SPACE INVADERS"; //title (will be score later)
void setup () {
size (600, 600);
background (0);
fill (0);
//initialize player
playerMovement = width/2;
// instantiate ships and their positions
int row = 0;
for (int i=0; i<=shipCount-1; i++) { // go through all the ships
int modulo = 11; // max number of ship per row
if (i%modulo == 0) { //if there are more ships than the max number...
row++; //...a new row is made available
}
float xPos = spacingLeft + i%modulo * distBetweenShips; //set the ships xPos
float yPos = spacingTop + row * distBetweenShips; //set the ships yPos acc to their row
ships.add(new Ship(xPos, yPos)); //add the ships to the list of ships
}
}
void draw () {
background(0);
boolean moveDown = false;
noFill();
for (int i=0; i<=ships.size()-1; i++) {
//check if a ship is close to the game's edge
if (ships.get(i).isCloseToEdge(spacingLeft-1)==true) {
direction*=-1; //turn the ship's direction around
moveDown = true;
break;
}
}
for (int i=0; i<=ships.size()-1; i++) {
//go through the list of all ships to..
if (moveDown) ships.get(i).incrementRow(spacingTop/2); //shift a ship into the next row
ships.get(i).moveShip(direction * speed); //move a ship
ships.get(i).displayShip(); //display a ship
}
playerCharacterControls(); //control the player's ship
displayText(); //display the title
}
void playerCharacterControls () {
//player styles
stroke(playerColor);
if (keyPressed) {
//if a key is pressed
if (key == CODED) {
if (keyCode == LEFT) {
//move to the left for the left arrow key
playerMovement -= 2.5f;
} else if (keyCode == RIGHT) {
//move to the right for the right arrow key
playerMovement += 2.5f;
}
}
}
float pY = height - height/7; //local variable to set the y pos of the player
float pWidth = 20; //width of the player's ship
float pHeight = 20; //height of the player's ship
//contrain the player ship's movement so it can't leave the screen
playerMovement = constrain(playerMovement, 0 + spacingLeft, width - spacingLeft);
rect(playerMovement - pWidth/2, pY - pHeight/2, pWidth, pHeight); // draw rect
point (playerMovement, pY); // draw center of the rect
}
void displayText () {
textSize(12);
fill (255);
text (t, 250, 20);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment