我正在制作一个吃豆人游戏,我在一个数组中有一个地图,我在地图中分配不同的值来绘制不同的图像,以创建一个吃豆人地图......但它一直在整个地方闪烁
识别问题的任何帮助都会很棒
我意识到这是很多阅读,但我已经看了几个小时并没有弄清楚......我是编程新手,所以我认为更有经验的眼睛真的可以让我受益
绘制地图
public void drawMap() {
BufferStrategy bs = this.getBufferStrategy();
do {
do {
Graphics g = null;
g = getGraphics();
try {
for (int x = 0; x < graphics.map.width; x++) {
for (int y = 0; y < graphics.map.height; y++) {
tile = graphics.map.level1[y][x];
//System.out.println(y + ", " + x);
int tileX = x * 21; //(int) (x * 21.42857142857143);
int tileY = y * 26; //(int) (y * 25.80645161290323);
if (tile == 0) {
g.drawImage(wall, tileX, tileY, 21, 26, null);
//System.out.println(x + ", " + y);
}
if (tile == 1) {
//g.drawImage(space,tileX, tileY, (int)21.42857142857143, (int) 25.80645161290323,null);
}
}
}
} finally {
g.dispose();
}
} while (bs.contentsRestored());
bs.show();
} while (bs.contentsLost());
}
使成为
public void render() {
BufferStrategy bs = this.getBufferStrategy();
do {
do {
drawn = false;
Graphics g = null;
try {
g = bs.getDrawGraphics();
if (MAIN_MENU == false && GAME == true) {
if (level1test) {
if (!drawn) {
drawMap();
drawn = true;
}
//g.drawImage(level1,0,0,getWidth(),getHeight(),null);
}
if (level2test == true) {
g.drawImage(level2, 0, 0, getWidth(), getHeight(), null);
}
g.drawImage(point, playerX, playerY, playerHeight, playerWidth, null);
} else if (MAIN_MENU == false && GAME == false) {
g.drawImage(settingsBackground, 0, 0, getWidth(), getHeight(), null);
} else {
g.drawImage(background, 0, 0, getWidth(), getHeight(), null);
}
} finally {
g.dispose();
}
} while (bs.contentsRestored());
bs.show();
} while (bs.contentsLost());
}
运行和定时器
public void run()
{
long lastTime = System.nanoTime();
double nsPerTick = 1000000000 / 60D;
long lastTimer = System.currentTimeMillis();
double delta = 0;
int frames = 0;
int ticks = 0;
while (running == true) {
long now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
boolean render = false;
while (delta >= 1) {
ticks++;
tick();
delta -= 1;
render = true;
}
try {
Thread.sleep(3); //keep the Frames from going to high
} catch (InterruptedException e) {
e.printStackTrace();
}
if(render == true){
frames++;
render();
}
if (System.currentTimeMillis() - lastTimer >= 1000) {
lastTimer +=1000;
//System.out.println("Frames: " + frames + " Ticks: " + ticks);
frames = 0;
ticks = 0;
}
}
}
地图数组
public static int width = 28;
public static int height = 31;
public static int[][] level1 = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
{0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,},
{0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,},
{0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,},
{0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,},
{0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0,},
{0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0,},
{0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,1,1,0,},
{0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,},
{1,1,1,1,1,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,1,1,1,1,1,},
{1,1,1,1,1,0,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,1,1,1,1,1,},
{1,1,1,1,1,0,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,1,1,1,1,1,},
{1,1,1,1,1,0,1,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