编写以下代码 用java swing编写一款贪吃蛇游戏,要求有关卡设计
时间: 2024-03-09 22:48:53 浏览: 65
基于JAVA swing设计的吃蛇小游戏软件源代码.zip
好的,我会给你提供一份简单的Java Swing贪吃蛇游戏代码,其中包含了关卡设计的实现。你可以参考这份代码进行学习和修改,实现更加丰富的功能。
```
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
public class SnakeGame extends JFrame implements ActionListener, KeyListener {
private static final long serialVersionUID = 1L;
private final int WIDTH = 600;
private final int HEIGHT = 600;
private final int DOT_SIZE = 10;
private final int ALL_DOTS = 900;
private final int RAND_POS = 29;
private final int DELAY = 150;
private int score = 0;
private int level = 1;
private int[][] levelMap;
private boolean inGame = true;
private Timer timer;
private Random rand = new Random();
private ArrayList<Point> snake;
private Point food;
private SnakeGame() {
setTitle("Snake Game");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
addKeyListener(this);
initGame();
setVisible(true);
}
private void initGame() {
levelMap = getLevelMap(level);
snake = new ArrayList<Point>();
snake.add(new Point(3, 3));
snake.add(new Point(3, 4));
snake.add(new Point(3, 5));
newFood();
timer = new Timer(DELAY, this);
timer.start();
}
private void newFood() {
int x = rand.nextInt(RAND_POS) * DOT_SIZE;
int y = rand.nextInt(RAND_POS) * DOT_SIZE;
food = new Point(x, y);
}
private void move() {
Point head = snake.get(0);
Point tail = snake.get(snake.size() - 1);
Point newHead = new Point(head.x, head.y);
if (head.x == food.x && head.y == food.y) {
score += 10;
snake.add(tail);
newFood();
} else {
snake.remove(snake.size() - 1);
}
switch (getDirection()) {
case KeyEvent.VK_LEFT:
newHead.x -= DOT_SIZE;
break;
case KeyEvent.VK_RIGHT:
newHead.x += DOT_SIZE;
break;
case KeyEvent.VK_UP:
newHead.y -= DOT_SIZE;
break;
case KeyEvent.VK_DOWN:
newHead.y += DOT_SIZE;
break;
}
if (newHead.x < 0 || newHead.x >= WIDTH || newHead.y < 0 || newHead.y >= HEIGHT) {
inGame = false;
}
for (Point p : snake) {
if (newHead.x == p.x && newHead.y == p.y) {
inGame = false;
break;
}
}
if (inGame) {
snake.add(0, newHead);
}
}
private int getDirection() {
Point head = snake.get(0);
Point neck = snake.get(1);
if (head.x - neck.x == DOT_SIZE) {
return KeyEvent.VK_LEFT;
} else if (neck.x - head.x == DOT_SIZE) {
return KeyEvent.VK_RIGHT;
} else if (head.y - neck.y == DOT_SIZE) {
return KeyEvent.VK_UP;
} else {
return KeyEvent.VK_DOWN;
}
}
private int[][] getLevelMap(int level) {
// 简单的关卡设计,可以根据需要进行修改
switch (level) {
case 1:
return new int[][] {
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
case 2:
return new int[][] {
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,2,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
default:
return new int[][] {
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
}
}
private void checkLevelCompleted() {
boolean completed = true;
for (int i = 0; i < levelMap.length; i++) {
for (int j = 0; j < levelMap[0].length; j++) {
if (levelMap[i][j] == 2) {
completed = false;
break;
}
}
}
if (completed) {
level++;
JOptionPane.showMessageDialog(null, "Congratulations! Level " + (level - 1) + " completed!");
levelMap = getLevelMap(level);
snake.clear();
initGame();
}
}
private void draw(Graphics g) {
// 绘制游戏地图
for (int i = 0; i < levelMap.length; i++) {
for (int j = 0; j < levelMap[0].length; j++) {
if (levelMap[i][j] == 1) {
g.setColor(Color.gray);
g.fillRect(j * DOT_SIZE, i * DOT_SIZE, DOT_SIZE, DOT_SIZE);
}
}
}
// 绘制蛇
for (Point p : snake) {
g.setColor(Color.green);
g.fillRect(p.x, p.y, DOT_SIZE, DOT_SIZE);
}
// 绘制食物
g.setColor(Color.red);
g.fillRect(food.x, food.y, DOT_SIZE, DOT_SIZE);
// 绘制得分
g.setColor(Color.black);
g.drawString("Score: " + score, 10, 20);
// 绘制关卡
g.drawString("Level: " + level, 10, 40);
}
@Override
public void actionPerformed(ActionEvent e) {
if (inGame) {
move();
checkLevelCompleted();
}
repaint();
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
case KeyEvent.VK_RIGHT:
case KeyEvent.VK_UP:
case KeyEvent.VK_DOWN:
if (getDirection() != e.getKeyCode()) {
move();
}
break;
}
}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void paint(Graphics g) {
super.paint(g);
draw(g);
}
public static void main(String[] args) {
new SnakeGame();
}
}
```
这份代码实现了基本的贪吃蛇游戏逻辑,包括蛇的移动、食物的生成、游戏结束的判断、得分的计算等等。另外,还添加了简单的关卡设计,可以在不同的关卡中挑战不同的地图布局。
希望这份代码可以帮助你更好地理解如何使用Java Swing来实现贪吃蛇游戏,并实现关卡设计。
阅读全文