我正在制作一个pacman游戏,我在一个数组中有一个MAP,我在地图中指定不同的值来绘制不同的图像,以便创建一个pacman地图......但它会在整个地方闪烁< / p>
任何帮助识别问题都会很精彩
我意识到这是很多阅读,但我已经看了好几个小时而没想出来......我是编程的新手,所以我认为更有经验的眼睛真的可以让我受益
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,0,0,1,1,1,1,1,1,1,1,1,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
};
答案 0 :(得分:2)
你需要双缓冲,问题是java用背景颜色绘制画布,然后放上grahpics指令,所以,如果你需要更改图像,你可以在“隐藏的图像”中做到这一点,当它准备就绪时将其打印在画布区域中,用新渲染的图像替换图像。 更多信息Double Buffer in java。 编辑: 几乎忘了,真的很容易做到,这里有一个例子double buffer example