Advertisement
Guest User

Java OOP example

a guest
Sep 7th, 2015
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.event.WindowAdapter;
  2. import java.awt.event.WindowEvent;
  3. import java.util.ArrayList;
  4. import javax.swing.JFrame;
  5.  
  6. import org.jdesktop.application.Application;
  7.  
  8. class Entity {
  9.  
  10.   Grid grid;
  11.  
  12.   int x;
  13.   int y;
  14.  
  15.   public Entity(Grid grid) {
  16.     this.grid = grid;
  17.     x = 0;
  18.     y = 0;
  19.   }
  20.  
  21.   public void moveTo(int x, int y) {
  22.     remove();
  23.  
  24.     this.x = x;
  25.     this.y = y;
  26.     grid.put(this.x, this.y, this);
  27.   }
  28.  
  29.   public boolean remove() {
  30.     if (grid.get(x, y) == this)
  31.       grid.clear(x, y);
  32.     else
  33.       return false;
  34.  
  35.     return true;
  36.   }
  37.  
  38.   public void process(long timemillis) {
  39.  
  40.   }
  41.  
  42.   public void render(long timemillis) {
  43.  
  44.   }
  45.  
  46. }
  47.  
  48. class Grid {
  49.  
  50.   int w = 30;
  51.   int h = 10;
  52.  
  53.   public Entity[][] cells;
  54.  
  55.   public Grid(int w, int h) {
  56.     this.w = w;
  57.     this.h = h;
  58.     cells = new Entity[h][w];
  59.   }
  60.  
  61.   public Entity get(int x, int y) {
  62.     return cells[y][x];
  63.   }
  64.  
  65.   public void put(int x, int y, Entity entity) {
  66.     cells[y][x] = entity;
  67.   }
  68.  
  69.   public void clear(int x, int y) {
  70.     cells[y][x] = null;
  71.   }
  72.  
  73.   public void process(long timemillis) {
  74.     // here you can process grid
  75.  
  76.     // like, change some tiles, progress animations, etc
  77.   }
  78.  
  79.   public void render(long timemillis) {
  80.     // here you can draw grid
  81.   }
  82.  
  83. }
  84.  
  85. class Game {
  86.  
  87.   Grid grid;
  88.  
  89.   Player player;
  90.  
  91.   ArrayList<Entity> entities;
  92.  
  93.   int score;
  94.  
  95.   long lastProcess;
  96.  
  97.   public Game() {
  98.     grid = new Grid(30, 20);
  99.   }
  100.  
  101.   public void newGame() {
  102.     lastProcess = 0;
  103.     score = 0;
  104.     player = new Player(this);
  105.     entities = new ArrayList<>();
  106.   }
  107.  
  108.   public void render(long timemillis) {
  109.     grid.render(timemillis);
  110.  
  111.     player.render(timemillis);
  112.  
  113.     for (Entity entity : entities)
  114.       entity.render(timemillis);
  115.   }
  116.  
  117.   public void process(long timemillis) {
  118.     grid.process(timemillis); // process grid (like, some animation)
  119.  
  120.     player.process(timemillis); // process player
  121.  
  122.     for (Entity entity : entities) // process entities
  123.       entity.process(timemillis);
  124.  
  125.  
  126.     // test player/G collisions
  127.  
  128.     Entity toRight = grid.get(player.x + 1, player.y);
  129.  
  130.     if (toRight != null) {
  131.       // have something to the playes right
  132.  
  133.       if (toRight instanceof G) // it's a G instance!
  134.         onGCollidedPlayer(player, (G) toRight); /*
  135.      // also, if we have some other entity types, like bonuses/secrets
  136.      // we can test them separately
  137.  
  138.      if (toRight instanceof Bonus) // it's a Bonus instance!
  139.      onPlayerPickBonus(player, (Bonus) toRight);
  140.  
  141.      if (toRight instanceof Secret) // it's a Secret instance!
  142.      onPlayerPickedSecret(player, (Secret) toRight);
  143.      */
  144.     }
  145.  
  146.     if (timemillis - lastProcess > 300) { // each 300 msec ()
  147.       lastProcess = timemillis;
  148.  
  149.       Entity spawn = null;
  150.  
  151.       switch (((int)(Math.random() * 10)) % 1) {
  152.       case 0: // spawn G
  153.         spawn = new G(this); break;
  154.       }
  155.      
  156.       if (spawn != null) {
  157.         spawn.moveTo(grid.w, (int)(Math.random() * 9999) % grid.h);
  158.         entities.add(spawn);
  159.       }
  160.     }
  161.   }
  162.  
  163.   public void onGCollidedLeftEdge(G g) {
  164.     // do somethidg on g/left-edge collision
  165.     // like decreasing score
  166.  
  167.     score -= g.awardsPoints;
  168.   }
  169.  
  170.   public void onGCollidedPlayer(Player player, G g) {
  171.     // do somethidg on g/player collision
  172.     // like increasing score
  173.  
  174.     score += g.awardsPoints;
  175.  
  176.     // also, remove colided G
  177.     g.remove();
  178.   }
  179.  
  180. }
  181.  
  182. // entity, sliding to left
  183. class G extends Entity {
  184.  
  185.   Game game;
  186.  
  187.   // speed of moving
  188.   int speed;
  189.  
  190.   // some score points for capturing/losing this G
  191.   int awardsPoints;
  192.  
  193.   // don't forget to redefine constructor
  194.   public G(Game game) {
  195.     super(game.grid);
  196.     this.game = game;
  197.     // can specify speed here
  198.     speed = 1;
  199.  
  200.     awardsPoints = 10 + (int) (Math.random() * 100) % 91; // 10-100
  201.   }
  202.  
  203.   // we're a "sliding to left entity"
  204.   public void moveLeft(int distance) {
  205.     int newX = x - distance;
  206.  
  207.     if (newX <= 0)
  208.       // we're reached left grid edge
  209.       // removing this G entity grom grid
  210.       remove(); // and can make something else
  211.  
  212.     moveTo(newX, y);
  213.   }
  214.  
  215.   // override parent process() method
  216.   @Override
  217.   public void process(long timemillis) {
  218.     moveLeft(speed); // we should move left by amount, specified by "speed"
  219.   }
  220.  
  221.   @Override
  222.   public void render(long timemillis) {
  223.     // here goes rendering code for G entities
  224.   }
  225. }
  226.  
  227. class Player extends G {
  228.  
  229.   public Player(Game game) {
  230.     super(game);
  231.   }
  232.  
  233.   public void moveVertically(int direction) {
  234.     int newY = y + direction * speed;
  235.     if (newY < 0)
  236.       newY = 0;
  237.     else
  238.       if (newY >= grid.h)
  239.         newY = grid.h - 1;
  240.  
  241.     moveTo(x, newY);
  242.   }
  243.  
  244.   // override processing method if needed...
  245.   @Override
  246.   public void process(long timemillis) {
  247.   }
  248.  
  249.   @Override
  250.   public void render(long timemillis) {
  251.     // here goes rendering code for player
  252.   }
  253. }
  254.  
  255. /**
  256. *
  257. * @author Ankh Zet (ankhzet@gmail.com)
  258. */
  259. public class Launcher extends Application {
  260.  
  261.   JFrame mainFrame = null;
  262.   Thread gameThread;
  263.  
  264.   int renderFPS = 30; //
  265.  
  266.   int processPPS = 10;
  267.   // five processings per second (around 1000 / 10 = 100 msec between each)
  268.  
  269.   boolean running;
  270.  
  271.   Game game;
  272.  
  273.   public static void main(String[] args) {
  274.     launch(Launcher.class, args);
  275.   }
  276.  
  277.   @Override
  278.   protected void startup() {
  279.     running = false;
  280.  
  281.     game = new Game();
  282.  
  283.     gameThread = new Thread(new Runnable() {
  284.  
  285.       @Override
  286.       public void run() {
  287.         running = true;
  288.         // here goes the rendering/processing "meat";
  289.         long lastIteration = System.currentTimeMillis();
  290.         long lastRender = lastIteration;
  291.         long lastProcess = lastIteration;
  292.  
  293.         long msecPerRender = 1000 / renderFPS;
  294.         long msecPerProcess = 1000 / processPPS;
  295.  
  296.         while (running) {
  297.           long now = System.currentTimeMillis();
  298.  
  299.           long delta = now - lastRender;
  300.           if (delta >= msecPerRender) {
  301.             lastRender = now - (delta - msecPerRender);
  302.             game.render(lastRender);
  303.           }
  304.  
  305.           delta = now - lastProcess;
  306.           if (delta >= msecPerProcess) {
  307.             lastProcess = now - (delta - msecPerProcess);
  308.             game.process(lastProcess);
  309.           }
  310.         }
  311.       }
  312.     });
  313.  
  314.     mainFrame = new JFrame("window");
  315.     mainFrame.addWindowListener(new LauncherListener());
  316.     mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  317.  
  318.     gameThread.start();
  319.  
  320.     mainFrame.setLocationByPlatform(true);
  321.     mainFrame.setVisible(true);
  322.     mainFrame.setSize(300, 500);
  323.   }
  324.  
  325.   // to stop the application execution
  326.   public void stop() {
  327.     running = false;
  328.   }
  329.  
  330.   // here you can move yours player
  331.   public void movePlayer() {
  332.     boolean moveUp = true; // calc direction
  333.     if (moveUp)
  334.       game.player.moveVertically(1);
  335.     else
  336.       game.player.moveVertically(-1);
  337.   }
  338.  
  339.   @Override
  340.   protected void shutdown() {
  341.     mainFrame.setVisible(false);
  342.     gameThread.stop();
  343.   }
  344.  
  345.   private class LauncherListener extends WindowAdapter {
  346.  
  347.     @Override
  348.     public void windowClosing(WindowEvent e) {
  349.       exit();
  350.     }
  351.  
  352.   }
  353.  
  354. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement