Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (736)
Games in Android Showcase (223)
games submitted by our members
Games in WIP (813)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: _Scyth_ (posted 2012-12-28 14:37:25, viewed 490 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231  
package com.epacgames.games.redxog;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.image.BufferStrategy;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class Game implements Runnable{
   
   final int WIDTH = 800;
   final int HEIGHT = 600;
   public static final int BLACK = 0;
   public static final int GRASS = 1;
   public static final int EARTH = 2;
   
   int[][] tiles;
   
   JFrame frame;
   Canvas canvas;
   BufferStrategy bufferStrategy;
   Graphics2D g;
   InputStream level1 = this.getClass().getResourceAsStream("level.txt");
   
   public Game(){
      frame = new JFrame("Basic Game");
      
      JPanel panel = (JPanel) frame.getContentPane();
      panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
      panel.setLayout(null);
      
      canvas = new Canvas();
      canvas.setBounds(0, 0, WIDTH, HEIGHT);
      canvas.setIgnoreRepaint(true);
      
      panel.add(canvas);
      
      canvas.addMouseListener(new MouseControl());
      
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setResizable(false);
      frame.setVisible(true);
      
      canvas.createBufferStrategy(2);
      bufferStrategy = canvas.getBufferStrategy();
      
      canvas.requestFocus();
      
      try {
          createTiles(getIntsFromTextFile(level1));
       } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
       }
      
      try {
      tiles = getIntsFromTextFile(level1);
   } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   }
      
   }
   
        
   private class MouseControl extends MouseAdapter{
      
   }
   
   long desiredFPS = 60;
    long desiredDeltaLoop = (1000*1000*1000)/desiredFPS;
    
   boolean running = true;
   
   public void run(){
      
      long beginLoopTime;
      long endLoopTime;
      long currentUpdateTime = System.nanoTime();
      long lastUpdateTime;
      long deltaLoop;
      
      while(running){
         beginLoopTime = System.nanoTime();
         
         render();
         
         lastUpdateTime = currentUpdateTime;
         currentUpdateTime = System.nanoTime();
         update((int) ((currentUpdateTime - lastUpdateTime)/(1000*1000)));
         
         endLoopTime = System.nanoTime();
         deltaLoop = endLoopTime - beginLoopTime;
           
           if(deltaLoop > desiredDeltaLoop){
               //Do nothing. We are already late.
           }else{
               try{
                   Thread.sleep((desiredDeltaLoop - deltaLoop)/(1000*1000));
               }catch(InterruptedException e){
                   //Do nothing
               }
           }
      }
   }
   
   private void render() {
      Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
      g.clearRect(0,0, WIDTH, HEIGHT);
      render(g);
      g.dispose();
      bufferStrategy.show();
   }

   /**
    * Rewrite this method for your game
    */
   protected void update(int deltaTime){
     
   }
   
   /**
    * Rewrite this method for your game
    */
   
   public void render(Graphics2D g) {
      renderTiles(g);
   }

   public void renderTiles(Graphics2D g) {
      // Iterate through all tiles:
      for (int x = 0; x < tiles.length; x++) {
         for (int y = 0; y < tiles[x].length; x++) {
            renderTile(x, y, tiles[x][y], g);
         }
      }
   }
   public void renderTile(int x, int y, int tileID, Graphics2D g) {
         switch (tileID) {
         // don't forget the breaks! :)
         case BLACK: g.setColor(Color.black); g.drawRect(x * 32, y * 32, 32, 32); break; 
         case GRASS: g.setColor(Color.green); g.drawRect(x * 32, y * 32, 32, 32); break;
         case EARTH: g.setColor(Color.MAGENTA); g.drawRect(x * 32, y * 32, 32, 32); break;
         }
      }
   
   public static void main(String [] args){
      Game ex = new Game();
      new Thread(ex).start();
   }
   
   public int[][] getIntsFromTextFile(InputStream input) throws IOException {
      // Create a list of rows. It needs to be resizable, because
      // we don't know the number of rows the text file has in the
      // beginning.
      ArrayList<int[]> yList = new ArrayList<int[]>();
      // A bit try-finally stuff, so we can close the reader properly.
      BufferedReader reader = null;
      try {
         // We create a BufferedReader by putting InputStreamReader-
         // glue between the Reader and the FileInputStream.
         reader = new BufferedReader(new InputStreamReader(input));
         
         // This variable counts the y rows.
         int y = 0;
         // This variable will hold every newly read line
         String line = null;
         // This reads a line and puts it into "line".
         // line is then tested against null, because when
         // the line is null, then we reached the end of the
         // file.
         while ((line = reader.readLine()) != null) {
            // We parse the string into a int[] and put it
            // into the list of rows.
            yList.add(parseString(line));
         }
      } finally {
         // "finally" we close the reader ;)
         if (reader != null) {
            reader.close();
         }
      }
      // This now creates an int[][] out of the ArrayList<int[]>.
      // This can be done, since we know the number of rows
      // now. You can see ArrayList's as a resizable array.
      int[][] ints = new int[yList.size()][];
      for (int i = 0; i < ints.length; i++) {
         ints[i] = yList.get(i);
      }
      return ints;
   }

   public int[] parseString(String str) {
      // We can create a int[], since we know
      // the length of the incoming string,
      // which is for example: "00192773716749".
      int[] array = new int[str.length()];
      
      // We loop through each character in the string
      // and create an integer from the returned char
      // with Integer.parseInt(...);
      for (int i = 0; i < str.length(); i++) {
         char c = str.charAt(i);
         // I say "" + c to create a string
         // out of the char. I'm not sure if
         // it's needed. You could try it without.
         array[i] = Integer.parseInt("" + c);
      }
      return array;
   }
    public void createTiles(int[][] dataArray) {

      }
   public void createTile(int xPos, int yPos, int tileID)
   {
      
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 
cybrmynd (120 views)
2017-08-02 12:28:51

cybrmynd (144 views)
2017-08-02 12:19:43

cybrmynd (138 views)
2017-08-02 12:18:09

Sralse (154 views)
2017-07-25 17:13:48

Archive (624 views)
2017-04-27 17:45:51

buddyBro (733 views)
2017-04-05 03:38:00

CopyableCougar4 (1260 views)
2017-03-24 15:39:42

theagentd (1239 views)
2017-03-24 15:32:08

Rule (1216 views)
2017-03-19 12:43:22

Rule (1268 views)
2017-03-19 12:42:17
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!