Newbie needs start help :)

Hi!
Im new to Java game development and dont know where to start… :smiley:

So that’s what i’ve done already:

import java.awt.*;
import java.awt.event.*;

public class MainPanel extends Panel implements KeyListener {
      private MyCanvas bier;
      private double xs, ys, x, y;

      public MainPanel() {
            setLayout(null);

            bier = new MyCanvas("bier.jpg");
            bier.setBounds(20, 20, 100, 100);
            add(bier);
            x = bier.getX();
            y = bier.getY();

            Thread t = new Thread(new Runnable() {
                  public void run() {
                        while (true) {
                              try {
                                    Thread.sleep(1000 / 60);
                              } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                              }
                              x += xs;
                              y += ys;
                              bier.setLocation((int) x, (int) y);
                        }
                  }
            });
            t.start();

      }

      public void keyTyped(KeyEvent ev) {
      }

      public void keyPressed(KeyEvent ev) {
            int x = bier.getX(), y = bier.getY();

            if (ev.getKeyCode() == KeyEvent.VK_DOWN)
                  ys += 0.1;
            else if (ev.getKeyCode() == KeyEvent.VK_UP)
                  ys -= 0.1;
            else if (ev.getKeyCode() == KeyEvent.VK_LEFT)
                  xs -= 0.1;
            else if (ev.getKeyCode() == KeyEvent.VK_RIGHT)
                  xs += 0.1;
      }

      public void keyReleased(KeyEvent ev) {
      }

}


public class MyCanvas extends Canvas {
      Image image;

      public MyCanvas(String file) {
            image = Toolkit.getDefaultToolkit().getImage(file);
            
      }
      
      public void paint(Graphics g) {
            if (image != null) {
                  g.drawImage(image, 0, 0, this);
            } else
                  System.out.print("a");
      }
}

I think this is very bad coding, but i dont know how to optimize it.
The problem is that this app doesnt run on every computer at the same speed i think! So how can i fix that (ticks? time?)?
And how can i program a double buffer or hardware accelerated game?

It would be very nice if u could give me some tips! :slight_smile:

BIG THX

Hi

A way of getting the framerate (fps) is to use a timer and to get the length of the last frame by subtracting the time from the previous frame from the new time.


while(true) {
  //do your stuff
  newTime = getCurrentTime();  // e.g. System.currentTimeMillis(); you just have to respect, that currentTimeMilis returns the time in milli seconds and it's a very slow counter. So you would have to trick with it. (by counting the frames it didn't change etc.)
  fps = 1/(newTime-oldTime);
  oldTime = newTime();
}

Arne

and what is that for? ???
and whats NewTime()?
i always get a / by 0 exception

How about try out this game engine: GTGE
And all your headache will be gone 8)
It has done the fps thing, and double buffering that you mention, and the beauty thing is you could always go back to replace everything :wink:
If you have any troubles you can visit the engine forum to ask. :slight_smile:

You could also have a look at this thread from last week that is full of resources for learning how to program games. (The first few posts are nonsense, but it gets better.)