-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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> |
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); | ||
} | ||
} |
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 | ||
*/ | ||
|
||
} |
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); | ||
} |
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); | ||
} | ||
} |