Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (121)
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   
  Show Posts
Pages: [1]
1  Game Development / Networking & Multiplayer / Re: MoleBox on: 2005-07-27 14:30:57
This whole topic is very interesting.

I for myself often thought about the great benefit for java game development if you could distribute your game as easily as a single exe file (on windows, the most important platform for games, besides the consoles). The usabilty is just so simple and easy to manage and java could be a real competitor in gamedev on windows machines. 'Cause this is the real bottleneck !!

I wonder why sun doesnt react on that. Do they think executable jars, applets and webstart are sufficient?
Perhaps for just4fun games (4k games, etc.) But since we are able to use real 3D hardware acceleration (LWJGL as an example), and lots of other really professional stuff like networking, sounds, physics, ect... games grow and personally I like the "download it" , "install it", and "play it" model because I (and most other people) are used to it. Imagine to play Battlefield or other big players via Webstart!

I would like to know if there are any news concerning the legality molebox distrubution of the jvm?

2  Game Development / Shared Code / transaccel images causes stutter on: 2005-06-30 14:49:40
Hi!

I tried to find out how to get the best performance out of java using the new nanoTimer from Java 5.

I wrote a basic full-screen rendering loop (afaik a very fast one) but I've got a problem concerning image-acceleration.

You can download the app at:

http://www.donmatheo.de/java/performanceDemo/performanceDemo.zip
(jar-files include sources)

There are two jar-files in the zip:

- performanceDemo_transacc.jar:
This is the loop using the command : System.setProperty("sun.java2d.translaccel", "true");

- performanceDemo_nontransacc.jar
This is the same loop but without using System.setProperty("sun.java2d.translaccel", "true");

The application loads a test.png image (with transparency). You can move the image with the cursor-keys (left,right,up,down).

My test-platform is:
P4 Mobile, 2,2 GHz
Mobile Radeon 9000
1GB RAM
OS: WinXP SP2
JRE: 1.5.0_03

When I run it with "System.setProperty("sun.java2d.translaccel", "true");" I get about ten times faster performance than witout it. (~1000fps). But the movement is very jerky when I press "right" or "down". "up" and "left" is very smooth.
When I disable "System.setProperty("sun.java2d.translaccel", "true");", the framerate drops to ~110fps but the movement is more fluently.

Anyway there's sometimes a white flicker of the whole screen. I don't know how to get rid of this.

Any help would be appreciated Smiley
3  Java Game APIs & Engines / Java 2D / Re: Active Menu Backrounds on: 2005-06-16 09:00:49
The most simple way is to put an animated gif image onto a JButton with 'new JButton(new ImageIcon("myanimated.gif"))'

If your game ist runnig in an active rendering loop (as it is supposed to be) you can just make a custom paint()-method for your menu-item which draws an image over it every time the whole frame gets an repaint(). The menu-item can be every kind of AWT or Swing component (Label, Frame, Button, MenuItem, etc.)

pseudocode:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
// create the item with a custom paint()-method
JButton menu_item = new JButton() {
  paint(Grahpics g) {
  g.drawImageI(...);  // drawing the bg-image
  g.drawString(...);  // drawing the text over the bg-image
}
}

//add the item to the Frame
Frame.add(menu_item);

while(true) {  //rendering-loop
  menu_item.repaint();
}
4  Java Game APIs & Engines / Java 2D / Quicktime Java API on: 2005-06-16 08:47:16
Hi,

I dont know where to post cause it's not explicit game-relatet but I think there are lots of gurus here in this forum that could possibly help me out here.

I wrote a movie-player using the quicktime-java-api from apple. So far it works great.

I wanted to write my own controller-panel (start/stop buttons, position-slider, etc.) that also shows the current movie-position as a timecode (hh:mm:ss:ff).
I managed to get the necessary components and I am able to get the current-movie time and jump to specific time.

But my problem is, that I need to update the slider's position and the time-display constantly when the movie is playing. I don´t want to put the whole thing into an active rendering loop. I tried mulit-threading, but it didn't really work (the whole program reacted jerky while starting an extra thread to update the time). I think there could be as feature of the quicktimeplayer-instance that fires events when a new frame has been drawn (cause the player itself has to have some kind of rendering loop). Does anyone know how I can make a listener for frame-drawing events (if there are some)?
5  Java Game APIs & Engines / Java 2D / Re: Weird resolution problem on: 2005-05-23 12:56:03
Quote
Also, why not use display mode's dimensions instead of component's?


