Skip to content

Instantly share code, notes, and snippets.

@spotco
Last active January 3, 2016 10:59
Show Gist options
  • Select an option

  • Save spotco/8452705 to your computer and use it in GitHub Desktop.

Select an option

Save spotco/8452705 to your computer and use it in GitHub Desktop.
ShooterGame
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.util.*;
public class ShooterGame extends GamePanel {
public static void main(String[] args) {
new ShooterGame(500,500);
}
public enum Side {
LEFT, RIGHT, UP, DOWN
}
public static Random rand = new Random();
public int _player_x, _player_y;
public boolean _player_dead = false;
public Side _last_facing;
public ArrayList<Bullet> _player_bullets = new ArrayList<Bullet>();
public ArrayList<Enemy> _enemies = new ArrayList<Enemy>();
public ShooterGame(int width, int height) {
super(width, height);
_player_x = this.get_width()/2;
_player_y = this.get_height()/2;
_last_facing = Side.UP;
}
@Override
public void update() {
this.clear();
if (_player_dead) {
_g.setColor(Color.red);
_g.drawString("(Press R to respawn)", this.get_width()/2-60, this.get_height()/2);
if (this.is_key_down(KeyEvent.VK_R)) {
_player_x = this.get_width()/2;
_player_y = this.get_height()/2;
_player_bullets.clear();
_enemies.clear();
_player_dead = false;
}
} else {
_g.setColor(Color.black);
_g.drawString("Arrow keys to move, CTRL to shoot", 0, this.get_height());
update_player();
update_enemies();
update_bullets();
}
}
private void update_player() {
if (this.is_key_down(KEY_UP)) {
_player_y-=3;
_last_facing = Side.UP;
}
if (this.is_key_down(KEY_DOWN)) {
_player_y+=3;
_last_facing = Side.DOWN;
}
if (this.is_key_down(KEY_LEFT)) {
_player_x-=3;
_last_facing = Side.LEFT;
}
if (this.is_key_down(KEY_RIGHT)) {
_player_x+=3;
_last_facing = Side.RIGHT;
}
if (this.is_key_down(KeyEvent.VK_CONTROL)) {
int bvx = 0, bvy = 0;
if (_last_facing == Side.LEFT) {
bvx = -8;
} else if (_last_facing == Side.RIGHT) {
bvx = 8;
}
if (_last_facing == Side.UP) {
bvy = -8;
} else if (_last_facing == Side.DOWN) {
bvy = 8;
}
Bullet neu = new Bullet(_player_x, _player_y, bvx, bvy);
_player_bullets.add(neu);
}
_g.setColor(Color.green);
_g.fillOval(_player_x-5, _player_y-5, 10, 10);
}
private void update_bullets() {
for (int i_bullet = _player_bullets.size()-1; i_bullet >= 0; i_bullet--) {
Bullet itr_bullet = _player_bullets.get(i_bullet);
itr_bullet.update(this);
if (!this.point_on_screen(itr_bullet._x, itr_bullet._y)) {
_player_bullets.remove(itr_bullet);
}
}
}
private void update_enemies() {
for (int i_enemy = _enemies.size()-1; i_enemy >= 0; i_enemy--) {
Enemy itr_enemy = _enemies.get(i_enemy);
itr_enemy.update(this);
boolean do_remove = false;
for (int i_bullet = _player_bullets.size()-1; i_bullet >= 0; i_bullet--) {
Bullet itr_bullet = _player_bullets.get(i_bullet);
if (is_collide(itr_enemy._x, itr_enemy._y, itr_bullet._x, itr_bullet._y, 5 + 3)) {
_player_bullets.remove(itr_bullet);
do_remove = true;
break;
}
}
if (do_remove) {
_enemies.remove(itr_enemy);
continue;
}
if (is_collide(itr_enemy._x, itr_enemy._y, _player_x, _player_y, 5 + 5)) {
_player_dead = true;
}
}
if (rand.nextInt(20)==0) {
int side = rand.nextInt(4);
int ex = 0, ey = 0;
if (side == 0) {
ex = rand.nextInt(this.get_width());
} else if (side == 1) {
ex = rand.nextInt(this.get_width());
ey = this.get_height();
} else if (side == 2) {
ey = rand.nextInt(this.get_height());
} else {
ex = this.get_width();
ey = rand.nextInt(this.get_height());
}
_enemies.add(new Enemy(ex,ey));
}
}
private boolean point_on_screen(int x, int y) {
return (x > 0) && (x < this.get_width()) && (y > 0) && (y < this.get_height());
}
public boolean is_collide(int x1, int y1, int x2, int y2, int radius_sum){
double dist = Math.sqrt(Math.pow(x2-x1, 2) + Math.pow(y2-y1, 2));
return dist <= radius_sum;
}
public class Bullet {
public int _x, _y, _vx, _vy;
public Bullet(int x, int y, int vx, int vy) {
_x = x;
_y = y;
_vx = vx;
_vy = vy;
}
public void update(ShooterGame game) {
_x += _vx;
_y += _vy;
game._g.setColor(Color.blue);
game._g.fillOval(_x-3, _y-3, 6, 6);
}
}
public class Enemy {
public int _x, _y;
public Enemy(int x, int y) {
_x = x;
_y = y;
}
public void update(ShooterGame game) {
if (rand.nextInt(2)==0) {
if (_x > game._player_x) {
_x-=2;
} else {
_x+=2;
}
} else {
if (_y > game._player_y) {
_y-=2;
} else {
_y+=2;
}
}
game._g.setColor(Color.red);
game._g.fillOval(_x-5, _y-5, 10, 10);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment