www.webdeveloper.com
Results 1 to 2 of 2

Thread: Javascript game: need help!

Hybrid View

  1. #1
    Join Date
    Jun 2013
    Posts
    2

    Javascript game: need help!

    Okay, so basically the problem I have been having is a .png isnt being loaded for my 2D top view game I am making ( For PC & Mac.). It has 2 errors, one on the line "load();" and another on the line "screen.render(x, y)" and another on "pixels[x + y * width] = Sprite.grass.pixels[(x & 15) + (y & 15) * 16];", here is the entire code(it is a bit long...):

    Code:
    Game stuff:
    
    game.java:
    
    package game;
    //this means nothing vv
    import game.graphics.Screen;
    import game.input.Keyboard;
    
    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;
    
    public class game extends Canvas implements Runnable {
    private static final long serialVersionUID = 1L;
    public static int width = 300;
    public static int height = width / 16 * 9;
    public static int scale = 3; //50400 pixels array
    public static String title = "Quest for Adventure";
    private Thread thread;
    private JFrame frame;
    private Keyboard key;
    private boolean running = false;
    private String title1;
    private Screen screen;
    private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
    private Object game;
    
    public game() {
    Dimension size = new Dimension(width*scale, height*scale);
    setPreferredSize(size);
    screen = new Screen(width, height);
    frame = new JFrame();
    key = new Keyboard();
    addKeyListener(key);
    }
    public synchronized void start() {
    running = true;
    thread = new Thread(this, "Display") ;
    thread.start();
    }
    public synchronized void stop() {
    running = false;
    try {
    thread.join();
    } catch(InterruptedException e) {
    e.printStackTrace(); }
    }
    public void run() {
    long lastTime = System.nanoTime();
    long timer = System.currentTimeMillis();
    final double ns = 1000000000.0 / 60.0;
    double delta = 0;
    int frames = 0;
    int updates = 0;
    while (running) {
    long now = System.nanoTime();
    delta += (now-lastTime) / ns;
    lastTime = now;
    while (delta >= 1) {
    update();
    updates++;
    delta--;
    }
    render();
    frames++;
    if (System.currentTimeMillis() - timer > 1000) {
    timer += 1000;
    System.out.println(updates + " ups," + frames + " fps ");
    frame.setTitle(title + " | " + updates + " ups, " + frames + " fps");
    updates = 0;
    frames = 0;
    }
    }
    stop();
    }
    int x=0, y=0;
    public void update() {
    key.update();
    if (key.up) y--;
    if (key.down) y++;
    if (key.left) x--;
    if (key.right) x++;
    }
    public void render() {
    BufferStrategy bs = getBufferStrategy();
    if (bs == null) {
    createBufferStrategy(3);
    return;
    }
    screen.clear();
    screen.render(x, y);
    for (int i = 0; i < pixels.length; i++) {
    pixels[i] = screen.pixels[i];
    }
    Graphics g = bs.getDrawGraphics();
    g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
    g.dispose();
    bs.show();
    }
    //this is the window options. ( Window as in what eclipse is)
    public static void main(String[] args) {
    game game = new game();
    game.frame.setResizable(false);
    game.frame.setTitle(game.title);
    game.frame.add(game);
    game.frame.pack();
    game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.frame.setLocationRelativeTo(null);
    game.frame.setVisible(true);
    game.requestFocus();
    game.start();
    }
    }
    
    keyboard.java:
    
    package game.input;
    
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    public class Keyboard implements KeyListener {
    private boolean[] keys = new boolean[120];
    public boolean up, down, left, right; 
    public void update() {
    up = keys[KeyEvent.VK_UP] || keys[KeyEvent.VK_W];
    down = keys[KeyEvent.VK_DOWN] || keys[KeyEvent.VK_S];
    left = keys[KeyEvent.VK_LEFT] || keys[KeyEvent.VK_A];
    right = keys[KeyEvent.VK_RIGHT] || keys[KeyEvent.VK_D];
    }
    public void keyPressed(KeyEvent e) {
    keys[e.getKeyCode()] = true;
    }
    public void keyReleased(KeyEvent e) {
    keys[e.getKeyCode()] = false;
    }
    public void keyTyped(KeyEvent e) {
    }
    
    }
    
    
    screen.java:
    
    package game.graphics;
    
    import java.util.Random;
    
    //This is here to display the pixels 
    public class Screen {
    private int width, height;
    public int[] pixels;
    public final int MAP_SIZE = 64;
    public final int MAP_SIZE_MASK = MAP_SIZE - 1;
    public int[] tiles = new int[MAP_SIZE * MAP_SIZE];
    private Random random = new Random();
    public Screen(int width, int height) {
    this.width = width;
    this.height = height;
    pixels = new int[width * height];
    for (int i = 0; i < MAP_SIZE * MAP_SIZE; i++) {
    tiles[i] = random.nextInt(0xffffff);
    }
    }
    public void clear() {
    for (int i = 0; i < pixels.length; i++) {
    pixels[i] = 0;
    }
    }
    public void render(int xOffset, int yOffset) {
    for (int y = 0; y < height; y++) {
    int yy = y + yOffset;
    //if(yy < 0 || yy >= height) break; //don't need this.
    for (int x = 0; x < width; x++) {
    int xx = x + xOffset;
    //if (xx < 0 || xx >= width) break; // we don't need this anymore.
    int tileIndex =((xx >> 4) & MAP_SIZE_MASK) + ((yy >> 4) & MAP_SIZE_MASK) * MAP_SIZE;
    pixels[x + y * width] = Sprite.grass.pixels[(x & 15) + (y & 15) * 16];
    }
    }
    }
    }
    
    
    spritesheet.java:
    
    package game.graphics;
    
    import java.awt.image.BufferedImage;
    
    import javax.imageio.ImageIO;
    
    public class Spritesheet {
    private String path;
    public final int SIZE;
    public int[] pixels;
    public static Spritesheet tiles = new Spritesheet("/Textures/spritesheet.png", 256);
    public Spritesheet(String path, int size) {
    this.path = path;
    SIZE = size;
    pixels = new int[SIZE * SIZE];
    load();
    } 
    private void load() {
    BufferedImage image = ImageIO.read(Spritesheet.class.getResource(path));
    int w = image.getWidth();
    int h = image.getHeight();
    image.getRGB(0, 0, w, h, pixels, 0, w);
    } catch(IOException) e {
    Throwable e;
    e.printStackTrace();
    }
    
    }
    
    
    sprite.java:
    
    package game.graphics;
    
    public class Sprite {
    public final int SIZE;
    private int x, y;
    public int[] pixels;
    private Spritesheet sheet;
    public int getSize(){
    return SIZE;
    }
    public Sprite grass = new Sprite(16, 0, 0, Spritesheet.tiles);
    public Sprite(int size, int x, int y, Spritesheet sheet) {
    SIZE = size;
    pixels = new int[SIZE * SIZE];
    this.x = x * size;
    this.y = y * size;
    this.sheet = sheet;
    load();
    }
    
    private void load() {
    for (int y = 0; y < SIZE; y++) {
    for (int x = 0; x<SIZE;x++) {
    pixels[x + y * SIZE] = sheet.pixels[(x + this.x) + (y + this.y) * SIZE];
    }
    }
    }
    
    }
    and that is my entire code so far. The error is it comes up as a blank screen, and I am on mac so it doesnt crash for me, when it would it when I do it on a windows. Thanks for reading through the whole thing and please help

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Had you written that code you would know that it isn't JavaScript.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles