我正在尝试使用键盘上的回车键为我的java平台游戏制作一个重置按钮。我的关卡加载的方式是BufferedImage
对象通过名为LoadImageLevel
的方法传递。我试图做的只是说
if(key == KeyEvent.VK_ENTER){ LoadImageLevel(level)}
其中level是存储关卡的BufferedImage
对象。这确实在按下键时重置了水平,但是一旦水平重置几次,游戏就会变得非常缓慢而且角色无法移动,因为它太慢了。
我认为这是因为每次重置关卡时,它都会将图像放在彼此之上,直到它一次做得太多,这样就会减慢游戏速度。
我一直在四处寻找是否有办法在每次重置关卡时清除之前的BufferedImages,这样它一次只能运行1张图像,从而保持游戏速度......我希望。很遗憾,clear()
课程中没有remove()
或BufferedImage
方法,但我无法制作一个。
任何帮助将不胜感激,谢谢!
package com.tim.neon.window;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
//import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.util.Random;
//import com.tim.neon.framework.GameObject;
import com.tim.neon.framework.KeyInput;
import com.tim.neon.framework.ObjectId;
import com.tim.neon.objects.Block;
import com.tim.neon.objects.Player;
//Developed by Timothy O'Leary with much help from RealTutsGML
public class Game extends Canvas implements Runnable {
/**
*
*/
private static final long serialVersionUID = 5023365827045558937L;
private boolean running = false;
private Thread thread;
public static int WIDTH, HEIGHT;
private BufferedImage level = null;
Handler handler;
Camera cam;
Random rand = new Random();
private void init(){
WIDTH = getWidth();
HEIGHT = getHeight();
BufferedImageLoader loader = new BufferedImageLoader();
level = loader.loadImage("/level2.png");//loading the level
handler = new Handler();
cam = new Camera(0, 0);
LoadImageLevel(level);
this.addKeyListener(new KeyInput(handler));
}
public synchronized void start(){
if(running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
public void run(){
init(); //
this.requestFocus(); //
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int updates = 0;
int frames = 0;
while(running){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >= 1){
tick();
updates++;
delta--;
}
render();
frames++;
if(System.currentTimeMillis() - timer > 1000){
timer += 1000;
System.out.println("FPS: " + frames + " Ticks: " + updates);
frames = 0;
updates = 0;
}
}
}
private void tick(){
handler.tick();
for(int i = 0; i < handler.object.size(); i++){
if(handler.object.get(i).getId() == ObjectId.Player){
cam.tick(handler.object.get(i));
}
}
}
private void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
Graphics2D g2d = (Graphics2D) g;
//////////////////////////
//draw here
g.setColor(Color.black);
g.fillRect(0, 0, getWidth(), getHeight());
g2d.translate(cam.getX(), cam.getY());//begin of cam
handler.render(g);
g2d.translate(-cam.getX(), -cam.getY());//end of cam
/////////////////////////
g.dispose();
bs.show();
}
private void LoadImageLevel(BufferedImage image){//function to load levels drawn pixel by pixel in paint
int w = image.getWidth();
int h = image.getHeight();
System.out.println("width, height:" + " " + w + " " + h);
for(int xx = 0; xx < h; xx++){
for(int yy = 0; yy < w; yy++){
int pixel = image.getRGB(xx, yy);
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
if(red == 255 && green == 255 && blue == 255) handler.addObject(new Block(xx*32, yy*32, ObjectId.Block));
if(red == 0 && green == 0 && blue == 255) handler.addObject(new Player(xx*32, yy*32,handler, ObjectId.Player));
}
}
}
public static void main(String args[]){
new Window(800, 600, "Neon Platform Game Prototype", new Game());
}
}
package com.tim.neon.framework;
import java.awt.Component;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import com.tim.neon.objects.Block;
import com.tim.neon.objects.Player;
import com.tim.neon.window.BufferedImageLoader;
import com.tim.neon.window.Game;
import com.tim.neon.window.BufferedImageLoader;
import com.tim.neon.window.Handler;
import com.tim.neon.window.Game;
import com.tim.neon.window.Window;
public class KeyInput extends KeyAdapter {
Handler handler;
//public BufferedImage level = null;//i added
public KeyInput(Handler handler){
this.handler = handler;
}
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
//BufferedImageLoader loader = new BufferedImageLoader();///
//level = loader.loadImage("/level2.png");//loading the level///
for(int i = 0; i < handler.object.size(); i++){
GameObject tempObject = handler.object.get(i);
if(tempObject.getId() == ObjectId.Player){
if(key == KeyEvent.VK_D) tempObject.setVelX(5);
if(key == KeyEvent.VK_A) tempObject.setVelX(-5);
if(key == KeyEvent.VK_SPACE && !tempObject.isJumping()){//for double jump do if(key == KeyEvent.VK.SPACE) tempObject.setVelY(-15)
tempObject.setJumping(true);
tempObject.setVelY(-15);//change number to increase or decrease jump height
}
}
}
if(key == KeyEvent.VK_ESCAPE){
System.exit(1);
}
//if(key == KeyEvent.VK_ENTER){/// trying to make reset button
// level.repaint();
//}///
}
public void keyReleased(KeyEvent e){
int key = e.getKeyCode();
for(int i = 0; i < handler.object.size(); i++){
GameObject tempObject = handler.object.get(i);
if(tempObject.getId() == ObjectId.Player){
if(key == KeyEvent.VK_D) tempObject.setVelX(0);
if(key == KeyEvent.VK_A) tempObject.setVelX(0);
}
}
}
/*private void LoadImageLevel(BufferedImage image){// this function is used in both this clas and the Game class
int w = image.getWidth();
int h = image.getHeight();
System.out.println("width, height:" + " " + w + " " + h);
for(int xx = 0; xx < h; xx++){
for(int yy = 0; yy < w; yy++){
int pixel = image.getRGB(xx, yy);
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
if(red == 255 && green == 255 && blue == 255) handler.addObject(new Block(xx*32, yy*32, ObjectId.Block));
if(red == 0 && green == 0 && blue == 255) handler.addObject(new Player(xx*32, yy*32,handler, ObjectId.Player));
}
}
}*/
}