That did it !!  Wink

a huge thanks to you !

(btw. I am using jre 1.5.0_01)
6  Game Development / Game Play & Game Design / Re: What makes a game ADDICTING? on: 2005-05-18 13:40:02
Well I think there are different approches for addictive games.

take a look at a few examples (you all know):

Tetris:
The one and only GAME! Very basic game elements but unlimited combinations. Easy to learn, hard to stop playing. Tetris is what I would call "a fun game". A game that fits easily into a 15minutes break and keeps you busy a whole night (I you're a freak) with some basic geometrical forms and an increasing drop-speed.
What tetris really does is to test the players reactions. It tries to train him, pushing him to his limits. And the player wants to be pushed (who doesnt?) Wink

Diablo:
One really addictive game. Mostly titled as an RPG, but I doubt it is one. What Diablo does is to wake and feed a basic instinkt of human evolution: hunting and collecting.
We all started sitting in caves, hunting down wild animals and collecting stuff... and we still do... and we love it Wink
Nobody really cares about the background of town Trinsic. What are the names of the NPCs? Is there a deep Story? Long dialogues? - No! There's a "Hunting bow of the fox", "Kite shield of the zodiac" and so on...
What I try to say: you can put the concept of diable in any other game Universe, make it SciFi, make it a Real-life Sim.. . it doesnt really matter

Monkey Island
Why are Adventures of old days in everybodys mind when we are talking about great games? Because they were great! What was great? The stunning 3D grahpics? The voice actors? - It was the storytelling! People like to read exciting books, watch movies. Little children listen totally excited to fary-tales, their parent tell them before they go to sleep. It inspires their phantasy. Reaching the gamers phantasies is the key to addicitve story-telling games.

I think these are quite good examples for want I try to say. If you want to make an addictive game, try not to reinvent the wheel. Go into the mind of the gamers. Make games from gamers for gamers (as once was the motto from interplay). Be smart try to get the right mix of these basic three elements from above, you'll make a great game some people will remember Smiley
7  Game Development / Artificial Intelligence / Re: Steering behaviors.  We need a Java versi on: 2005-05-18 11:57:44
great, thanks!! I was also looking for that Smiley
8  Java Game APIs & Engines / Java 2D / Re: Weird resolution problem on: 2005-05-18 11:42:06
thx for the reply.

I tried pack() but it didnt help.

It seemes the problem occured first, when I unplugged my second monitor (if I remember correctly...some time ago). Now I am currently working with a laptop. Strange enough that similar applications run perfectly.

Even more strange thing is, that there's always width = 0, but the height works correctly.
9  Java Game APIs & Engines / Java 2D / Weird resolution problem on: 2005-05-06 08:14:32
Hi there,

I wrote a small active rendering loop that worked quite fast. But somehow I managed to get the thing broken and I have no idea what causes the trouble.

here's the basic part of the loop:

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  
    //Constructor
    public GameWindow(GraphicsDevice device, int userDefinedGameTicks) {

        gd = device;
        setUndecorated(true);
        if (gd.isFullScreenSupported()) {
            gd.setFullScreenWindow(this);
        } else {
            System.out.println("Fullscreen not supported");
            System.exit(0);
        }

        DisplayMode[] dmodes = gd.getDisplayModes();
        Config.readIni();
        if (gd.isDisplayChangeSupported()) {
            gd.setDisplayMode(dmodes[Config.getDm()]);
        } else {
            System.out.println("Displaymode not supported");
            System.exit(0);
        }
       
        //debug information
        System.out.println(getWidth());
        System.out.println(getHeight());

            createBufferStrategy(2);
            bs = this.getBufferStrategy();

        width = this.getWidth();
        height = this.getHeight();

        fTime = .00001;
        timer = new AdvancedTimer();
        gameticks_per_second = userDefinedGameTicks;
        gametick_time = timer.getTicksPerSecond() / gameticks_per_second;
        iLib.loadAllImages();
        timer.start();

        run();
    }

    public static void main(String[] args) {
        System.setProperty("sun.java2d.translaccel", "true");
        System.setProperty("sun.java2d.accthreshold", "0");
        System.setProperty("sun.java2d.ddscale", "true");
        System.setProperty("sun.java2d.ddforcevram", "true");

        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice device = env.getDefaultScreenDevice();

        new GameWindow(device, 20);
    }


The result is a white screen without any rendering. There's this system-message:

"java.lang.IllegalArgumentException: Width (0) and height (600) cannot be <= 0"

So I checked the initial Desktop-Resolution (1600x1200) with a System.out...  before I switch to the Loop-Resolution (which is bzw. 800x600)

And all I get is:
width: 0
height: 1200

really strange thing. any ideas??
10  Java Game APIs & Engines / Java 2D / Re: pacman pathfinding on: 2005-01-19 14:20:28
Great, it runs or I should say it stops now. But somehow the coordinates that are stored in the "path" ArrayList are messed up a bit (my character is moving strangely, jumping around)

To make it easy I tested on an map without borders and walls. My map is a simple tilemap stored in a 2dimensional array.

I got a few questions concering this part:

Quote


1  
2  
3  
4  
5  
        if(map.getValue(x-1, y, 2, 2) == 0) moves[0] = !parent.equals(x-1, y); 
        if(map.getValue(x+1, y, 2, 2) == 0) moves[1] = !parent.equals(x+1, y);
        if(map.getValue(x, y-1, 2, 2) == 0) moves[2] = !parent.equals(x, y-1);
        if(map.getValue(x, y+1, 2, 2) == 0) moves[3] = !parent.equals(x, y+1);
     



I suppose map.getValue(...) returns some kind of integer where 0 means "not blocked". Am I right? And why are there 4 parameters?

I have to check the results of path to find out why the coordinates are messed up.

I also have another problem to solve. Because my walls are not as big as my tiles (instead the walls are BETWEEN the tiles) I have to write my own collision-Method that handles tile transitions...havin an idea already, but first I need to fix up the other problems. Some help would be appreciated Wink
11  Java Game APIs & Engines / Java 2D / Re: pacman pathfinding on: 2005-01-19 13:33:25
I tried to implement your algorithm jbanes. I made two Classes. One Position Class and a Pathfinder Class with the method from above.

My app is still doing an infinite loop caculating the paths. Did anyone get the code running?

I also have a question about this line of code:

Quote

1  
             path.append(start, 0);


Afaik theres no method "append" to an arraylist . Did you mean "add(int, object)" ?  (this didn't solve my problem)

12  Java Game APIs & Engines / Java 2D / Re:  [Imaging] Searching library for simulati on: 2004-12-07 10:42:26
there's an example of simulating 2D Water here:

http://freespace.virgin.net/hugo.elias/graphics/x_water.htm

however it is done in c++ (but this shouldn't be a problem).
13  Game Development / Game Play & Game Design / Re: Great games article on: 2004-01-30 22:27:29
I hope this topic does not deal only with J2ME.

For all of you who want to create RPG games, heres a really great article concerning game design of rpg (and mmorpg).

http://mu.ranter.net/theory/printversion.html

a must read!  Wink
14  Java Game APIs & Engines / Java 2D / Re: OMG it is still flickering. on: 2003-12-03 09:09:48
ok, now Smiley

with the gage-timer it runs a lot smoother.

thx a lot Smiley


btw. can I download the scoller example as a zip file somewhere?
15  Game Development / Performance Tuning / Re: An efficient discrete step timing loop. on: 2003-11-20 13:52:10
In your example you had a "timer" object that picked the current time. But how can I create such an object?

I heard of the GAGE-Engine but I don't want to use external Libraries in my project.

Are there some other useful timers in the JDK?
16  Game Development / Performance Tuning / Re: An efficient discrete step timing loop. on: 2003-11-20 08:23:36
Quote
Well "unit" isn't an actual variable or anything.  It's just the distance you want to move in a given amount of time.  That could be 200 pixels per second or 1 inch per minute, etc.  So, in the example, clientTick is the actual framerate you get that may or may not fluctuate wildly from one second to the next, while gameTick is sort of the theoretical, constant framerate that you want to achieve a.k.a the "unit" value.  The interpolation mechanism, then, sort of works as an intermediary between the actual and theoretical framerate to keep the sprite moving at constant "unit" increments.  Thus, if you want ALL of your movement to appear to move faster, you would increase your "unit" value, which, in the case of the example given would be the value of GAMETICKS_PER_SECOND.  Anyway, that's my current understanding of it...


I still got a problem. I have a simple application where a spaceship can be moved along the x-axis of the screen.
the movement (pixels/second) is very slow with the abve example (but smooth). I can't figure out how to speed up the movement of them ship. If I set the increment to a higher value, the movement becomes inconsistent.

here's my implementation of the loop:
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  
public class GameWindow extends JFrame implements Runnable {

  private static final int GAMETICKS_PER_SECOND = 25;
  private static final int MILLISECONDS_PER_GAMETICK = 1000 / GAMETICKS_PER_SECOND;  
  private static final int screenwidth = 800;
  private static final int screenheight = 600;
  private ScenePanel pScene = new ScenePanel(this);  
  boolean running = true;
  boolean left = false;
  boolean right = false;
  boolean fire = false;
  boolean lock = false;
  int fps;
  int dir = 1; // TEST: enemy automove direction

  Ship player = new Ship("gfx/bat.gif", 480, 4, 8, 100,500);
  Ship enemy = new Ship("gfx/cow.gif", 40, 4, 8, 100, 500);

  public GameWindow() {
    this.setResizable(false);
    this.setTitle("Spacefight");
    this.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(KeyEvent e) {
        this_keyPressed(e);
      }
    });
    this.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyReleased(KeyEvent e) {
        this_keyReleased(e);
      }
    });
      this.setContentPane(pScene);
    setSize(screenwidth, screenheight);
      
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      Dimension size = getSize();
      screenSize.height = screenSize.height/2;
      screenSize.width = screenSize.width/2;
      size.height = size.height/2;
      size.width = size.width/2;
      int y = screenSize.height - size.height;
      int x = screenSize.width - size.width;
      setLocation(x, y);

      
    setVisible(true);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
  }
  void this_keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_SPACE) {
      if(!lock ) {
        fire = true;
        player.shoot(player.posX+player.w/2);
      }
    }
    if (e.getKeyCode() == KeyEvent.VK_LEFT) {
      right = false;
      left = true;
    }
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
      right = true;
      left = false;
    }
  }
  void this_keyReleased(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_LEFT) {
      left = false;
    }
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
      right = false;
    }
  }

  public void run() {
      long previousTime = System.currentTimeMillis();
      long passedTime = 0;
      long frametime = previousTime;
      int framecount = 0;  
      while (running)      {  
              float interpolation = passedTime/(float)MILLISECONDS_PER_GAMETICK;
              clientTick(interpolation);  
              long time = System.currentTimeMillis();  
              passedTime += (time-previousTime);  
              previousTime = time;  
              while (passedTime>MILLISECONDS_PER_GAMETICK)  
              {  
                  framecount++;
                  if (time > (frametime + 999)) {
                    fps = framecount;
                    framecount = 0;
                    frametime = time;
                  }
                  gameTick();  
                  passedTime-=MILLISECONDS_PER_GAMETICK;  
              }  
      }
  }
      
  public void clientTick(float interpolation) {
      player.posX =(int) player.xIFloat.getValue(interpolation);
      enemy.posX =(int) enemy.xIFloat.getValue(interpolation);
      repaint();        
  }
  
  public void gameTick() {
      if (left) {
        if (player.posX > 8) {      
            player.xFloatPos = player.xIFloat.getValue(1);
            player.xFloatPos-=1;
            player.xIFloat.setValue(player.xFloatPos);
        }
        else {
            player.posX = 8;
        }
      }
      if (right) {
        if (player.posX < screenwidth-player.w-12) {
            player.xFloatPos = player.xIFloat.getValue(1);
            player.xFloatPos+=1;
            player.xIFloat.setValue(player.xFloatPos);
        }
        else {
            player.posX = screenwidth-player.w-12;
        }
      }
  }

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

} // End Class GameWindow

