My first Ludum dare entry
I would like to enter the Ludum Dare competition this year using Java in eclipse, gimp and garage band. I have decided after looking at the rules that using this game engine would benefit me greatly. This is the first time I will be entering and as far as I can tell this is legal, but if it isn’t then please tell me.
class first:
import
java.awt.*;
import
javax.swing.*;
import
com.semcom.engine.graphics.Animation;
import
com.semcom.engine.graphics.Sprite;
import
com.semcom.engine.input.InputHandler;
public
class First {
publicstaticvoid main(String []args){
First f =
new First();
f.run();
}
public First() {
}
private Animation manA;
private ScreenManager s;
private Image back;
publicstaticdoublerotationo = 0;
publicstaticdoublerotationn = 0;
publicstaticdoublerotationa;
publicstatic Sprite man;
publicstaticbooleanrunning = true;
privatestaticfinal DisplayMode modes1[] = {
new DisplayMode(1920, 1080, 64, 0),
new DisplayMode(1920, 1080, 32, 0),
new DisplayMode(1920, 1080, 24, 0),
new DisplayMode(1920, 1080, 16, 0),
new DisplayMode(1280, 720, 64, 0),
new DisplayMode(1280, 720, 32, 0),
new DisplayMode(1280, 720, 24, 0),
new DisplayMode(1280, 720, 16, 0),
new DisplayMode(800, 600, 64, 0),
new DisplayMode(800, 600, 32, 0),
new DisplayMode(800, 600, 24, 0),
new DisplayMode(800, 600, 16, 0),
new DisplayMode(640, 480, 64, 0),
new DisplayMode(640, 480, 32, 0),
new DisplayMode(640, 480, 24, 0),
new DisplayMode(640, 480, 16, 0),
};
publicvoid loadImages() {
back = new ImageIcon(“res\\images\\background.jpg”).getImage();
Image face1 =
new ImageIcon(“res\\images\\man.png”).getImage();
Image face2 =
new ImageIcon(“res\\images\\man2.png”).getImage();
manA = new Animation();
manA.addScene(face1, 250);
manA.addScene(face2, 250);
man = new Sprite(manA);
man.setVelocityX(0.3f);
man.setVelocityY(0.3f);
}
publicvoid run() {
s = new ScreenManager();
try {
DisplayMode dm =
s.findFirstCompatibleMode(modes1);
s.setFullScreen(dm);
loadImages();
loop();
}
finally {
s.restoreScreen();
}
}
publicvoid loop() {
long startingTime = System.currentTimeMillis();
long cumTime = startingTime;
long endTime = 100;
while(cumTime – startingTime < endTime) {
long timePassed = System.currentTimeMillis() – cumTime;
cumTime += timePassed;
update(timePassed);
if (InputHandler.mouseX > man.getX() + (man.getWidth() / 2) && InputHandler.mouseY < man.getY() + (man.getHeight() / 2))
{
rotationn = Math.round(Math.toRadians(Math.atan((man.getY() – InputHandler.mouseY)/(InputHandler.mouseX – man.getX()))));
}
elseif (InputHandler.mouseX > man.getX() + (man.getWidth() / 2) && InputHandler.mouseY > man.getY() + (man.getHeight() / 2))
{
rotationn = Math.round(Math.toRadians(Math.atan((InputHandler.mouseY – man.getY())/(InputHandler.mouseX – man.getX())) + Math.toRadians(90)));
}
elseif (InputHandler.mouseX < man.getX() + (man.getWidth() / 2) && InputHandler.mouseY > man.getY() + (man.getHeight() / 2))
{
rotationn = Math.round(Math.toRadians(Math.atan((InputHandler.mouseY – man.getY())/(man.getX() – InputHandler.mouseX))) + Math.toRadians(180));
}
elseif (InputHandler.mouseX < man.getX() + (man.getWidth() / 2) && InputHandler.mouseY < man.getY() + (man.getHeight() / 2))
{
rotationn = Math.round(Math.toRadians(Math.atan((man.getY() – InputHandler.mouseY)/(man.getX() – InputHandler.mouseX))) + Math.toRadians(270));
}
rotationa = rotationn – rotationo;
Graphics2D g =
s.getGraphics();
draw(g);
g.dispose();
System.
out.println(“rotationn: “ + rotationn);
System.
out.println(“rotationo: “ + rotationo);
s.update();
rotationo = rotationn;
if (cumTime – startingTime >= 10000)
{
running = false;
}
if(running){
endTime = cumTime + 1;
}
else{
endTime = 0;
}
try{
Thread.sleep(20);
}
catch(Exception ex) {
System.
err.println(“Error: “ + ex);
}
}
}
publicvoid draw(Graphics2D g) {
g.drawImage(
back, 0, 0, null);
g.drawImage(
man.getImage(), Math.round(man.getX()), Math.round(man.getY()), null);
}
publicvoid update(long timePassed) {
if(man.getX() < 0) {
man.setVelocityX(Math.abs(man.getVelocityX()));
}
elseif (man.getX() + man.getWidth() >= s.getWidth()) {
man.setVelocityX(-Math.abs(man.getVelocityX()));
}
if(man.getY() < 0) {
man.setVelocityY(Math.abs(man.getVelocityY()));
}
elseif (man.getY() + man.getHeight() >= s.getHeight()) {
man.setVelocityY(-Math.abs(man.getVelocityY()));
}
man.update(timePassed);
}
}
class loop:
import
java.util.Random;
public
class Loop implements Runnable{
String
name;
inttime;
Random
r = new Random();
public Loop(String s)
{
name = s;
time = r.nextInt(999);
}
publicvoid run() {
try{
System.
out.printf(“%s is sleeping for %d\n”, name, time);
Thread.sleep(
time);
System.
out.printf(“%s is done\n”, name);
}
catch(Exception e)
{
}
}
}
class ScreenManager:
import
java.awt.*;
import
java.awt.image.BufferedImage;
import
java.awt.image.BufferStrategy;
import
javax.swing.JFrame;
public
class ScreenManager {
private GraphicsDevice vc;
public ScreenManager() {
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
vc = e.getDefaultScreenDevice();
}
public DisplayMode[] getCompatibleDisplayModes() {
returnvc.getDisplayModes();
}
public DisplayMode findFirstCompatibleMode(DisplayMode modes[]) {
DisplayMode goodModes[] =
vc.getDisplayModes();
for(int x = 0; x <modes.length; x++){
for(int y = 0; y < goodModes.length; y++){
if(displayModesMatch(modes[x], goodModes[y])) {
return modes[x];
}
}
}
returnnull;
}
public DisplayMode getCurrentDisplayMode() {
returnvc.getDisplayMode();
}
publicboolean displayModesMatch(DisplayMode m1, DisplayMode m2) {
if(m1.getWidth() != m2.getWidth() || m1.getHeight() != m2.getHeight()) {
returnfalse;
}
if(m1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m1.getBitDepth() != m2.getBitDepth()) {
returnfalse;
}
if(m1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m1.getRefreshRate() != m2.getRefreshRate()) {
returnfalse;
}
returntrue;
}
publicvoid setFullScreen(DisplayMode dm) {
JFrame f =
new JFrame();
f.setUndecorated(
true);
f.setIgnoreRepaint(
true);
f.setResizable(
false);
f.setSize(800, 640);
vc.setFullScreenWindow(f);
if(dm != null && vc.isDisplayChangeSupported()) {
try{
vc.setDisplayMode(dm);
}
catch(Exception e) {System.out.println(“”);}
}
f.createBufferStrategy(2);
}
public Graphics2D getGraphics() {
Window w =
vc.getFullScreenWindow();
if(w != null) {
BufferStrategy s = w.getBufferStrategy();
return (Graphics2D)s.getDrawGraphics();
}
else {
returnnull;
}
}
publicvoid update() {
Window w =
vc.getFullScreenWindow();
if(w != null) {
BufferStrategy s = w.getBufferStrategy();
if(!s.contentsLost()) {
s.show();
}
}
}
public Window getFullScreenWindow() {
returnvc.getFullScreenWindow();
}
publicint getWidth() {
Window w =
vc.getFullScreenWindow();
if(w != null) {
return w.getWidth();
}
else {
return 0;
}
}
publicint getHeight() {
Window w =
vc.getFullScreenWindow();
if(w != null) {
return w.getHeight();
}
else {
return 0;
}
}
publicvoid restoreScreen(){
Window w =
vc.getFullScreenWindow();
if(w != null) {
w.dispose();
vc.setFullScreenWindow(null);
}
}
public BufferedImage createCompatibleImage(int w, int h, int t) {
Window win =
vc.getFullScreenWindow();
if(win != null) {
GraphicsConfiguration gc = win.getGraphicsConfiguration();
return gc.createCompatibleImage(w, h, t);
}
returnnull;
}
}
class Animation:
import
java.util.ArrayList;
import
java.awt.Image;
public
class Animation {
private ArrayList scenes;
privateintsceneIndex;
privatelongmovieTime;
privatelongtotalTime;
public Animation() {
scenes = new ArrayList();
totalTime = 0;
start();
}
publicsynchronizedvoid addScene(Image i, long t) {
totalTime += t;
scenes.add(new Onescene(i, totalTime));
}
publicsynchronizedvoid start() {
movieTime = 0;
sceneIndex = 0;
}
publicsynchronizedvoid update(long timePassed) {
if(scenes.size() > 1 ) {
movieTime += timePassed;
if(movieTime >= totalTime) {
movieTime = 0;
sceneIndex = 0;
}
while(movieTime > getScene(sceneIndex).endTime) {
sceneIndex++;
}
}
}
publicsynchronized Image getImage() {
if(scenes.size() == 0) {
returnnull;
}
else {
return getScene(sceneIndex).pic;
}
}
private Onescene getScene(int x) {
return (Onescene)scenes.get(x);
}
privateclass Onescene{
Image
pic;
longendTime;
public Onescene(Image pic, long endTime) {
this.pic = pic;
this.endTime = endTime;
}
}
}
class screen:
import
java.awt.*;
import
javax.swing.*;
public
class Screen {
private GraphicsDevice gd;
public Screen(){
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
gd = env.getDefaultScreenDevice();
}
publicvoid setFullScreen(DisplayMode dm, JFrame frame)
{
frame.setUndecorated(
true);
frame.setResizable(
false);
gd.setFullScreenWindow(frame);
if(dm != null && gd.isDisplayChangeSupported())
{
try{
gd.setDisplayMode(dm);
}
catch(Exception e){
}
}
}
public DisplayMode[] getCompatibleDisplayModes(){
returngd.getDisplayModes();
}
public DisplayMode findFirstCompatibleMode(DisplayMode modes[]){
DisplayMode[] goodModes =
gd.getDisplayModes();
for (int i = 0; i < modes.length; i++)
{
for (int y = 0; y < goodModes.length; y++)
{
if(displayModesMatch(modes[i], goodModes[y])){
return modes[i];
}
}
}
returnnull;
}
public DisplayMode getCurrentDisplayMode(){
returngd.getDisplayMode();
}
publicboolean displayModesMatch(DisplayMode m1, DisplayMode m2)
{
if(m1.getWidth() != m2.getWidth() || m1.getHeight() != m2.getHeight()){
returnfalse;
}
if(m1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m1.getBitDepth() != m2.getBitDepth())
{
returnfalse;
}
if (m1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m1.getRefreshRate() != m2.getRefreshRate())
{
returnfalse;
}
returntrue;
}
public Window getFullScreeenWindow(){
returngd.getFullScreenWindow();
}
publicvoid restoreScreen(){
Window w =
gd.getFullScreenWindow();
if (w != null){
w.dispose();
}
gd.setFullScreenWindow(null);
}
}
class sprite
import
java.awt.Image;
public
class Sprite {
private Animation a;
privatefloatx;
privatefloaty;
privatefloatvx;
privatefloatvy;
public Sprite(Animation a) {
this.a = a;
}
publicvoid update(long timePassed) {
x += vx * timePassed;
y += vy * timePassed;
a.update(timePassed);
}
publicfloat getX() {
returnx;
}
publicfloat getY() {
returny;
}
publicvoid setX(float x) {
this.x = x;
}
publicvoid setY(float y) {
this.y = y;
}
publicint getWidth() {
returna.getImage().getWidth(null);
}
publicint getHeight() {
returna.getImage().getHeight(null);
}
publicfloat getVelocityX() {
returnvx;
}
publicfloat getVelocityY() {
returnvy;
}
publicvoid setVelocityX(float vx) {
this.vx = vx;
}
publicvoid setVelocityY(float vy) {
this.vy = vy;
}
public Image getImage() {
returna.getImage();
}
}
class inputhandler
import
java.awt.event.FocusEvent;
import
java.awt.event.FocusListener;
import
java.awt.event.KeyEvent;
import
java.awt.event.KeyListener;
import
java.awt.event.MouseEvent;
import
java.awt.event.MouseListener;
import
java.awt.event.MouseMotionListener;
public
class InputHandler implements KeyListener, MouseListener, MouseMotionListener, FocusListener {
publicstaticboolean[] key= newboolean[68836];
publicstaticintmouseX;
publicstaticintmouseY;
publicstaticintmouseButton;
publicstaticbooleanclicked = false;
@Override
publicvoid mouseDragged(MouseEvent e) {
}
@Override
publicvoid mouseMoved(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
}
@Override
publicvoid mouseClicked(MouseEvent e) {
}
@Override
publicvoid mouseEntered(MouseEvent e) {
}
@Override
publicvoid mouseExited(MouseEvent e) {
}
@Override
publicvoid mousePressed(MouseEvent e) {
}
@Override
publicvoid mouseReleased(MouseEvent e) {
}
@Override
publicvoid keyPressed(KeyEvent e) {
int keycode = e.getKeyCode();
if (keycode > 0 && keycode < key.length)
{
key[keycode] = true;
}
}
@Override
publicvoid keyReleased(KeyEvent e) {
int keycode = e.getKeyCode();
if (keycode > 0 && keycode < key.length)
{
key[keycode] = false;
}
}
@Override
publicvoid keyTyped(KeyEvent e) {
}
@Override
publicvoid focusGained(FocusEvent e) {
}
@Override
publicvoid focusLost(FocusEvent e) {
for (int i = 0; i < key.length; i++)
{
key[i] = false;
}
}
}
and that is what I have got. It is very basic and all it does at the minute is make a man run across a screen and bounce at the edges. As I said before this is the first time I will be entering and as far as I can tell this is legal, but if it isn’t then please tell me.
Good Luck everybody

