Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mdiez7 committed Aug 4, 2015
0 parents commit 0bbacf0
Show file tree
Hide file tree
Showing 14 changed files with 463 additions and 0 deletions.
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/copyright/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Projects.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/res" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Binary file added res/bg_menu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions src/net/codelets/javaplatformer/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.codelets.javaplatformer;

import javax.swing.*;
import java.awt.*;

/**
* Created by Mark Diez on 8/3/2015.
*/
public class Game {
public static void main(String[] args) {
JFrame frame = new JFrame("No Name Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(new GamePanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
88 changes: 88 additions & 0 deletions src/net/codelets/javaplatformer/GamePanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package net.codelets.javaplatformer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
* Created by Mark Diez on 8/3/2015.
*/
public class GamePanel extends JPanel implements Runnable, KeyListener {
// public
public static final int WIDTH = 850;
public static final int HEIGHT = 500;
// private
private boolean isRunning;
private Thread gameThread;
private GameStateManager gsm;

public GamePanel() {
this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
this.addKeyListener(this);
this.setFocusable(true);
this.isRunning = false;
this.start();
}

public void start() {
System.out.println("Went in start");
gsm = new GameStateManager(new GameStateMenu(gsm));
isRunning = true;
gameThread = new Thread(this);
gameThread.start();
}

@Override
public void run() {
long start, wait, elapsed;
start = wait = elapsed = 0;
while(isRunning) {
start = System.nanoTime();
this.update();
this.repaint();
elapsed = System.nanoTime() - start;

// 1000/60 is the target 60 FPS ~ elapsed/1000000 converts elapsed time to milliseconds
wait = 1000/60 - elapsed / 1000000;
if(wait <= 0) {
wait = 5;
}

try {
Thread.sleep(wait);
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}

public void update() {
gsm.update();
}

@Override
public void paintComponent(Graphics g) {
gsm.draw(g);
}

@Override
public void keyPressed(KeyEvent e) {
gsm.keyPressed(e.getKeyCode());
}

@Override
public void keyReleased(KeyEvent e) {
gsm.keyReleased(e.getKeyCode());
}

@Override
public void keyTyped(KeyEvent e) {

}

/*
Getters and Setters
*/

}
22 changes: 22 additions & 0 deletions src/net/codelets/javaplatformer/GameState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package net.codelets.javaplatformer;

import java.awt.*;

/**
* Created by Mark Diez on 8/3/2015.
*/
public abstract class GameState {

private GameStateManager gsm;

public GameState(GameStateManager gsm) {
this.gsm = gsm;
this.init();
}

public abstract void init();
public abstract void update();
public abstract void draw(Graphics g);
public abstract void keyPressed(int key);
public abstract void keyReleased(int key);
}
36 changes: 36 additions & 0 deletions src/net/codelets/javaplatformer/GameStateManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package net.codelets.javaplatformer;

import java.awt.*;
import java.util.Stack;

/**
* Created by Mark Diez on 8/3/2015.
*/
public class GameStateManager {
private Stack<GameState> gameStateStack;

public GameStateManager(GameState menuState) {
gameStateStack = new Stack<GameState>();
this.push(menuState);
}

public void push(GameState newState) {
gameStateStack.push(newState);
}

public void update() {
gameStateStack.peek().update();
}

public void draw(Graphics g) {
gameStateStack.peek().draw(g);
}

public void keyPressed(int key) {
gameStateStack.peek().keyPressed(key);
}

public void keyReleased(int key) {
gameStateStack.peek().keyReleased(key);
}
}
Loading

0 comments on commit 0bbacf0

Please sign in to comment.