您当前的位置:首页 > 美文分享

java贪吃蛇(教你用java做贪吃蛇)

时间:2022-04-07 21:13:54

java贪吃蛇(教你用java做贪吃蛇)

要用java写贪吃蛇呢,你要掌握这几个知识点

1、集合的应用,Arraylist。

用来存储蛇块对象的。

2、swing编程

你需要绘制图形界面

3、事件监听

键盘监听和按钮点击事件监听

4、线程或定时器的应用(这里我们就用线程)

5、贪吃蛇的核心算法

(面向对象作为java的基本功在这里就不写出来了)

掌握了这些知识点之后,我们就可以来制作贪吃蛇了

1、创建蛇块对象

蛇块属性有位置位置用x,y表示,

package snakeeat;

public class Snakeact {

private int x;

private int y;

public int getX() {

return x;

}

public void setX(int x) {

this.x=x;

}

public int getY() {

return y;

}

public void setY(int y) {

this.y=y;

}

}

2、创建窗口

package snakeeat;

import javax.swing.*;

public class Snake extends JFrame{

public Snake (){

Snakewin win=new Snakewin();

add(win);

setTitle("贪吃蛇");

setSize (435,390);

setLocation(200,200);

setVisible(true);

}

public static void main(String[] args) {

new Snake();

}

}

3、绘制面板(很多逻辑方法都在这个类里)

package snakeeat;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.util.List;

public class Snakewin extends JPanel implements ActionListener, KeyListener, Runnable {

JButton newGame, stopGame;//开始和结束的按钮

int score = 0;//分数

int speed = 1;//蛇运动的速度

int rx = 0;//食物的X坐标

int ry = 0;

int num1 = 0, num2 = 0;

int temp = 0;//判断方向

boolean start = false;//判断游戏是否开始

JLabel lable;

List<Snakeact> list = new ArrayList<Snakeact>();

Random r = new Random();

JDialog die = new JDialog();

JButton ok;

//构造方法,初始化各种值

public Snakewin() {

newGame = new JButton("开始");

stopGame = new JButton("结束");

this.setLayout(new FlowLayout(FlowLayout.LEFT));

newGame.addActionListener(this);

stopGame.addActionListener(this);

this.addKeyListener(this);

this.add(newGame);

this.add(stopGame);

die.setLayout(new GridLayout(2, 1));

ok = new JButton("继续");

ok.addActionListener(this);

die.setSize(200, 200);

die.setLocation(400, 300);

die.add(ok);

die.setVisible(false);

if (start) {

lable = new JLabel("您的分数是:" + score);

die.add(lable);

}

}

//绘制图形界面和蛇

public void paintComponent(Graphics g) {

super.paintComponent(g);

g.drawRect(10, 40, 400, 300);

g.drawString("分数:" + score, 150, 20);

g.drawString("速度:" + speed, 150, 35);

g.setColor(new Color(0, 255, 0));

if (start) {

g.fillRect(10 + rx * 10, 40 + ry * 10, 10, 10);

g.setColor(new Color(255, 0, 0));

for (int i = 0; i < list.size(); i++) {

g.fillRect(10 + list.get(i).getX() * 10, 40 + list.get(i).getY() * 10, 10, 10);

}

eat();

}

}

//吃食物的方法

public void eat() {

if (list.size() != 0) {

if (list.get(0).getX() == rx && list.get(0).getY() == ry) {

score++;

if (score % 10 == 0) {

speed++;

}

rx = r.nextInt(40);

ry = r.nextInt(30);

Snakeact tempAct = new Snakeact();

tempAct.setX(list.get(list.size() - 1).getX());

tempAct.setY(list.get(list.size() - 1).getY());

list.add(tempAct);

}

}

}

//蛇身移动方法

public void sonMove() {

Snakeact tempAct = new Snakeact();

for (int i = 0; i < list.size(); i++) {

if (i == 1) {

list.get(i).setX(list.get(0).getX());

list.get(i).setY(list.get(0).getY());

} else if (i > 1) {

tempAct = list.get(i - 1);

list.set(i - 1, list.get(i));

list.set(i, tempAct);

}

}

}

//蛇头移动方法

public void move(int x, int y) {

if (list.size() != 0) {

if (minYes(x, y)) {

sonMove();

list.get(0).setX(list.get(0).getX() + x);

list.get(0).setY(list.get(0).getY() + y);

} else {

die.setVisible(true);

}

}

}

//按钮事件监听

public void actionPerformed(ActionEvent e) {

if (e.getSource() == newGame) {

newGame.setEnabled(false);

start = true;

rx = r.nextInt(40);

ry = r.nextInt(30);

Snakeact tempAct = new Snakeact();

tempAct.setX(20);

tempAct.setY(15);

list.add(tempAct);

requestFocus(true);

Thread mThread = new Thread(this);

mThread.start();

repaint();

}

if (e.getSource() == stopGame) {

System.exit(0);

}

if (e.getSource() == ok) {

list.clear();

newGame.setEnabled(true);

die.setVisible(false);

score = 0;

speed = 1;

}

}

public boolean minYes(int x, int y) {

if (list.size() != 0) {

if (!maxYes(list.get(0).getX() + x, list.get(0).getY() + y)) {

return false;

}

}

return true;

}

//判断是否出界

public boolean maxYes(int x, int y) {

if (x < 0 || x >= 40 || y < 0 || y >= 30) {

return false;

}

for (int i = 0; i < list.size(); i++) {

if (i > 1 && list.get(i).getX() == list.get(0).getX() && list.get(i).getY() == list.get(0).getY()) {

return false;

}

}

return true;

}

//键盘敲击事件监听

public void keyPressed(KeyEvent e) {

if (start) {

switch (e.getKeyCode()) {

case KeyEvent.VK_UP:

if (temp != 2) {

move(0, -1);

temp = 1;

}

repaint();

break;

case KeyEvent.VK_DOWN:

if (temp != 1) {

move(0, 1);

temp = 2;

}

repaint();

break;

case KeyEvent.VK_LEFT:

if (temp != 4) {

move(-1, 0);

temp = 3;

}

repaint();

break;

case KeyEvent.VK_RIGHT:

if (temp != 3) {

move(1, 0);

temp = 4;

}

repaint();

break;

default:

break;

}

}

}

@Override

public void keyTyped(KeyEvent e) {

// TODO Auto-generated method stub

}

@Override

public void keyReleased(KeyEvent e) {

}

//线程的run方法

public void run() {

while (start) {

switch (temp) {

case 1:

move(0, -1);

break;

case 2:

move(0, 1);

break;

case 3:

move(-1, 0);

break;

case 4:

move(1, 0);

break;

default:

move(1, 0);

break;

}

repaint();

try {

Thread.sleep(600 / speed);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

每一点都是一个类哦

效果图

有问题可以加qq群708653884交流哦

监听 贪吃蛇 方法 线程 绘制

最新文章