资源免费
我们一直在努力

java贪吃蛇大作业[caired.com-彩红网3D模型下载CG模型平面素材专利检索网站源码]

java贪吃蛇大作业[caired.com-彩红网3D模型下载CG模型平面素材专利检索网站源码]
之前看见有人发了个C语言的贪吃蛇,想起我在大一下Java老师给我们留的大作业就是贪吃蛇,所以发出来给大家分享交流一下。
老师要求是有障碍物,能换皮肤,能够暂停,这可把我们一群非计算机的学生难倒了。
本人是在校电子专业的本科生,这个贪吃蛇也算是被逼着写出来的,有什么缺陷还望指正。
SnakeMain.java

import javax.swing.*;  
public final 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();
  }
}

SnakeWin.java

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,/*skin,*/pause,continuegame;
   JComboBox skin;
   int fenShu=0,Speed=0,red,green,blue;//分数与速度还有颜色
   boolean start = false,islive=true,colorful=false;
   Random r = new Random();//产生随机数,生成果实
   int rx = 0,ry = 0;//果实坐标
   List list = new ArrayList();//蛇身
   int temp = 0;//控制蛇身自动移动
   int u=4;//防止蛇头反向
   int tempeat1=0,tempeat2=0;
   JDialog dialog = new JDialog();
   JLabel label = new JLabel("你挂了,你的分数是"+fenShu);
   JButton ok = new JButton("好吧");
   Thread nThread;//增加线程,蛇身自动移动
   public SnakeWin(){
       newGame = new JButton("开始");
       stopGame = new JButton("退出");
       skin = new JComboBox();
       skin=new JComboBox(new String[]{"黑蛇", "绿蛇", "红蛇", "随机蛇","七彩皮皮蛇"});
       pause = new JButton("暂停");
       continuegame = new JButton("继续"); 
       this.setLayout(new FlowLayout(FlowLayout.LEFT));
       newGame.addActionListener(this);
       stopGame.addActionListener(this);
       skin.addItemListener(new ItemListener(){
           public void itemStateChanged(final ItemEvent e) {
            int index = skin.getSelectedIndex();
            switch(index){
            case 0:{
                changeSkin(1);
                break;
            }
            case 1:{
                changeSkin(2);
                break;
            }
            case 2:{
                changeSkin(3);
                break;
            }
            case 3:{
                changeSkin(4);
                break;
            }
            case 4:{
                changeSkin(5);
                break;
            }
            default:
                break;
            }
            requestFocus(true);
        }
       });
       continuegame.addActionListener(this);
       pause.addActionListener(this);
       this.addKeyListener(this);
       this.add(newGame);
       this.add(stopGame);
       this.add(skin); 
       this.add(pause);
       this.add(continuegame);
       ok.addActionListener(this );
       dialog.setLayout(new GridLayout(1,2));
       dialog.add(label);
       dialog.add(ok);
       dialog.setSize(400,100);
       dialog.setLocation(200, 200);
       dialog.setVisible(false);
   }
class Food{
    private int x;
    private int y;
    void produceFood(){
        x=r.nextInt(40);
        y=r.nextInt(30);
    }
    public int getX(){
        return x;
    }
    public int getY(){
        return y;
    }
}
Food food = new Food();
Wall wall=new Wall();
   public void paintComponent(Graphics g){
       super.paintComponent(g);
       g.drawRect(10, 40, 400, 300);
       g.drawString("分数"+fenShu, 150+180+35, 20);
       g.drawString("速度"+Speed, 150+180+35, 35);
       g.setColor(new Color(0,0,255));
       if(start){
            if(colorful){changeSkin(5);}
            g.fillRect(10+food.getX()*10, 40+food.getY()*10, 10, 10);
            g.setColor(new Color(red,green,blue));
            for(int i = 0;i=5){
            tempeat2=tempeat1;
            if(Speed<=9){
                Speed+=1;
            }
        }
    }
}
 
       //蛇身移动
public void otherMove(){
    snakeBody piece = new snakeBody();
    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){
            piece = list.get(i-1);
            list.set(i-1,list.get(i));
            list.set(i,piece);
        }
    }
}
 
       //蛇头移动
public void move(int x,int y){
    if(islive){
        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;i1&&list.get(0).getX()==list.get(i).getX()&&list.get(0).getY()==list.get(i).getY()){
            return false;
        }
    for(int j=0;j<10;j++){
        if(list.get(0).getX()==wall.list1.get(j).x&&list.get(0).getY()==wall.list1.get(j).y){
            return false;
        }
    }
    for(int j=0;j<10;j++){
        if(list.get(0).getX()==wall.list2.get(j).x&&list.get(0).getY()==wall.list2.get(j).y){
            return false;
        }
    }
    for(int j=0;j<10;j++){
        if(list.get(0).getX()==wall.list3.get(j).x&&list.get(0).getY()==wall.list3.get(j).y){
            return false;
        }
    }
    for(int j=0;j<10;j++){
        if(list.get(0).getX()==wall.list4.get(j).x&&list.get(0).getY()==wall.list4.get(j).y){
            return false;
        }
    }
    }
    return true;
}
@Override
public void keyPressed(KeyEvent e) {
    if(start){
         
        switch (e.getKeyCode()){
        case KeyEvent.VK_UP:
            if(u!=2){
                move(0,-1);
                u=1;
                temp=1;}
            break;
             
        case KeyEvent.VK_DOWN:
            if(u!=1){
                move(0,1);
                u=2;
                temp=2;}
            break;
             
        case KeyEvent.VK_LEFT:
            if(u!=4){
                move(-1,0);
                u=3;
                temp=3;}
            break;
             
        case KeyEvent.VK_RIGHT:
            if(u!=3){
                move(1,0);
                u=4;
                temp=4;}
            break;
             
            default:
                break;
        }
    }
 
}
    @Override
    public void keyReleased(KeyEvent arg0) {}
    @Override
    public void keyTyped(KeyEvent arg0) {}
    @Override
    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(500-(50*Speed));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }   
    }
}

SnakeBody,java

public class snakeBody {
    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;
    }
 
}

Wall.java

import java.util.List;
import java.util.*;
 
class Brick{
    int x;
    int y;
    public void setXY(int x,int y){
        this.x=x;
        this.y=y;
    }
}
public class Wall{
    List list1=new ArrayList();
    List list2=new ArrayList();
    List list3=new ArrayList();
    List list4=new ArrayList();
    public void d(){
        for(int i=0;i<10;i++){
            Brick brick1=new Brick();
            Brick brick2=new Brick();
            Brick brick3=new Brick();
            Brick brick4=new Brick();
            brick1.setXY(i, 15);
            list1.add(brick1);
            brick2.setXY(i+30, 15);
            list2.add(brick2);
            brick3.setXY(20, i);
            list3.add(brick3);
            brick4.setXY(20, i+20);
            list4.add(brick4);
            }
         
   
    }
}   
赞(0) 打赏
未经允许不得转载:彩红网 » java贪吃蛇大作业[caired.com-彩红网3D模型下载CG模型平面素材专利检索网站源码]
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