Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (492)
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   
Pages: [1]
  ignore  |  Print  
  Help getting started  (Read 1333 times)
0 Members and 1 Guest are viewing this topic.
Offline zeroone
« Posted 2006-11-23 23:21:53 »

I’m sure there are many young and energetic Java programmers out there that are excited about J4K and want to develop games, but simply don’t know where to begin.  Here’s a simple template I put together that may encourage those people to contribute an entry:

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

public class M extends JFrame {
   
  boolean[] K = new boolean[65535]; // pressed keys
 
  public M() {
   
    final int WIDTH = 640;
    final int HEIGHT = 480;
    final int FRAMES_PER_SECOND = 60;
    final long FRAME_PERIOD = 1000000000L / FRAMES_PER_SECOND;
   
    Random random = new Random();
       
    // Define model
   final int BALLS = 5;
    final int BALL_SIZE = WIDTH / 10;
    final int PADDLE_WIDTH = BALL_SIZE * 2;
    final int PADDLE_HEIGHT = HEIGHT / 20;
    final int BALL_VELOCITY = 2;
    final int PADDLE_VELOCITY = 10;
    int[][] balls = new int[BALLS][4]; // [ball index][{x, y, Vx, Vy}]
   for(int i = 0; i < BALLS; i++) {
      balls[i] = new int[] {
        random.nextInt(WIDTH - BALL_SIZE),                     // x
       random.nextInt(HEIGHT - BALL_SIZE - PADDLE_HEIGHT),    // y
       random.nextBoolean() ? -BALL_VELOCITY : BALL_VELOCITY, // Vx
       random.nextBoolean() ? -BALL_VELOCITY : BALL_VELOCITY, // Vy
     };
    }
    int paddleX = (WIDTH - PADDLE_WIDTH) / 2;
   
    // Setup frame
   JPanel panel = (JPanel)getContentPane();
    panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));    
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setLocationRelativeTo(null);
    show();    
   
    // Create off-screen image for rendering
   Image image = createImage(WIDTH, HEIGHT);
    Graphics imageGraphics = image.getGraphics();
   
    long nextFrameStart = System.nanoTime();
    while(true) {
      do {
// -- UPDATE MODEL BEGIN -------------------------------------------------------
     
      // Move the balls  
     for(int i = 0; i < BALLS; i++) {
        int[] ball = balls[i];
        if (ball[0] >= WIDTH - BALL_SIZE) {
          ball[2] = -BALL_VELOCITY;
        } else if (ball[0] <= 0) {
          ball[2] = BALL_VELOCITY;
        }
        if (ball[1] >= HEIGHT - BALL_SIZE - PADDLE_HEIGHT) {
          ball[3] = -BALL_VELOCITY;
        } else if (ball[1] <= 0) {
          ball[3] = BALL_VELOCITY;
        }    
        ball[0] += ball[2];
        ball[1] += ball[3];
      }
     
      // Move the paddle
     if (K[KeyEvent.VK_LEFT] && paddleX > 0) {
        paddleX -= PADDLE_VELOCITY;
      }
      if (K[KeyEvent.VK_RIGHT] && paddleX < WIDTH - PADDLE_WIDTH) {
        paddleX += PADDLE_VELOCITY;
      }
     
// -- UPDATE MODEL END ---------------------------------------------------------
       nextFrameStart += FRAME_PERIOD;
      } while(nextFrameStart < System.nanoTime());    
// -- RENDER FRAME BEGIN -------------------------------------------------------
     imageGraphics.setColor(Color.BLACK);
      imageGraphics.fillRect(0, 0, WIDTH, HEIGHT);
     
      // Render balls
     imageGraphics.setColor(Color.RED);
      for(int i = 0; i < BALLS; i++) {
        int[] ball = balls[i];
        imageGraphics.fillOval(ball[0], ball[1], BALL_SIZE, BALL_SIZE);
      }
     
      // Render paddle
     imageGraphics.setColor(Color.YELLOW);
      imageGraphics.fillRect(
          paddleX, HEIGHT - PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT);
     
      // Draw to screen
     Graphics panelGraphics = panel.getGraphics();
      if (panelGraphics != null) {
        panelGraphics.drawImage(image, 0, 0, null);
        panelGraphics.dispose();
      }
// -- RENDER FRAME END ---------------------------------------------------------
     long remaining = nextFrameStart - System.nanoTime();
      if (remaining > 0) {
        try {
          Thread.sleep(remaining / 1000000);
        } catch(Throwable t) {          
        }
      }
    }
  }
 
  public void processKeyEvent(KeyEvent e) {
    K[e.getKeyCode()] = e.getID() == 401;
  }
 
  public static void main(String[] args) {
    new M();
  }
}


The template is not a complete game.  It displays 5 bouncing balls and a paddle that can be moved with the arrow keys.  There is no logic to enable the balls and the paddle to interact.  The ball coordinates and velocities are stored in an int array.  Other entries have used java.awt.Rectangle for grouping 4 values together, but I used an array since it can store an arbitrary number of values.   See the Game loop thread (http://www.java-gaming.org/forums/index.php?topic=15138.0) for an explation of frame skipping.

Once you compile your game to a class file, compress the class file with kzip and change the extension of the file to "jar".  FTP the jar to a web accessible folder and then add a jnlp file to make it web start launchable:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
<?xml version="1.0" encoding="utf-8"?>
<jnlp
    spec="1.5+"
    codebase="http://www.yourdomainname.com/gamedirectory"
    href="thisfile.jnlp">
  <information>    
    <title>Your Game</title>  
    <vendor>Your Name</vendor>
    <icon href="icon.gif"/>
    <icon kind="splash" href="splash.gif"/>  
  </information>    
  <resources>    
    <j2se version="1.5+"/>    
    <jar href="thegame.jar"/>  
  </resources>  
  <application-desc main-class="M"/>
</jnlp>
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.

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!