我用Java编写了一个蛇游戏。问题在于the虫快了,因为我的蛇发疯了。
我试图通过降低时钟频率来降低游戏速度。现在,游戏感觉很迟钝,输入延迟很大。我该如何解决?
import javax.swing.*;
public class Main {
public Main() {
JFrame frame = new JFrame();
Gamepanel gamepanel = new Gamepanel();
frame.add(gamepanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Snake Game v1.0");
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
import java.awt.Color;
import java.awt.*;
public class Bodypart {
private int xCoor, yCoor, width, height;
public Bodypart(int xCoor, int yCoor, int tileSize) {
this.xCoor = xCoor;
this.yCoor = yCoor;
width = tileSize;
height = tileSize;
}
public void tick() {
}
public void draw(Graphics g) {
g.setColor(Color.YELLOW);
g.fillRect(xCoor * width, yCoor * height, width, height);
}
public int getxCoor() {
return xCoor;
}
public void setxCoor(int xCoor) {
this.xCoor = xCoor;
}
public int getyCoor() {
return yCoor;
}
public void setyCoor(int yCoor) {
this.yCoor = yCoor;
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JPanel;
public class Gamepanel extends JPanel implements Runnable, KeyListener{
private static final long serialVersionUID = 1L;
public static final int WIDTH = 500, HEIGHT = 500;
private Thread thread;
private boolean running;
private boolean right = true, left = false, up = false, down = false;
private Bodypart b;
private ArrayList<Bodypart> snake;
private Apple apple;
private ArrayList<Apple> apples;
private Random r;
private int xCoor = 10, yCoor = 10, size = 15;
private int ticks = 0;
public Gamepanel() {
setFocusable(true);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
addKeyListener(this);
snake = new ArrayList<Bodypart>();
apples = new ArrayList<Apple>();
r = new Random();
start();
}
public void start() {
running = true;
thread = new Thread(this);
thread.start();
}
public void stop() {
running = false;
try {
thread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void tick() {
if(snake.size() == 0) {
b = new Bodypart (xCoor, yCoor, 10);
snake.add(b);
}
if(right) xCoor++;
if(left) xCoor--;
if(up) yCoor--;
if(down) yCoor++;
ticks = 0;
b = new Bodypart(xCoor, yCoor, 10);
snake.add(b);
if(snake.size() > size) {
snake.remove(0);
}
if(apples.size() ==0) {
int xCoor = r.nextInt(49);
int yCoor = r.nextInt(49);
apple = new Apple(xCoor, yCoor, 10);
apples.add(apple);
}
for(int i = 0 ; i < apples.size() ; i++) {
if(xCoor == apples.get(i).getxCoor() && yCoor == apples.get(i).getyCoor()) {
size++;
apples.remove(i);
i++;
}
}
//collosion on snake border body part
for(int i = 0 ; i < snake.size(); i++) {
if(xCoor == snake.get(i).getxCoor() && yCoor == snake.get(i).getyCoor()) {
if(i !=snake.size()- 1) {
System.out.println();
stop();
}
}
}
//collision on border
if(xCoor < 0 || xCoor > 49 || yCoor < 0 || yCoor > 49) {
System.out.println("Game Over");
stop();
}
}
public void paint(Graphics g) {
g.clearRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);
for(int i = 0; i < WIDTH/10 ; i++) {
g.drawLine(i * 10, 0, i *10, HEIGHT);
}
for(int i = 0; i < HEIGHT/10 ; i++) {
g.drawLine(0, i * 10 , HEIGHT, i * 10);
}
for(int i = 0 ; i <snake.size(); i++) {
snake.get(i).draw(g);
}
for(int i = 0 ; i < apples.size(); i++) {
apples.get(i).draw(g);
}
}
@Override
public void run() {
long lastTime = System.nanoTime();
final double amountofTicks = 20D;
double ns = 1000000000 / amountofTicks;
double delta = 0;
while(running) {
long now = System.nanoTime();
delta += (now-lastTime) / ns;
lastTime = now;
if(delta >= 1) {
tick();
delta--;
}
repaint();
}
stop();
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_RIGHT && !left) {
right = true;
up = false;
down = false;
}
if(key == KeyEvent.VK_LEFT && !right) {
left = true;
up = false;
down = false;
}
if(key == KeyEvent.VK_UP && !down) {
up = true;
left = false;
right = false;
}
if(key == KeyEvent.VK_DOWN && !up) {
down = true;
left = false;
right = false;
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
我不知道如何改善输入延迟。 有人可以帮忙吗?