Java Snake游戏:当蛇不可见时,苹果会显示



我正在关注以下视频来设计一个蛇游戏:https://www.youtube.com/watch?v=91a7ceECNTc

我一步一步地跟着它,但当我运行它时,蛇不会出现在我的屏幕上,只有苹果。我想我在实现public void paint(Graphics g);时有问题。有人能帮我吗?

这是我的主类代码

import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame ();
GamePanel panel = new GamePanel();
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Snake");
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
}

这是面板类:

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 = 1000, HEIGHT = 1000; //Dimensions of the panel (Will be set by user input later)
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 = 100, yCoor = 100, size = 10;
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);
}
ticks++;
if (ticks > 250000) {
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(99);
int yCoor = r.nextInt(99);
apple = new Apple(xCoor, yCoor, 10);
apples.add(apple);
}
}
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() {
while (running) {
tick();
repaint();
}
}
@Override
public void keyTyped(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;
right = false;
left = false;
}
if (key == KeyEvent.VK_DOWN && !up) {
down = true;
right = false;
left = false;
}
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}

蛇的身体部位分类:

import java.awt.Color;
import java.awt.Graphics;
public class BodyPart {
public 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 getCoorX() {
return xCoor;
}
public void setCoorX (int xCoor) {
this.xCoor = xCoor;
}
public int getCoorY() {
return yCoor;
}
public void setCoorY(int yCoor) {
this.yCoor = yCoor;
}
}

和苹果的类别:

import java.awt.Color;
import java.awt.Graphics;
public class Apple {
public int xCoor, yCoor, width, height;
public Apple(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.RED);
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;
}
}

好吧,问题归结为一些基本数学。。。

如果我们看看BodyPartdraw方法,你会发现。。。

g.fillRect(xCoor * width, yCoor * height, width, height);

好吧,很基本,但所有这些值真的都设置好了吗?

如果我们看看tick方法(其中创建了BodyParts(,我们可以发现。。。

b = new BodyPart(xCoor, yCoor, 10);
snake.add(b);

好的,那么widthheight就是10,但是xCooryCoor呢?

它们首先与类一起初始化为实例字段。。。

private int xCoor = 100, yCoor = 100, size = 10;

因此,简单的数学知识告诉我们BodyPart的初始位置是100 * 10,它等于1000x1000

如果我们也看看。。。

public static final int WIDTH = 1000, HEIGHT = 1000; //Dimensions of the panel (Will be set by user input later)

setPreferredSize(new Dimension(WIDTH, HEIGHT));

我们可以看到CCD_ 14最初实际上是在屏幕外设置的。

所以,如果我们把初始位置改成更像。。。

private int xCoor = 10, yCoor = 10, size = 10;

你会找到你失踪的蛇。

一般建议

您应该避免覆盖paint。它在油漆链的高处,很容易把它搞砸。相反,请选择paintComponent(并确保您正在呼叫super.paintComponent(。JPanel将为您清除Graphics上下文(使用组件的背景色(。

摆动不是线程安全的。您不应该在事件调度线程的上下文之外修改UI或UI所依赖的任何状态。

当前的"主"循环有引入脏更新的危险,这可能会在以后引发问题。请参阅Swing中的并发。作为"一般"偏好,您应该考虑使用SwingTimer。它不会阻止EDT,但它的"记号"是在EDT内部生成的,因此从内部更新UI和/或其状态更安全。

执行操作时应避免使用"幻数"。。。

for (int i = 0; i < WIDTH/10; i++) {
g.drawLine(i*10, 0, i*10, HEIGHT);
}

这里,WIDTHHEIGHT可以不表示组件的实际大小。而是使用JPanel#getWidthJPanel#getHeight

作为一般建议,您应该避免使用setPreferred/Minimum/MaximumSize,因为其他人很容易将其更改为您不想要的状态。相反,重写getPreferred/Minimum/MaximumSize,这样可以保持控制。

最新更新