Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
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: MashedPotatoe (posted 2012-07-25 23:14:30, viewed 92 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   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279   280   281   282   283   284  
// main class snippet
      
//from the constructor
canvas.createBufferStrategy(3);
bs = canvas.getBufferStrategy();

// from the rest of the main class
long desiredFPS = 60;
long desiredDeltaLoop = (1000000000)/desiredFPS;
   
boolean running = true;
   
@Override
public void run() {
   long beginLoop;
   long endLoop;
   long currentTime = System.nanoTime();
   long timeSinceUpdate;
   long loop;

      
   while (running) {
         
      beginLoop = System.nanoTime();
      
      timeSinceUpdate = currentTime;
      currentTime = System.nanoTime();
      update((int) ((currentTime - timeSinceUpdate) / (1000000)));
         
      render();
         
      endLoop = System.nanoTime();
      loop = endLoop - beginLoop;
         
      if (loop > desiredDeltaLoop) {
      } else {
         try {
            Thread.sleep((desiredDeltaLoop - loop) / (1000000));
         } catch (InterruptedException e) {
         }
      }
   }
      
}

// let's the current screen update   
private void update(int delta) {
   screen.update(delta);
}

private void render() {
   Graphics2D g = (Graphics2D) bs.getDrawGraphics();
   g.setBackground(Color.black);
   g.clearRect(0, 0, WIDTH, HEIGHT);
   render(g);
   g.dispose();
   bs.show();
      
}

// let's the current screen render it's stuff
private void render(Graphics2D g) {
   screen.render(g);
}



// snippet from the game screen 
@Override
public void render(Graphics2D g) {
   if (DS.debugMode) {
      g.setColor(Color.red);
   }
   player.render(g);
      
}

@Override
public void update(int delta) {
   player.update(delta);
   cam.update(delta);
      
}

@Override
public void keyInput(KeyEvent e) {
   switch (e.getKeyCode()) {
      case KeyEvent.VK_ESCAPE:
         DS.game.enterScreen(Game.MENUSCREEN);
         break;
      case KeyEvent.VK_F1:
         DS.debugMode = !DS.debugMode;
      default: 
         player.keyInput(e);
   }
      
}


// code snippet of the mob class which the player class inherits
// this is everything relevant to moving around

protected boolean moving = false;
protected int direction = 0;


public void update(int delta) {
      if (moving)
      move(delta);
}
   
private void move(int delta) {
   if (x == targetX && y == targetY) {
      moving = false;
      return;
   }
      
   switch (direction) {
      case DS.LEFT:
         moveLeft(delta);
         break;
      case DS.UPLEFT:
         moveLeft(delta);
         moveUp(delta);
         break;
      case DS.UP:
         moveUp(delta);
         break;
      case DS.UPRIGHT:
         moveRight(delta);
         moveUp(delta);
         break;
      case DS.RIGHT:
         moveRight(delta);
         break;
      case DS.DOWNRIGHT:
         moveRight(delta);
         moveDown(delta);
         break;
      case DS.DOWN:
         moveDown(delta);
         break;
      case DS.DOWNLEFT:
         moveLeft(delta);
         moveDown(delta);
         break;
   }
      
}
   
private void moveLeft(int delta) {
   if (x - delta * .1f <= targetX) {
      x = targetX;
   } else {
      x -= delta * .1f;
   }
}
   
private void moveUp(int delta) {
   if (y - delta * .1f <= targetY) {
      y = targetY;
   } else {
      y -= delta * .1f;
   }
}
   
private void moveRight(int delta) {
   if (x + delta * .1f >= targetX){
      x = targetX;
   } else {
      x += delta * .1f;
   }
}
   
private void moveDown(int delta) {
   if (y + delta * .1f >= targetY) {
      y = targetY;
   } else {
      y += delta * .1f;
   }
}



// code snippet from the player class (sub class of mob)

public void update(int delta) {
   super.update(delta);
}
   
public void render(Graphics2D g) {      
   g.drawImage(current, x - DS.cam.camX, y - DS.cam.camY, null);
}
   
public void keyInput(KeyEvent e) {
   if (moving)
      return;
   switch (e.getKeyCode()) {
      case KeyEvent.VK_NUMPAD4:
      case KeyEvent.VK_KP_LEFT:
      case KeyEvent.VK_LEFT:
         direction = DS.LEFT;
         current = DS.playerLeftSprite;
         moving = true;
         targetX = x - DS.tileSize;
         targetY = y;
         break;
      case KeyEvent.VK_NUMPAD7:
      case KeyEvent.VK_PAGE_UP:
         direction = DS.UPLEFT;
         current = DS.playerLeftSprite;
         moving = true;
         targetX = x - DS.tileSize;
         targetY = y - DS.tileSize;
         break;
      case KeyEvent.VK_NUMPAD8:
      case KeyEvent.VK_KP_UP:
      case KeyEvent.VK_UP:
         direction = DS.UP;
         moving = true;
         targetX = x;
         targetY = y - DS.tileSize;
         break;
      case KeyEvent.VK_NUMPAD9:
      case KeyEvent.VK_HOME:
         direction = DS.UPRIGHT;
         current = DS.playerRightSprite;
         moving = true;
         targetX = x + DS.tileSize;
         targetY = y - DS.tileSize;
         break;
      case KeyEvent.VK_NUMPAD6:
      case KeyEvent.VK_KP_RIGHT:
      case KeyEvent.VK_RIGHT:
         direction = DS.RIGHT;
         current = DS.playerRightSprite;
         moving = true;
         targetX = x + DS.tileSize;
         targetY = y;
         break;
      case KeyEvent.VK_NUMPAD3:
      case KeyEvent.VK_END:
         direction = DS.DOWNRIGHT;
         current = DS.playerRightSprite;
         moving = true;
         targetX = x + DS.tileSize;
         targetY = y + DS.tileSize;
         break;
      case KeyEvent.VK_NUMPAD2:
      case KeyEvent.VK_KP_DOWN:
      case KeyEvent.VK_DOWN:
         direction = DS.DOWN;
         moving = true;
         targetX = x;
         targetY = y + DS.tileSize;
         break;
      case KeyEvent.VK_NUMPAD1:
      case KeyEvent.VK_PAGE_DOWN:
         direction = DS.DOWNLEFT;
         current = DS.playerLeftSprite;
         moving = true;
         targetX = x - DS.tileSize;
         targetY = y + DS.tileSize;
         break;
      default:
         // ignore the rest
         break;
            
   }
      
}


// fields from DS used in the snippets
public static final int LEFT = 0;
public static final int UPLEFT = 1;
public static final int UP = 2;
public static final int UPRIGHT = 3;
public static final int RIGHT = 4;
public static final int DOWNRIGHT = 5;
public static final int DOWN = 6;
public static final int DOWNLEFT = 7;

public static int tileSize = 32;





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
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (41 views)
2014-09-24 16:13:29

Pippogeek (32 views)
2014-09-24 16:12:22

Pippogeek (22 views)
2014-09-24 16:12:06

Grunnt (47 views)
2014-09-23 14:38:19

radar3301 (30 views)
2014-09-21 23:33:17

BurntPizza (65 views)
2014-09-21 02:42:18

BurntPizza (37 views)
2014-09-21 01:30:30

moogie (44 views)
2014-09-21 00:26:15

UprightPath (53 views)
2014-09-20 20:14:06

BurntPizza (55 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!