17  Game Development / Performance Tuning / Re: An efficient discrete step timing loop. on: 2003-11-19 13:30:04
Quote


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
IFloat xPlayerPos = new IFloat(0);

public void clientTick(float interpolation)
{
  renderPlayerAt(xPlayerPos.getValue(interpolation));
}

public void gameTick()
{
  float xPos = xPlayerPos.getValue(1);
  xPos+=1;
  xPlayerPos.setValue(xPos);
}


Now if gameTick() gets called once every second, the player would move exactly one "unit" per second, but clientTick() would still make the motion smooth.


What do you mean with "Unit"? Where is the unit defined? is it the "xPos+=1" or the "xPlayerPos.getValue(1) ??

I'like to speed up the movement of my object. If I try e.g. "xPos+=10" the object moves faster, but is hopping between several pixels (according to the interpolation-value) when there is no further key event.
18  Java Game APIs & Engines / Java 2D / Re: OMG it is still flickering. on: 2003-11-19 08:03:26
Quote
Unless this is an applet game I'd strongly recommend dropping the repaint() approach in favor of an active rendering loop.  As i think has already been pointed out 3000 calls to repaint doesn't necc equal 3000 frames as the system is free to coalesce repaint attempts.


Please forgive me my lack of knowledge but what exactly is an active rendering loop??

