java编写一个贪吃蛇游戏,具有计分和登录功能
时间: 2024-01-23 08:04:16 浏览: 26
好的,下面是一个简单的 Java 贪吃蛇游戏,包括计分和登录功能。
首先,我们需要创建一个 Snake 类,包含蛇的身体、蛇的方向和蛇的移动方法:
```java
import java.util.LinkedList;
public class Snake {
private LinkedList<Node> body;
private int direction;
public Snake() {
body = new LinkedList<Node>();
body.add(new Node(10, 10));
body.add(new Node(10, 9));
body.add(new Node(10, 8));
direction = 1;
}
public LinkedList<Node> getBody() {
return body;
}
public void setDirection(int direction) {
this.direction = direction;
}
public void move() {
Node head = body.getFirst();
Node newHead = new Node(head.getRow(), head.getCol());
if (direction == 1) {
newHead.setRow(newHead.getRow() - 1);
} else if (direction == 2) {
newHead.setCol(newHead.getCol() + 1);
} else if (direction == 3) {
newHead.setRow(newHead.getRow() + 1);
} else if (direction == 4) {
newHead.setCol(newHead.getCol() - 1);
}
body.addFirst(newHead);
body.removeLast();
}
}
```
接下来,我们需要创建一个 Node 类,包含节点的行和列:
```java
public class Node {
private int row;
private int col;
public Node(int row, int col) {
this.row = row;
this.col = col;
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getCol() {
return col;
}
public void setCol(int col) {
this.col = col;
}
}
```
然后,我们需要创建一个 Food 类,包含食物的位置:
```java
public class Food {
private Node position;
public Food() {
position = new Node((int) (Math.random() * 20), (int) (Math.random() * 20));
}
public Node getPosition() {
return position;
}
public void setPosition(Node position) {
this.position = position;
}
}
```
接着,我们需要创建一个 Game 类,控制游戏的运行:
```java
import java.util.Scanner;
public class Game {
private Snake snake;
private Food food;
private int score;
private boolean gameOver;
private String username;
public Game(String username) {
snake = new Snake();
food = new Food();
score = 0;
gameOver = false;
this.username = username;
}
public void start() {
while (!gameOver) {
printBoard();
moveSnake();
checkCollision();
checkFood();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Game over! Your score is " + score);
saveScore();
}
private void printBoard() {
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
boolean isBody = false;
for (Node node : snake.getBody()) {
if (node.getRow() == i && node.getCol() == j) {
System.out.print("O");
isBody = true;
break;
}
}
if (!isBody && food.getPosition().getRow() == i && food.getPosition().getCol() == j) {
System.out.print("X");
} else if (!isBody) {
System.out.print(".");
}
}
System.out.println();
}
System.out.println("Score: " + score);
}
private void moveSnake() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter direction (w/a/s/d): ");
String input = scanner.nextLine();
if ("w".equals(input)) {
snake.setDirection(1);
} else if ("d".equals(input)) {
snake.setDirection(2);
} else if ("s".equals(input)) {
snake.setDirection(3);
} else if ("a".equals(input)) {
snake.setDirection(4);
}
snake.move();
}
private void checkCollision() {
Node head = snake.getBody().getFirst();
for (Node node : snake.getBody()) {
if (node != head && node.getRow() == head.getRow() && node.getCol() == head.getCol()) {
gameOver = true;
break;
}
}
if (head.getRow() < 0 || head.getCol() < 0 || head.getRow() >= 20 || head.getCol() >= 20) {
gameOver = true;
}
}
private void checkFood() {
Node head = snake.getBody().getFirst();
if (head.getRow() == food.getPosition().getRow() && head.getCol() == food.getPosition().getCol()) {
snake.getBody().add(food.getPosition());
food.setPosition(new Node((int) (Math.random() * 20), (int) (Math.random() * 20)));
score++;
}
}
private void saveScore() {
System.out.println("Saving score...");
// TODO: 将分数保存到文件或数据库中
}
}
```
最后,我们需要创建一个 Main 类,处理登录和游戏的开始:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter username: ");
String username = scanner.nextLine();
Game game = new Game(username);
game.start();
}
}
```
这样,我们就完成了一个简单的 Java 贪吃蛇游戏,具有计分和登录功能。