I used the java processing library to create a game called asteroids. In my game an asteroid is spawned from the screen and the user controls a rocket ship attempting to shot them. If an asteroid is shot, the score is incremented by 1.
import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PImage;
import java.util.Random;
public class Main extends PApplet {
PImage background;
PImage asteroid;
PFont f;
public static void main(String[] args) {
PApplet.main("Main");
}
public float speed = 10;
public float x = 400;
public float y = 350;
public float rotationAmount = 0;
public boolean moveForward = false;
public boolean moveBackward = false;
public boolean moving = false; //displays the fire
public boolean rotateLeft = false;
public boolean rotateRight = false;
public boolean fastBoi = false;
public boolean isFired = false;
public float bulletX = 0;
public float bulletY = 0;
public boolean moveUp = false;
public boolean moveDown = false;
public boolean collision = false;
float[] yCoordinates = {150, 10, 200, 300, 50};
float[] xCoordinates = {150, 10, 200, 300, 50};
Random generator = new Random();
int coordinateIndexY = generator.nextInt(yCoordinates.length);
public float yCoordinate = yCoordinates[coordinateIndexY];
int coordinateIndexX = generator.nextInt(xCoordinates.length);
public float xCoordinate = xCoordinates[coordinateIndexX];
public int score = 0;
public void settings() {
size(800, 800);
}
public void setup() {
background = loadImage("background.jpg");
asteroid = loadImage("asteroid.png");
f = createFont("Arial", 16, true);
}
public void draw() {
background(background);
randomAsteroid();
textFont(f, 50);
fill(0, 0, 255);
text("Score: " + score, 10, 50);
//Move and control the rocket ship.
move();
changeRotation();
boundaries();
bullet();
translate(x, y);
rotate(rotationAmount);
drawRocketShip();
}
//Set up and draw the rocket ship.
public int rocketX = 0;
public int rocketY = 0;
public void drawRocketShip() {
//Body color.
stroke(0, 149, 185);
fill(0, 149, 185);
//Body
rect(rocketX, rocketY, 75, 50);
//front
triangle(rocketX + 75, rocketY + 1, 100, rocketY + 25, rocketX + 75, rocketY + 49);
//window color
fill(255, 255, 255);
//window
ellipse(rocketX + 60, rocketY + 25, 30, 15);
//wings
stroke(0, 149, 185);
strokeWeight(3);
fill(255, 255, 255);
triangle(rocketX + 25, rocketY, rocketX - 15, rocketY - 25, rocketX, rocketY);
triangle(rocketX + 25, rocketY + 50, rocketX - 15, rocketY + 75, rocketX, rocketY + 50);
if (moving) {
//Fire trail
fill(255, 0, 0);
noStroke();
triangle(rocketX - 10, rocketY + 10, rocketX - 30, rocketY + 25, rocketX - 10, rocketY + 40);
}
}
public void keyPressed() {
if (key == 'd') {
moveForward = true;
moving = true;
} else if (key == '1') {
rotateLeft = true;
moving = true;
} else if (key == '2') {
rotateRight = true;
moving = true;
} else if (key == 'a') {
moveBackward = true;
moving = true;
} else if (key == 'f') {
fastBoi = true;
moving = true;
} else if (key == 'n') {
isFired = true;
OnCollision();
} else if (key == 'w') {
moveUp = true;
moving = true;
} else if (key == 's') {
moveDown = true;
moving = true;
}
}
public void keyReleased() {
if (key == 'd') {
moveForward = false;
moving = false;
} else if (key == '1') {
rotateLeft = false;
moving = false;
} else if (key == '2') {
rotateRight = false;
moving = false;
} else if (key == 'a') {
moveBackward = false;
moving = false;
} else if (key == 'f') {
fastBoi = false;
moving = false;
} else if (key == 'n') {
isFired = false;
} else if (key == 'w') {
moveUp = false;
moving = false;
} else if (key == 's') {
moveDown = false;
moving = false;
}
}
public void move() {
if (moveForward) {
if (fastBoi) {
x += (speed * 5) * cos(rotationAmount);
y += (speed * 5) * sin(rotationAmount);
} else {
x += speed * cos(rotationAmount);
y += speed * sin(rotationAmount);
}
} else if (moveBackward) {
if (fastBoi) {
x -= (speed * 5) * cos(rotationAmount);
y -= (speed * 5) * sin(rotationAmount);
} else {
x -= speed * cos(rotationAmount);
y -= speed * sin(rotationAmount);
}
} else if (moveUp) {
x -= speed * sin(rotationAmount);
y -= speed * cos(rotationAmount);
} else if (moveDown) {
x += speed * sin(rotationAmount);
y += speed * cos(rotationAmount);
}
}
public void changeRotation() {
if (rotateLeft) {
rotationAmount -= 0.8;
} else if (rotationAmount < 0) {
rotationAmount = 2 * PI;
} else if (rotateRight) {
rotationAmount += 0.8;
}
}
public void boundaries() {
if (x < 70) {
x = 70;
} else if (y < 80) {
y = 80;
} else if (x > 650) {
x = 650;
} else if (y > 650) {
y = 650;
}
}
public void bullet() {
if (isFired) {
firing();
}
}
public void firing() {
bulletX = x + 150;
bulletY = y + 20;
ellipse(bulletX, bulletY, 20, 20);
}
public void OnCollision() {
if (sqrt((bulletX - xCoordinate) * (bulletX - xCoordinate) + (bulletY - yCoordinate) * (bulletY - yCoordinate)) < 75) { //75
rect(150, 150, 150, 150);
score++;
collision = true;
}
}
public void randomAsteroid() {
image(asteroid, xCoordinate, yCoordinate);
if (xCoordinate < 70) {
xCoordinate = 70;
} else if (yCoordinate < 80) {
yCoordinate = 80;
} else if (xCoordinate > 650) {
xCoordinate = 70;
} else if (yCoordinate > 650) {
yCoordinate = 650;
}
xCoordinate += (speed * 0.3) * cos(rotationAmount);
}
}