Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Updated everything but the spritesheet, it is set to accept a 64x64
pixel spritesheet(each character is 64x64
- Loading branch information
Showing
5 changed files
with
237 additions
and
77 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,70 @@ | ||
package gui; | ||
package main; | ||
|
||
public class Gui { | ||
|
||
|
||
import java.awt.Dimension; | ||
import java.awt.event.ActionEvent; | ||
import java.awt.event.ActionListener; | ||
import java.awt.image.BufferStrategy; | ||
|
||
|
||
|
||
|
||
import javax.swing.JFrame; | ||
import javax.swing.JMenu; | ||
import javax.swing.JMenuBar; | ||
import javax.swing.JMenuItem; | ||
import javax.swing.JPanel; | ||
|
||
public class Game implements ActionListener { | ||
BufferStrategy strategy; | ||
public Game() { | ||
JFrame container = new JFrame("CSE 2102"); | ||
container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
|
||
JPanel panel = (JPanel) container.getContentPane(); | ||
panel.setPreferredSize(new Dimension(800,600)); | ||
panel.setLayout(null); | ||
|
||
panel.setBounds(0,0,800,600); | ||
|
||
panel.setIgnoreRepaint(true); | ||
|
||
JMenuBar jmenubar = new JMenuBar(); | ||
|
||
JMenu jmGameOptions = new JMenu("Game"); | ||
|
||
JMenuItem jmiOpen = new JMenuItem("Open"); | ||
jmGameOptions.add(jmiOpen); | ||
jmiOpen.addActionListener(this); | ||
|
||
JMenuItem jmiClose = new JMenuItem("Close"); | ||
jmGameOptions.add(jmiClose); | ||
jmiClose.addActionListener(this); | ||
|
||
JMenuItem jmiSave = new JMenuItem("Save"); | ||
jmGameOptions.add(jmiSave); | ||
jmiSave.addActionListener(this); | ||
|
||
JMenuItem jmiExit = new JMenuItem("Exit"); | ||
jmGameOptions.add(jmiExit); | ||
jmiExit.addActionListener(this); | ||
|
||
jmenubar.add(jmGameOptions); | ||
|
||
|
||
container.setJMenuBar(jmenubar); | ||
container.setVisible(true); | ||
|
||
|
||
|
||
container.pack(); | ||
container.setResizable(false); | ||
|
||
|
||
} | ||
public void actionPerformed(ActionEvent ae) { | ||
String comStr = ae.getActionCommand(); | ||
System.out.println(comStr + " Selected"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package main; | ||
|
||
import java.awt.image.BufferedImage; | ||
import java.io.IOException; | ||
|
||
import javax.imageio.ImageIO; | ||
|
||
public class BufferedImageLoader { | ||
private BufferedImage image; | ||
|
||
public BufferedImage loadimage(String path) throws IOException{ | ||
image = ImageIO.read(getClass().getResource(path)); | ||
return image; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,136 @@ | ||
package main; | ||
|
||
|
||
|
||
import java.awt.Dimension; | ||
import java.awt.event.ActionEvent; | ||
import java.awt.event.ActionListener; | ||
import java.awt.image.BufferStrategy; | ||
|
||
|
||
|
||
|
||
import javax.swing.JFrame; | ||
import javax.swing.JMenu; | ||
import javax.swing.JMenuBar; | ||
import javax.swing.JMenuItem; | ||
import javax.swing.JPanel; | ||
|
||
public class Game implements ActionListener { | ||
BufferStrategy strategy; | ||
public Game() { | ||
JFrame container = new JFrame("CSE 2102"); | ||
container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
|
||
JPanel panel = (JPanel) container.getContentPane(); | ||
panel.setPreferredSize(new Dimension(800,600)); | ||
panel.setLayout(null); | ||
|
||
panel.setBounds(0,0,800,600); | ||
|
||
panel.setIgnoreRepaint(true); | ||
|
||
JMenuBar jmenubar = new JMenuBar(); | ||
|
||
JMenu jmGameOptions = new JMenu("Game"); | ||
|
||
JMenuItem jmiOpen = new JMenuItem("Open"); | ||
jmGameOptions.add(jmiOpen); | ||
jmiOpen.addActionListener(this); | ||
|
||
JMenuItem jmiClose = new JMenuItem("Close"); | ||
jmGameOptions.add(jmiClose); | ||
jmiClose.addActionListener(this); | ||
|
||
JMenuItem jmiSave = new JMenuItem("Save"); | ||
jmGameOptions.add(jmiSave); | ||
jmiSave.addActionListener(this); | ||
|
||
JMenuItem jmiExit = new JMenuItem("Exit"); | ||
jmGameOptions.add(jmiExit); | ||
jmiExit.addActionListener(this); | ||
|
||
jmenubar.add(jmGameOptions); | ||
|
||
|
||
container.setJMenuBar(jmenubar); | ||
container.setVisible(true); | ||
|
||
|
||
|
||
container.pack(); | ||
container.setResizable(false); | ||
|
||
|
||
} | ||
public void actionPerformed(ActionEvent ae) { | ||
String comStr = ae.getActionCommand(); | ||
System.out.println(comStr + " Selected"); | ||
} | ||
} | ||
package main; | ||
|
||
import java.awt.Canvas; | ||
import java.awt.Dimension; | ||
import java.awt.Graphics; | ||
import java.awt.image.BufferStrategy; | ||
import java.awt.image.BufferedImage; | ||
import java.io.IOException; | ||
|
||
import javax.swing.JFrame; | ||
|
||
public class Game extends Canvas implements Runnable{ | ||
|
||
private static final long serialVersionUID = 1L; | ||
public static final int Width = 800; | ||
public static final int Height = 600; | ||
public final String Title = "Padriaca"; | ||
|
||
private boolean gameon = false; | ||
private Thread thread; | ||
|
||
|
||
private BufferedImage image = new BufferedImage(Width, Height, BufferedImage.TYPE_INT_RGB); | ||
private BufferedImage spriteSheet = null; | ||
|
||
|
||
private BufferedImage player; | ||
|
||
public void init() | ||
{ | ||
BufferedImageLoader loader = new BufferedImageLoader(); | ||
try{ | ||
|
||
spriteSheet = loader.loadimage("/sprite_sheet.png"); | ||
|
||
}catch(IOException e){ | ||
e.printStackTrace(); | ||
} | ||
SpriteSheet ss = new SpriteSheet(spriteSheet); | ||
player = ss.grabimage(1, 1, 16, 16); | ||
} | ||
|
||
|
||
|
||
public static void main(String args[]){ | ||
Game game = new Game(); | ||
|
||
game.setPreferredSize(new Dimension(Width, Height)); | ||
|
||
JFrame screen = new JFrame(game.Title); | ||
screen.add(game); | ||
screen.pack(); | ||
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
screen.setResizable(false); | ||
screen.setVisible(true); | ||
|
||
game.start(); | ||
} | ||
|
||
|
||
private synchronized void start(){ | ||
if (gameon) | ||
return; | ||
gameon = true; | ||
thread = new Thread(this); | ||
thread.start(); | ||
} | ||
|
||
private synchronized void stop(){ | ||
if (!gameon) | ||
return; | ||
gameon = false; | ||
try { | ||
thread.join(); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
System.exit(1); | ||
} | ||
|
||
|
||
//game loop | ||
public void run() { | ||
init(); | ||
long lasttime = System.nanoTime(); | ||
final double ticks = 60.0; | ||
double ns = 1000000000 / ticks; | ||
double delta = 0; | ||
int updates = 0; | ||
int frames = 0; | ||
long timer = System.currentTimeMillis(); | ||
while(gameon){ | ||
long now = System.nanoTime(); | ||
delta += (now - lasttime) / ns; | ||
lasttime = now; | ||
if(delta >= 1){ | ||
tick(); | ||
updates++; | ||
delta --; | ||
} | ||
render(); | ||
frames++; | ||
if (System.currentTimeMillis() - timer > 1000){ | ||
timer += 1000; | ||
System.out.println(updates +" Ticks, FPS: " + frames); | ||
updates = 0; | ||
frames = 0; | ||
} | ||
|
||
} | ||
stop(); | ||
|
||
} | ||
|
||
|
||
private void tick() { | ||
|
||
} | ||
private void render(){ | ||
BufferStrategy bufferstrat = this.getBufferStrategy(); | ||
if (bufferstrat == null){ | ||
createBufferStrategy(3); | ||
return; | ||
} | ||
Graphics g = bufferstrat.getDrawGraphics(); | ||
///////////////// | ||
g.drawImage(image, 0, 0, getWidth(), getHeight(), this); | ||
|
||
g.drawImage(player, 100, 100, this); | ||
|
||
///////////////// | ||
g.dispose(); | ||
bufferstrat.show(); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main; | ||
|
||
import java.awt.image.BufferedImage; | ||
|
||
public class SpriteSheet { | ||
private BufferedImage image; | ||
|
||
public SpriteSheet(BufferedImage image){ | ||
this.image = image; | ||
|
||
} | ||
|
||
public BufferedImage grabimage(int col, int row, int width, int height){ | ||
BufferedImage img= image.getSubimage((col * 64) - 64, (row * 64)-64, width, height); | ||
return img; | ||
} | ||
|
||
} |