所以我是一个相对较新的编程,我正在尝试使用LWJGL制作2D游戏。我使用数组列表中的tile创建了一个背景图,然后我更新了必要的东西,比如播放器等,然后我将它们绘制到屏幕上,然后更新屏幕。我已经看到其他人这样做很好,但每当我这样做时,它每秒消耗几乎100 MB的RAM,导致我的计算机在游戏运行30秒或更短时间内冻结。我不确定是否是导致此内存使用的图形或其他内容,我已经检查过以确保在没有我知道的情况下没有运行代码,如果有人应该告诉我哪些错误会很好。我已经提供了我的游戏中使用的主要类,总共大约10个,但我只显示了3个,因为我认为它足以说明我的观点。我知道有些代码可能看起来好多了,但就像我说的那样,我对这一切都是新手。谢谢!
BOOT CLASS
public class Boot {
public Boot() {
beginSession();
tile menu = new tile(0, 0, 1024, 1024, TileType.Menu);
tile outline = new tile(0, 0, 395, 96, TileType.Outline);
Display.sync(60);
while (!Display.isCloseRequested()) {
System.out.println("MENU");
menu.Draw();
if (Display.isCloseRequested()) {
Display.destroy();
}
if (Mouse.getX() > 380 && Mouse.getX() < 650 && Mouse.getY() < 320 && Mouse.getY() > 250) {
outline.setX(322);
outline.setY(305);
outline.Draw();
}
if (Mouse.isButtonDown(0) && Mouse.getX() > 380 && Mouse.getX() < 650 && Mouse.getY() < 320 && Mouse.getY() > 250) {
new Play();
}
if (Mouse.getX() > 340 && Mouse.getX() < 700 && Mouse.getY() < 210 && Mouse.getY() > 130) {
outline.setX(322);
outline.setY(425);
outline.Draw();
}
if (Mouse.isButtonDown(0) && Mouse.getX() > 340 && Mouse.getX() < 700 && Mouse.getY() < 210 && Mouse.getY() > 130) {
new About();
}
Display.update();
}
}
public static void main(String[] args) {
new Boot();
}
}
播放课程
公共课Play {
public Play() {
//@formatter:off
//MAPS//
int[][] map0 = {
{ 6, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
{ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
{ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6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[][] map3left3 = {
{ 6, 6, 6, 6, 6, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6 },
{ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
{ 6, 6, 6, 6, 6, 0, 6, 7, 6, 6, 6, 6, 6, 6, 6, 6 },
{ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
{ 6, 6, 6, 6, 6, 6, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6 },
{ 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 },
{ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 },
{ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }, };
//@formatter:on
TileGrid grid0 = new TileGrid(map0);
TileGrid grid1 = new TileGrid(map1);
TileGrid grid2 = new TileGrid(map2);
TileGrid grid3 = new TileGrid(map3);
TileGrid grid3left1 = new TileGrid(map3left1);
TileGrid grid3left2 = new TileGrid(map3left2);
TileGrid grid3left3 = new TileGrid(map3left3);
// ALL TILES IN GAME THAT ARE ALWAYS NEEDED GO HERE//
Asteriod a = new Asteriod(quickLoad("asteriodfour"), -100, 0, grid0, 64, 64, 10, 100);
Lander l = new Lander(quickLoad("LunarLander"), grid0.getTile(7, 0), grid0, 240, 228, 0, 0);
Wave wave = new Wave(15, a);
Astronaut player = new Astronaut(quickLoad("astronaut"), l.getX(), 310, 256, 256);
// LADNING THE LANDER//
while (l.isLanded() == false) {
Display.sync(60);
gridType = 3;
if (Display.isCloseRequested()) {
Display.destroy();
}
switch (l.getMapType()) {
case 0:
grid0.Draw();
break;
case 1:
grid1.Draw();
break;
case 2:
grid2.Draw();
break;
case 3:
grid3.Draw();
break;
}
l.Draw();
l.UpdateLander();
if (l.isLanded() == false && l.isFirst()) {
wave.UpdateAsteroid();
} else {
wave.getAsteriodList().clear();
}
if (wave.getColY() > 390 && l.getMapType() == 3) {
wave.getAsteriodList().clear();
}
if (l.getX() - wave.getColX() < 35 && wave.getColX() - l.getX() < 95 && l.getY() - wave.getColY() < 35 && wave.getColY() - l.getY() < 200) {
wave.explodeAsteroid();
l.takeDamage();
}
Display.update();
}
onMoon = true;
// PLAYER EVERYTHING//
while (onMoon == true) {
Display.sync(60);
if (Display.isCloseRequested()) {
Display.destroy();
}
if (player.getX() < 5 && gridType == 3) {
gridType = 4;
player.setX(1000);
}
switch (gridType) {
case 3:
grid3.Draw();
l.Draw();
l.UpdateLander();
break;
case 4 :
grid3left1.Draw();
}
player.Draw();
player.UpdateAstronaut();
if (player.isLeft() == true) {
player.setTexture(quickLoad("astronaut"));
} else {
player.setTexture(quickLoad("astronautRight"));
}
Display.update();
}
}
}
LANDER CLASS
公共类Lander扩展了艺术家{
public Lander(Texture texture, tile startTile, TileGrid grid, int width, int height, float speed, int angle) {
this.texture = texture;
this.x = startTile.getX();
this.y = startTile.getY();
this.width = width;
this.height = height;
this.angle = angle;
}
public void Draw() {
drawQuadTexRotate(texture, x, y, width, height, angle);
}
//UPDATES LANDER//
public void UpdateLander() {
tile forceField = new tile(685, 510, 256, 32, TileType.ForceField);
tile forceFieldOff = new tile(745, offY, 64, 32, TileType.Off);
tile forceFieldOn = new tile(755, onY, 32, 32, TileType.On);
tile battery = new tile(13 * 64, 8 * 64, 96, 96, TileType.Battery8);
tile outOfFuel = new tile(820, 480, outOfFuelX, 32, TileType.OutOfFuel);
forceField.Draw();
forceFieldOn.Draw();
forceFieldOff.Draw();
battery.setTexture(quickLoad(percentage()));
outOfFuel.Draw();
battery.Draw();
screenMove();
landerHealth();
if (landed == false)
y = (float) (y + 1.4);
if (canMove == true) {
if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
x -= 1;
if (angle > -35) {
angle -= 0.2;
}
fuelUsed += 0.1;
if (fuelUsed > 70) {
fuelLeft -= 1;
fuelUsed = 0;
}
}
if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
y -= 0.6;
fuelUsed += 0.1;
if (fuelUsed > 70) {
fuelLeft -= 1;
fuelUsed = 0;
}
}
if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
if (angle < 25) {
angle += 0.2;
}
x += 1;
fuelUsed += 0.1;
if (fuelUsed > 70) {
fuelLeft -= 1;
fuelUsed = 0;
}
}
if (fuelLeft < 0.1) {
outOfFuelX = 170;
canMove = false;
}
}
}
//LANDER HEALTH ONLY RAN WHEN UPDATED//
public void landerHealth() {
tile h = new tile(680, 540, 180, 96, TileType.Health100);
if (health < 90 && health > 80) {
h.setTexture(quickLoad("health90"));
h.Draw();
}
if (health < 80 && health > 70) {
h.setTexture(quickLoad("health80"));
h.Draw();
}
if (health < 70 && health > 60) {
h.setTexture(quickLoad("health70"));
h.Draw();
}
if (health < 60 && health > 50) {
h.setTexture(quickLoad("health60"));
h.Draw();
}
if (health < 50 && health > 40) {
h.setTexture(quickLoad("health50"));
h.Draw();
}
if (health < 40 && health > 30) {
h.setTexture(quickLoad("health40"));
h.Draw();
}
if (health < 30 && health > 20) {
h.setTexture(quickLoad("health30"));
h.Draw();
}
if (health < 20 && health > 10) {
h.setTexture(quickLoad("health20"));
h.Draw();
}
if (health < 10 && health > 0) {
h.setTexture(quickLoad("health10"));
h.Draw();
}
if (health <= 0) {
h.setTexture(quickLoad("health0"));
h.Draw();
landerExplode();
canMove = false;
}
h.Draw();
}
//SCREEN MOVE ONLY RAN WHEN UPDATED//
public void screenMove() {
System.out.println("SCREEN MOVE");
if (landed == false) {
if (y > 500 && mapType < 3) {
mapType++;
y -= 650;
}
if (mapType == 3) {
if (y < 230 && y > 226 && angle < 4 && angle > -4) {
canMove = false;
landed = true;
eX2 = x - 15;
eY2 = y - 15;
offY = 2000;
onY = 540;
}
if (y > 234 && (angle > 4 || angle < -4)) {
landerExplode();
}
}
}
}
//ONLY RAN WHEN UPDATED, DEATH SCREEN NEEDED//
public void landerExplode() {
if (landerExplodeCounter < 850) {
if (first) {
eX1 = x;
eX2 = x - 15;
eX3 = x;
eY1 = y;
eY2 = y - 15;
eY3 = y;
x = 2000;
first = false;
}
System.out.println("EX");
landerExplodeCounter++;
tile landerp1 = new tile(x, y, 240, 228, TileType.LanderPart1);
tile landerp2 = new tile(x, y, 240, 228, TileType.LanderPart2);
tile landerp3 = new tile(x, y, 240, 228, TileType.LanderPart3);
landerp1.setX(eX1 -= 0.7);
landerp1.setY(eY1 -= 0.4);
landerp2.setX(eX2 += 0.5);
landerp2.setY(eY2 -= 0.2);
landerp3.setX(eX3 -= 0.1);
landerp3.setY(eY3 += 0.8);
landerp1.Draw();
landerp2.Draw();
landerp3.Draw();
}
}
//ONLY RAN WHEN UPDATED//
public void takeDamage() {
System.out.println("TAKE DAMAGE");
if (landed == false) {
health = (float) (health - 5.5);
System.out.println(health);
}
}
//ONLY RAN WHEN UPDATED//
private String percentage() {
System.out.println("PERCENTAGE");
String name = "Battery";
switch (fuelLeft) {
case 8:
name = name + "8";
break;
case 7:
name = name + "7";
break;
case 6:
name = name + "6";
break;
case 5:
name = name + "5";
break;
case 4:
name = name + "4";
break;
case 3:
name = name + "3";
break;
case 2:
name = name + "2";
break;
case 1:
name = name + "1";
break;
case 0:
name = name + "0";
break;
}
return name;
}
答案 0 :(得分:0)
您的代码是否暂停?如果它每次在没有停止的情况下通过while循环更新,它肯定会吃你的ram。