最简易的贪吃蛇代码(存起来慢慢增加功能)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
/**
 *
 * @author Administrator
 */
public class Snaker {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Myframe frame=new Myframe();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        // TODO code application logic here
    }

}
class Myframe extends JFrame {
    Mypanel mypanel=new Mypanel();
   public Myframe(){//界面设置;
        super("贪吃蛇游戏");
        setLayout(new BorderLayout());//设置布局管理器;
        JButton button=new JButton("开始游戏");
        JLabel label=new JLabel("点击开始按Tab键开始游戏");//为什么要按Tab键呢?
        JPanel j=new JPanel();

        j.add(button);
        j.add(label);
        add(j,BorderLayout.NORTH);
        add(mypanel,BorderLayout.CENTER);
        this.setSize(400, 400);//设置窗口大小;
        this.setLocation(200, 200);//设置位置;
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                mypanel.thread.start();//button事件;
            }
        });
    }
}
class Mypanel extends JPanel implements Runnable{
    int x=100,y=100,dir=0,num=0,flag=1;
    Thread thread;//定义线程;
    Rectangle2D[] snake=new Rectangle2D[10];//2D矩形数组;
    Rectangle2D[] food=new Rectangle2D[10];
    public Mypanel(){
       setLayout(null);
       setFocusable(true);
       this.addKeyListener(new KeyAdapter(){
            @Override
          public void keyPressed(KeyEvent e){
             int k=e.getKeyCode();
             if(k==KeyEvent.VK_DOWN){dir=0;}
             if(k==KeyEvent.VK_UP){dir=1;}
             if(k==KeyEvent.VK_RIGHT){dir=2;}
             if(k==KeyEvent.VK_LEFT){dir=3;}
          }//键盘事件;
       });
      
       thread =new Thread(this);//实例化线程;
       for(int i=1;i<10;i++){
           snake[i]=new Rectangle2D.Double();//实例化蛇身;
       }
       snake[0]=new Rectangle2D.Double(100,100,10,10);
       for(int i=0;i<10;i++){
           food[i]=new Rectangle2D.Double(i*40+5, i*20+5, 10 , 10);//实例化食物;
       }
       setBackground(Color.WHITE);
    }
    public void run(){
        while(true){
        if(dir==0){
            try{
           snake[0].setFrame(x, y=y+5, 10, 10); //向下移动;
           repaint();//重绘;
            if(x==food[num].getX()&&y==food[num].getY()){
                num++;
            }
           Thread.sleep(100);//停止0.1秒;
          
        }
        catch(Exception e){System.out.println("抛出异常");}
    }
        else if(dir==1){
            try{
                snake[0].setFrame(x, y=y-5, 10, 10);
                repaint();
                if(x==food[num].getX()&&y==food[num].getY()){
                    num++;
                }
           
            Thread.sleep(100);
            }
            catch(Exception e){
                System.out.println("抛出异常");
            }
        }
        else if(dir==2){
            try{
                snake[0].setFrame(x=x+5, y, 10, 10);
                repaint();
                if(x==food[num].getX()&&y==food[num].getY()){
                    num++;
                }
                Thread.sleep(100);
            }
            catch(Exception e){}
        }
        else if(dir==3){
            try{
                snake[0].setFrame(x=x-5, y, 10, 10);
                repaint();
                if(x==food[num].getX()&&y==food[num].getY()){
                    num++;
                }
                Thread.sleep(100);
            }
            catch(Exception e){}
        }
        for(int i=(num<10?num:9);i>0;i--){
            snake[i].setFrame(snake[i-1].getX(), snake[i-1].getY(), 10, 10);
        }
        if(num==10){
            flag=0;
            repaint();
            break;
        }
        }
    }
   

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);//不明白为什么去掉此句就不能刷新了;
       Graphics2D g2=(Graphics2D)g;
       g2.draw(snake[0]);
       g2.setColor(Color.red);
       g2.fill(snake[0]);
       g2.setColor(Color.yellow);
       for(int i=(num<10?num:9);i>0;i--){
           g2.draw(snake[i]);
           g2.fill(snake[i]);
       }
       g2.setColor(Color.blue);
       if(num<10){
           g2.draw(food[num]);
           g2.fill(food[num]);
       }
     
        if(flag==0){
            g2.drawString("~~~你真NB!!~~~~    通关了,  哈哈!!", 150, 200);
        }
       
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值