Fate1
Apprentice

- Joined
- Apr 21, 2005
- Sythe Gold
- 5
- Messages
- 773
- Age
- 34
I've been thinking of leaning more towards being a game developer instead of a software developer as I intended. This was my first game that I tried to make. Nothing special, simple graphics. Took me a couple hours overall to make since it was my first. I am wanting to learn some more and hopefully can pursue this as my career but anywho here it is.
Feel free to use the source of this anyway you like.
Runs in an applet btw.
Any constructive criticism would be nice 
Feel free to use the source of this anyway you like.
Runs in an applet btw.
Code:
package shootergame;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
/**
* Space invaders like game.
* @author Fate
* @version Nov 30, 2010
*
*/
public class ShooterGame extends Applet implements Runnable, KeyListener {
private Graphics gfx;
private boolean running = true;
private boolean moveUp = false;
private boolean moveDown = false;
private boolean moveLeft = false;
private boolean moveRight = false;
private int fps = 50;
private int bulletCounter;
private int upgradesCounter;
private int enemyCounter;
private int weaponDamage = 1;
private int weaponUpgradeNumber = 1;
private int score = 0;
private Image img;
private Player p;
private ArrayList<Bullets> bullets;
private ArrayList<Upgrades> upgrades;
private ArrayList<Enemy> enemies;
@Override
public void init() {
setSize(300, 400);
setBackground(Color.BLACK);
addKeyListener(this);
p = new Player(getWidth() / 2, getHeight() - 55, 30, 8, Color.RED);
bullets = new ArrayList<Bullets>();
upgrades = new ArrayList<Upgrades>();
enemies = new ArrayList<Enemy>();
}
@Override
public void start() {
Thread th = new Thread(this);
th.start();
}
public void run() {
while (running) {
repaint();
try {
Thread.sleep(1000 / fps);
} catch (Exception e) {
}
}
}
@Override
public void stop() {
running = false;
}
@Override
public void destroy() {
running = false;
}
@Override
public void paint(Graphics g) {
if (p.health > 0) {
/*Displays health bar, score, and weapon.*/
g.setColor(Color.RED);
g.fillRect(10, getHeight() - 20, p.health / 10, 12);
g.setColor(Color.WHITE);
g.drawString(("Health Bar"), 10, getHeight() - 25);
g.drawString("" + p.health, 40, getHeight() - 10);
g.drawString("Weapon Upgrade: " + weaponUpgradeNumber, getWidth() - 130, getHeight() - 25);
g.drawString("Score: " + score, getWidth() - 130, getHeight() - 10);
g.drawLine(0, getHeight() - 40, getWidth(), getHeight() - 40);
//draws all bullets in the ArrayList
for (Bullets b : bullets) {
b.draw(g);
}
//draws all the upgrades in the ArrayList
for (Upgrades u : upgrades) {
u.draw(g);
}
//draws all the enemies in the ArrayList
for (Enemy e : enemies) {
e.drawImage(g);
}
update();
} else if (p.health <= 0) { //if players health is 0 or less game ends.
g.setColor(Color.WHITE);
g.drawString("GAME OVER", getWidth() / 2, getHeight() / 2);
}
p.draw(g);
}
/*
* Double buffer method
*/
@Override
public void update(Graphics g) {
if (img == null) {
img = createImage(this.getWidth(), this.getHeight());
gfx = img.getGraphics();
}
gfx.setColor(getBackground());
gfx.fillRect(0, 0, this.getWidth(), this.getHeight());
gfx.setColor(getForeground());
paint(gfx);
g.drawImage(img, 0, 0, this);
}
//Allows the player to move around A LOT smoother
public void update() {
//moves the player up down left right
if (moveUp && p.y > 0) {
p.moveUp();
}
if (moveDown && p.y + p.height < getHeight() - 42) {
p.moveDown();
}
if (moveLeft && p.x > 0) {
p.moveLeft();
}
if (moveRight && p.x + p.width < getWidth()) {
p.moveRight();
}
/*
* has 10 / 500 chance to spawn an enemy. If it spawns one that chance
* that another one will spawn is higher. So the more enemies that spawn
* the faster they will respawn.
*/
Random ra = new Random();
int rand = ra.nextInt(500);
int chance = 10;
if (rand < chance) {
enemyCounter = 1;
chance++;
} else {
enemyCounter = 0;
}
//creates an enemy with a random health amount and random speed
if (enemyCounter == 1) {
Enemy e = null;
Random r = new Random();
int x = r.nextInt(getWidth());
int randHealth = r.nextInt(500);
int randVelocity = r.nextInt(5) + 1;
e = new Enemy(x, 0, 32, 32, randHealth, randVelocity, "spaceMonster.png");
enemies.add(e);
repaint();
enemyCounter++;
}
//checks to see if upgradeCounter is equal to 1
//if it is it will create a random upgrade and add it to the ArrayList
if (upgradesCounter == 1) {
Random r = new Random();
Upgrades u = null;
int randomUpgrade = r.nextInt(30);
int x = r.nextInt(getWidth());
if (randomUpgrade <= 10) { //restores health
u = new Upgrades(x, 0, 3, 3, Color.RED);
}
if (randomUpgrade > 10 && randomUpgrade <= 20) { //speed upgrade
u = new Upgrades(x, 0, 3, 3, Color.GREEN);
}
if (randomUpgrade > 20 && randomUpgrade <= 30) { //weapon upgrade
u = new Upgrades(x, 0, 3, 3, Color.ORANGE);
}
upgrades.add(u);
repaint();
upgradesCounter++;
}
//checks to see if bulletCounter is equal to 1
//if it is it will create a bullet and add it to the ArrayList
if (bulletCounter == 1) {
Bullets b = null;
if (weaponUpgradeNumber == 1) { //first gun you have
b = new Bullets(p.x + 13, p.y - p.height + 2, 3, 3, Color.YELLOW);
weaponDamage = 100;
}
if (weaponUpgradeNumber == 2) { //second gun with bigger bullets and higher damage
b = new Bullets(p.x + 13, p.y - p.height + 2, 6, 6, Color.YELLOW);
weaponDamage = 200;
}
if (weaponUpgradeNumber == 3) { //same bullet size as the second gun but shoots two at a time
Bullets c;
b = new Bullets(p.x + 8, p.y - p.height + 2, 6, 6, Color.YELLOW);
c = new Bullets(p.x + 18, p.y - p.height + 2, 6, 6, Color.YELLOW);
bullets.add(c);
weaponDamage = 200;
}
if (weaponUpgradeNumber >= 4) { //single shot but higher damage
b = new Bullets(p.x + 13, p.y - p.height + 2, 8, 8, Color.YELLOW);
weaponDamage = 500;
}
bullets.add(b);
repaint();
bulletCounter++;
}
//Moves enemies toward the player. If the enemy passes they
//interface line it will be deleted. If the enemy intersects with
//the player, the player will be damaged and the enemy will be removed
//if enemy intersects with bullet, the bullet will damage enemy
//health depending on what gun they have.
for (int j = 0; j < enemies.size(); j++) {
enemies.get(j).move();
if (enemies.get(j).y + enemies.get(j).height > getHeight() - 42) {
enemies.remove(j);
j--;
continue;
}
if (enemies.get(j).r.intersects(p.r)) {
enemies.remove(j);
p.health -= weaponDamage;
j--;
continue;
}
for (int i = 0; i < bullets.size(); i++) {
try {
if (enemies.get(j).r.intersects(bullets.get(i).r)) {
enemies.get(j).health -= 100;
bullets.remove(i);
if (enemies.get(j).health <= 0) {
enemies.remove(j);
score += 50;
continue;
}
}
} catch (Exception e) {
}
}
}
//this loops fires all bullets that are created
//if it moves off the screen or hits an enemy it will be
//removed from the ArrayList thus remocing it from the applet.
for (int i = 0; i < bullets.size(); i++) {
bullets.get(i).move();
if (bullets.get(i).y <= 0) {
bullets.remove(i);
i--;
continue;
}
}
/**
* Loops the size of the ArrayList checking if the player has
* collided with the upgrade. If so they player will receive
* that upgrade and the upgrade will be removed from the
* ArrayList thus removing it from the applet.
*/
for (int i = 0; i < upgrades.size(); i++) {
upgrades.get(i).move();
if (upgrades.get(i).y >= getHeight()) {
upgrades.remove(i);
i--;
continue;
}
if (upgrades.get(i).r.intersects(p.r)) {
if (upgrades.get(i).col == Color.GREEN) {
p.velocity++;
upgrades.remove(i);
i--;
continue;
}
if (upgrades.get(i).col == Color.RED) {
if (p.health < 1500) {
p.health += 100;
}
upgrades.remove(i);
i--;
continue;
}
if (upgrades.get(i).col == Color.ORANGE) {
weaponUpgradeNumber++;
upgrades.remove(i);
i--;
continue;
}
}
}
}
/**
* Basic movement for the player. Develops a random chance for an upgrade
* to be dropped when they player shoots.
*/
public void keyTyped(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_W) {
moveUp = true;
}
if (key == KeyEvent.VK_S) {
moveDown = true;
}
if (key == KeyEvent.VK_A) {
moveLeft = true;
}
if (key == KeyEvent.VK_D) {
moveRight = true;
}
if (key == KeyEvent.VK_SPACE) {
bulletCounter = 1;
Random r = new Random();
int rand = r.nextInt(1000);
if (rand == 3 || rand == 489 || rand == 968 || rand == 302) {
upgradesCounter = 1;
}
}
}
/**
* Basic movement for the player. Develops a random chance for an upgrade
* to be dropped when they player shoots.
*/
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_W) {
moveUp = true;
}
if (key == KeyEvent.VK_S) {
moveDown = true;
}
if (key == KeyEvent.VK_A) {
moveLeft = true;
}
if (key == KeyEvent.VK_D) {
moveRight = true;
}
if (key == KeyEvent.VK_SPACE) {
bulletCounter = 1;
Random r = new Random();
int rand = r.nextInt(1000);
if (rand == 3 || rand == 489 || rand == 968 || rand == 302) {
upgradesCounter = 1;
}
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_W) {
moveUp = false;
}
if (key == KeyEvent.VK_S) {
moveDown = false;
}
if (key == KeyEvent.VK_A) {
moveLeft = false;
}
if (key == KeyEvent.VK_D) {
moveRight = false;
}
if (key == KeyEvent.VK_SPACE) {
bulletCounter = 0;
}
}
}
Code:
package shootergame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
/**
* Enemy Class
* @author Fate
* @version Nov 30, 2010
*/
public class Enemy {
int x, y, height, width, velocity,health;
Image img;
Color col;
Rectangle r;
/**
* Constructor for creating an enemy as a rectangle
* @param x the x coord
* @param y the y coord
* @param height the height of the enemy
* @param width the width of the enemy
* @param health the total health the enemy has
* @param velocity the speed of the enemy
* @param col the color
*/
public Enemy(int x, int y, int height, int width,int health, int velocity, Color col) {
this.x = x;
this.y = y;
this.height = height;
this.width = width;
this.velocity = velocity;
this.col = col;
this.health = health;
img = null;
r = new Rectangle(x,y,height,width);
}
/**
*
* @param x the x coord
* @param y the y coord
* @param height the height of the enemy
* @param width the width of the enemy
* @param health the total health the enemy has
* @param velocity the speed of the enemy
* @param s the location of the image for the enemy
*/
public Enemy(int x, int y, int height, int width, int health, int velocity, String s) {
this.x = x;
this.y = y;
this.height = height;
this.width = width;
this.velocity = velocity;
img = Toolkit.getDefaultToolkit().getImage(s);
this.health = health;
col = null;
r = new Rectangle(x,y,height,width);
}
/**
* Draws the enemy as an image.
* @param s
*/
public void setImage(String s) {
img = Toolkit.getDefaultToolkit().getImage(s);
}
/**
* Moves the enemy downwards, as well as the rectangle for collision
*/
public void move() {
y += velocity;
r.setLocation(x,y); //moves the rectangle - for testing collision
}
public void draw(Graphics g) {
g.setColor(col);
g.fillRect(x, y, width, height);
}
public void drawImage(Graphics g) {
g.drawImage(img, x, y, null);
}
}
Code:
package shootergame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
/**
* Player class
* @author Fate
* @version Nov 29, 2010
*/
public class Player {
int velocity;
int x, y;
int width, height;
int health = 1000;
Color col;
Rectangle r;
Image img;
/**
* Constructor for display a rectangle
* @param x the x coord
* @param y the y coord
* @param width the width
* @param height the height
* @param col the color of the player
*/
public Player(int x, int y, int width, int height, Color col) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.col = col;
health = 1000;
velocity = 3;
r = new Rectangle(x, y, width, height);
}
/**
* Constructor for displaying an image for player
* @param x the x coord
* @param y the y coord
* @param width the width
* @param height the height
* @param s the location of the image
*/
public Player(int x, int y, int width, int height, String s) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
col = Color.RED;
health = 1000;
velocity = 3;
r = new Rectangle(x, y, width, height);
img = Toolkit.getDefaultToolkit().getImage(s);
}
public void draw(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(col);
g2.fillRect(x, y, width, height);
}
public void drawImage(Graphics g) {
g.drawImage(img, x, y, null);
}
public void setImage(String s) {
img = Toolkit.getDefaultToolkit().getImage(s);
}
public void moveLeft() {
x -= velocity;
r.setLocation(x,y);
}
public void moveRight() {
x += velocity;
r.setLocation(x,y);
}
public void moveUp() {
y -= velocity;
r.setLocation(x, y);
}
public void moveDown() {
y += velocity;
r.setLocation(x, y);
}
}
Code:
package shootergame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
/**
* Upgrade class
* @author Fate
* @version Nov 30, 2010
*/
public class Upgrades {
Rectangle r;
int x, y, width, height;
Color col;
/**
* Constructor for creating an upgrade as a rectangle
* @param x the x coord
* @param y the y coord
* @param width the width
* @param height the height
* @param col the color
*/
public Upgrades(int x, int y, int width, int height, Color col) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.col = col;
r = new Rectangle(x, y, width, height);
}
public void move() {
y += 2;
r.setLocation(x, y);
}
public void draw(Graphics g) {
g.setColor(col);
g.fillRect(x, y, width, height);
}
}
Code:
package shootergame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
/**
* Bullets class
* @author Fate
* @version Nov 29, 2010
*/
public class Bullets {
int x, y, width, height;
Color col;
int velocity;
Rectangle r;
public Bullets(int x, int y, int width, int height, Color col) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.col = col;
velocity = 10;
r = new Rectangle(x, y, width, height);
}
public void draw(Graphics g) {
g.setColor(col);
g.fillRect(x, y, width, height);
}
public void move() {
y -= velocity;
r.setLocation(x, y);
}
}

