java贪吃蛇简单版

游戏思路

步骤1:实现静态的蛇

蛇的类(初始化部分):

image

地图类:

image

步骤2:通过键盘让蛇移动起来

蛇的移动方法:

image

键盘监听事件:

image

步骤3:通过定时器让蛇移动起来

创建并使用定时器:

image

[步骤4:添加生成点的方法]

节点的类

image

食物继承节点,并重写构造方法。

image

步骤5:吃东西的方法

在移动的基础上将删除的尾节点复原

image

步骤6:死亡

两种死亡条件

image

成果展示

image.png

完整代码

**package com.company;**
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
public class Main {
public static void main(String[] args) {
new Map().addGame(new Game());
}
}
class Node {
int x;
int y;
Node nextNode;
public Node(){}
public Node(int x,int y){
this.x = x;
this.y = y;
}
}
class Food extends Node {
public Food(){
x = new Random().nextInt(39);
y = new Random().nextInt(38);
}
}
class Snake {
Node head;
Node tail;
Node hid = null;
//当前方向
int dir=-1;
//下一次移动的方向
int nextDir=-1;
public Snake(){
//初始化蛇身
this.head = new Node(5,6);
this.head.nextNode = new Node(5,7);
this.head.nextNode.nextNode = new Node(5,8);
this.tail = this.head.nextNode.nextNode;
this.tail.nextNode = this.hid;
}
public void eatFood(){
tail = hid;
hid = null;
}
public void move() {
//1:左 -1:右 2:上 3:下
if (nextDir != -dir) {
dir = nextDir;
}
else if (nextDir==dir) {
return;
}
//增加头结点
Node node = null;
switch (dir) {
case 1: node = new Node(head.x-1, head.y);break;
case -1: node = new Node(head.x+1, head.y);break;
case 2: node = new Node(head.x, head.y-1);break;
case -2: node = new Node(head.x, head.y+1);break;
}
node.nextNode = head;
head = node;
//删除尾节点
node = head;
while(node!=null) {
if (node.nextNode == tail) {
tail = node;
hid = tail.nextNode;
break;
}
node = node.nextNode;
}
}
}
class Map extends JFrame {
JFrame jFrame;
public Map(){
jFrame = new JFrame("贪吃蛇");
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);
jFrame.setBounds(200,200,600,615);
jFrame.setResizable(false);
}
public void addGame(Game game) {
jFrame.add(game);
game.requestFocus();
}
}
class Game extends JPanel {
Snake snake = new Snake();
Food food = new Food();
int unit = 15;
Timer timer;
{
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
snake.move();
checkCollide();
repaint();
}
};
timer = new Timer(100,actionListener);
timer.start();
KeyListener keyListener = new KeyListener(){
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
snake.nextDir = 2;
break;
case KeyEvent.VK_DOWN:
snake.nextDir = -2;
break;
case KeyEvent.VK_LEFT:
snake.nextDir = 1;
break;
case KeyEvent.VK_RIGHT:
snake.nextDir = -1;
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
};
this.addKeyListener(keyListener);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Node node = snake.head;
while (node!=snake.tail.nextNode) {
g.setColor(Color.green);
g.fillRoundRect(food.x*unit,food.y*unit,unit,unit,unit,unit);
g.setColor(Color.red);
g.fillRect(node.x*unit,node.y*unit,unit,unit);
node = node.nextNode;
}
}
public void checkCollide(){
if (snake.head.x==food.x&&snake.head.y==food.y) {
snake.eatFood();
food = new Food();
}
Node node = snake.head.nextNode;
while(node!=snake.tail.nextNode) {
if (snake.head.x==node.x&&snake.head.y==node.y) {
JOptionPane.showMessageDialog(this,"你碰到了自己的身体","死亡",JOptionPane.WARNING_MESSAGE);
System.exit(0);
timer.stop();
}
else if (snake.head.x==-1||snake.head.x==39||snake.head.y==-1||snake.head.y==39) {
JOptionPane.showMessageDialog(this,"你碰到了墙","死亡",JOptionPane.WARNING_MESSAGE);
System.exit(0);
timer.stop();
}
node = node.nextNode;
}
}
}

本次收获和感受

果然学习还是要多敲代码,很多知识是你觉的你会了。但是电脑不要你觉的。还有就是编写代码的时候要细心,不然有时候de了一个小时的bug最后发现是一个单词拼错了会崩溃的哟