import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import static javax.swing.JOptionPane.YES_OPTION;
import static javax.swing.JOptionPane.showMessageDialog;
class myBigFrame extends JFrame implements MouseListener, KeyListener {
private final int X = 0, Y = 1;
boolean canPlay = false;
private boolean ifAlive = true;
private ArrayList<int[]> mySnake = new ArrayList<>(4);
private ArrayList<int[]> myFood = new ArrayList<>(3);
private int score = 0;
//构造方法包含:对snakeArrayList初始化、对foodArrayList的初始化、对画面的不断刷新、对是否吃到food的判断和响应;
myBigFrame() {
myBigPanel myPanel = new myBigPanel();
Timer timerRepaint = new Timer(true);
Timer timerRun = new Timer(true);
Timer timerEat = new Timer(true);
this.add(myPanel);
//对数组连进行初始化;初始的四位数组链;
for (int i = 0; i < 4; i++) mySnake.add(new int[]{0, 0});
for (int i = 0; i < 3; i++) myFood.add(new int[]{0, 0});
//对列表初始化。
for (int i = 0; i < 4; i++) {
mySnake.set(i, new int[]{0, 3 - i});
}
for (int i = 0; i < 3; i++) {
myFood.set(i, new int[]{(int) (Math.random() * 27), (int) (Math.random() * 18)});
}
setSize(1130, 810);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setTitle("贪食蛇游戏!");
setVisible(true);
addMouseListener(this);
addKeyListener(this);
timerRun.schedule(
new TimerTask() {
public void run() {
//分别判断两个四个方向设置一个按照当前方向不断运动的机制;
if (mySnake.get(0)[X] - mySnake.get(1)[X] == 1 && canPlay && ifAlive) {
//向右走
for (int i = mySnake.size() - 1; i > 0; i--) mySnake.set(i, mySnake.get(i - 1));
if (mySnake.get(0)[X] < 26)
mySnake.set(0, new int[]{mySnake.get(0)[X] + 1, mySnake.get(0)[Y]});
else ifAlive = false;
} else if (mySnake.get(0)[X] - mySnake.get(1)[X] == -1 && canPlay && ifAlive) {
//向左走
for (int i = mySnake.size() - 1; i > 0; i--) mySnake.set(i, mySnake.get(i - 1));
if (mySnake.get(0)[X] > 0)
mySnake.set(0, new int[]{mySnake.get(0)[X] - 1, mySnake.get(0)[Y]});
else ifAlive = false;
} else if (mySnake.get(0)[Y] - mySnake.get(1)[Y] == 1 && canPlay && ifAlive) {
//向下走
for (int i = mySnake.size() - 1; i > 0; i--) mySnake.set(i, mySnake.get(i - 1));
if (mySnake.get(0)[Y] < 17)
mySnake.set(0, new int[]{mySnake.get(0)[X], mySnake.get(0)[Y] + 1});
else ifAlive = false;
} else if (mySnake.get(0)[Y] - mySnake.get(1)[Y] == -1 && canPlay && ifAlive) {
//向上走
for (int i = mySnake.size() - 1; i > 0; i--) mySnake.set(i, mySnake.get(i - 1));
if (mySnake.get(0)[Y] > 0)
mySnake.set(0, new int[]{mySnake.get(0)[X], mySnake.get(0)[Y] - 1});
else ifAlive = false;
}
}
}, 0, 100);
timerRepaint.schedule(
new TimerTask() {
@Override
public void run() {
//由JFrame继承而来的刷新方法。
if (canPlay && ifAlive) myBigFrame.super.repaint();
if (!ifAlive && canPlay) {
JOptionPane.showMessageDialog(myPanel, "游戏结束!");
if (JOptionPane.showConfirmDialog(myPanel, "是否重新开始?") == YES_OPTION) {
showMessageDialog(myPanel, "开始啦!");
mySnake = new ArrayList<>(4);
myFood = new ArrayList<>(3);
for (int i = 0; i < 4; i++) mySnake.add(new int[]{0, 0});
for (int i = 0; i < 3; i++) myFood.add(new int[]{0, 0});
for (int i = 0; i < 4; i++) {
mySnake.set(i, new int[]{0, 3 - i});
}
for (int i = 0; i < 3; i++) {
myFood.set(i, new int[]{(int) (Math.random() * 27), (int) (Math.random() * 18)});
}
myBigFrame.this.canPlay = true;
myBigFrame.this.ifAlive = true;
} else {
JOptionPane.showMessageDialog(myPanel, "关闭!");
myBigFrame.this.canPlay = false;
myBigFrame.this.setVisible(false);
}
}
}
}, 0, 10);
timerEat.schedule(
new TimerTask() {
@Override
public void run() {
if (ifEatAndChangeFood()) mySnake.add(mySnake.get(mySnake.size() - 1));
}
}, 0, 5);
}
private boolean ifEatAndChangeFood() {
for (int i = 0; i < 3; i++)
if (mySnake.get(0)[X] == myFood.get(i)[X] && mySnake.get(0)[Y] == myFood.get(i)[Y]) {
myFood.set(i, new int[]{(int) (Math.random() * 27), (int) (Math.random() * 18)});
return true;
}
return false;
}
//方向键按下事件;
public void keyPressed(KeyEvent e) {
boolean ifGetDirectionKeyIn = e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_DOWN ||
e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_RIGHT;
if (ifGetDirectionKeyIn && canPlay)
for (int i = mySnake.size() - 1; i > 0; i--) mySnake.set(i, mySnake.get(i - 1));
if (e.getKeyCode() == KeyEvent.VK_UP && canPlay)
mySnake.set(0, new int[]{mySnake.get(0)[X], mySnake.get(0)[Y] - 1});
else if (e.getKeyCode() == KeyEvent.VK_DOWN && canPlay)
mySnake.set(0, new int[]{mySnake.get(0)[X], mySnake.get(0)[Y] + 1});
else if (e.getKeyCode() == KeyEvent.VK_LEFT && canPlay)
mySnake.set(0, new int[]{mySnake.get(0)[X] - 1, mySnake.get(0)[Y]});
else if (e.getKeyCode() == KeyEvent.VK_RIGHT && canPlay)
mySnake.set(0, new int[]{mySnake.get(0)[X] + 1, mySnake.get(0)[Y]});
else System.out.print(e.getKeyChar() + " ");
score = mySnake.size() - 4;
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void mouseClicked(MouseEvent e) {
canPlay = true;
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
canPlay = false;
}
class myBigPanel extends JPanel {
public void paintComponent(Graphics graphics) {
//线框颜色
graphics.setColor(new Color(0x000000));
for (int i = 0; i < 28; i++) {
graphics.drawLine(20 + i * 40, 20, 20 + i * 40, 740);
}
for (int j = 0; j < 19; j++) {
graphics.drawLine(20, 20 + j * 40, 1100, 20 + j * 40);
}
for (int i = 0; i < mySnake.size(); i++) {
int Xpoint = mySnake.get(i)[0];
int Ypoint = mySnake.get(i)[1];
if (i == 0) {
//蛇头颜色
graphics.setColor(new Color(0x0032F2));
graphics.fillRect(22 + Xpoint * 40, 22 + Ypoint * 40, 36, 36);
} else {
//蛇身颜色
graphics.setColor(new Color(0x60BDFF));
graphics.fillRect(22 + Xpoint * 40, 22 + Ypoint * 40, 36, 36);
}
}
for (int[] ints : myFood) {
int X_point = ints[0];
int Y_point = ints[1];
//食物颜色
graphics.setColor(new Color(0xFFFC52));
graphics.fillOval(22 + X_point * 40, 22 + Y_point * 40, 36, 36);
}
//字体颜色
graphics.setColor(new Color(0xFD1100));
graphics.setFont(new Font("微软雅黑", Font.BOLD, 24));
graphics.drawString("你的得分是:" + String.valueOf(score), 400, 770);
}
}
}
//主函数
public class eatingSnake {
public static void main(String args[]) {
myBigFrame myBigFrame = new myBigFrame();
myBigFrame.setVisible(true);
if (JOptionPane.showConfirmDialog(myBigFrame, "是否现在开始呢?") == YES_OPTION) {
showMessageDialog(myBigFrame, "开始啦!");
myBigFrame.canPlay = true;
} else {
showMessageDialog(myBigFrame, "关闭!");
myBigFrame.setVisible(false);
}
}
}
关于贪食蛇的源代码
Posted aimxzj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于贪食蛇的源代码相关的知识,希望对你有一定的参考价值。
以上是关于关于贪食蛇的源代码的主要内容,如果未能解决你的问题,请参考以下文章