1
Making a game in Javascript, need help!
Hello! I am the creator of a game called Quest for Adventure, and I really need help making it! I have run into a problem, and I don't know how to fix it! Here is the code:
Check out my blog to see what is wrong, there is a video on there.
http://www.planetminecraft.com/blog/i-n ... h-my-game/
[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 :D
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];
}
}
}
}
[/code]
Check out my blog to see what is wrong, there is a video on there.
http://www.planetminecraft.com/blog/i-n ... h-my-game/
[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 :D
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];
}
}
}
}
[/code]
10
i am recruiting java coders. not java script but java. secret gaming project startup. you can be a kid but no squeakers. PM me
if your not interested, sorry for OT
good luck with your browser adventure!
if your not interested, sorry for OT
good luck with your browser adventure!
I would love to join, but most of the code was my tutor, with some input by me. But if you could teach me some ( As I am not taught java, or any code at school. ) that would be great. I only have basic knowledge, from a website called Code Academy.
Can I help you with textures?
Sure, if you want. Basically, just make some basic floor textures, in a 16x16 grid. I will put your name in the credits if I use your textures!
If you are a good artist could you please make me a youtube profile picture, with 'hchambers150' and the symbol '¥' in it, twitch profile picture, with the symbol '¥' in it, and a twitch offline picture, with 'hchambers150', the symbol '¥', and the profile picture in the top left corner, along with your name / link in one of the corners, as big as the submit button, and maybe some channel art, featuring mine craft, my youtube name in the middle and the symbol '¥'.
I do not know the quality they all should be, but please do the offline picture as high quality as possible, and try and make it look really cool
Thanks bro, you do not have to do this, quite obviously.
If you are a good artist could you please make me a youtube profile picture, with 'hchambers150' and the symbol '¥' in it, twitch profile picture, with the symbol '¥' in it, and a twitch offline picture, with 'hchambers150', the symbol '¥', and the profile picture in the top left corner, along with your name / link in one of the corners, as big as the submit button, and maybe some channel art, featuring mine craft, my youtube name in the middle and the symbol '¥'.
I do not know the quality they all should be, but please do the offline picture as high quality as possible, and try and make it look really cool
Thanks bro, you do not have to do this, quite obviously.
Look at Slick2D, it looks like Java AWT: http://slick.ninjacave.com/
-.- LWJGL is an Engine not a software. It makes things easier. A lot easier. Not to mention this is Java not Javascript?
And i know you have put effort in but if you change things now rather than later it will be easier for you in the long run.
And i know you have put effort in but if you change things now rather than later it will be easier for you in the long run.
I don't see whats wrong. But when coding a game in java i do recommend using LWJGL
I am using Eclipse, and I have put quite a lot of effort into it so far, and I dont really want to change things.
You havent told us what the problem is....
There is a link to my blog which has a video on the problem, but basically there is an image which is name spritesheet.png which is also in the post, and it should be loading the top left texture for the floor of my world, but it will not do it and it will just load a blank screen, but I think it should be crashing because I watched a tutorial and it crashed for him ( But they were on a windows PC, I am on Mac using Eclipse. )