I&acute;'ll try the timing-loop algo posted in the other topic. It looks very suitable for me Smiley Hope it works.

thx so far... Smiley

I'll post my results here if sth important happens Wink



19  Java Game APIs & Engines / Java 2D / Re: OMG it is still flickering. on: 2003-11-18 14:42:25
with "replay(0);" there's a slight improvement (couldn't say exactly because there is still flickering)

the setContent did nothing to improve the performance.

Sad

any other ideas?  Huh  Roll Eyes
20  Java Game APIs & Engines / Java 2D / Re: OMG it is still flickering. on: 2003-11-18 11:03:28
A big thanks so far, Kev  Smiley

I'll try your proposals as soon as I'm getting eclipse running on this PC where I'm currently sitting Wink

21  Java Game APIs & Engines / Java 2D / OMG it is still flickering. on: 2003-11-18 09:03:26
Hi folks!

I was trying to create a small space-shooter (pong style) to get some experience with java 2D and realtime rendering.

a jar containing a runnable version can be found at: http://www.hdm-stuttgart.de/~ml18/jumat/jumat.zip

My problem is, that the movement of the ships is still flickering although I am using double-buffering.

Further implementations should be socket or RMI based multiplayer so I decided to run the game in a seperate thread (perhaps this could be useful later, but don't know exactly)

But even if I remove the Thread and run the game in the main Thread (it gets up to 30000fps) the flickering still exists Sad

Here's the main part of the code:

The Frame containg a JPanel in the center:
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  
public class GameWindow extends JFrame implements Runnable {

  private ScenePanel pScene = new ScenePanel(this);
  BorderLayout borderLayout1 = new BorderLayout();
  boolean running = true;
  boolean left = false;
  boolean right = false;
  boolean fire = false;
  boolean lock = false;

  int screenwidth = 800;
  int screenheight = 600;
  int fps;
  int dir = 1; // TEST: Player2 automove direction
  float frameduration;
  Ship player1 = new Ship("gfx/bat.gif", 368, 480, 300, 800, 100);
  Ship player2 = new Ship("gfx/cow.gif", 368, 40, 250, 850, 100);

  public GameWindow() {
    this.setResizable(false);
    this.setTitle("Spacefight");
    this.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(KeyEvent e) {
        this_keyPressed(e);
      }
    });
    this.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyReleased(KeyEvent e) {
        this_keyReleased(e);
      }
    });
    this.getContentPane().setLayout(borderLayout1);
    this.getContentPane().add(pScene,  BorderLayout.CENTER);
    setSize(screenwidth, screenheight);
      
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      Dimension size = getSize();
      screenSize.height = screenSize.height/2;
      screenSize.width = screenSize.width/2;
      size.height = size.height/2;
      size.width = size.width/2;
      int y = screenSize.height - size.height;
      int x = screenSize.width - size.width;
      setLocation(x, y);

      
    setVisible(true);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
  }
  void this_keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_SPACE) {
      if(!lock) {
        lock = true;
        fire = true;
        player1.shot.x = player1.posX+player1.w/2 - player1.shot.w/2;
      }
    }
    if (e.getKeyCode() == KeyEvent.VK_LEFT) {
      right = false;
      left = true;
    }
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
      right = true;
      left = false;
    }
  }
  void this_keyReleased(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_LEFT) {
      left = false;
    }
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
      right = false;
    }
  }

  public void run() {
    int count = 0;
    long fpsStartTime = System.currentTimeMillis();
    long  startTime;

    synchronized (this) {
      while (running) {
        startTime = System.currentTimeMillis();
        {try {this.wait(1);}  catch (Exception e) {}}
        pScene.repaint();
        count++;
        if (System.currentTimeMillis() > (fpsStartTime + 999)) {
          fps = count;
          count = 0;
          fpsStartTime = System.currentTimeMillis();
        }
        frameduration = (System.currentTimeMillis() - startTime) / 1000.000f;
        // Begin of Player2 automove
        if (dir==0) {
          if (player2.posX > 8) {
            player2.posX -= player2.movespeed * frameduration;
          }
          else {
            player2.posX = 8;
            dir = 1;
          }
        }
        if (dir==1) {
          if (player2.posX < screenwidth-player2.w-12) {
            player2.posX += player2.movespeed * frameduration;
          }
          else {
            player2.posX = screenwidth-player2.w-12;
            dir = 0;
          }
        }
        // End of Player2 automove
        if (left) {
          if (player1.posX > 8) {
            player1.posX -= player1.movespeed * frameduration;
          }
          else {
            player1.posX = 8;
          }
        }
        if (right) {
          if (player1.posX < screenwidth-player1.w-12) {
            player1.posX += player1.movespeed * frameduration;
          }
          else {
            player1.posX = screenwidth-player1.w-12;
          }
        }
        if (fire) {
          player1.shot.y -= player1.firespeed * frameduration;
          if (player2.checkHit(player1.shot)) {
            player2.hitpoints -= player1.shot.dmg;
            player1.shot.y = player1.shot.initY;
            fire = false;
            lock = false;
            if (player2.hitpoints <= 0) {
              player2.hitpoints = 100; // TEST: explosion will follow. GAME OVER
            }
          }
          if (player1.shot.y <= 0) {
            player1.shot.y = player1.shot.initY;
            fire = false;
            lock = false;
          }
        }
      }
    }
  }

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

}


