Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (491)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (555)
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  
  1kvaders  (Read 1411 times)
0 Members and 1 Guest are viewing this topic.
Offline MetalBeetle

Senior Newbie





« Posted 2011-02-28 22:40:36 »

So for amusement's sake I thought I'd see what kind of game fits into 1 kB of Java, given that about half of that seems to be taken up by overhead. This is what I managed to cram in:

http://www.metalbeetle.com/1kvaders.html

Click once to focus on applet. Then use arrow keys to move and space to fire. Don't let the green blobs get to the bottom of the screen.

It's fairly terrible, but just about qualifies as a game. (I won't submit it to the official contest as it's clearly terrible compared to what you can do with quadruple the space.)
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-02-28 22:57:38 »

That's quite cool with only 1kb.

Offline MetalBeetle

Senior Newbie





« Reply #2 - Posted 2011-02-28 23:16:32 »

About 560 kB of that is just the scaffolding of having a main loop and keyboard input listeners. So 1 kB is probably about the lowest amount of memory you could have a competition on. A competition full of half-baked space invaders clones, simple driving games, and maybe tic-tac-toe?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Alan_W

JGO Knight


Medals: 8
Projects: 3


Java tames rock!


« Reply #3 - Posted 2011-02-28 23:41:55 »

I raise you Pong 1k  Grin

The pack200 is 1023 bytes.  I've linked to the jar, which is a bit bigger.

pack200 file: http://homepage.ntlworld.com/alan.d.waddington/java/competitions/java1k/Pong1k_kzip128.jar.pack.gz

Controls:

Click for Focus
Space to Start
X and S to control bat

Time flies like a bird. Fruit flies like a banana.
Offline MetalBeetle

Senior Newbie





« Reply #4 - Posted 2011-03-01 09:02:20 »

Awesome! It's a bit more of a game than mine. Can I have a look at the source code? Mine is here:

https://gist.github.com/848861
Offline Alan_W

JGO Knight


Medals: 8
Projects: 3


Java tames rock!


« Reply #5 - Posted 2011-03-01 19:11:00 »

Actually I thought yours was better  Cheesy

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  
* Copyright 2011 Alan Waddington
 *
 * Controls
 * Up:    S
 * Down:  X
 * Start: SPACE
 */

import java.applet.Applet;
import java.awt.Color;
import java.awt.Event;
import java.awt.image.BufferedImage;
import java.awt.Graphics;

/** Play Pong */
public class P extends Applet implements Runnable {

    private final static int SCREENWIDTH  = 800;    // Applet size
   private final static int SCREENHEIGHT = 600;
    private final static int BATWIDTH     = 10;     // Bat dimensions
   private final static int BATHEIGHT    = 30;
    private final static int BALLSIZE     = 10;     // Ball diameter
   private final static int BATX         = 20;  // Bat indent from screen edge

    private final static float PLAYERSPEED   = 0.0002f;   // Player bat speed
   private final static float COMPUTERSPEED = 0.000186f; // Computer bat speed
   private final static float BALLSPEED     = 0.0002f;   // Ball Speed (X & Y)
   private final static int   WIN           = 3;         // Winning Score

    private static boolean input[] = new boolean[0x10000]; // Keyboard map

    /** Start Game */
    @Override
    public void start() {
        new Thread(this).start();
    }

