用Java写一个贪吃蛇游戏代码
时间: 2024-06-04 12:12:33 浏览: 18
由于贪吃蛇游戏比较复杂,代码较长,因此无法在此处完整呈现。以下是一个基本的示例,只包含游戏逻辑的基本框架:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SnakeGame extends JFrame implements KeyListener {
private static final long serialVersionUID = 1L;
private Snake snake;
private Food food;
private boolean gameOver;
private int score;
public SnakeGame() {
setTitle("贪吃蛇");
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
snake = new Snake();
food = new Food();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
gameOver = false;
score = 0;
}
public void paint(Graphics g) {
super.paint(g);
if (!gameOver) {
snake.move();
if(snake.collidesWith(food)) {
food.generate();
score++;
snake.grow();
}
if(snake.collidesWithItself()) {
gameOver = true;
}
snake.draw(g);
food.draw(g);
g.drawString("得分: " + score, 10, 20);
} else {
g.drawString("游戏结束!", 200, 200);
g.drawString("得分: " + score, 200, 220);
}
}
public void keyPressed(KeyEvent e) {
if(!gameOver) {
if(e.getKeyCode() == KeyEvent.VK_LEFT) {
snake.setDirection(Snake.Direction.LEFT);
} else if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
snake.setDirection(Snake.Direction.RIGHT);
} else if(e.getKeyCode() == KeyEvent.VK_UP) {
snake.setDirection(Snake.Direction.UP);
} else if(e.getKeyCode() == KeyEvent.VK_DOWN) {
snake.setDirection(Snake.Direction.DOWN);
}
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public static void main(String[] args) {
SnakeGame game = new SnakeGame();
game.setVisible(true);
while(!game.gameOver) {
game.repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Snake {
enum Direction {
LEFT, RIGHT, UP, DOWN;
}
private Node head;
private Node tail;
private int size;
private Direction direction;
public Snake() {
head = new Node(250, 250);
tail = head;
size = 1;
direction = Direction.LEFT;
}
public void move() {
switch(direction) {
case LEFT:
head = new Node(head.getX() - Node.WIDTH, head.getY());
break;
case RIGHT:
head = new Node(head.getX() + Node.WIDTH, head.getY());
break;
case UP:
head = new Node(head.getX(), head.getY() - Node.HEIGHT);
break;
case DOWN:
head = new Node(head.getX(), head.getY() + Node.HEIGHT);
break;
}
tail = tail.getNext();
tail.setPrev(null);
head.setNext(tail);
tail.setPrev(head);
}
public boolean collidesWith(Food food) {
return head.getX() == food.getX() && head.getY() == food.getY();
}
public boolean collidesWithItself() {
Node node = tail;
while(node != null) {
if(node.getX() == head.getX() && node.getY() == head.getY()) {
return true;
} else {
node = node.getNext();
}
}
return false;
}
public void grow() {
Node newTail = new Node(tail.getX(), tail.getY());
tail.setNext(newTail);
newTail.setPrev(tail);
tail = newTail;
size++;
}
public void setDirection(Direction direction) {
if(Math.abs(this.direction.ordinal() - direction.ordinal()) != 2) {
this.direction = direction;
}
}
public void draw(Graphics g) {
Node node = head;
while(node != null) {
node.draw(g);
node = node.getNext();
}
}
private class Node {
public static final int WIDTH = 10;
public static final int HEIGHT = 10;
private int x;
private int y;
private Node prev;
private Node next;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setPrev(Node prev) {
this.prev = prev;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public void draw(Graphics g) {
g.drawRect(x, y, WIDTH, HEIGHT);
g.fillRect(x, y, WIDTH, HEIGHT);
}
}
}
class Food {
private int x;
private int y;
public Food() {
generate();
}
public void generate() {
x = (int) (Math.random() * 49) * 10;
y = (int) (Math.random() * 49) * 10;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void draw(Graphics g) {
g.setColor(Color.RED);
g.drawRect(x, y, Node.WIDTH, Node.HEIGHT);
g.fillRect(x, y, Node.WIDTH, Node.HEIGHT);
}
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)