The JPanel where the drawing takes place
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  
class ScenePanel extends JPanel {

   GameWindow gw;
   Image back = loadImage("gfx/stars0001.jpg");
   private int bufferWidth;
   private int bufferHeight;
   private Image bufferImage;
   private Graphics bufferGraphics;

   private MediaTracker mt;

   ScenePanel(GameWindow gw) {
     this.gw = gw;
   }

   public Image loadImage(String file) {
     Image img = Toolkit.getDefaultToolkit().getImage(file);
     mt = new MediaTracker(this);
     mt.addImage(img, 0);
     try {
       mt.waitForAll();
     }
     catch (InterruptedException e) { e.printStackTrace(); }
     return img;
   }

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

   public void paint(Graphics g){
     if(bufferWidth!=getSize().width || bufferHeight!=getSize().height || bufferImage==null || bufferGraphics==null)
       resetBuffer();
     if(bufferGraphics!=null){
       bufferGraphics.clearRect(0,0,bufferWidth,bufferHeight);
       paintBuffer(bufferGraphics);
       g.drawImage(bufferImage,0,0,this);
     }
   }

   private void resetBuffer(){
     bufferWidth=getSize().width;
     bufferHeight=getSize().height;
     if(bufferGraphics!=null){
       bufferGraphics.dispose();
       bufferGraphics=null;
     }
     if(bufferImage!=null){
       bufferImage.flush();
       bufferImage=null;
     }
     System.gc();
     bufferImage=createImage(bufferWidth,bufferHeight);
     bufferGraphics=bufferImage.getGraphics();
   }


   public void paintBuffer(Graphics g) {
     g.drawImage(back, 0, 0, this);
     g.drawImage(gw.player1.img, gw.player1.posX, gw.player1.posY, this);
     g.drawImage(gw.player2.img, gw.player2.posX, gw.player2.posY, this);
     g.setColor(Color.green);
     g.drawRect(gw.player2.posX, gw.player2.posY, gw.player2.w, gw.player2.h);
     g.drawRect(gw.player1.posX, gw.player1.posY, gw.player1.w, gw.player1.h);
     if (gw.fire) {
       g.drawImage(gw.player1.shot.img, gw.player1.shot.x, gw.player1.shot.y, this);
     }
     g.setColor(Color.red);
     g.fillRect(636,10,(int)(gw.player2.hitpoints*1.5),10);
     g.fillRect(636,553,(int)(gw.player1.hitpoints*1.5),10);
     g.setColor(Color.orange);
     g.drawString("FPS: " + gw.fps, 4, 16);

   }
 }


Would be really cool if someone could help me figure out where the flicker comes from Smiley
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.

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

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

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

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

lcass (39 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (74 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45
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!