我正在努力制作游戏Snake,尽管我现在无法重新粉刷蛇。我正在使用计时器,当我将间隔设置为150毫秒时,它可以正常工作,但是当我将它设置为更快的速度(如30毫秒)时,屏幕上没有任何显示。带有repaint()调用的计时器在Game类中。谢谢!
游戏类
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
import java.util.*;
import java.util.Timer;
import java.util.TimerTask;
public class Game extends JFrame{
JPanel mainPanel = new JPanel();
BorderLayout layout = new BorderLayout();
//Game game;
Snake snake = new Snake(this);
public Game(){
super("Snake by Alex 2017");
setSize((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth()),(int)(Toolkit.getDefaultToolkit().getScreenSize().getHeight()));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
mainPanel.setLayout(null);
mainPanel.setSize((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth()),(int)(Toolkit.getDefaultToolkit().getScreenSize().getHeight()));
mainPanel.setBackground(Color.BLACK);
add(mainPanel);
mainPanel.setVisible(true);
start();
addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
snake.keyPressed(e);
}
});
setFocusable(true);
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
snake.paint(g2d);
//System.out.println("hello");
}
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
try {
move();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
TimerTask update = new TimerTask() {
public void run() {
repaint();
}
};
public void start(){
timer.scheduleAtFixedRate(task, 0, 250);
timer.schedule(update, 0, 30);
}
public void move() throws InterruptedException{
snake.move();
}
public static void main(String[] args) throws InterruptedException{
Game game = new Game();
}
}
Snake Class
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import javax.swing.RepaintManager;
public class Snake {
public int x = 60;
public int y = 80;
public int xa = 0;
public int ya = 0;
public int speed = 20;
private Game game;
public Snake(Game game){
this.game = game;
}
public void move(){
x += xa;
y += ya;
}
public void keyPressed(KeyEvent e){
//RIGHT
if(e.getKeyCode() == 39){
xa = speed;
ya = 0;
}
//LEFT
if(e.getKeyCode() == 37){
xa = -speed;
ya = 0;
}
//UP
if(e.getKeyCode() == 38){
ya = -speed;
xa = 0;
}
//DOWN
if(e.getKeyCode() == 40){
ya = speed;
xa = 0;
}
//move();
//game.repaint();
}
public Rectangle getBounds(){
return new Rectangle(x, y, 20, 20);
}
public void paint(Graphics2D g2d){
g2d.setColor(new Color(48, 255, 55));
g2d.fillRect(x, y, 20, 20);
}
}
答案 0 :(得分:0)
你要做的就是创建一个一直循环并呈现游戏的线程,如:
public void run() {
while (true) {
render();
}
}
你的run方法是正确的,但是你还需要一次又一次地在一个额外的线程中扩展Thread来重新绘制see this post for multithreading。
有关BufferStrategy的所有详细信息,请按照此Tutorial进行操作。 我认为它解释得非常好,我自己也用它。
答案 1 :(得分:0)
尝试按照https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html中的说明使用javax.swing.Timer;
实现ActionListener并放在repaint()
处,然后像这样创建Timer
Timer timer = new Timer(5, this);
timer.start();
这将在您有actionPerformed()
的地方调用repaint()