Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (516)
Games in Android Showcase (122)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Game is not running smoot?  (Read 2459 times)
0 Members and 1 Guest are viewing this topic.
Offline aslan

Senior Newbie




Java games rock!


« Posted 2002-12-15 07:29:32 »

Am creating this game and somehow the game is not running smooth. Can anyone help me with this plz.....


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

public class Fighter extends JApplet implements Runnable,KeyListener
{
      Thread runner;
      Image buffer;
      Graphics gf;

      int x=400;
      int y=400;

      int skudXStart1=0;
      int skudYStart1=0;
      int skudXSlut1=0;
      int skudYSlut1=0;

      int skudXStart2=0;
      int skudYStart2=0;
      int skudXSlut2=0;
      int skudYSlut2=0;

      boolean right,left,space;


      public void init()
      {
          addKeyListener(this);
          setSize(800,600);
      }
      public void start()
      {

            if (runner == null)
            {
               runner = new Thread(this);
               runner.start();
            }
      }

      public void stop()
      {

            if(runner != null)
            {
               runner = null;
            }
      }

      public void run()
      {

            while(gf==null)
            {
              try {
              Thread.currentThread().sleep(50);
              } catch(InterruptedException e){}

            }

            while(Thread.currentThread()==runner)
            {

          repaint();

          gf.setColor(Color.white);
          gf.fillRect(0, 0, 800, 600);


              gf.drawImage((new ImageIcon("raptorSmall.gif")).getImage(),x,y,this);
              gf.setColor(Color.red);
              gf.drawLine(skudXStart1,skudYStart1,skudXSlut1,skudYSlut1);
              gf.drawLine(skudXStart2,skudYStart2,skudXSlut2,skudYSlut2);

              if (space)
              {
                    skudYStart1-=50;
                    skudYSlut1-=50;
                    skudYStart2-=50;
                    skudYSlut2-=50;

                    if (skudYStart1 <= 0)
                    {
                          space = false;
                    }
              }
              if (left)
              {
                    x -= 10;
              }
              if (right)
              {
                    x += 10;
            }

              try {
              Thread.currentThread().sleep(50);
              } catch(InterruptedException e) {}

            }
      }
      public void paint(Graphics g)
      {
          update(g);
      }

      public void update(Graphics g)
      {
            if (gf == null)
            {
              buffer = createImage(800, 600);
              gf = buffer.getGraphics();
            }
          g.drawImage(buffer, 0, 0, this);

      }
      public void keyPressed(KeyEvent e)
      {
             if(e.getKeyCode() == KeyEvent.VK_ESCAPE )
             {
                  System.exit(0);
             }
             if(e.getKeyCode() == KeyEvent.VK_LEFT)
             {
                  left=true;
             }
             if(e.getKeyCode() == KeyEvent.VK_RIGHT)
             {
                  right=true;
             }
             if(e.getKeyCode() == KeyEvent.VK_SPACE)
             {
                  if (skudYStart1 <= 0)
                  {
                        skudXStart1=x+20;
                        skudYStart1=y+30;
                        skudXSlut1=x+20;
                        skudYSlut1=450;
                        skudXStart2=x+78;
                        skudYStart2=y+30;
                        skudXSlut2=x+78;
                        skudYSlut2=450;
                  }

                  space=true;

           }
      }
      public void keyReleased(KeyEvent e)
      {
            if(e.getKeyCode() == KeyEvent.VK_LEFT)
            {
               left=false;
            }
            if(e.getKeyCode() == KeyEvent.VK_RIGHT)
            {
               right=false;
            }
            if(e.getKeyCode() == KeyEvent.VK_SPACE)
            {
            }
      }
      public void keyTyped(KeyEvent e){}


}
Offline Olof Hanell

Senior Newbie




Smooth scrolling rocks.


« Reply #1 - Posted 2002-12-15 09:28:50 »

uhm.
gf.drawImage((new ImageIcon("raptorSmall.gif")).getImage(),x,y,this);

