Я пытаюсь создать игру на Java, но по какой-то причине она работает очень медленно (по ощущениям, около 1-2 кадров в секунду), когда я не ввожу клавиатуру и не перемещаю курсор.
Я пытался найти ошибки, приводящие к проблемам с производительностью, но не смог их найти, как и не могу найти в Интернете людей с такой же проблемой. это код «основного» класса, где создается поток и рисует на экране:
package main;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import javax.swing.JPanel;
public class Main extends JPanel implements Runnable{
public keyHandler keyH = new keyHandler(this);
public Summoner summon = new Summoner(this);
Thread thread1;
Graphics2D g2;
int FPS=60;`your text`
public int screenWidth;
public int screenHeigth;
//standerth widht unit (1/10 of the screens width)
public int width;
//standerth height unit (1/10 of the screens height)
public int height;
public int pos;
private Color col = new Color(0,0,0);
public int jumpHeight;
public Main() {
this.setBackground(Color.white);
this.setDoubleBuffered(true);
this.setFocusable(true);
this.addKeyListener(keyH);
}
public void startGameThread() {
thread1 = new Thread(this);
thread1.start();
}
public void setup() {
setFullScreen();
}
public void setFullScreen() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
gd.setFullScreenWindow(Flappy.frame);
screenWidth = Flappy.frame.getWidth();
screenHeigth = Flappy.frame.getHeight();
height = screenHeigth/10;
width = (Flappy.frame.getWidth()/10);
pos = (screenHeigth/10)*5;
jumpHeight = screenHeigth/15;
summon.blockPos = screenWidth;
}
@Override
public void run() {
double drawInterval = 1000000000/FPS;
double nextDrawTime = System.nanoTime() + drawInterval;
while(thread1 != null) {
update();
repaint();
try {
double remainingTime = nextDrawTime - System.nanoTime();
remainingTime = remainingTime/1000000;
if(remainingTime < 0) {
remainingTime = 0;
}
Thread.sleep((long) remainingTime);
nextDrawTime += drawInterval;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
void update() {
pos += 1;
if (pos > screenHeigth-height) {
//bottom
pos = screenHeigth-height;
}
if (pos
Подробнее здесь: https://stackoverflow.com/questions/790 ... -mouse-inp
Почему мой Java-код для моей игры Flappy Bird тормозит, когда я не нажимаю клавиши или мышь ⇐ JAVA
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение