SJGL (Now on GitHub!)

I found some issues.

  • When I have a LWJGL program running (I forgot about it until I launched the jar), yours doesn’t draw anything, and it looks like SHC’s
  • When the background and text finally showed up, neither of the images were there

Operating System: Windows 8.1

CopyableCougar4

Issue #1: I don’t know why a LWJGL program (which uses OpenGL) would affect my program (Java2D, shamefully).

Issue #2: Are you sure you extracted both of the image files, player_16x16.png and cake_16x16.png?

I have exactly the same problem than SHC under Mageia Linux 4 with OpenJDK 1.7:
[gouessej@localhost Find The Cake]$ java -jar FindTheCake.jar
Creating Window Find The Cake
Size = 640x480
Resizable = false
Component null added to window.

The images and the JAR are in the same directory.

Edit.: It worked the second time but not the first time :clue:

[quote]Issue #1: I don’t know why a LWJGL program (which uses OpenGL) would affect my program (Java2D, shamefully).
[/quote]
This may just be something with the first time running (I Dunno)

[quote]Issue #2: Are you sure you extracted both of the image files, player_16x16.png and cake_16x16.png?
[/quote]
Yes

CopyableCougar4

Sorry for the double post, but:

When I run it from the command prompt it shows up (?).

CopyableCougar4

This may just be something with the first time running (I Dunno)

[quote]Issue #2: Are you sure you extracted both of the image files, player_16x16.png and cake_16x16.png?
[/quote]
Yes

CopyableCougar4
[/quote]
I have no idea what might be going on: If you want to try compiling the source code, here you go (1 class, 100 or-so lines):


import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import com.darkcart.lib.sjgl.BasicGame;
import com.darkcart.lib.sjgl.Window;
import com.darkcart.lib.sjgl.simplegl.Font;
import com.darkcart.lib.sjgl.simplegl.GL;
import com.darkcart.lib.sjgl.sound.PlayMIDI;
import com.darkcart.lib.sjgl.util.Color;
import com.darkcart.lib.sjgl.util.RandomInt;

public class FindTheCake extends BasicGame implements KeyListener {
	private static final long serialVersionUID = 1L;

	// Globals
	public Window win;
	public GL gl;
	public int playerX = 300;
	public int playerY = 140;
	public int cakeX = 50;
	public int cakeY = 140;
	public int cakeFound = 0;

	public void init() {
		gl = new GL();

		PlayMIDI midi = new PlayMIDI();
		midi.loadMidi("Ringo's Theme (This Boy).mid");
		midi.playMidi();
		
		// Creating Window
		win = new Window();
		win.createWindow("Find The Cake", 640, 480, false);
		win.addTool(this);
		// Adding KeyListener to Window
		win.addKeyListener(this);
		win.setFocusable(true);
	}

	public void paint(Graphics g) {
		// Drawing text, images, background
		gl.init(g);
		gl.background(Color.lightGreen);
		gl.text("Find The Cake", Color.black, new Font("Helvetica", Font.PLAIN, 24), 230, 30);
		gl.text("Cake!", cakeX, cakeY);
		gl.text("Cake Found: " + cakeFound, new Font("Helvetica", Font.BOLD, 12), 10, 10);
		gl.loadImage("player_16x16.png");
		gl.image(playerX, playerY, 64, 64);
		gl.loadImage("cake_16x16.png");
		gl.image(cakeX, cakeY, 64, 64);
		logic();
	}

	public void logic() {
		// Collisions
		if (playerX == cakeX && playerY == cakeY || playerX == cakeX + 40 && playerY == cakeY
				|| playerX == cakeX - 40 && playerY == cakeY) {
			cakeFound++;
			newPosition();
		}
	}

	public void newPosition() {
		RandomInt rand = new RandomInt();
		int rand1 = rand.nextInt(500);
		cakeX = rand1;
		repaint();
	}

	// KeyListener (inherited) methods
	public void keyPressed(KeyEvent e) {
		int keyCode = e.getKeyCode();
		if (keyCode == KeyEvent.VK_W) {
			playerY = playerY - 1;
			repaint();
		}
		if (keyCode == KeyEvent.VK_S) {
			playerY = playerY + 1;
			repaint();
		}
		if (keyCode == KeyEvent.VK_A) {
			playerX = playerX - 1;
			repaint();
		}
		if (keyCode == KeyEvent.VK_D) {
			playerX = playerX + 1;
			repaint();
		}
	}

	public void keyReleased(KeyEvent e) {
	}

	public void keyTyped(KeyEvent e) {
	}

	public static void main(String[] args) {
		FindTheCake ftc = new FindTheCake();
		ftc.init();
	}
}

Oh, so you fixed it?

Yea, I have the same problem as copyablecougar.

Operating system: Windows 8.1

Try running it from the console. Worked for Copyable.

Well it shows all the images. But the collision detection doesn’t work. I walked around all underneath the cake and it didn’t move or do anything.

Since you’re using Java2D, why not just use [icode]java.awt.Rectangle[/icode] for collision detection, since the current one doesn’t work.

Browsing the code, there are only 9 possible positions where the player can be that the cake will move.

CopyableCougar4

Ah, my collision detection works like this currently:


if (playerX == cakeX && playerY == cakeY || playerX == cakeX + 40 && playerY == cakeY
				|| playerX == cakeX - 40 && playerY == cakeY) {
			cakeFound++;
			newPosition();
		}

Yeah, that’s not good collision detection (sorry for being blunt). It only works at 3 x values in the entire display.

CopyableCougar4

Find The Cake exists mainly to show off the library, so it’s not a serious game.

Okay, but don’t you think it would make the library look better if the main feature of the demo worked?

CopyableCougar4

I’m with copyablecougar

This just sounds like laziness. Your game is supposed to be showing off your library, which means it should actually work. Your “collision detection” is merely seeing if the player’s position equals the cake’s position, which is (no offense) terrible way to do any sort of collision detection. Use hit boxes, and look up AABBs. The concept is super simple. I’ve also made a video on detecting AABB rectangle based collisions if you need some help:
wlYkneC3iFA

Remember that this game will be showing off the library’s features, so it should be polished and full of gameplay. Generally a demo should not be something you can whip up in less than an hour, it should be far more polished and feature rich.

Hey opiop, thanks for the video. I’ll watch it, implement the code, and upload a new version of Find The Cake as soon as I can, which will probably be tomorrow or Thursday.

Sorry, I thought that you would take this opportunity to show us how capable your library is. In my humble opinion, your example could become a playground to test some features, especially if you’re not motivated to implement an elaborated gameplay.

I’ve been working on SJGL for the past few days, so, without further ado, here’s SJGL 0.7

Change log (Warning: long!):

  • Added com.darkcart.lib.sjgl.sound package
  • Sound.java is now called PlayMp3.java
  • Added PlayMIDI class that can load an play MIDI files.
  • ImportExport.java is now called FileManager.java
  • Added RandomInt class
  • Added GameListener class
  • Added MapLoader class: you can now load tilemaps similar to what you would find in Mazer
  • Added com.darkcart.lib.sjgl.network package
  • Added NetworkingUtils.java: WIP, mainly just getting info from URLs now.
  • Color.java is no longer just a wrapper for java.awt.Color
  • Added addListener(GameListener l) in Window.java
  • You can now pass SimpleGL’s image() method a BufferedImage
  • Images can now be loaded from URLs and URIs
  • GitHub page now has ‘examples’ folder where, well, examples of SJGL are.

GitHub will take a little bit to be updated. The download for SJGL 0.7 can be found in the OP.

You should seriously consider changing [icode]logic()[/icode] to [icode]update()[/icode] and [icode]paint()[/icode] to [icode]render()[/icode].

Also, why the hell are you taking in Java2D’s graphics object in the paint function when ‘GL’ is doing all of the rendering? I know that it’s overwritten from AWT, but you should still have a different function for that.

Also, I’d recommend changing the name to something like BJGL (Beginners Java Game Library), since that’s really the only audience you can target with this.

Also, that changelog is actually pretty small. :stuck_out_tongue:

  • Jev