记得这是大一结束时老师让交的项目代码,当时在网上搜索了许多残缺不全的源码而且也比较麻烦,所以今天准备分享一下当时自己完善后的代码,自己能力有限,如果有哪位朋友进一步完善的话可以在评论区分享一下谢谢。
1.snakeMain
package Snake5;
import java.awt.Graphics;
import javax.swing.*;
public class snakeMain extends JFrame {
public snakeMain() {
snakeWin win = new snakeWin();
add(win);
setTitle("贪吃蛇小游戏");
setSize(435,390);
setLocation(200, 200);
setVisible(true);
}
public static void main(String[] args) {
new snakeMain();
}
}
2.snakeWin
package Snake5;
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{
int fenShu=0,Speed=0;
boolean start = false;
int rx=0,ry=0;
int eat1=0,eat2=0;
JDialog dialog = new JDialog();
JLabel label = new JLabel("所得"+fenShu+"为");
JButton ok = new JButton("重新再来");
Random r = new Random();
JButton newGame,stopGame;
List<snakeAct> list = new ArrayList<snakeAct>();
int temp=0;
Thread nThread;
public snakeWin() {
newGame = new JButton("新游戏");
stopGame = new JButton("结束");
newGame.addActionListener(this);
stopGame.addActionListener(this);
this.addKeyListener(this);
this.setLayout(new FlowLayout(FlowLayout.LEFT));
this.add(newGame);
this.add(stopGame);
dialog.setLayout(new GridLayout(2, 1));
dialog.add(label);
dialog.add(ok);
dialog.setSize(200, 200);
dialog.setLocation(200, 200);
dialog.setVisible(false);
ok.addActionListener(this);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawRect(10, 40, 400, 300);
g.drawString("当前分数"+fenShu, 150, 15);
g.drawString("当前速度"+Speed, 150, 35);
g.setColor(new Color(0, 255, 0));
if(start){
//添加食物
g.fillRect(10+rx*10, 40+ry*10, 10, 10);
for (int i = 0; i < list.size(); i++) {
g.setColor(new Color(0, 0, 255));
//绘制小蛇的身体
g.fillRect(10+list.get(i).getX()*10, 40+list.get(i).getY()*10, 10, 10);
}
}
}
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);
this.requestFocus();
nThread = new Thread(this);
nThread.start();
repaint();
if(e.getSource()==stopGame){
nThread.stop();
int option=JOptionPane.showConfirmDialog(this, "结束进程","贪吃蛇小游戏",JOptionPane.YES_NO_OPTION);
if(option==JOptionPane.YES_OPTION) {
System.exit(0);
}else if(option==JOptionPane.NO_OPTION){
//等同于线程暂停后开始
this.requestFocus();
nThread = new Thread(this);
nThread.start();
repaint();
}
}
}
if(e.getSource()==stopGame){
int option=JOptionPane.showConfirmDialog(this, "结束进程","贪吃蛇小游戏",JOptionPane.YES_NO_OPTION);
if(option==JOptionPane.YES_OPTION) {
System.exit(0);
}
}
if(e.getSource()==ok){
list.clear();
start=false;
newGame.setEnabled(true);
dialog.setVisible(false);
fenShu=0;
Speed=0;
repaint();
}
}
private void eat() {
if (rx==list.get(0).getX()&&ry==list.get(0).getY()) {
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);
fenShu = fenShu+100*Speed+10;
eat1++;
if(eat1-eat2>=4){
//没吃到4个小球速度提升一次
eat2=eat1;
Speed++;
}
}
}
public void otherMove(){
//移动过程中删除掉下标为0的索引相当于删除一节蛇身
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 (minYes(x, y)) {
otherMove();
list.get(0).setX(list.get(0).getX()+x);
list.get(0).setY(list.get(0).getY()+y);
eat();
repaint();
}else {
nThread = null;
label.setText("最终得分:"+fenShu+" 再接再励!");
dialog.setVisible(true);
}
}
public boolean minYes(int x,int y){
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(0).getX()==list.get(i).getX()&&list.get(0).getY()==list.get(i).getY()) {
return false;
}
}
return true;
}
public void keyPressed(KeyEvent e) {
if(start){
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
move(0, -1);
temp=1;
break;
case KeyEvent.VK_DOWN:
move(0, 1);
temp=2;
break;
case KeyEvent.VK_LEFT:
move(-1, 0);
temp=3;
break;
case KeyEvent.VK_RIGHT:
move(1, 0);
temp=4;
break;
default:
break;
}
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
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:
break;
}
repaint();
try {
Thread.sleep(300-30*Speed);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
3.snakeAct
package Snake5;
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;
}
}
整理不易如果转载请注明出处