    /** Game Thread */
    public void run() {

        // Initialisation
       float playerX0 = 0, playerY0 = 0;
        float playerX1 = 0, playerY1 = 0;
        float ballX = 0, ballY = 0;

        float ballSpeedX = BALLSPEED;
        float ballSpeedY = BALLSPEED;

        int playerScore = 0;
        int computerScore = 0;

        boolean gameOver = true;
        boolean reset = true;

        long time, lastTime;
        long deltaTime = 0;
        lastTime = time = System.nanoTime()/1000;

        // Create screen image (double buffer)
       BufferedImage screen = new BufferedImage(
            SCREENWIDTH, SCREENHEIGHT, BufferedImage.TYPE_INT_RGB);
        while(!isActive()) Thread.yield();  // Appletviewer workaround
       Graphics g = screen.getGraphics();
        Graphics gs = getGraphics();
        //requestFocus();

        // Game loop
       do {
            lastTime = time;
            time = System.nanoTime()/1000;
            deltaTime = time - lastTime;

            // Restart game
           if (reset) {
                reset = false;
                playerScore = 0;
                computerScore = 0;
                playerX0 = BATX-BATWIDTH;
                playerY0 = SCREENHEIGHT/2;
                playerX1 = SCREENWIDTH-BATX;
                playerY1 = SCREENHEIGHT/2;
                ballX = SCREENWIDTH/2;
                ballY = SCREENHEIGHT/2;
            }

            // Player Bat Position
           if (input['s'] && playerY0>0)
                playerY0-=PLAYERSPEED*deltaTime;
            if (input['x'] && playerY0<SCREENHEIGHT-BATHEIGHT)
                playerY0+=PLAYERSPEED*deltaTime;

            // Computer Bat Position
           if (playerY1+BATHEIGHT/2>ballY+BALLSIZE/2) {
                playerY1-=COMPUTERSPEED*deltaTime;
            }
            if (playerY1+BATHEIGHT/2<ballY+BALLSIZE/2) {
                playerY1+=COMPUTERSPEED*deltaTime;
            }

            // Move Ball
           if (!gameOver) {
                ballX += ballSpeedX*deltaTime;
                ballY += ballSpeedY*deltaTime;
            }
            // Collision Logic
           if (ballY<0) {                              // Top
               ballY = - ballY;
                ballSpeedY *=-1;
            }
            if (ballY>SCREENHEIGHT-BALLSIZE) {          // Bottom
               ballY = 2*(SCREENHEIGHT-BALLSIZE) - ballY;
                ballSpeedY *=-1;
            }
            if (ballX<playerX0+BATWIDTH &&             // Left (Bat)
               ballY-playerY0<BATHEIGHT+BALLSIZE &&
                playerY0-ballY<BALLSIZE) {
                ballX = 2*(playerX0+BATWIDTH) - ballX;
                ballSpeedX *=-1;
            }
            if (ballX+BALLSIZE>playerX1 &&              // Right (Bat)
               ballY-playerY1<BATHEIGHT+BALLSIZE &&
                playerY1-ballY<BALLSIZE) {
                ballX = 2*(playerX1-BALLSIZE) - ballX;
                ballSpeedX *=-1;
            }
            if (ballX<-BATWIDTH) {                      // Player misses
               computerScore++;
                ballX = SCREENWIDTH/2;
                ballY = SCREENHEIGHT/2;
                ballSpeedX *=-1;
            }
            if (ballX>SCREENWIDTH) {                    // Computer misses
               playerScore++;
                ballX = SCREENWIDTH/2;
                ballY = SCREENHEIGHT/2;
                ballSpeedX *=-1;
            }

            if (playerScore>=WIN || computerScore>=WIN) { // Detect game over
               gameOver = true;
            }

            // Draw bats and ball
           g.setColor(Color.BLACK);
            g.fillRect(0,0,SCREENWIDTH, SCREENHEIGHT);
            g.setColor(Color.GREEN);
            g.fillRect((int)playerX0, (int)playerY0, BATWIDTH, BATHEIGHT);
            g.fillRect((int)playerX1, (int)playerY1, BATWIDTH, BATHEIGHT);
            g.fillRect((int)ballX, (int)ballY, BALLSIZE, BALLSIZE);

            // Display Scores and messages
           //g.setFont(getFont().deriveFont(java.awt.Font.BOLD,20));
           g.drawString("["+String.valueOf(playerScore+"]"), SCREENWIDTH/4, 20);
            if (playerScore==WIN) {
                g.drawString("You Win",SCREENWIDTH/2-20,100);
            }
            g.drawString("["+String.valueOf(computerScore)+"]", SCREENWIDTH*3/4, 20);
            if (computerScore==WIN) {
                g.drawString("I Win",SCREENWIDTH/2-10,100);
            }
            if (gameOver) {
                g.drawString("Press SPACE",SCREENWIDTH/2-35,130);
                if (input[' ']) {
                    gameOver = false;
                    reset = true;
                }
            }

            // Update screen

            gs.drawImage(screen, 0,0,null);

            Thread.yield();
        } while (isActive());

    }

    /** Process Keyboard Events */
    @Override
    public boolean handleEvent(Event e) {
        input[e.key] = (e.id&1)==1;
        return false;
    }

}

Time flies like a bird. Fruit flies like a banana.
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 (29 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (38 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 (34 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (48 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!