Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.util.ArrayList;
- import javax.swing.JFrame;
- import org.jdesktop.application.Application;
- class Entity {
- Grid grid;
- int x;
- int y;
- public Entity(Grid grid) {
- this.grid = grid;
- x = 0;
- y = 0;
- }
- public void moveTo(int x, int y) {
- remove();
- this.x = x;
- this.y = y;
- grid.put(this.x, this.y, this);
- }
- public boolean remove() {
- if (grid.get(x, y) == this)
- grid.clear(x, y);
- else
- return false;
- return true;
- }
- public void process(long timemillis) {
- }
- public void render(long timemillis) {
- }
- }
- class Grid {
- int w = 30;
- int h = 10;
- public Entity[][] cells;
- public Grid(int w, int h) {
- this.w = w;
- this.h = h;
- cells = new Entity[h][w];
- }
- public Entity get(int x, int y) {
- return cells[y][x];
- }
- public void put(int x, int y, Entity entity) {
- cells[y][x] = entity;
- }
- public void clear(int x, int y) {
- cells[y][x] = null;
- }
- public void process(long timemillis) {
- // here you can process grid
- // like, change some tiles, progress animations, etc
- }
- public void render(long timemillis) {
- // here you can draw grid
- }
- }
- class Game {
- Grid grid;
- Player player;
- ArrayList<Entity> entities;
- int score;
- long lastProcess;
- public Game() {
- grid = new Grid(30, 20);
- }
- public void newGame() {
- lastProcess = 0;
- score = 0;
- player = new Player(this);
- entities = new ArrayList<>();
- }
- public void render(long timemillis) {
- grid.render(timemillis);
- player.render(timemillis);
- for (Entity entity : entities)
- entity.render(timemillis);
- }
- public void process(long timemillis) {
- grid.process(timemillis); // process grid (like, some animation)
- player.process(timemillis); // process player
- for (Entity entity : entities) // process entities
- entity.process(timemillis);
- // test player/G collisions
- Entity toRight = grid.get(player.x + 1, player.y);
- if (toRight != null) {
- // have something to the playes right
- if (toRight instanceof G) // it's a G instance!
- onGCollidedPlayer(player, (G) toRight); /*
- // also, if we have some other entity types, like bonuses/secrets
- // we can test them separately
- if (toRight instanceof Bonus) // it's a Bonus instance!
- onPlayerPickBonus(player, (Bonus) toRight);
- if (toRight instanceof Secret) // it's a Secret instance!
- onPlayerPickedSecret(player, (Secret) toRight);
- */
- }
- if (timemillis - lastProcess > 300) { // each 300 msec ()
- lastProcess = timemillis;
- Entity spawn = null;
- switch (((int)(Math.random() * 10)) % 1) {
- case 0: // spawn G
- spawn = new G(this); break;
- }
- if (spawn != null) {
- spawn.moveTo(grid.w, (int)(Math.random() * 9999) % grid.h);
- entities.add(spawn);
- }
- }
- }
- public void onGCollidedLeftEdge(G g) {
- // do somethidg on g/left-edge collision
- // like decreasing score
- score -= g.awardsPoints;
- }
- public void onGCollidedPlayer(Player player, G g) {
- // do somethidg on g/player collision
- // like increasing score
- score += g.awardsPoints;
- // also, remove colided G
- g.remove();
- }
- }
- // entity, sliding to left
- class G extends Entity {
- Game game;
- // speed of moving
- int speed;
- // some score points for capturing/losing this G
- int awardsPoints;
- // don't forget to redefine constructor
- public G(Game game) {
- super(game.grid);
- this.game = game;
- // can specify speed here
- speed = 1;
- awardsPoints = 10 + (int) (Math.random() * 100) % 91; // 10-100
- }
- // we're a "sliding to left entity"
- public void moveLeft(int distance) {
- int newX = x - distance;
- if (newX <= 0)
- // we're reached left grid edge
- // removing this G entity grom grid
- remove(); // and can make something else
- moveTo(newX, y);
- }
- // override parent process() method
- @Override
- public void process(long timemillis) {
- moveLeft(speed); // we should move left by amount, specified by "speed"
- }
- @Override
- public void render(long timemillis) {
- // here goes rendering code for G entities
- }
- }
- class Player extends G {
- public Player(Game game) {
- super(game);
- }
- public void moveVertically(int direction) {
- int newY = y + direction * speed;
- if (newY < 0)
- newY = 0;
- else
- if (newY >= grid.h)
- newY = grid.h - 1;
- moveTo(x, newY);
- }
- // override processing method if needed...
- @Override
- public void process(long timemillis) {
- }
- @Override
- public void render(long timemillis) {
- // here goes rendering code for player
- }
- }
- /**
- *
- * @author Ankh Zet (ankhzet@gmail.com)
- */
- public class Launcher extends Application {
- JFrame mainFrame = null;
- Thread gameThread;
- int renderFPS = 30; //
- int processPPS = 10;
- // five processings per second (around 1000 / 10 = 100 msec between each)
- boolean running;
- Game game;
- public static void main(String[] args) {
- launch(Launcher.class, args);
- }
- @Override
- protected void startup() {
- running = false;
- game = new Game();
- gameThread = new Thread(new Runnable() {
- @Override
- public void run() {
- running = true;
- // here goes the rendering/processing "meat";
- long lastIteration = System.currentTimeMillis();
- long lastRender = lastIteration;
- long lastProcess = lastIteration;
- long msecPerRender = 1000 / renderFPS;
- long msecPerProcess = 1000 / processPPS;
- while (running) {
- long now = System.currentTimeMillis();
- long delta = now - lastRender;
- if (delta >= msecPerRender) {
- lastRender = now - (delta - msecPerRender);
- game.render(lastRender);
- }
- delta = now - lastProcess;
- if (delta >= msecPerProcess) {
- lastProcess = now - (delta - msecPerProcess);
- game.process(lastProcess);
- }
- }
- }
- });
- mainFrame = new JFrame("window");
- mainFrame.addWindowListener(new LauncherListener());
- mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
- gameThread.start();
- mainFrame.setLocationByPlatform(true);
- mainFrame.setVisible(true);
- mainFrame.setSize(300, 500);
- }
- // to stop the application execution
- public void stop() {
- running = false;
- }
- // here you can move yours player
- public void movePlayer() {
- boolean moveUp = true; // calc direction
- if (moveUp)
- game.player.moveVertically(1);
- else
- game.player.moveVertically(-1);
- }
- @Override
- protected void shutdown() {
- mainFrame.setVisible(false);
- gameThread.stop();
- }
- private class LauncherListener extends WindowAdapter {
- @Override
- public void windowClosing(WindowEvent e) {
- exit();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement