java--贪吃蛇

这个小游戏项目,是一早上四节课的内容,是讲课过程内容,所以以视频为主。

1 制作欢迎界面

1.1讲解视频

java 贪吃蛇欢迎界面

1.2代码:

package com.zhangwei.class2.tanchishe;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Huanying extends JFrame {
    Huanying hy;
    public Huanying(){
        this.setSize(500,300);
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        this.setLocation((width - this.getSize().width) / 2,(height - this.getSize().height) / 2);
        this.setTitle("贪吃蛇----点击任意位置开始");
        //  调整图片的大小
        Image img=new ImageIcon("d:\\img\\fm.png").getImage();
        img=img.getScaledInstance(500, 300, Image.SCALE_SMOOTH);
        JLabel jl=new JLabel(new ImageIcon(img));

        this.setIconImage(img);
        this.add(jl);
        this.setVisible(true);
        hy=this;
        this.addMouseListener(new MouseListener(){

            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {
                hy.setVisible(false);
                //展示游戏开始的界面
               // System.out.println("游戏界面展示");
                new Youxi();
            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });

    }
}

2 制作贪吃蛇的块

2.1讲解视频

贪吃蛇的块

2.2代码

package com.zhangwei.class2.tanchishe;

import java.awt.*;

public class Kuai {
    int daxiao;
    int xingzhuang;// 0--fangkuai 1  yuanxing
    Color yanse;
    int  x;
    int y;
    public  Kuai(int daxiao, int xingzhuang, Color yanse,int x, int y) {
        this.daxiao = daxiao;
        this.xingzhuang = xingzhuang;
        this.yanse = yanse;
        this.x = x;
        this.y=y;
    }

    //显示
    public void show(Graphics g)
    {
        // 设置g的颜色
        g.setColor(yanse);
        g.drawRect(x,y,daxiao,daxiao);
        g.fillRect(x+2,y+2,daxiao-4,daxiao-4);
    }
}

3 制作贪吃蛇的身体

3.1 讲解视频

贪吃蛇的身体

3.2代码

package com.zhangwei.class2.tanchishe;

import java.awt.*;
import java.util.ArrayList;

public class She {
    int x;
    int y;
    Color colors[]={Color.red,Color.green,Color.CYAN,Color.yellow,Color.blue};
   // Kuai ks[]=new Kuai[4];
    ArrayList<Kuai> body;
    int fangxiang=37;
    public She(int x,int y,int cd) {
        body = new ArrayList<>();
        this.x = x;
        this.y = y;
        for(int i=0;i<cd;i++) {
            Kuai k=new Kuai(Shezhi.size,0,colors[i%5],Shezhi.screenwidt/2+i*Shezhi.size,Shezhi.screenheight/2);
            body.add(k);
        }

    }
    public void yidong(int fangxiang)
    {
        this.fangxiang=fangxiang;
        for(int i=body.size()-1;i>0;i--)
        {
            Kuai k=body.get(i);
            k.x=body.get(i-1).x;
            k.y=body.get(i-1).y;
        }

        //蛇头的运动
        Kuai k=body.get(0); // 得到蛇头
        if(this.fangxiang==Fangxiang.SHANG)
        {
           // k.y=k.y-Shezhi.size;
            k.y-=Shezhi.size;
        }
        else if(this.fangxiang==Fangxiang.XIA)
        {
            k.y+=Shezhi.size;
        }
        else if(this.fangxiang==Fangxiang.ZUO)
        {
            k.x-=Shezhi.size;
        }
        else if(this.fangxiang==Fangxiang.YOU)
        {
            k.x+=Shezhi.size;
        }
        else
        {
            System.out.println(" wusuoshicong,大概是overleaf");
        }

    }
    public void paint(Graphics g) {
        for(int i=0;i<body.size();i++) {
            Kuai k=body.get(i);// 得到第i个块
            k.show(g);

        }

    }
}

后续视频中的修正代码:

package com.zhangwei.class2.tanchishe;

import java.awt.*;
import java.util.ArrayList;

public class She {
    int x;
    int y;
    Color colors[]={Color.red,Color.green,Color.CYAN,Color.yellow,Color.blue};
   // Kuai ks[]=new Kuai[4];
    ArrayList<Kuai> body;
    int fangxiang=37;
    public She(int x,int y,int cd) {
        body = new ArrayList<>();
        this.x = x;
        this.y = y;
        for(int i=0;i<cd;i++) {
            Kuai k=new Kuai(Shezhi.size,0,colors[i%5],Shezhi.screenwidt/2+i*Shezhi.size,Shezhi.screenheight/2);
            body.add(k);
        }

    }
    public void yidong(int fangxiang)
    {
        this.fangxiang=fangxiang;
        for(int i=body.size()-1;i>0;i--)
        {
            Kuai k=body.get(i);
            k.x=body.get(i-1).x;
            k.y=body.get(i-1).y;
        }

        //蛇头的运动
        Kuai k=body.get(0); // 得到蛇头
        if(this.fangxiang==Fangxiang.SHANG)
        {
           // k.y=k.y-Shezhi.size;
            k.y-=Shezhi.size;
        }
        else if(this.fangxiang==Fangxiang.XIA)
        {
            k.y+=Shezhi.size;
        }
        else if(this.fangxiang==Fangxiang.ZUO)
        {
            k.x-=Shezhi.size;
        }
        else if(this.fangxiang==Fangxiang.YOU)
        {
            k.x+=Shezhi.size;
        }
        else
        {
            System.out.println(" wusuoshicong,大概是overleaf");
        }
        this.shifouZhuangqiang();
        this.shifouzizhuang();

    }
    public void paint(Graphics g) {
        // 清除区域影子
       // g.clearRect(0,0,Shezhi.screenwidt,Shezhi.screenheight);
        g.setColor(Color.black);
        g.fillRect(0,0,Shezhi.size,Shezhi.size);
        for(int i=0;i<body.size();i++) {
            Kuai k=body.get(i);// 得到第i个块
            k.show(g);

        }

    }
    public boolean shifouZhuangqiang()
    {
        Kuai k=body.get(0);
        if(k.x<Shezhi.size || k.y<Shezhi.size || k.x>Shezhi.screenwidt-Shezhi.size|| k.y>Shezhi.screenheight-Shezhi.size)
        {
            this.fangxiang=0;
            return false;
        }
        else
        {
            return  true;
        }
    }
    public boolean shifouzizhuang()
    {
        Kuai k=body.get(0);
        //判断是否撞到自己的身体
        for(int i=1;i<body.size();i++)
        {
            Kuai tem=body.get(i);
            if(k.x==tem.x && k.y==tem.y)
            {
                this.fangxiang=0;
                break;
            }
        }
        return false;

    }
}

4 游戏面板

4.1 讲解视频

游戏面板

4.2 代码

package com.zhangwei.class2.tanchishe;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Youxi  extends JFrame {
    public  She  she;
    Youxi  ziji;
    public  Youxi(){
        ziji=this;
        this.setSize(Shezhi.screenwidt,Shezhi.screenheight);
        this.setVisible(true);
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        this.setLocation((width - this.getSize().width) / 2,(height - this.getSize().height) / 2);
        this.setTitle("贪吃蛇游戏");

        she=new She(Shezhi.screenwidt/2,Shezhi.screenheight/2,30);
        this.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {
                she.yidong(e.getKeyCode());
                repaint();
            }

            @Override
            public void keyReleased(KeyEvent e) {

            }
        });
    }
    //重写  父类的方法
    public void paint(Graphics g){

        // super.paint(g);
        g.setColor(Color.black);
        g.fillRect(0,0,this.getWidth(),this.getHeight());

        she.paint(g);
    }
    public void startGame()
    {
        new yinqin().start();
    }

    public class yinqin extends Thread{
        public void run()
        {
            while(true)
            {
                System.out.println("111");
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                she.yidong(she.fangxiang);
               // she.paint(ziji.getGraphics());
                repaint();

            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值