我我想用Java写贪吃蛇
时间: 2023-03-21 14:02:17 浏览: 55
好的,我可以回答你的问题。要用Java编写贪吃蛇游戏,你可以先了解Java的图形用户界面(GUI)编程。Java提供了许多GUI库,例如Swing、JavaFX等。你可以选择其中一个库来编写游戏。在编写过程中,你需要使用Java中的面向对象编程思想,例如类、对象、继承、封装等概念。此外,你还需要掌握Java的事件处理机制,用来处理用户的操作事件。最后,你需要编写游戏逻辑,包括贪吃蛇的移动、食物的生成、碰撞检测等。希望这些提示可以帮助你完成贪吃蛇游戏的编写。
相关问题
用JAVA帮我写个贪吃蛇
好的,以下是一个简单的Java贪吃蛇实现,代码中有注释说明每一步的作用。
```java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SnakeGame extends JPanel implements Runnable, KeyListener {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 500;
public static final int HEIGHT = 500;
private Thread thread;
private boolean running;
private Node head;
private List<Node> snake;
private Node food;
private Random r;
private int xDir, yDir;
private int size;
private int ticks;
public SnakeGame() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setFocusable(true);
addKeyListener(this);
snake = new ArrayList<Node>();
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) {
e.printStackTrace();
}
}
public void tick() {
if (snake.size() == 0) {
head = new Node(0, 0);
snake.add(head);
}
if (food == null) {
int x = r.nextInt(WIDTH / Node.SIZE) * Node.SIZE;
int y = r.nextInt(HEIGHT / Node.SIZE) * Node.SIZE;
food = new Node(x, y);
}
if (ticks % 2 == 0) {
Node tail = null;
tail = snake.remove(snake.size() - 1);
tail.setX(head.getX() + xDir * Node.SIZE);
tail.setY(head.getY() + yDir * Node.SIZE);
head = tail;
snake.add(0, head);
for (int i = 1; i < snake.size(); i++) {
if (head.getX() == snake.get(i).getX() && head.getY() == snake.get(i).getY()) {
stop();
}
}
if (head.getX() < 0) {
head.setX(WIDTH);
}
if (head.getX() > WIDTH) {
head.setX(0);
}
if (head.getY() < 0) {
head.setY(HEIGHT);
}
if (head.getY() > HEIGHT) {
head.setY(0);
}
if (head.getX() == food.getX() && head.getY() == food.getY()) {
size++;
food = null;
}
}
ticks++;
}
public void paint(Graphics g) {
g.clearRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);
if (food != null) {
food.draw(g);
}
for (Node n : snake) {
n.draw(g);
}
g.setColor(Color.WHITE);
g.drawString("Score: " + size, 10, 20);
}
public void run() {
while (running) {
tick();
repaint();
}
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_UP && yDir != 1) {
xDir = 0;
yDir = -1;
}
if (key == KeyEvent.VK_DOWN && yDir != -1) {
xDir = 0;
yDir = 1;
}
if (key == KeyEvent.VK_LEFT && xDir != 1) {
xDir = -1;
yDir = 0;
}
if (key == KeyEvent.VK_RIGHT && xDir != -1) {
xDir = 1;
yDir = 0;
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake");
SnakeGame game = new SnakeGame();
frame.add(game);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
game.start();
}
}
class Node {
public static final int SIZE = 10;
private int x, y;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void draw(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(x, y, SIZE, SIZE);
}
}
```
运行该程序,你就可以玩一局贪吃蛇了。
帮我用Java写一套贪吃蛇代码
好的,以下是一份简单的Java贪吃蛇代码:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SnakeGame extends JFrame implements ActionListener {
private final int WIDTH = 300;
private final int HEIGHT = 300;
private final int DOT_SIZE = 10;
private final int ALL_DOTS = 900;
private final int RAND_POS = 29;
private final int DELAY = 140;
private int x[] = new int[ALL_DOTS];
private int y[] = new int[ALL_DOTS];
private int dots;
private int apple_x;
private int apple_y;
private boolean leftDirection = false;
private boolean rightDirection = true;
private boolean upDirection = false;
private boolean downDirection = false;
private boolean inGame = true;
private Timer timer;
private Image ball;
private Image apple;
private Image head;
public SnakeGame() {
setTitle("Snake");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(WIDTH, HEIGHT);
setResizable(false);
setLocationRelativeTo(null);
ImageIcon iid = new ImageIcon(getClass().getResource("dot.png"));
ball = iid.getImage();
ImageIcon iia = new ImageIcon(getClass().getResource("apple.png"));
apple = iia.getImage();
ImageIcon iih = new ImageIcon(getClass().getResource("head.png"));
head = iih.getImage();
addKeyListener(new TAdapter());
initGame();
}
private void initGame() {
dots = 3;
for (int z = 0; z < dots; z++) {
x[z] = 50 - z * 10;
y[z] = 50;
}
locateApple();
timer = new Timer(DELAY, this);
timer.start();
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (inGame) {
g.drawImage(apple, apple_x, apple_y, this);
for (int z = 0; z < dots; z++) {
if (z == 0) {
g.drawImage(head, x[z], y[z], this);
} else {
g.drawImage(ball, x[z], y[z], this);
}
}
Toolkit.getDefaultToolkit().sync();
} else {
gameOver(g);
}
}
private void gameOver(Graphics g) {
String msg = "Game Over";
Font small = new Font("Helvetica", Font.BOLD, 14);
FontMetrics metr = getFontMetrics(small);
g.setColor(Color.white);
g.setFont(small);
g.drawString(msg, (WIDTH - metr.stringWidth(msg)) / 2, HEIGHT / 2);
}
private void checkApple() {
if ((x[0] == apple_x) && (y[0] == apple_y)) {
dots++;
locateApple();
}
}
private void move() {
for (int z = dots; z > 0; z--) {
x[z] = x[(z - 1)];
y[z] = y[(z - 1)];
}
if (leftDirection) {
x[0] -= DOT_SIZE;
}
if (rightDirection) {
x[0] += DOT_SIZE;
}
if (upDirection) {
y[0] -= DOT_SIZE;
}
if (downDirection) {
y[0] += DOT_SIZE;
}
}
private void checkCollision() {
for (int z = dots; z > 0; z--) {
if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
inGame = false;
}
}
if (y[0] >= HEIGHT) {
inGame = false;
}
if (y[0] < 0) {
inGame = false;
}
if (x[0] >= WIDTH) {
inGame = false;
}
if (x[0] < 0) {
inGame = false;
}
if (!inGame) {
timer.stop();
}
}
private void locateApple() {
int r = (int) (Math.random() * RAND_POS);
apple_x = ((r * DOT_SIZE));
r = (int) (Math.random() * RAND_POS);
apple_y = ((r * DOT_SIZE));
}
@Override
public void actionPerformed(ActionEvent e) {
if (inGame) {
checkApple();
checkCollision();
move();
}
repaint();
}
private class TAdapter extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if ((key == KeyEvent.VK_LEFT) && (!rightDirection)) {
leftDirection = true;
upDirection = false;
downDirection = false;
}
if ((key == KeyEvent.VK_RIGHT) && (!leftDirection)) {
rightDirection = true;
upDirection = false;
downDirection = false;
}
if ((key == KeyEvent.VK_UP) && (!downDirection)) {
upDirection = true;
rightDirection = false;
leftDirection = false;
}
if ((key == KeyEvent.VK_DOWN) && (!upDirection)) {
downDirection = true;
rightDirection = false;
leftDirection = false;
}
}
}
public static void main(String[] args) {
new SnakeGame().setVisible(true);
}
}
```
希望这份代码能够帮到你!
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)