我正在制作一个蛇游戏,除了蛇的一部分吃掉之外,它的效果非常好。
到目前为止,这是我的代码:
障碍类
class Obstacle {
int x, y;
public Obstacle(int x, int y) {
this.x = x;
this.y = y;
}
public void draw(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(x, y, 10, 10);
}
}
食品类
class Food {
int x, y;
public Food(int x, int y) {
this.x = x;
this.y = y;
}
public void draw(Graphics g) {
g.setColor(Color.red);
g.fillRect(x, y, 10, 10);
}
}
身体等级
class Body {
int x, y;
public Body(int x, int y) {
this.x = x;
this.y = y;
}
public void draw(Graphics g) {
g.setColor(Color.green);
g.fillRect(x, y, 10, 10);
g.setColor(Color.black);
g.drawRect(x, y, 10, 10);
}
}
屏幕类
class Screen extends JPanel implements ActionListener, KeyListener {
int WIDTH = 800, HEIGHT = 800;
public static int time = 50;
Timer t;
int x = 400, y = 400;
boolean right = false, left = false, up = false, down = false;
public static int grade = 0;
LinkedList<Food> foods;
LinkedList<Body> snake;
LinkedList<Obstacle> block;
Body b;
Food f;
Obstacle k;
Random rand = new Random();
int size = 3;
public Screen() {
init();
}
public void init() {
block = new LinkedList<Obstacle>();
foods = new LinkedList<Food>();
snake = new LinkedList<Body>();
level();
t = new Timer(time, this);
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
public void tracking() {
if (x < 0 || x > 800 || y < 0 || y > 800) {
hit();
}
}
public void level() {
Object[] options = {"Level 1", "Level 2", "Level 3"};
int option = JOptionPane.showOptionDialog(null, "Please Select a Level", "Level Option",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[2]);
switch (option) {
case JOptionPane.YES_OPTION:
System.out.println("Before: " + time);
time = 50;
System.out.println("After: " + time);
break;
case JOptionPane.NO_OPTION:
System.out.println("Before: " + time);
time = 30;
System.out.println("After: " + time);
break;
case JOptionPane.CANCEL_OPTION:
System.out.println("Before: " + time);
time = 10;
System.out.println("After: " + time);
break;
}
}
public void hit() {
t.stop();
if (JOptionPane.showConfirmDialog(null, "Snake: You hit the wall! \nRestart the Game?", "OUCH!!!",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
x = 400;
y = 400;
while (snake.size() != 3) {
snake.remove();
size = 3;
}
init();
} else {
System.exit(0);
}
}
public void move() {
if (right) x += 10;
if (left) x -= 10;
if (up) y -= 10;
if (down) y += 10;
}
public void snake() {
if (snake.size() == 0) {
b = new Body(x, y);
snake.add(b);
}
move();
b = new Body(x, y);
snake.add(b);
if (snake.size() > size) {
snake.remove(0);
}
}
public void food() {
if (foods.size() == 0) {
int ok = 0;
int rx = 0;
int ry = 0;
while (ok != 1) {
rx = (int) (rand.nextInt(700) + 1);
ry = (int) (rand.nextInt(700) + 1);
if ((rx % 10) == 0 && (ry % 10) == 0) {
ok = 1;
}
}
f = new Food(rx, ry);
foods.add(f);
}
if (snake.get(snake.size() - 1).x == foods.get(0).x && snake.get(snake.size() - 1).y == foods.get(0).y) {
foods.remove();
size++;
grade += 100;
}
}
public void block() {
if (block.size() == 0) {
int ok = 0;
int rx = 0;
int ry = 0;
while (ok != 15) {
rx = (int) (rand.nextInt(750) + 1);
ry = (int) (rand.nextInt(750) + 1);
if ((rx % 10) == 0 && (ry % 10) == 0) {
k = new Obstacle(rx, ry);
block.add(k);
int temp = 10;
int r = (int) (rand.nextInt(2) + 1);
for (int i = 0; i < 5; i++) {
if (r == 1) {
k = new Obstacle(rx + temp , ry);
} else if (r == 2) {
k = new Obstacle(rx , ry + temp);
}
block.add(k);
if ((k.x == foods.get(0).x && k.y == foods.get(0).y) ||
(k.x == 400 && k.y == 400) ||
((k.x >= 350 && k.x <= 450) && (k.y >= 350 && k.y <= 450))) {
block.remove(k);
}
temp += 10;
}
ok++;
}
}
}
for (int i = 0; i < block.size(); i++) {
if (snake.get(snake.size() - 1).x == block.get(i).x && snake.get(snake.size() - 1).y == block.get(i).y) {
hit();
}
}
}
public void update() {
snake();
food();
block();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.DARK_GRAY);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.BLACK);
for (int i = 1; i < WIDTH / 10; i++) {
g.drawLine(i * 10, 0, i * 10, HEIGHT);
}
for (int i = 1; i < HEIGHT / 10; i++) {
g.drawLine(0, i * 10, WIDTH, i * 10);
}
for (int i = 0; i < snake.size(); i++) {
snake.get(i).draw(g);
}
for (int i = 0; i < foods.size(); i++) {
foods.get(i).draw(g);
}
for (int i = 0; i < block.size(); i++) {
block.get(i).draw(g);
}
}
@Override
public void actionPerformed(ActionEvent e) {
update();
tracking();
repaint();
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_RIGHT && left == false) {
up = false;
down = false;
right = true;
}
if (key == KeyEvent.VK_LEFT && right == false) {
up = false;
down = false;
left = true;
}
if (key == KeyEvent.VK_UP && down == false) {
left = false;
right = false;
up = true;
}
if (key == KeyEvent.VK_DOWN && up == false) {
left = false;
right = false;
down = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
}
和主要课程 SnakeGame
public class SnakeGame extends JFrame {
public static void main(String[] args) {
JFrame f = new JFrame();
Screen s = new Screen();
f.add(s);
f.setSize(810, 810);
f.setVisible(true);
f.setLocationRelativeTo(null);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
所以一开始,当蛇的头部吃掉尾巴时,我决定做一个比较,但是因为我的蛇在移动;尾巴&#39; x和y跟随头部的x和y,因此比较始终为真
我还想到蛇何时改变它的方向,然后做一些代码来跟踪x y坐标及其尾x y坐标,但我不知道如何步进
当蛇吃自己的时候还有其他方法让游戏停止吗?答案 0 :(得分:1)
当您致电move();
时,您会获得x
和y
的新值,即蛇的“头部”移动到的位置。我看到你的LinkedList<Body> snake;
具有蛇身体每个部分的位置。
如果每次打电话给move();
,你将你的下一个'头部'位置与蛇的所有其他部分进行比较,你会发现它是否会被击中。
这样的事情:
for (int i = 0; i < snake.size(); i++) {
if ((x == snake.get(i).x) && y == snake.get(i).y) {
hit();
}
}
游戏将以hit();
开头,因为'head x
和y
与您的全球x
和y
变量匹配,因此您必须修复(即等待move();
调用开始检查)。
修改强>
以下是一个示例修改:
将boolean started = false;
添加到Screen class
:
更改snake()
方法以检查冲突:
public void snake() {
if (snake.size() == 0) {
b = new Body(x, y);
snake.add(b);
}
move();
if (started) {
for (int i = 0; i < snake.size(); i++) {
if ((x == snake.get(i).x) && y == snake.get(i).y) {
hit();
}
}
}
b = new Body(x, y);
snake.add(b);
if (snake.size() > size) {
snake.remove(0);
}
}
将started = true;
添加到food()
方法的末尾(找不到更好的地方,但我认为你可以!):
public void food() {
if (foods.size() == 0) {
int ok = 0;
int rx = 0;
int ry = 0;
while (ok != 1) {
rx = (int) (rand.nextInt(700) + 1);
ry = (int) (rand.nextInt(700) + 1);
if ((rx % 10) == 0 && (ry % 10) == 0) {
ok = 1;
}
}
f = new Food(rx, ry);
foods.add(f);
}
if (snake.get(snake.size() - 1).x == foods.get(0).x
&& snake.get(snake.size() - 1).y == foods.get(0).y) {
foods.remove();
size++;
grade += 100;
started = true;
}
}
它应该有效!