This will load the image from disk each frame. Bad! Load it into an image object once and draw that one instead.  May help some I guess.
Offline aslan

Senior Newbie




Java games rock!


« Reply #2 - Posted 2002-12-15 10:18:53 »

Didn't help much...... still not smooth  Huh
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zparticle

Senior Duke




Thick As A Brick


« Reply #3 - Posted 2002-12-15 12:48:58 »

Just for fun get rid of the 50 ms sleep in your rendering loop.

Offline aslan

Senior Newbie




Java games rock!


« Reply #4 - Posted 2002-12-15 12:55:06 »

ehhh... which one? I have 2....
Offline zparticle

Senior Duke




Thick As A Brick


« Reply #5 - Posted 2002-12-15 14:47:08 »

The second one, the first one doesn't control the rendering.

Offline aslan

Senior Newbie




Java games rock!


« Reply #6 - Posted 2002-12-15 14:50:17 »

Its flickering like hell now??
Offline trilambda

Junior Newbie





« Reply #7 - Posted 2002-12-16 18:44:52 »

I'm kind of a newb myself, but it looks like you are actually not double buffering here.

The way I'm using double buffering in my game (it's my first attempt though) is to do this:

- the update(Graphics g) method builds the buffer graphics if it's not created.  Then using the buffer graphics to clear everything and redraw by calling paint(bufferGraphics)
- the paint(Graphics g) method does all the drawing, this looks like it's happening in your game thread's loop
- the thread will just update the environment (move objects, etc) and then call repaint().

I think this is referred to as "passive" rendering, since it's not just using this.getGraphics() in the game loop (which I think is called "active" rendering).  Remember, I'm a newb to game dev, so this could all be wrong.  
Grin

For reference, this is where I learned double buffering techniques:
http://javaboutique.internet.com/tutorials/Java_Game_Programming/BildschirmflackernEng.html

Offline trilambda

Junior Newbie





« Reply #8 - Posted 2002-12-16 18:52:12 »


nevermind, it looks like you are properly double buffering, things are just in weird spots.  I dunno why it's flickering then... sorry.
Offline Abuse

JGO Knight


Medals: 13


falling into the abyss of reality


« Reply #9 - Posted 2002-12-17 23:24:09 »

im sorry if you take offence, but your code is abit.....
hmm.....
unconventional Shocked

I suggest you take a look at some sample game code to get the basic structure fixed. This will sort out your flickering/stuttery animation.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline trembovetski

Senior Duke




If only I knew what I'm talking about!


« Reply #10 - Posted 2002-12-18 04:30:14 »

If you're creating (and disposing) a new image on each frame, you're likely to run into garbage collection thrashing.

Also, for the type of the application you're writing, using repaint() might not be a best choice, as the rendering happens asynchronously on another thread (so you can't really guarantee the same intervals between the frames)

I'd suggest to use active rendering.

The idea is to take the rendering into your own hands.
Say, you'd have a method called
render(Graphics g);
which would render the world to the passed graphics object.

Then in your run() method, inside the loop, you'd have something like:
 changeTheWorld();
 render(backBufferGraphics);
 copyBackBufferToTheScreen();
 SleepUntilTheNextFrame();

the copyBackBufferToTheScreen() just copies the
backbuffer to the screen, something like this:
Graphics g = renderingPanel.getGrahics();
 g.drawImage(backBuffer, 0, 0, null);

Ideally, you'd use BufferStrategy/FullScreen apis to do this.
Check out the tutorials on the Java2D and AWT home pages for more info on active rendering:
http://java.sun.com/products/java-media/2D/
http://java.sun.com/docs/books/tutorial/extra/fullscreen/

I'm sure you can find lots of exampleson this board as well.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

TehJavaDev (31 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (40 views)
2014-10-26 19:37:11

Luminem (21 views)
2014-10-26 10:17:50

Luminem (26 views)
2014-10-26 10:14:04

theagentd (32 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (57 views)
2014-10-16 15:22:06

Norakomi (46 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!