gpt4 book ai didi

java - Swing 2D 游戏性能低下

转载 作者:行者123 更新时间:2023-11-30 03:24:51 25 4
gpt4 key购买 nike

我正在克隆《Flappy Bird》。我的性能表现很好:60 fps。当时它只有 1 个柱子/障碍物。一旦我添加了 3 个,我的 fps 就下降到 30 及以下。然后现在游戏就玩不了了。我知道这与一直执行 repaint() 有关。

这是代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

/**
* Created by Lazar on 25/05/15.
*/
public class Environment extends JComponent implements ActionListener {

public static final Dimension dimension = new Dimension(800,600);
BufferedImage img;
BufferedImage ptica1;
BufferedImage ptica2;
double skokbrojac = 0;
int brzina = 4; // speed // MUST Background % brzina = 0
int dx;
int dx2;
int pad = 0; //drop
Timer timer;
boolean parno;
boolean skok = false;

//Stubovi // Pillars
Stub stub1 = new Stub();
Stub stub2 = new Stub();
Stub stub3 = new Stub();
ArrayList<Stub>stubovi = new ArrayList<Stub>();
int razmakStub; // Space between pillars

public Environment() {
setPreferredSize(dimension);
img = Util.openImage("pozadina.png");
ptica1 = Util.openImage("ptica1.png");
ptica2 = Util.openImage("ptica2.png");

stubovi.add(stub1);
stubovi.add(stub2);
stubovi.add(stub3);

dx = img.getWidth()/2;
timer = new Timer(1000/60,this);
timer.start();

addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
skok = true; // start jump
skokbrojac = 0; //jump frame counter
}
});

}

protected void paintComponent(Graphics g){
Graphics2D g2d = (Graphics2D)g;
//g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
if(dx == img.getWidth()){ //image horizontal scroll
dx2 = 0;
}
if(dx2 == img.getWidth()/2){ //image horizontal scroll
dx = dimension.width;
}
g2d.drawImage(img,getWidth() - dx, 0, null); //draw background
if(dx >= img.getWidth()){
g2d.drawImage(img,getWidth() - dx2, 0, null);
}
if(parno){
g2d.drawImage(ptica1,dimension.width/2, 290 + pad, null); //draw bird
}
else{
g2d.drawImage(ptica2,dimension.width/2, 290 + pad, null); //draw bird
}
stub1.postoji = true; //pillar1 exists?
if(razmakStub > 240){
stub2.postoji = true;
}
if(razmakStub > 480){ //pillar1 exists?
stub3.postoji = true;
}
for(Stub i : stubovi){ //draw pillars if they exist
if(i.postoji)
i.crtaj(g2d);
}
}

@Override
public void actionPerformed(ActionEvent e) {
dx = dx + brzina;
dx2 = dx2 + brzina;

if(skokbrojac > 5) // jump frame lenght
skok = false;
if(skok){
pad -= 15; // jump height
}
else{
pad += 8; //rate of the fall
}
skokbrojac++;
parno ^= true; // for different bird images
if(290 + pad >= 536 || 290 + pad<= 3) //border hit detect
timer.stop();
razmakStub += brzina;
for(Stub i : stubovi){ //reset pillars and make them move
if(i.postoji){
if(i.getDx() < -50){
i.setDx(800);
i.randomDy();
}
i.setDx(i.getDx() - brzina);
}
}
repaint();
}
}

Complete project source

还要记住,这确实是未经修饰的版本,因此代码很丑陋。我正在寻找一种提高性能的解决方案。

主类:

import javax.swing.*;

/**
* Created by Lazar on 25/05/15.
*/
public class Main {

public static void main(String[] args){

SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Frame(new Environment());
}
});
}
}

框架类:

import javax.swing.*;

/**
* Created by Lazar on 25/05/15.
*/
public class Frame extends JFrame{

public Frame(JComponent content){
setContentPane(content);
setTitle("Flappy");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(getPreferredSize());
setResizable(false);
setVisible(true);
setLocationRelativeTo(null);
}
}

stub /支柱类:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
* Created by Lazar on 26/05/15.
*/
public class Stub {

BufferedImage dole;
BufferedImage gore;
Random r = new Random();
int dx = 700;
int dy = r.nextInt(250) + 250;
boolean postoji = false;

public void crtaj(Graphics2D g2d){
dole = Util.openImage("stub_dole.png");
gore = Util.openImage("stub_gore.png");
g2d.drawImage(dole, dx, dy, null);
g2d.drawImage(gore, dx, -(560-dy), null);
}

public void setDx(int dx) {
this.dx = dx;
}

public void randomDy(){
this.dy = r.nextInt(250) + 250;
}

public int getDx() {
return dx;
}
}

Ptica/Brid 类:

import java.awt.Graphics;
import java.awt.image.BufferedImage;



/**
* Created by Lazar on 26/05/15.
*/

public class Ptica {

BufferedImage ptica1;
BufferedImage ptica2;
boolean ptica;
boolean skok = false;
int pad = 0;
double skokBrojac = 0;

public Ptica(){
ptica1 = Util.openImage("/slike/ptica1.png");
ptica2 = Util.openImage("/slike/ptica2.png");
}

public void crtajPticu(Graphics g2d){

ptica ^= true;

if(ptica){
g2d.drawImage(ptica1, Environment.dimension.width/2, Environment.dimension.height/2-110 + pad, null);
}
else{
g2d.drawImage(ptica2, Environment.dimension.width/2, Environment.dimension.height/2-110 + pad, null);
}

System.out.println(pad);
}


public void setSkok(boolean skok) {
this.skok = skok;
}

public void setSkokBrojac(double skokBrojac) {
this.skokBrojac = skokBrojac;
}

public double getSkokBrojac() {
return skokBrojac;
}

public boolean isSkok() {
return skok;
}

public void setPad(int pad) {
this.pad = pad;
}

public int getPad() {
return pad;
}


}

实用程序类:

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
* Created by Lazar on 25/05/15.
*/
public class Util {

public static BufferedImage openImage(String name){
try {
if(!name.startsWith("/slike/")){
name="/slike/"+name;
}
return ImageIO.read(Util.class.getResource(name));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

最佳答案

  • 避免将所有类添加到默认包中,这可能会导致某些 Java 版本上的类加载出现问题
  • 绘画应该描绘状态,而不是做出决定或改变状态
  • 不要重复加载资源

例如,从 Stub 类(EnvironmentpaintComponent 调用 crtaj)中,您可以执行以下操作:以下...

public void crtaj(Graphics2D g2d){
dole = Util.openImage("stub_dole.png");
gore = Util.openImage("stub_gore.png");
g2d.drawImage(dole, dx, dy, null);
g2d.drawImage(gore, dx, -(560-dy), null);
}

加载图像可能需要一些时间。您应该有一个“缓存”类来管理它们(加载它们一次),或者在创建 Stub 类时加载它们(我更喜欢缓存类,就像您创建和销毁许多Stub 中,加载 Stub 类(例如构造函数)中的资源可能会成为瓶颈

对于example ,通过使用可重用的对象缓存(而不是重新创建对象并重新加载其资源),它可以同时移动 200-300 个对象,增加到超过 4000 个对象

关于java - Swing 2D 游戏性能低下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30493158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com