Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (491)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Applet tile engine on: 2006-04-15 04:16:32
I am not trying to make a game with an applet, just do some simple tile rendering things. I have come up with a nice effect, animated tiles too. The only problem is sometimes it loads in my browser and sometimes it does not. I am currenly using firefox, if I open the applet with ie it loads more often but still not all the time. I posted my full source and was wondering if someone can tell me why this is happening. Thanks.

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  
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Tile_engine extends Applet implements KeyListener, Runnable {

  private int keyPressed = -1;
  private int screenX = 0;
  private int screenY = 0;
  private final int TILE_MAX = 11;
  private final int TILE_SIZE = 32;
  private final int APPLET_WIDTH = 640;
  private final int APPLET_HEIGHT = 480;
  private final int SKY = 0;
  private final int CLOUD_1 = 1;
  private final int CLOUD_2 = 2;
  private final int RED_BRICKS = 3;
  private final int GRASS = 4;
  private final int DIRT = 5;
  private final int STONE = 6;
  private final int QBOX1 = 7;
  private final int QBOX2 = 8;
  private final int QBOX3 = 9;
  private final int QBOX4 = 10;
  private final int MAP_ROWS = 15;
  private final int MAP_COLS = 65;
  private Image background = null;
  private Image buffer = null;
  private Graphics bgBuff;
  private Graphics bufferg;
  private Image[] tile = new Image[TILE_MAX];

  private final int map[][] =
    {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0},
     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,6,6,6,6,6,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0},
     {0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
     {0,0,0,0,0,0,0,0,0,0,0,0,7,7,7,7,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,6,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
     {0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,0,0,0,0,0,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5,5,5,5,5,5,5,5,5,5,0,0,5,0,5,0,0,5,5,5,0,0,0,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,0,0,0,5,5,5,5}};
   
 
  public void init( ) {
    tile[SKY] = getImage(getDocumentBase(), "sky.png");
    tile[CLOUD_1] = getImage(getDocumentBase(), "cloud1.png");
    tile[CLOUD_2] = getImage(getDocumentBase(), "cloud2.png");
    tile[RED_BRICKS] = getImage(getDocumentBase(), "redBricks.png");
    tile[GRASS] = getImage(getDocumentBase(), "grass.png");
    tile[DIRT] = getImage(getDocumentBase(), "dirt.png");
    tile[STONE] = getImage(getDocumentBase(), "stone.png");
    tile[QBOX1] = getImage(getDocumentBase(), "qbox1.png");
    tile[QBOX2] = getImage(getDocumentBase(), "qbox2.png");
    tile[QBOX3] = getImage(getDocumentBase(), "qbox3.png");
    tile[QBOX4] = getImage(getDocumentBase(), "qbox4.png");

    buffer = createImage(TILE_SIZE * MAP_COLS, TILE_SIZE * MAP_ROWS);
    bufferg = buffer.getGraphics();

    addKeyListener(this);

    if(buffer != null) {
      int tile_id;
      for(int y=0;y<MAP_ROWS;y++) {
        for(int x=0;x<MAP_COLS;x++) {
          tile_id = map[y][x];
          bufferg.drawImage(tile[tile_id], x * TILE_SIZE, y * TILE_SIZE, this);
        }
      }
    }

  }

  public void start( ) {
    Thread th = new Thread(this);
    // start this thread
   th.start();
  }

  public void run( ) {
    Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    while(true) {
      animate_tiles();
   
      try {
   Thread.sleep(200);
      } catch(InterruptedException ex) {
      }
      Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    }
  }
 
  public void animate_tiles( ) {
    int tile_id;
   
    for(int y=0;y<MAP_ROWS;y++) {
      for(int x=0;x<MAP_COLS;x++) {
        tile_id = map[y][x];
     
        switch(tile_id) {
          case QBOX1: map[y][x] = QBOX2; break;
          case QBOX2: map[y][x] = QBOX3; break;
          case QBOX3: map[y][x] = QBOX4; break;
          case QBOX4: map[y][x] = QBOX1; break;
          default: break; //not needed  
       }
      }
    }
  }

  public void paint(Graphics g) {}

  public void update(Graphics g) {
    //animate_tiles();

    if(buffer != null) {
      int tile_id;
      for(int y=0;y<MAP_ROWS;y++) {
        for(int x=0;x<MAP_COLS;x++) {
          tile_id = map[y][x];
          bufferg.drawImage(tile[tile_id], x * TILE_SIZE, y * TILE_SIZE, this);
        }
      }
    }

    paint(bufferg);
    g.drawImage(buffer, screenX, screenY, this);
    repaint();
  }

  public void keyReleased( KeyEvent e ) { }
  public void keyPressed( KeyEvent e ) { }
  public void keyTyped( KeyEvent e ) {  
    char c = e.getKeyChar();
    if(c == 'z') { screenX+=5; }
    else if(c == 'c') { screenX-=5; }
  }  
}
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (33 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (40 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (25 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
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!