My game gives me a white screen when I run it with tiles moving enabled

When I try to run my game with x++ and y++ enabled after update(), I get a white screen.

(Somewhat) working code:

package com.jr.tm.graphics;

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 com.jr.tm.Input.Keyboard;

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; 
public static String title = "The Struggle";

private Thread thread;
private JFrame frame;
private Keyboard key;
private boolean running = false;

private Screen screen; 

private BufferedImage image= new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData(); 

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 = true) {
		long now = System.nanoTime();
		delta += (now - lastTime) / ns;
		lastTime = now;
		while (delta >= 1) {
			update();
			updates++;
			delta--;
		}
		render();
		frames++;
		
		if (System.currentTimeMillis() - timer > 10000)
			timer += 1000;
		System.out.println(updates+ "ups, " + frames + "fps");
		frame.setTitle( title + " | " + updates+ "ups, " + frames + "fps");
		updates = 0;
		frames = 0;
	}
	stop();
}
int x = 0; int y = 0;

public void update() {
	//x++;
	//y++;
}

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(); }

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.start();

}

}

Not working code:

package com.jr.tm.graphics;

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 com.jr.tm.Input.Keyboard;

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; 
public static String title = "The Struggle";

private Thread thread;
private JFrame frame;
private Keyboard key;
private boolean running = false;

private Screen screen; 

private BufferedImage image= new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData(); 

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 = true) {
		long now = System.nanoTime();
		delta += (now - lastTime) / ns;
		lastTime = now;
		while (delta >= 1) {
			update();
			updates++;
			delta--;
		}
		render();
		frames++;
		
		if (System.currentTimeMillis() - timer > 10000)
			timer += 1000;
		System.out.println(updates+ "ups, " + frames + "fps");
		frame.setTitle( title + " | " + updates+ "ups, " + frames + "fps");
		updates = 0;
		frames = 0;
	}
	stop();
}
int x = 0; int y = 0;

public void update() {
	x++;
	y++;
}

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(); }

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.start();

}

}
package com.jr.tm.graphics;

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 com.jr.tm.Input.Keyboard;

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; 
public static String title = "The Struggle";

private Thread thread;
private JFrame frame;
private Keyboard key;
private boolean running = false;

private Screen screen; 

private BufferedImage image= new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData(); 

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 = true) {
		long now = System.nanoTime();
		delta += (now - lastTime) / ns;
		lastTime = now;
		while (delta >= 1) {
			update();
			updates++;
			delta--;
		}
		render();
		frames++;
		
		if (System.currentTimeMillis() - timer > 10000)
			timer += 1000;
		System.out.println(updates+ "ups, " + frames + "fps");
		frame.setTitle( title + " | " + updates+ "ups, " + frames + "fps");
		updates = 0;
		frames = 0;
	}
	stop();
}
int x = 0; int y = 0;

public void update() {
	x++;
	y++;
}

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(); }

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.start();

}

}
package com.jr.tm.graphics;

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 com.jr.tm.Input.Keyboard;

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; 
public static String title = "The Struggle";

private Thread thread;
private JFrame frame;
private Keyboard key;
private boolean running = false;

private Screen screen; 

private BufferedImage image= new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData(); 

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 = true) {
		long now = System.nanoTime();
		delta += (now - lastTime) / ns;
		lastTime = now;
		while (delta >= 1) {
			update();
			updates++;
			delta--;
		}
		render();
		frames++;
		
		if (System.currentTimeMillis() - timer > 10000)
			timer += 1000;
		System.out.println(updates+ "ups, " + frames + "fps");
		frame.setTitle( title + " | " + updates+ "ups, " + frames + "fps");
		updates = 0;
		frames = 0;
	}
	stop();
}
int x = 0; int y = 0;

public void update() {
	x++;
	y++;
}

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(); }

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.start();

}

}
package com.jr.tm.graphics;

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 com.jr.tm.Input.Keyboard;

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; 
public static String title = "The Struggle";

private Thread thread;
private JFrame frame;
private Keyboard key;
private boolean running = false;

private Screen screen; 

private BufferedImage image= new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData(); 

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 = true) {
		long now = System.nanoTime();
		delta += (now - lastTime) / ns;
		lastTime = now;
		while (delta >= 1) {
			update();
			updates++;
			delta--;
		}
		render();
		frames++;
		
		if (System.currentTimeMillis() - timer > 10000)
			timer += 1000;
		System.out.println(updates+ "ups, " + frames + "fps");
		frame.setTitle( title + " | " + updates+ "ups, " + frames + "fps");
		updates = 0;
		frames = 0;
	}
	stop();
}
int x = 0; int y = 0;

public void update() {
	x++;
	y++;
}

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(); }

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.start();

}

}
Please, help!

If you would like to see my other classes as well, just let me know.

Welcome to the JGO. :slight_smile:
We generally like to help, but please don’t expect us to read through something like 500+ lines of code.
Use the [ code ] and [ /code ] tags (without spaces) and only share the part of your code that is relevant to your issue.
If you have to share really long code use JGO’s Pastebin feature.

After a quick look at your code I would say that your issue should be inside your Screen class but I can’t help any more then this because you haven’t shared that class with us.

Sorry man, I wasn’t sure where the error was and didn’t want to take any chances.
Here is my Screen class:

 package com.jr.tm.graphics;

import java.util.Random;

public class Screen {
	
	private int width , height;
	public int[] pixels; 
	public final int MAP_SIZE = 8;
	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);
			tiles[0] = 0;
	}
	
	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;
			for (int x= 0; x < width; x++) {
				int xx = x + xOffset;
				//if (xx < 0 || xx  >= width) break;
				int tileIndex = ((xx >> 4) & MAP_SIZE_MASK) + ((yy >> 4) & MAP_SIZE_MASK) * MAP_SIZE;//// x/y >> 4 = x/y / 16
				pixels[xx + yy * width] = tiles[tileIndex];
			} 
				
		}
	}

} 

See anything wrong here???

Maybe it’s moving to fast and you don’t see it, try moving it just 10 pixels and stop!

Honestly just keep watching the tutorial (theCherno) and rewrite the code. I’ve watched his tutorials too, they work fine. You made a mistake copying his code somewhere.

By the way, the game is only running at 0 ups and 1 fps :-\ … Would this have anything to do with it? If so, how can I fix this?

How would you recommend that I do this?