用java写一个在vivo y31s上运行的贪吃蛇程序
时间: 2023-10-01 17:03:20 浏览: 93
好的,这是一个比较简单的Java项目,你可以按照以下步骤来实现:
1. 创建一个新的Java项目,在Eclipse或者其他IDE中打开。
2. 创建一个Snake类,包含以下成员变量和方法:
成员变量:
- int x,y: 表示蛇头的横纵坐标
- int[] bodyX, bodyY: 分别表示蛇身体各个点的横纵坐标
- int length: 表示蛇身体的长度
- int direction: 表示蛇头的运动方向
方法:
- void move(): 移动蛇头和蛇身体的位置
- void draw(Graphics g): 在屏幕上绘制蛇的形状
- boolean isGameOver(): 判断游戏是否结束
3. 创建一个GamePanel类,继承JPanel类,实现以下方法:
- void paint(Graphics g): 绘制游戏面板
- void keyPressed(KeyEvent e): 处理用户按键事件
4. 在GamePanel类中创建一个Snake对象和一个Timer对象,用于控制游戏的运行和刷新。
5. 编写游戏的主函数,创建一个JFrame窗口,将GamePanel添加到窗口中并显示。
注意:在编写程序的过程中,需要注意游戏的逻辑和细节处理,例如:蛇碰到边界或者自身时游戏结束,蛇吃到食物长度增加等等。
以下是示例代码:
Snake类:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class Snake {
private int x, y;
private int[] bodyX, bodyY;
private int length;
private int direction;
private Random rand = new Random();
public Snake() {
bodyX = new int[100];
bodyY = new int[100];
x = rand.nextInt(20) * 20;
y = rand.nextInt(20) * 20;
length = 3;
direction = rand.nextInt(4);
for (int i = 0; i < length; i++) {
bodyX[i] = x - i * 20;
bodyY[i] = y;
}
}
public void move() {
for (int i = length - 1; i > 0; i--) {
bodyX[i] = bodyX[i - 1];
bodyY[i] = bodyY[i - 1];
}
switch (direction) {
case 0:
x += 20;
break;
case 1:
y += 20;
break;
case 2:
x -= 20;
break;
case 3:
y -= 20;
break;
}
bodyX[0] = x;
bodyY[0] = y;
}
public void draw(Graphics g) {
g.setColor(Color.green);
for (int i = 0; i < length; i++) {
g.fillRect(bodyX[i], bodyY[i], 20, 20);
}
}
public boolean isGameOver() {
if (x < 0 || x >= 400 || y < 0 || y >= 400) {
return true;
}
for (int i = 1; i < length; i++) {
if (x == bodyX[i] && y == bodyY[i]) {
return true;
}
}
return false;
}
public void setDirection(int direction) {
this.direction = direction;
}
public void grow() {
bodyX[length] = bodyX[length - 1];
bodyY[length] = bodyY[length - 1];
length++;
}
}
```
GamePanel类:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.Timer;
public class GamePanel extends JPanel implements ActionListener, KeyListener {
private Snake snake;
private Timer timer;
public GamePanel() {
snake = new Snake();
timer = new Timer(100, this);
timer.start();
addKeyListener(this);
setFocusable(true);
}
public void paint(Graphics g) {
super.paint(g);
setBackground(Color.black);
snake.draw(g);
}
public void actionPerformed(ActionEvent e) {
snake.move();
if (snake.isGameOver()) {
timer.stop();
System.out.println("Game Over!");
}
repaint();
}
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
switch (code) {
case KeyEvent.VK_UP:
if (snake.getDirection() != 1) {
snake.setDirection(3);
}
break;
case KeyEvent.VK_DOWN:
if (snake.getDirection() != 3) {
snake.setDirection(1);
}
break;
case KeyEvent.VK_LEFT:
if (snake.getDirection() != 0) {
snake.setDirection(2);
}
break;
case KeyEvent.VK_RIGHT:
if (snake.getDirection() != 2) {
snake.setDirection(0);
}
break;
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
```
主函数:
```java
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Snake");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GamePanel panel = new GamePanel();
frame.add(panel);
frame.setVisible(true);
}
}
```
这样就可以在vivo y31s上运行贪吃蛇游戏了。