Skip to content

Instantly share code, notes, and snippets.

@ozziexsh
Created January 5, 2015 05:22
Show Gist options
  • Save ozziexsh/129d5f20493952fb0eb6 to your computer and use it in GitHub Desktop.
Save ozziexsh/129d5f20493952fb0eb6 to your computer and use it in GitHub Desktop.
Shooting Game
package com.teyg;
import java.io.IOException;
import java.nio.file.Paths;
import org.jsfml.graphics.FloatRect;
import org.jsfml.graphics.RenderWindow;
import org.jsfml.graphics.Sprite;
import org.jsfml.graphics.Texture;
import org.jsfml.system.Vector2f;
public class Bullet {
public float x, y;
public float speed;
private Texture bTexture = new Texture();
private Sprite bSprite;
public FloatRect bounds;
public Bullet(float x, float y) {
this.x = x + 10;
this.y = y + 10;
speed = 15;
try {
bTexture.loadFromFile(Paths.get("assets/bullet.png"));
bTexture.setSmooth(true);
} catch(IOException e) {
e.printStackTrace();
}
bSprite = new Sprite(bTexture);
bSprite.setOrigin(Vector2f.div(new Vector2f(bTexture.getSize()), 2));
bSprite.setPosition(x, y);
bounds = bSprite.getGlobalBounds();
}
public void update() {
x += speed;
bSprite.setPosition(x + 30, y - 30);
bSprite.setScale(0.5f, 0.5f);
bounds = new FloatRect(x, y, bTexture.getSize().x, bTexture.getSize().y);
}
public void render(RenderWindow window) {
window.draw(bSprite);
}
}
package com.teyg;
import java.io.IOException;
import java.nio.file.Paths;
import org.jsfml.graphics.FloatRect;
import org.jsfml.graphics.RenderWindow;
import org.jsfml.graphics.Sprite;
import org.jsfml.graphics.Texture;
import org.jsfml.system.Vector2f;
public class Enemy {
public float x, y;
public float speed;
private Texture eTexture = new Texture();
private Sprite eSprite;
private Texture healthTexture = new Texture();
private Texture healthBgText = new Texture();
private Sprite healthSprite;
private Sprite healthBgSprite;
public FloatRect bounds;
public boolean isVisible = true;
public int health = 50;
public Enemy(float x, float y) {
this.x = x;
this.y = y;
speed = 5;
try {
eTexture.loadFromFile(Paths.get("assets/enemy.png"));
healthTexture.loadFromFile(Paths.get("assets/health.png"));
healthBgText.loadFromFile(Paths.get("assets/healthbg.png"));
} catch (IOException e) {
e.printStackTrace();
}
eSprite = new Sprite(eTexture);
eSprite.setOrigin(Vector2f.div(new Vector2f(eTexture.getSize()), 2));
healthSprite = new Sprite(healthTexture);
healthSprite.setOrigin(Vector2f.div(new Vector2f(healthTexture.getSize()), 2));
healthBgSprite = new Sprite(healthBgText);
healthBgSprite.setOrigin(Vector2f.div(new Vector2f(healthBgText.getSize()), 2));
bounds = eSprite.getGlobalBounds();
}
public void update() {
x -= speed;
eSprite.setPosition(x, y);
healthSprite.setPosition(x, y - 85);
healthSprite.setScale(new Vector2f(0.1f * health, 0.15f));
healthBgSprite.setPosition(x, y - 85);
healthBgSprite.setScale(new Vector2f(0.1f * health, 0.2f));
bounds = eSprite.getGlobalBounds();
}
public void render(RenderWindow window) {
window.draw(eSprite);
window.draw(healthBgSprite);
window.draw(healthSprite);
}
}
package com.teyg;
import java.util.ArrayList;
import java.util.Random;
import org.jsfml.graphics.Color;
import org.jsfml.graphics.RenderWindow;
import org.jsfml.window.VideoMode;
import org.jsfml.window.event.Event;
import org.jsfml.window.event.KeyEvent;
public class Main {
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
Random random = new Random();
Player player = new Player();
ArrayList<Enemy> enemy = new ArrayList<Enemy>();
boolean flag = false;
public Main() {
RenderWindow window = new RenderWindow();
window.create(new VideoMode(800, 600), "To Be Finished: The Game");
window.setFramerateLimit(30);
while (window.isOpen()) {
window.clear(Color.BLACK);
if (enemy.size() <= 3) {
float rX = random.nextFloat() * ((HEIGHT - 100) - 0) + 0;
enemy.add(new Enemy(900, rX));
}
player.update();
player.render(window);
for (Enemy e : enemy) {
e.update();
e.render(window);
}
for(int i = 0; i < player.bullets.size(); i++) {
for (int x = 0; x < enemy.size(); x++) {
if (player.bullets.get(i).bounds.intersection(enemy.get(x).bounds) != null) {
enemy.remove(x);
flag = true;
}
}
if (flag) {
player.bullets.remove(i);
flag = false;
}
}
for (int i = 0; i < enemy.size(); i++) {
if (player.bounds.intersection(enemy.get(i).bounds) != null) {
if (player.health > 0) {
player.health -= 10;
enemy.remove(i);
}
}
}
window.display();
for (Event event : window.pollEvents()) {
switch (event.type) {
case CLOSED:
window.close();
break;
case KEY_PRESSED:
KeyEvent keyEvent = event.asKeyEvent();
player.shoot(keyEvent.key);
}
}
}
}
public static void main(String[] args) {
Main main = new Main();
}
}
package com.teyg;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import org.jsfml.graphics.FloatRect;
import org.jsfml.graphics.RenderWindow;
import org.jsfml.graphics.Sprite;
import org.jsfml.graphics.Texture;
import org.jsfml.system.Vector2f;
import org.jsfml.window.Keyboard;
public class Player {
public float x, y;
public float speed;
private Texture pTexture = new Texture();
private Texture healthTexture = new Texture();
private Texture healthBgText = new Texture();
private Sprite pSprite;
private Sprite healthSprite;
private Sprite healthBgSprite;
public FloatRect bounds;
public int health = 50;
public ArrayList<Bullet> bullets = new ArrayList<Bullet>();
public Player() {
x = 400;
y = 300;
speed = 10;
try {
pTexture.loadFromFile(Paths.get("assets/player.png"));
pTexture.setSmooth(true);
healthTexture.loadFromFile(Paths.get("assets/health.png"));
healthBgText.loadFromFile(Paths.get("assets/healthbg.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pSprite = new Sprite(pTexture);
pSprite.setOrigin(Vector2f.div(new Vector2f(pTexture.getSize()), 2));
pSprite.setScale(new Vector2f(0.5f, 0.5f));
pSprite.setPosition(400, 300);
healthSprite = new Sprite(healthTexture);
healthSprite.setOrigin(Vector2f.div(new Vector2f(healthTexture.getSize()), 2));
healthBgSprite = new Sprite(healthBgText);
healthBgSprite.setOrigin(Vector2f.div(new Vector2f(healthBgText.getSize()), 2));
bounds = pSprite.getGlobalBounds();
}
public void update() {
if (Keyboard.isKeyPressed(Keyboard.Key.S)) {
y += speed;
}
if (Keyboard.isKeyPressed(Keyboard.Key.W)) {
y -= speed;
}
if (Keyboard.isKeyPressed(Keyboard.Key.A)) {
x -= speed;
}
if (Keyboard.isKeyPressed(Keyboard.Key.D)) {
x += speed;
}
Iterator<Bullet> i = bullets.iterator();
while(i.hasNext()) {
Bullet b = i.next();
b.update();
if (b.y >= 800) {
i.remove();
}
}
pSprite.setPosition(x, y);
healthSprite.setPosition(x, y - 85);
healthSprite.setScale(new Vector2f(0.1f * health, 0.15f));
healthBgSprite.setPosition(x, y - 85);
healthBgSprite.setScale(new Vector2f(0.1f * health, 0.2f));
bounds = pSprite.getGlobalBounds();
}
public void shoot(Keyboard.Key key) {
if (key == Keyboard.Key.SPACE) {
bullets.add(new Bullet(x, y));
}
}
public void render(RenderWindow window) {
window.draw(pSprite);
//window.draw(healthBgSprite);
window.draw(healthSprite);
for (Bullet b : bullets) {
b.render(window);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment