Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
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 / Properly (?) Double Buffered Applet is Really Choppy on: 2006-07-01 00:23:02
Hi all. I looked around here for the proper way to double buffer an applet, and I think I've got it, but it's realllly choppy when it runs. (It's a little 2d jump 'n run game with not much in it yet). If you have any idea why, please tell =) 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  
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  
public class Game extends JApplet implements KeyListener, Runnable
{  
   //doublebuffer stuff
  Graphics bufferGraphics;
   Image offscreenImage;
   Dimension dim;
   
   //constants
  int SCREEN_WIDTH = 800;
   int SCREEN_HEIGHT = 600;
   int SPRITE_SIZE = 64;
   Color BGColor = Color.white;
   int PLAYER_SPEED_X = 10;
   int PLAYER_JUMP_SPEED = 20;
   int GRAVITY_ACCEL = 1;
   
   //directional constants
  int LEFT = -1;
   int RIGHT = 1;
   int STOPPED = 0;
   int UP = -1;
   int DOWN = 1;
   
   //images
  Image playerImage;
   
   //movement
  boolean freeFall = true;
   int playerDirX = STOPPED;
   int playerVelY = 0;
   int playerPosX = 0;
   int playerPosY = 0;
   
   //game control variables
  boolean gameIsRunning = false;
   Thread t;
   
   int i = 0;
   
   public void init()
   {
      addKeyListener(this);
      gameIsRunning = true;
     
      //prepare offscreen image for double buffering
     setSize(800,600);
      dim = getSize();
      offscreenImage = createImage(dim.width,dim.height);
      bufferGraphics = offscreenImage.getGraphics();
     
      playerImage = getImage(getDocumentBase(),"images/playerRight.gif");
     
      t = new Thread(this);
      t.start();
   }
   
   public void run()
   {
      while(gameIsRunning)
      {
         try
         {
            t.sleep(33);
         }
         catch (InterruptedException e) {}
         
         playerPosX += playerDirX*PLAYER_SPEED_X;
         playerPosY -= playerVelY;
         
         if(freeFall)
         {
            playerVelY -= GRAVITY_ACCEL;
         }        
         
         //wraparound
        if(playerPosX < 0)
         {
            playerPosX = 0;
         }
         if(playerPosX > SCREEN_WIDTH - SPRITE_SIZE)
         {
            playerPosX = SCREEN_WIDTH - SPRITE_SIZE;
         }
         
         //stop at the floor
        if(playerPosY > SCREEN_HEIGHT - SPRITE_SIZE)
         {
            freeFall = false;
            playerVelY = 0;
            playerPosY = SCREEN_HEIGHT - SPRITE_SIZE;
         }
         
         repaint();
      }
   }
   
   public void keyPressed(KeyEvent e)
   {      
      //move right
     if(e.getKeyCode() == KeyEvent.VK_RIGHT ||
            e.getKeyCode() == KeyEvent.VK_D)
      {
         playerImage = getImage(getDocumentBase(),"images/playerRight.gif");
         playerDirX = RIGHT;
      }
     
      //move left
     if(e.getKeyCode() == KeyEvent.VK_LEFT ||
            e.getKeyCode() == KeyEvent.VK_A)
      {
         playerImage = getImage(getDocumentBase(),"images/playerLeft.gif");
         playerDirX = LEFT;
      }
     
      //jump
     if(e.getKeyCode() == KeyEvent.VK_SPACE ||
            e.getKeyCode() == KeyEvent.VK_W ||
            e.getKeyCode() == KeyEvent.VK_UP)
      {
         //allow jumping if we're standing on a surface
        if(playerPosY == SCREEN_HEIGHT - SPRITE_SIZE ||
               playerPosX == 0 ||
               playerPosX == SCREEN_WIDTH - SPRITE_SIZE)
         {
            if(playerPosX == 0)
            {
               playerDirX = RIGHT;
               playerImage =
                  getImage(getDocumentBase(),"images/playerRight.gif");
            }
            if(playerPosX == SCREEN_WIDTH - SPRITE_SIZE)
            {
               playerDirX = LEFT;
               playerImage =
                  getImage(getDocumentBase(),"images/playerLeft.gif");
            }
            freeFall = true;
            playerVelY = PLAYER_JUMP_SPEED;
         }
      }
   }
   
   public void keyReleased(KeyEvent e)
   {
      //stop moving right
     if(e.getKeyCode() == KeyEvent.VK_RIGHT ||
            e.getKeyCode() == KeyEvent.VK_D)
      {
         if(playerDirX == RIGHT)
         {
            playerDirX = STOPPED;
         }
      }
     
      //stop moving left
     if(e.getKeyCode() == KeyEvent.VK_LEFT ||
            e.getKeyCode() == KeyEvent.VK_A)
      {
         if(playerDirX == LEFT)
         {
            playerDirX = STOPPED;
         }
      }
   }
   
   public void keyTyped(KeyEvent e)
   {
   }
   
   public void paint(Graphics g)
   {
      //clear screen
     bufferGraphics.setColor(Color.white);
      bufferGraphics.fillRect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
     
      //draw the player
     bufferGraphics.drawImage(playerImage,playerPosX,playerPosY,this);
     
      g.drawImage(offscreenImage,0,0,this);
   }
   
   public void stop()
   {
      t.stop();
   }
   
    public void update(Graphics g)
    {
         paint(g);
    }
}
2  Java Game APIs & Engines / Java 2D / Re: ::Pixel art resources thread:: on: 2006-06-06 17:06:35
pixel-arts.org/pixelopolis

^ best pixel art forum ('specially for people looking to improve and learn =))
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.

xsi3rr4x (29 views)
2014-04-15 18:08:23

BurntPizza (26 views)
2014-04-15 03:46:01

UprightPath (41 views)
2014-04-14 17:39:50

UprightPath (23 views)
2014-04-14 17:35:47

Porlus (40 views)
2014-04-14 15:48:38

tom_mai78101 (62 views)
2014-04-10 04:04:31

BurntPizza (122 views)
2014-04-08 23:06:04

tom_mai78101 (221 views)
2014-04-05 13:34:39

trollwarrior1 (188 views)
2014-04-04 12:06:45

CJLetsGame (197 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!