Results 1 to 1 of 1
- 04-10-2014, 03:54 AM #1
Senior Member
- Join Date
- Apr 2014
- Location
- United Kingdom
- Posts
- 193
- Rep Power
- 7
I've made my game into only ONE class, help with Seperating into different classes
NOTE: My Game is not finished, and it doesn't even draw an image, but I'm trying to make the system smooth.
I've managed somehow to make my game in a single Class, but it's very messy and I don't like it that way, It would look a lot more neat if I just had it in different classes with constructors and everything. I'll provide my code, it currently doesn't have any errors. Can someone like help me do some OBVIOUS changes that could perhaps improve performace and I'd really like to seperate this whole class into different classes and constructors, but in a way that I can understand.
Thanks
Augustas
Java Code:package pack.script.game; import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.image.BufferStrategy; import java.awt.image.BufferedImage; //import java.awt.image.DataBufferInt; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JFrame; public class Game extends Canvas implements Runnable, KeyListener { private static final long serialVersionUID = 1L; public boolean iRUNNING = false; public static final String sNAME = "Java Game"; public static final String sVER = "v1.00"; public static final int sSIZE = 20; public static final int sSCALE = 32; public static final int sBORDERS = 5; public static int iSCALE = 1; public static int tSIZE = iSCALE * sSCALE; public static final String sDISPLAYNAME = sNAME + " " + sVER; public static final int sWIDTH = sSIZE * sSCALE; public static final int sHEIGHT = (int) ((double)sWIDTH / 12D * 9D); public static int iWIDTH = sWIDTH * iSCALE; public static int iHEIGHT = sHEIGHT * iSCALE; public static Dimension sDIMENSION = new Dimension(iWIDTH,iHEIGHT); public static Map[] Maps; public static int[] XGRID = new int[(sWIDTH / sSCALE)]; public static int[] YGRID = new int[(sHEIGHT / sSCALE)]; public static int[] rXGRID = new int[XGRID.length]; public static int[] rYGRID = new int[YGRID.length]; public static boolean qInit = false; private BufferStrategy sBUFFERSTRATEGY; private BufferedImage iIMAGE = new BufferedImage (iWIDTH, iHEIGHT, BufferedImage.TYPE_INT_RGB); private Graphics sGRAPHICS; private JFrame sFRAME; //public static Map mAkhuForest = new Map("Akhu Forest", 1); //private int[] pixels = ((DataBufferInt) iIMAGE.getRaster().getDataBuffer()).getData(); public Game() { setMinimumSize(new Dimension(iWIDTH,iHEIGHT)); setMaximumSize(new Dimension(iWIDTH,iHEIGHT)); setPreferredSize(new Dimension(iWIDTH,iHEIGHT)); sFRAME = new JFrame(sDISPLAYNAME); sFRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); sFRAME.setLayout(new BorderLayout()); sFRAME.add(this, BorderLayout.CENTER); sFRAME.setSize(iWIDTH, iHEIGHT); sFRAME.pack(); sFRAME.setResizable(false); sFRAME.setLocationRelativeTo(null); sFRAME.setVisible(true); } public synchronized void start() { iRUNNING = true; for (int i = 0; i < rXGRID.length; i++) { rXGRID[i] = 5 + (i * (iSCALE * sSCALE)); } for (int i = 0; i < rYGRID.length; i++) { rYGRID[i] = 5 + (i * (iSCALE * sSCALE)); } //mAkhuForest.setPassage(nPASSARR(mAkhuForest)); new Thread(this).start(); } public int[][][] nPASSARR(Map mapname) { int width = mapname.getWidth() / sSCALE; int height = mapname.getHeight() / sSCALE; int[][][] Array = new int[width][height][5]; return Array; } public void run() { long cLASTTIME = System.nanoTime(); double sNSPERTICK = 1000000000D / 60D; int cFRAMES = 0; int cTICKS = 0; long cLASTTIMER = System.currentTimeMillis(); double delta = 0; while (iRUNNING) { long cNOWTIME = System.nanoTime(); delta += (cNOWTIME - cLASTTIME) / sNSPERTICK; cLASTTIME = cNOWTIME; boolean shouldRender = true; while (delta >= 1) { cTICKS++; tick(); delta--; shouldRender = true; } try { Thread.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } if (shouldRender) { cFRAMES++; } if (System.currentTimeMillis() - cLASTTIMER >= 1000) { cLASTTIMER += 1000; System.out.println(cFRAMES + " speed, " + cTICKS + " ticks"); cFRAMES = 0; cTICKS = 0; } } } public void tick() { sBUFFERSTRATEGY = getBufferStrategy(); if (sBUFFERSTRATEGY == null) { createBufferStrategy(3); return; } sGRAPHICS = sBUFFERSTRATEGY.getDrawGraphics(); sGRAPHICS.drawImage(iIMAGE, 0, 0, getWidth(), getHeight(), null); sGRAPHICS.dispose(); sBUFFERSTRATEGY.show(); } public int sX(int place) { return rXGRID[place - 1]; } public int sY(int place) { return rYGRID[place - 1]; } public static BufferedImage Img(String path) { BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); try { image = ImageIO.read(new File(path)); } catch (IOException e) { e.printStackTrace(); } return image; } public void drawImage (Graphics g, String path, int x, int y) { BufferedImage img = Img(path); int w = img.getWidth() * iSCALE; int h = img.getHeight() * iSCALE; g.drawImage(img, x, y, w, h, null); } public void gRECT(Graphics g, int x1, int y1, int x2, int y2, boolean fill) { int x[] = {x1,x2,x2,x1,x1}; int y[] = {y1,y1,y2,y2,y1}; if (fill == false) { g.drawPolygon(x, y, 5); } else { g.fillPolygon(x, y, 5); } } public void gPIX(Graphics g, int x, int y) { g.drawLine(x, y, x, y); } public static void main(String[] args) { new Game().start(); } public void keyPressed(KeyEvent arg0) { } public void keyReleased(KeyEvent arg0) { } public void keyTyped(KeyEvent arg0) { } }
Similar Threads
-
really confused; question on a game I made
By MW130 in forum New To JavaReplies: 12Last Post: 01-15-2013, 03:41 AM -
How to play a game you made and code it at the same time with eclipse
By mitchbutter1210 in forum EclipseReplies: 9Last Post: 06-20-2012, 09:25 PM -
A game made by me.
By utsav1995 in forum Reviews / AdvertisingReplies: 0Last Post: 05-30-2012, 08:41 PM -
Let's Bounce! (game I made for LD warmup weekend)
By KevinWorkman in forum Reviews / AdvertisingReplies: 0Last Post: 04-16-2012, 03:35 PM -
I've made a ConnectFour command line game, now I'd like to upgrade to GUIs!
By paulmmj in forum New To JavaReplies: 0Last Post: 05-14-2011, 09:44 PM
Bookmarks