Results 1 to 9 of 9
  1. #1
    Monkeymad358 is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Game Engine Not Loading

    Hey, this is my first time coding my own engine from various tutorials, and I'm stuck on a particular error. I know it has something to do with "public static void main(String [] args) {}", but everytime I add it, I just get 2 errors. One error is on the word main saying "void is an invalid type for the variable main", and an error at the end saying "Syntax error on token ")", ; expected". I've tried looking at other tutorials for an answer and I tried to copy it exactly, but I keep getting it no matter what I try. Anyone have any ideas?

    Java Code:
    package newGame;
    
    import java.awt.BorderLayout;
    import java.awt.Canvas;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
    import java.awt.image.DataBufferInt;
    import javax.swing.JFrame;
    import newGame.entities.Player;
    import newGame.gfx.Colours;
    import newGame.gfx.Font;
    import newGame.gfx.Screen;
    import newGame.gfx.SpriteSheet;
    import newGame.level.Level;
    
    public class Game extends Canvas implements Runnable {
    
    	private static final long serialVersionUID = 1L;
    	
    	public static final int WIDTH = 360;
    	public static final int HEIGHT = WIDTH * 9 / 16;
    	public static final int SCALE = 3;
    	public static final String NAME = "Elements of Metra";
    	
    	private JFrame frame;
    	
    	public boolean running = false;
    	public int tickCount = 0;
    	
    	private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    	private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
    	private int[] colours = new int[6*6*6];
    	
    	private Screen screen;
    	public InputHandler input;
    	public Level level;
    	public Player player;
    	
    	public Game() {
    		setMinimumSize(new Dimension(WIDTH *SCALE, HEIGHT *SCALE));
    		setMaximumSize(new Dimension(WIDTH *SCALE, HEIGHT *SCALE));
    		setPreferredSize(new Dimension(WIDTH *SCALE, HEIGHT *SCALE));
    	
    	frame = new JFrame(NAME);
    	
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setLayout(new BorderLayout());
    	
    	frame.add(this, BorderLayout.CENTER);
    	frame.pack();
    	
    	frame.setResizable(false);
    	frame.setLocationRelativeTo(null);
    	frame.setVisible(true);
    	}
    	
    	public void init(){
    		int index = 0;
    		for(int r = 0; r < 6; r++){
    			for(int g = 0; g < 6; g++){
    				for(int b = 0; b < 6; b++){
    					int rr = (r * 255/5);
    					int gg = (g * 255/5);
    					int bb = (b * 255/5);
    					
    					colours[index++] = rr << 16 | gg << 8 | bb;
    				}
    			}
    		}
    		
    		screen = new Screen(WIDTH, HEIGHT, new SpriteSheet("/sprite_sheet.png"));
    		input = new InputHandler(this);
    		level = new Level("/levels/small_test_level.png");
    		player = new Player(level, 0, 0, input);
    		level.addEntity(player);
    	}
    	
    	public synchronized void start() {
    		running = true;
    		new Thread(this).start();
    	}
    	
    	public synchronized void stop() {
    		running = false;
    	}
    	
    	public void run(){
    		long lastTime = System.nanoTime();
    		double nsPerTick = 1000000000D / 60D;
    		
    		int ticks = 0;
    		int frames = 0;
    	
    		long lastTimer = System.currentTimeMillis();
    		double delta = 0;
    		
    		init();
    	
    		while(running){
    			long now = System.nanoTime();
    			delta += (now - lastTime) /nsPerTick;
    			lastTime = now;
    			boolean shouldRender = true;
    			
    			while(delta >= 1){
    				ticks++;
    				tick();
    				delta -= 1;
    				shouldRender = true;
    			}
    		
    			try {
    				Thread.sleep(2);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    			
    			if (shouldRender){
    				frames++;
    				render();
    			}
    			
    			if(System.currentTimeMillis() - lastTimer >= 1000){
    				lastTimer += 1000;
    				System.out.println(ticks + " ticks, " + frames + " fps");
    				frames = 0;
    				ticks = 0;
    			}
    		}
    	}
    	
    	public void tick(){
    		tickCount++;
    		level.tick();
    		
    		for (int i = 0; i<pixels.length; i++){
    			pixels[i] = i + tickCount;
    		}
    	}
    	
    	public void render(){
    		BufferStrategy bs = getBufferStrategy();
    		if (bs == null){
    			createBufferStrategy(3);
    			return;
    		}
    		
    		int xOffset = player.x - (screen.width/2);
    		int yOffset = player.y - (screen.height/2);
    		
    		level.renderTiles(screen, xOffset, yOffset);
    		
    		for (int x = 0; x < level.width; x++){
    			int colour = Colours.get(-1,-1,-1, 000);
    			if (x % 10 == 0 & x != 0) {
    				colour = Colours.get(-1,-1, -1, 500);
    			}
    			Font.render((x % 10) + "", screen, 0 + (x * 8), 0, colour);
    		}
    		
    		level.renderEntites(screen);
    		
    		for (int y = 0; y < screen.height; y++){
    			for (int x = 0; x < screen.width; x++){
    				int colourCode = screen.pixels[x + y * screen.width];
    				if (colourCode < 255) pixels [x + y * WIDTH] = colours[colourCode];
    			}
    		}
    		
    		Graphics g = bs.getDrawGraphics();
    		
    		g.drawImage(this.image, 0, 0, getWidth(), getHeight(), null);
    		
    		g.dispose();
    		bs.show();
    		
    		public static void main(String [] args) {}
    	}
    }

    The tutorial I used to make it this far (If needed...) : YouTube
    Last edited by Monkeymad358; 05-31-2013 at 07:34 PM. Reason: /spoiler removed

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,785
    Rep Power
    5

    Default Re: Game Engine Not Loading

    You have the statement

    Java Code:
    public static void main(String [] args) {}
    inside of the render() method. It needs to be just within the Game class.

    Java Code:
    public class Game extends Canvas implements Runnable {
    ...
         public static void main(String [] args) {}
    ...
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Monkeymad358 is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Re: Game Engine Not Loading

    Quote Originally Posted by jim829 View Post
    You have the statement

    Java Code:
    public static void main(String [] args) {}
    inside of the render() method. It needs to be just within the Game class.

    Java Code:
    public class Game extends Canvas implements Runnable {
    ...
         public static void main(String [] args) {}
    ...
    }
    Regards,
    Jim
    Thanks, I now don't get any errors but the game doesn't load up anymore when I do both run game and debug mode. Is there something else wrong with the code or have I done something wrong?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,785
    Rep Power
    5

    Default Re: Game Engine Not Loading

    Where do you start the game? SwingUtilities.invokeLater(new Game()); Where do you call start()? There may be other problems which show up too.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Monkeymad358 is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Re: Game Engine Not Loading

    Sorry, I don't completely understand what you mean by SwingUtilities.invokeLater(new Game()); , as I am fairly new to coding. I thought I had Start() called at the top... I suppose that could be why it's not starting. I do also have another warning to do with not saving levels properly. That could have something to do with it maybe, though I'm not sure why it would.

    Java Code:
    package newGame.level;
    
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList; -- This has a warning saying it's not used...
    import java.util.List; -- So does this...
    
    import javax.imageio.ImageIO;
    
    import newGame.entities.Entity;
    import newGame.gfx.Screen;
    import newGame.level.tiles.Tile;
    
    public class Level {
    
    	private byte[] tiles;
    	public int width;
    	public int height;
    	public List<Entity> entities = new ArrayList<Entity>();
    	private String imagePath;
    	private BufferedImage image;
    	
    	public Level(String imagePath) {
    		if (imagePath != null) {
    			this.imagePath = imagePath;
    			this.loadLevelFromFile();
    		} else {
    			tiles = new byte[width * height];
    			this.width = 64;
    			this.height = 64;
    			tiles = new byte[width * height];
    			this.generateLevel();
    		}
    	}
    	
    	private void loadLevelFromFile() {
    		try {
    			this.image = ImageIO.read(Level.class.getResource(this.imagePath));
    			this.width = image.getWidth();
    			this.height = image.getHeight();
    			tiles = new byte [width * height];
    			this.loadTiles();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	private void loadTiles() {
    		int[] tileColours = this.image.getRGB(0, 0, width, height, null, 0, width);
    		for (int y = 0; y < height; y++) {
    			for (int x = 0; x < width; x++) {
    				tileCheck: for (Tile t : Tile.tiles) {
    					if (t != null && t.getLevelColour() == tileColours[x + y * width]) {
    						this.tiles[x + y * width] = t.getId();
    						break tileCheck;
    					}
    				}
    			}
    		}
    	}
    	
    	private void saveLevelToFile() { -- This is where one of the 3 warnings is...
    		try {
    			ImageIO.write(image, "png", new File(Level.class.getResource(this.imagePath).getFile()));
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	public void alterTile(int x, int y, Tile newTile) {
    		this.tiles[x + y * width] = newTile.getId();
    		image.setRGB(x, y, newTile.getLevelColour());
    	}
    	
    	public void generateLevel() {
    		for (int y = 0; y < height; y++) {
    			for (int x = 0; x < width; x++) {
    				if(x * y % 10 < 7) {
    				tiles[x + y * width] = Tile.GRASS.getId();
    				} else {
    					tiles[x + y * width] = Tile.STONE.getId();
    				}
    			}
    		}
    	}
    	
    	public void tick() {
    		for (Entity e : entities) {
    			e.tick();
    		}
    	}
    	
    	public void renderTiles(Screen screen, int xOffset, int yOffset) {
    		if(xOffset < 0) xOffset = 0;
    		if(xOffset > (width <<3) - screen.width) xOffset = ((width <<3) - screen.width);
    		if(yOffset < 0) yOffset = 0;
    		if(yOffset > (height <<3) - screen.height) yOffset = ((height <<3) - screen.height);
    		
    		screen.setOffset(xOffset, yOffset);
    		
    		for(int y = 0; y < height; y++) {
    			for(int x = 0; x < width; x++) {
    				getTile(x,y).render(screen, this, x <<3, y <<3);
    			}
    		}
    	}
    	
    	public void renderEntites(Screen screen) {
    		for (Entity e : entities) {
    			e.render(screen);
    		}
    		
    	}
    
    	public Tile getTile(int x, int y) {
    		if(0 > x || x >= width || 0 > y || y >= height) return Tile.VOID;
    		return Tile.tiles[tiles[x + y * width]];
    	}
    	
    	public void addEntity(Entity entity) {
    		this.entities.add(entity);
    	}
    
    }

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,785
    Rep Power
    5

    Default Re: Game Engine Not Loading

    There are two types of start in your program. The first one is the one you wrote:

    Java Code:
    public synchronized void start() { 
           running = true;
            new Thread(this).start();
     }
    The second one is the one which is called by the thread to invoke the implemented run() method.

    So how does your start() method that you wrote get called? Normally it would be from an instance of Game. So where do you create
    the instance of Game(). Perhaps I missed it but I didn't see it anywhere.

    Regarding the SwingUtilities.invokeLater method, please read about this at The Event Dispatch Thread (The Java™ Tutorials > Creating a GUI With JFC/Swing > Concurrency in Swing).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Monkeymad358 is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Re: Game Engine Not Loading

    I swear I had it somewhere, I must of accidently deleted it somewhere along the line. It worked a few days ago, but when I edited the main file it suddenly doesn't work. Not sure if this is important, but I have two unknown areas where it breaks, perhaps the two different ways to start the game. I tried adding a way to call start(); at the beginning, but that hasn't worked either.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,785
    Rep Power
    5

    Default Re: Game Engine Not Loading

    I checked the YouTube video. I don't really think he is teaching good technique but his focus is on the game. Anyway, he has this in his main.
    Java Code:
    new Game().start();
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Monkeymad358 is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Re: Game Engine Not Loading

    Quote Originally Posted by jim829 View Post
    I checked the YouTube video. I don't really think he is teaching good technique but his focus is on the game. Anyway, he has this in his main.
    Java Code:
    new Game().start();
    Regards,
    Jim
    Wow, I just put that in and it still wasn't working, and then I realised it was because it had accidently put "public static void main(String [] args)" before the bracket in the example below. This is what I had:
    Java Code:
    g.dispose();
    		bs.show();
    	}
    
                  }
    	
    	public static void main(String [] args) {}
    And this is what I was meant to do...

    Java Code:
    g.dispose();
    		bs.show();
    	}
    	
    	public static void main(String [] args) {}
    }
    Thanks for helping me with the "new Game().start();" though, I forgot I did accidently delete that. I have also watched other tutorials by people such as thenewboston, but to be honest I'd prefer to watch how DesignsbyZephyr, as he keeps me more interested since I'm doing this in my spear time. Anyway, thanks for helping me out :)

Similar Threads

  1. FANG Game Engine
    By shodai in forum Java Gaming
    Replies: 0
    Last Post: 03-08-2013, 02:33 AM
  2. game engine trouble
    By PRW56 in forum New To Java
    Replies: 57
    Last Post: 06-17-2012, 02:57 AM
  3. Replies: 1
    Last Post: 06-02-2011, 09:29 AM
  4. Game Engine in Java, is it practical idea?
    By chan_nguyen in forum New To Java
    Replies: 2
    Last Post: 09-14-2010, 03:20 PM
  5. Crate Game Engine 20080323
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 03-25-2008, 04:50 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •