Java贪吃蛇游戏:苹果显蛇不见
Java Snake Game: Apple showing while snake is invisible
我正在按照以下视频设计蛇游戏:
https://www.youtube.com/watch?v=91a7ceECNTc
我正在一步步跟着它,但是当我运行它时,蛇没有出现在我的屏幕上,只有苹果。我认为我在实施时有问题 public void paint(Graphics g);
有人可以帮助我吗?
这是我的主要代码class
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);
}
}
这是面板 class:
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
}
}
蛇的 body 部分 class:
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;
}
}
而苹果的Class:
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;
}
}
好的,所以问题归结为一些基础数学...
如果我们看一下 BodyPart
的 draw
方法,您会发现...
g.fillRect(xCoor * width, yCoor * height, width, height);
好的,很基本,但是所有这些值实际上也都设置了吗?
如果我们看一下 tick
方法(其中创建了 BodyPart
),我们可以发现...
b = new BodyPart(xCoor, yCoor, 10);
snake.add(b);
好的,所以 width
和 height
是 10
,但是 xCoor
和 yCoor
呢?
它们首先被初始化为实例字段以及 class...
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));
我们可以看到 BodyPart
实际上最初是在屏幕外设置的。
所以,如果我们将初始位置更改为更像...
private int xCoor = 10, yCoor = 10, size = 10;
你会找到你丢失的蛇。
一般建议...
您应该避免覆盖 paint
。它在油漆链中的位置太高,很容易搞砸。相反,更喜欢 paintComponent
(并确保你调用的是 super.paintComponent
)。 JPanel
然后将为您清除 Graphics
上下文(使用组件的背景颜色)。
Swing 不是线程安全的。您不应该修改 UI 或 UI 依赖于事件调度线程上下文之外的任何状态。
当前的 "main" 循环有引入脏更新的危险,这可能会在以后引起问题。请参阅 Concurrency in Swing. As a "general" preference, you should consider using a Swing Timer
。它不会阻止 EDT,但 "ticks" 是在 EDT 内部生成的,这使得从内部更新 UI and/or 其状态更加安全。
执行操作时应避免使用 "magic numbers"...
for (int i = 0; i < WIDTH/10; i++) {
g.drawLine(i*10, 0, i*10, HEIGHT);
}
这里,WIDTH
和HEIGHT
可能并不代表组件的实际大小。而是使用 JPanel#getWidth
和 JPanel#getHeight
。
作为一般建议,您应该避免使用 setPreferred/Minimum/MaximumSize
,其他人很容易将这些更改为您不想要的状态。相反,改写 getPreferred/Minimum/MaximumSize
,这样您就可以保持控制。
我正在按照以下视频设计蛇游戏: https://www.youtube.com/watch?v=91a7ceECNTc
我正在一步步跟着它,但是当我运行它时,蛇没有出现在我的屏幕上,只有苹果。我认为我在实施时有问题 public void paint(Graphics g);
有人可以帮助我吗?
这是我的主要代码class
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);
}
}
这是面板 class:
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
}
}
蛇的 body 部分 class:
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;
}
}
而苹果的Class:
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;
}
}
好的,所以问题归结为一些基础数学...
如果我们看一下 BodyPart
的 draw
方法,您会发现...
g.fillRect(xCoor * width, yCoor * height, width, height);
好的,很基本,但是所有这些值实际上也都设置了吗?
如果我们看一下 tick
方法(其中创建了 BodyPart
),我们可以发现...
b = new BodyPart(xCoor, yCoor, 10);
snake.add(b);
好的,所以 width
和 height
是 10
,但是 xCoor
和 yCoor
呢?
它们首先被初始化为实例字段以及 class...
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));
我们可以看到 BodyPart
实际上最初是在屏幕外设置的。
所以,如果我们将初始位置更改为更像...
private int xCoor = 10, yCoor = 10, size = 10;
你会找到你丢失的蛇。
一般建议...
您应该避免覆盖 paint
。它在油漆链中的位置太高,很容易搞砸。相反,更喜欢 paintComponent
(并确保你调用的是 super.paintComponent
)。 JPanel
然后将为您清除 Graphics
上下文(使用组件的背景颜色)。
Swing 不是线程安全的。您不应该修改 UI 或 UI 依赖于事件调度线程上下文之外的任何状态。
当前的 "main" 循环有引入脏更新的危险,这可能会在以后引起问题。请参阅 Concurrency in Swing. As a "general" preference, you should consider using a Swing Timer
。它不会阻止 EDT,但 "ticks" 是在 EDT 内部生成的,这使得从内部更新 UI and/or 其状态更加安全。
执行操作时应避免使用 "magic numbers"...
for (int i = 0; i < WIDTH/10; i++) {
g.drawLine(i*10, 0, i*10, HEIGHT);
}
这里,WIDTH
和HEIGHT
可能并不代表组件的实际大小。而是使用 JPanel#getWidth
和 JPanel#getHeight
。
作为一般建议,您应该避免使用 setPreferred/Minimum/MaximumSize
,其他人很容易将这些更改为您不想要的状态。相反,改写 getPreferred/Minimum/MaximumSize
,这样您就可以保持控制。