Sure, here goes:
Main class:
package com.natekramber.main;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import com.natekramber.entities.player.Player;
import com.natekramber.entities.player.Movement;
import com.natekramber.entities.zombie.Zombie;
import com.natekramber.menus.MenuMain;
public class Main extends Canvas implements Runnable {
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private static final long serialVersionUID = 1L;
public static final int HEIGHT = 360;
public static final int WIDTH = 640;
public static final int SCALE = 1;
public static final String title = "Momentum Test";
private boolean applet = false;
public double unprocessed = 0;
public long lastTime;
public String setState = "null";
boolean running = false;
String state = "null";
int ticks = 0;
public Graphics g;
public ActionListener listener = new ActionListener(this);
public StateManager statemanager = new StateManager(this);
public MenuMain menumain = new MenuMain(this);
public Player player = new Player(this);
public Movement movement = new Movement(this);
public SpriteSheet spritesheet = new SpriteSheet(this);
public Zombie zombie = new Zombie(this);
public synchronized void start() {
requestFocus();
running = true;
new Thread(this).start();
}
public synchronized void stop() {
running = false;
}
public void init() {
addKeyListener(listener);
statemanager.setState("menumain");
}
public void run() {
lastTime = System.nanoTime();
unprocessed = 0;
int frames = 0;
double nsPerTick = 1000000000.0 / 60.0;
long lastTimer1 = System.currentTimeMillis();
init();
while (running)
{
long now = System.nanoTime();
unprocessed += (now - lastTime) / nsPerTick;
lastTime = now;
while (unprocessed >= 1)
{
tick();
frames++;
render();
unprocessed -= 1;
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (System.currentTimeMillis() - lastTimer1 > 1000)
{
lastTimer1 += 1000;
System.out.println(ticks + " ticks, " + frames + " fps");
frames = 0;
ticks = 0;
}
}
System.exit(0);
}
public void tick() {
ticks++;
this.state = statemanager.state;
if ((!setState.equals(state)) && (!setState.equals("null")))
{
if (setState.equals("quitting"))
{
if (!isApplet()) stop();
if (isApplet()) setState = state;
}
statemanager.setState(setState);
setState = "null";
}
logicUpdates();
}
public void logicUpdates() {
if (state.equals("menumain")) menumain.update();
if (state.equals("playing")) {
player.update();
}
}
public void renderUpdates() {
if (state.equals("menumain")) menumain.render(g);
if (state.equals("playing")) {
player.render(g);
}
}
public void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null)
{
this.createBufferStrategy(3);
return;
}
g = image.getGraphics();
g.setFont(new Font("Arial", 5, 18));
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.WHITE);
renderUpdates();
///
g.drawString(state, 20, 20);
g.drawString(title, 20, 40);
///
g.dispose();
g = bs.getDrawGraphics();
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
g.dispose();
bs.show();
}
public static void main(String args[]) {
Main game = new Main();
game.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
game.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
game.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(game);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
game.start();
}
public boolean isApplet() {
return applet;
}
public void setApplet(boolean applet) {
this.applet = applet;
}
}
Player class:
package com.natekramber.entities.player;
import java.awt.Graphics;
import com.natekramber.main.Interfaces.Screen;
import com.natekramber.main.Main;
public class Player implements Screen {
Main main;
private int pHeight = 64;
private int pWidth = 64;
public Player(Main main) {
this.main = main;
}
public void render(Graphics g) {
g.drawImage(main.spritesheet.sprites[0], main.movement.getX(), main.movement.getY(), 64, 64, null);
}
public void update() {
main.movement.updateMovement();
}
public int getHeight() {
return pHeight;
}
public int getWidth() {
return pWidth;
}
}
That’s all that should be needed, I think.
-Nathan