大数据:大一整年感悟及总结

一、 期末感悟

1.个人感悟

大学生活第二个学期结束了,在这个学期里,在老师和同学们的关心下,通过自身不断的努力,各方面取得了一定的进步。

2.知识积累

在知识学习方面。学习刻苦,态度认真,只是在学习放法和能力上有些欠缺,在今后的学习中需要改进。比起上学期,在认知程度和态度上有了很大的进步,大一上期是刚来到大学校园,由于整个时间由自己支配,感到无所适从,时间经常转瞬即逝。经过一学期的适应,后大半学习基本上都能充分利用自己的时间。周六周末时间我经常和搭档去集训室学习,学习语言方面学习感兴趣的知识,以求提高自己的知识面,提升自己思考问题的角度。在学习上,我认为有一样东西非常重要,那就是学习态度!俗话说得好,态度决定一切。我以前总感觉不比别人差,问题能自己解决就自己解决,很少向别人请教。而现在,我开始养成一种谦虚、勤问的学习态度。因为我知道学习上的东西来不了弄虚作假,不懂就不懂,绝不能不懂装懂!

3.谦卑心理

孔夫子说过 三人行,必有我师,我想道理就在这里,作为21世纪的接班人,新世纪在悄悄降临之际也给我们带来了新的要求,经济日新月异,科技翻天覆地,所以更多、更快、更广的吸收新知识成了放在我们面前必须解决的一个问题,我通过这一学期的大学学习,对于专业方向、节奏、程度、难易度等等,也有所了解,投入不少时间在学习上。我希望在大学的后几年中,对学习任务有更高的要求,在这样的关键时刻,我会加倍努力学习,把更好的成绩带进新学期。所以,如果说这是对我的压力,倒不如说是对我的考验,我一定会全力以赴。

4.利用好碎片时间

一年之计在于春,一个学期之计在于开始,俗话说得好,好的开始是成功的一半,另一半就是好的结尾,新年、新春、新学期,也该有新的打算,虽然我在学习上有了一定的进步,可是我仍然存在不少缺点,还有很多需要改进的问题,比如骄傲自满,有时候会拖拖拉拉,时间总是宝贵的,我不想成为虚度光阴的人,不想自己在老的时候后悔自己这样浪费时间。作为学生,学业永远是第一位的。我将努力改正自己等等缺点,便自己向更好的方向发展。

5.为学历添砖加瓦

总之,大一的整个学年已经结术,这是不断学习、不断充实的一年。大学生活是很宝贵的,我不愿意平平淡淡过这几年,我要好好珍惜这难读得的读书机会,努力读书,为大学生活增添丰富美丽的色彩。

6.暑假计划

1.回到家先拿自己赚的钱好好旅游一番:准备和好朋友一起去绵阳 和父母一起去西藏 多出去走走,我经常跟我的一个老师交流。我觉得人不要被当前的环境影响自己。要提高自己的眼界和格局。俗话说得好,有多大格局 走多大的公路。
2.暑假学习计划:本来我是不想暴露的,但是还是算了。就跟你们聊聊
我准备回到家好好学习前端三剑客,HTML+CSS+JS+Juqery.
因为Java spring boot就是需要这些东西,以及python Django也是需要这些
我的方向是后端。
在这里插入图片描述

然后学完之后 开始做一个微信小程序。把自己做的软件放进去盈利。
一个月内结束,第二个月,JDBC+注解反射+Mbites+Java spring boot 全部学完 MySQL也全部学完。暑假足矣。

二、比赛经历及所获荣誉

1.全国高校第三届计算机挑战赛

二类赛:2021年十二月参加全国高校第三届计算机挑战赛得到了人生中的首奖,三个国家级三等奖,两个四川省一等奖,一个四川省二等奖。

2.蓝桥杯大赛——全国大学生TMT行业赛事

说来惭愧,平时关系不错的几个朋友,尤其是我的搭档 拿了蓝桥杯省一 还有几个朋友拿了省二和省三,我也是省三,原本我保底都是省二的,当成绩出来的时候我百思不得其解,结果去问了问LC每日一题群内的朋友,原来是我那道最小公约数暴力破解导致内存执行超时,很遗憾 15分就没有了。
希望大二的Java 大三的C++我能一雪前耻。

3.全国院校职业技能大赛——大数据技术与应用省赛

集训两个月Scala语言以及Spark框架,四月份代表学校参加全省院校职业技能大赛。然后两个队伍分别获得了全省第一名以及全省第二名,获得四川省院校职业技能大赛一等奖,奖金六千。

4.校级荣誉

综测暂为班级第一,入党积极分子,团支书 计算机协会成员,获校园打字杯第一名,优秀共青团员,优秀先进个人,华为优秀青少年社会实践,以及自由泳单人赛第六名,团体接力第二名。
我的目标是拿到大二的国家励志奖学金以及四川省优秀大学生素质A类证书以及python1+X证书以及四川省优秀大学生。

5.创业基金

与搭档一起在学校研究软件各自使用Python及Java做出的软件 搭档盈利为1~2万左右 我的盈利为4-6万左右。如果算下奖学金的话可能大一就盈利为六位数。
以及导师为我们申请的中国项目创业基金10万到五十万不等。

6.创业打算

我们的想法是大学毕业了把基金拿来创办一个软件项目外包公司,能拿出六位数注册资本。当然到时候可能会遇到种种困难这些都是后话了。

三、实训项目

1.Python——Django网页项目

请添加图片描述
请添加图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

耗时两天 代码量二千三百行 采用了Html+CSS+JS+JQuery
在这期间我发现了很多BUG,以及排版问题,然后逐一解决,真的,想要提升自己的薄弱之处,最好的方法就是用项目锻炼自己。

2.Java学生信息管理系统

2.1.登录界面

package xuesheng;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

class LoginCheck{
    private String name;
    private String password;
    public LoginCheck(String name,String password){
        this.name=name;
        this.password=password;
    }
    public boolean equals(){
        if("root".equals(name)&&"123456".equals(password)){
            return true;
        }else{
            return false;
        }
    }
};
class ActionHandle{
    private JFrame frame=new JFrame("学生信息管理系统");
    private JTextField name=new JTextField();//设置文本框
    private JPasswordField pass=new JPasswordField();
    private JLabel but1=new JLabel("用户名:");
    private JLabel but2=new JLabel("密   码:");
    private JButton but3=new JButton("登录");
    private JButton but4=new JButton("重置");
    
    public ActionHandle(){
        but3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                if(e.getSource()==but3){
                    String sname=name.getText();
                    String spass=new String(pass.getPassword());
                    LoginCheck log=new LoginCheck(sname,spass);
                    if(log.equals()){
                        try {
                            new Menu();
                        } catch (Exception e1) {
                            
                            e1.printStackTrace();
                        }
                        frame.setVisible(false);
                        
                    }else{
                        JOptionPane.showMessageDialog(null, "登录失败,错误的用户名或密码!");
                    }
                }
            }                   
        });     
        but4.addActionListener(
new ActionListener(){
            public void actionPerformed(ActionEvent e){
                if(e.getSource()==but4){
                    name.setText("");
                    pass.setText("");
                }
            }

        });
        frame.setLayout(null);
        but1.setBounds(80, 40 , 80,30);
        name.setBounds(140,42, 120, 25);    //
        but2.setBounds(80, 80 , 80,30);
        pass.setBounds(140,82, 120, 25);
        but3.setBounds(130, 150 , 60,30);
        but4.setBounds(210, 150 , 60,30);
        frame.setSize(400,330);     
        frame.setLocation(300, 200);
        frame.add(but1);
        frame.add(name);
        frame.add(pass);
        frame.add(but2);
        frame.add(but3);
        frame.add(but4);
        frame.setVisible(true);
    }
}
public class  Enter{
    public static void main(String[] args) {
        
        new ActionHandle();
    }

}

2.2.主界面

package xuesheng;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public class Menu {
    private JButton but1 = new JButton("增加数据"); // 按钮
    private JButton but2 = new JButton("删除数据");
    private JButton but3 = new JButton("修改数据");
    private JButton but4 = new JButton("查看数据");
    private JButton but0 = new JButton("退出系统");
    private JButton but5 = new JButton("显示");
    private JButton clear = new JButton("清空");
    private JTextField number = new JTextField();// 文本框
    private JTextField name = new JTextField();
    private JTextField dor = new JTextField();
    private JTextField address = new JTextField();
    private JTextField sex = new JTextField();
    private JTextField date = new JTextField();
    private JTextField pol = new JTextField();
    private JTextField phone = new JTextField();

    private JTextArea show = new JTextArea(16, 30); 
    private JLabel lab1 = new JLabel("姓名:");// 标签
    private JLabel lab2 = new JLabel("学院:");
    private JLabel num = new JLabel("学号:");
    private JLabel lab4 = new JLabel("性别:");
    private JLabel lab5 = new JLabel("专业班级:");
    private JLabel lab6 = new JLabel("出生日期:");
    private JLabel lab7 = new JLabel("政治面貌:");
    private JLabel lab8 = new JLabel("联系方式:");
    // private JLabel lab3 = new JLabel("请输入内容,完成操作。");

    private JFrame frame = new JFrame("信息管理系统"); // 框架
    private JFrame frame1 = new JFrame("显示信息");

    Hashtable<String, Person> has = new Hashtable<String, Person>();// 哈希表,加密,文件乱码    
    File file = new File("新建文件.txt");// 注意:新建一个文件
    public Menu() {
        if (!file.exists()) {
            try {
                ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));// 把一个实例的对象以文件的形式保存到磁盘上。        out.writeObject(has);
                out.close();
            } catch (IOException e) {
            }
        }
        but1.addActionListener(new ActionListener() { // 增加,内部类//进行某项操作时触发功能
			public void actionPerformed(ActionEvent e) {//用于接收操作事件的侦听器接口
				if (e.getSource() == but1) {
					but3.setEnabled(false);//使but3这个按钮变灰不可点击了
					String number1 = number.getText();
					if (number1.length() == 12) {
						try {
							ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));//读回对象
							has = (Hashtable) in.readObject();							                       in.close();
						} catch (Exception e1) {
						}
                 					} else {
						JOptionPane.showMessageDialog(null, "请输入12位数字的学号");//提示框显示
					}
					//
					if (number1.length() == 12) {
					if (has.containsKey(number1)) {						JOptionPane.showMessageDialog(null, "该生信息已存在,请到修改页面修改!");
					} else {
						String name1 = name.getText();
						String dor1 = dor.getText();
						String address1 = address.getText();
						String sex1 = sex.getText();
						String date1 = date.getText();
						String pol1 = pol.getText();
						String phone1 = phone.getText();
						Person per = null;
						per = new Person(number1, name1, dor1, address1, sex1, date1, pol1, phone1);
						has.put(number1, per);// ???
						try {
							ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
							out.writeObject(has);
							out.close();
                       JOptionPane.showMessageDialog(null, "添加成功!");
						} catch (Exception e1) {}
					}
					}
				}
			}
});
		clear.addActionListener(new ActionListener() { // 清空
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == clear) {//获得事件所作用的对象

					number.setText(null);
					dor.setText(null);
					name.setText(null);
					address.setText(null);
					sex.setText(null);
					date.setText(null);
					pol.setText(null);
					phone.setText(null);
				}
			}
		});
		but2.addActionListener(new ActionListener() { // 删除
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == but2) {
					but3.setEnabled(false);
					String number1 = number.getText();
					if (number1.length() == 12) {
						try {
							ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
							has = (Hashtable) in.readObject();
							in.close();
						} catch (Exception e1) {
						}
					} else {
						JOptionPane.showMessageDialog(null, "请输入12位数字的学号");
					}
					if (has.containsKey(number1)) {
						has.remove(number1);
						ObjectOutputStream out = null;
						JOptionPane.showMessageDialog(null, "删除成功");
						try {
							out = new ObjectOutputStream(new FileOutputStream(file));
							//out.writeObject(has);
							out.close();
						} catch (IOException ex) {
							Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex);//记录当前类可能发生的异常
						}
					} else {
						JOptionPane.showMessageDialog(null, "学号不存在");
					}
				}
			}
		});

		but3.addActionListener(new ActionListener() { // 修改
			public void actionPerformed(ActionEvent e) {
				//String number2 = number.getText();
				if (e.getSource() == but3) {
					but3.setEnabled(false);
				
					String number1 = number.getText();
					String name1 = name.getText();
					String dor1 = dor.getText();
					String address1 = address.getText();
					String sex1 = sex.getText();
					String date1 = date.getText();
					String pol1 = pol.getText();
					String phone1 = phone.getText();
					Person per = new Person(number1, name1, dor1, address1, sex1, date1, pol1, phone1);
					has.put(number1, per);
					if (number1.length() == 12) {
						try {
							ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
							has = (Hashtable) in.readObject();
							in.close();
						} catch (Exception e1) {
						}
					JOptionPane.showMessageDialog(null, "修改成功");
					
					try {
						ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
						out.writeObject(has);
						out.close();
					} catch (Exception e1) {
					}
				}
			}
		}});

		but4.addActionListener(new ActionListener() { // 查看
			public void actionPerformed(ActionEvent e) {

				if (e.getSource() == but4) {
					but3.setEnabled(false);
					String number1 = number.getText();
					if (number1.length() == 12) {
						if (has.containsKey(number1)) {
							try {
								ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
								has = (Hashtable) in.readObject();
								in.close();
							} catch (Exception e1) {
							}
							Person per = (Person) has.get(number1);
							name.setText(per.getName());
							dor.setText(per.getDor());
							address.setText(per.getAddress());
							sex.setText(per.getSex());
							date.setText(per.getDate());
							pol.setText(per.getPol());
							phone.setText(per.getPhone());

							but3.setEnabled(true);
						} else {
							JOptionPane.showMessageDialog(null, "学号不存在");
						}
					} else {
						JOptionPane.showMessageDialog(null, "请输入12位数字的学号");
					}
				}
			}

		});
        but5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == but5) {
                    frame1.setVisible(true);
                    try {
                        ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
                        has = (Hashtable) in.readObject();
                        in.close();
                    } catch (Exception ee) {
                    }
                    if (has.isEmpty()) {
                        show.append("目前还没有学生的信息记录!n");
// append(s:String)向文本域的文本追加字符串,简单的说就像system.out.println()                  
} else {
                                            
                    for (Enumeration enu = has.elements(); enu.hasMoreElements();) {
//存入内存的内容如果不经过遍历是显示不出来的
                            Person per = (Person) enu.nextElement();
                            String str = "  <学号>:" + per.getNum() + "n" + "  <姓名>:" + per.getName() + "n" + "  <学院>:"
                                    + per.getDor() + "n" + "  <性别>:" + per.getAddress() + "n" + "  <专业班级>:"
                                    + per.getSex() + "n" + "<出生日期>:" + per.getDate() + "n" + "  <政治面貌>:"
                                    + per.getPol() + "n" + " <电话>:" + per.getPhone() + "n" + "n";
                            show.append(str);
                        }
                        String str2 = "------------------------------结束---------------------------------------------------"
                                + "n";
                        show.append(str2);
                    }
                }
            }

        });
        but0.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == but0) {
                    frame.setVisible(false);
                    new ActionHandle();
                }
            }
        });
        frame.setLayout(null);
        but1.setBounds(30, 35, 90, 25);
        but2.setBounds(30, 75, 90, 25);
        but3.setBounds(30, 115, 90, 25);
        but4.setBounds(30, 155, 90, 25);
        but0.setBounds(240, 430, 100, 25); // setBounds(x,y,width,height);
        num.setBounds(150, 30, 70, 25);
        lab1.setBounds(150, 65, 70, 25);
        lab2.setBounds(150, 100, 70, 25);//
        lab4.setBounds(150, 135, 70, 25);
        lab5.setBounds(150, 170, 70, 25);
        lab6.setBounds(150, 205, 70, 25);
        lab7.setBounds(150, 240, 70, 25);
        lab8.setBounds(150, 275, 70, 25);
        number.setBounds(230, 30, 90, 25);
        name.setBounds(230, 65, 90, 25);
        dor.setBounds(230, 100, 90, 25);
        address.setBounds(230, 135, 90, 25);
        sex.setBounds(230, 170, 90, 25);
        date.setBounds(230, 205, 90, 25);
        pol.setBounds(230, 240, 90, 25);
        phone.setBounds(230, 275, 90, 25);
        // lab3.setBounds(130, 390, 250, 25);
        clear.setBounds(250, 310, 60, 25);
        but5.setBounds(150, 310, 60, 25);
        frame.add(lab1);
        frame.add(lab2);
        //frame.add(lab3);
        frame.add(lab4);
        frame.add(lab5);
        frame.add(lab6);
        frame.add(lab7);
        frame.add(lab8);
        frame.add(num);
        frame.add(number);
        frame.add(name);
        frame.add(dor);
        frame.add(address);
        frame.add(sex);
        frame.add(date);
        frame.add(pol);
        frame.add(phone);
        frame.add(clear);
        frame.add(but1);
        frame.add(but2);
        frame.add(but3);
        frame.add(but4);
        frame.add(but0);
        JScrollPane scroll = new JScrollPane(show);
            frame1.add(scroll,BorderLayout.CENTER);      
        frame.add(but5);
        frame.setSize(400, 500); // 页面大小
        frame1.setBounds(200, 200, 400, 300);
        frame.setLocation(300, 200);
        frame.setVisible(true);
        frame1.setVisible(false);
    }
}

2.3.个人信息

package xuesheng;

import java.io.Serializable;
public class Person implements Serializable {//使用Serializable类,可以使对象被序列化,用于持久化(保存)对象。
	private String num;
	private String name;
	private String dor;
        private String address;
        private String sex;
        private String date;
        private String pol;
        private String phone;
	//public Person(){}
	public Person(String num,String name,String dor,String address,String sex,String date,String pol,String phone ){
		this.num=num;
		this.name=name;
                this.dor=dor;
		this.address=address;
                this.sex=sex;
                this.date=date;
                this.pol=pol;
                this.phone=phone;
        }
        public void setNum(String num){
		this.num=num;
	}
	public String getNum(){
		return num;
	} 
	public void setName(String name){
		this.name=name;
	}
	public String getName(){
		return name;
	}
	public void setDor(String dor){
		this.dor=dor;
	}
	public String getDor(){
		return dor;
	}
	public void setAddress(String address){
		this.address=address;
	}
	public String getAddress(){
		return address;
	}
	public void setSex(String sex){
		this.sex=sex;
	}
	public String getSex(){
		return sex;
	}
	public void setDate(String date){
		this.date=date;
	}
	public String getDate(){
		return date;
	}
	public void setPol(String pol){
		this.pol=pol;
	}
	public String getPol(){
		return pol;
	}
	public void setPhone(String phone){
		this.phone=phone;
	}
	public String getPhone(){
		return phone;
	}       
}

2.4.成果展示

在这里插入图片描述
在这里插入图片描述

3.贪吃蛇游戏

package net.lyt.snake;

import javax.swing.*;

//数据中心
public class Data {
    //相对路径 tx.jpg
    //绝对路径 / 相当于当前的项目
    //头部图片
//    public static URL heardURL = Data.class.getResource("statics/header.png");
    public static ImageIcon head = new ImageIcon("statics/header.png");

//    public static URL bodyURL = Data.class.getResource("statics/body.png");
    public static ImageIcon body = new ImageIcon("statics/body.png");

//    public static URL downURL = Data.class.getResource("statics/down.png");
    public static ImageIcon down = new ImageIcon("statics/down.png");

//    public static URL foodURL = Data.class.getResource("statics/food.png");
    public static ImageIcon food = new ImageIcon("statics/food.png");

//    public static URL leftURL = Data.class.getResource("statics/left.png");
    public static ImageIcon left = new ImageIcon("statics/left.png");

//    public static URL rightURL = Data.class.getResource("statics/right.png");
    public static ImageIcon right = new ImageIcon("statics/right.png");

//    public static URL upURL = Data.class.getResource("statics/up.png");
    public static ImageIcon up = new ImageIcon("statics/up.png");
}
package net.lyt.snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class GamePanel extends JPanel implements KeyListener, ActionListener {

    //定义蛇的结构
    int length;      //蛇的长度
    int[] snakeX = new int[600];//蛇的x坐标
    int[] snakey = new int[500];//蛇的y坐标
    String fx;
    int score;//成绩
    //食物的坐标
    int foodx;
    int foody;
    Random random = new Random();

    //游戏当前状态:开始 停止
    boolean isStart = false;//默认是不开始!
    boolean isFail = false;//游戏失败状态
    //定时器 以毫秒为单位 1000ms=1s
    Timer timer = new Timer(300, this);//300毫秒执行1次

    //构造器
    public GamePanel() {
        init();
        //获得焦点和键盘事件
        this.setFocusable(true);//获得焦点事件
        this.addKeyListener(this);//获得键盘监听
        timer.start();//游戏一开始定时器就启动
    }

    //初始化方法
    public void init() {
        length = 3;
        snakeX[0] = 100;
        snakey[0] = 100;//脑袋的坐标
        snakeX[1] = 75;
        snakey[1] = 100;//第一个身体的坐标
        snakeX[2] = 50;
        snakey[2] = 100;//第二个身体的坐标
        fx = "R";//初始方向向右


        foodx = 25 + 25 * random.nextInt(34);//边界内
        foody = 75 + 25 * random.nextInt(24);

        score = 0;
    }

    //绘制面板,我们游戏中的所有东西,都是用这个画笔来画

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);//清屏
        this.setBackground(Color.WHITE);
        Data.head.paintIcon(this, g, 25, 11);//头部广告栏画上去
        g.fillRect(25, 75, 850, 600);//默认的游戏界面

        //画积分
        g.setColor(Color.red);
        g.setFont(new Font("微软雅黑", Font.BOLD, 16));
        g.drawString("长度" + length, 750, 35);
        g.drawString("分数" + score, 750, 50);
        //把小蛇画上去
        if (fx.equals("R")) {
            Data.right.paintIcon(this, g, snakeX[0], snakey[0]);//蛇头初始化向右,需要通过方向来判断
        } else if (fx.equals("L")) {
            Data.left.paintIcon(this, g, snakeX[0], snakey[0]);//蛇头初始化向右,需要通过方向来判断
        } else if (fx.equals("U")) {
            Data.up.paintIcon(this, g, snakeX[0], snakey[0]);//蛇头初始化向右,需要通过方向来判断
        } else if (fx.equals("D")) {
            Data.down.paintIcon(this, g, snakeX[0], snakey[0]);//蛇头初始化向右,需要通过方向来判断
        }

        for (int i = 1; i < length; i++) {
            Data.body.paintIcon(this, g, snakeX[1], snakey[1]);//第一个身体的坐标
        }
        Data.body.paintIcon(this, g, snakeX[2], snakey[2]);

        //画食物
        Data.food.paintIcon(this, g, foodx, foody);

//        g.drawImage(Data.food.getImage(), foodx, foody, this);

        // 游戏状态
        if (isStart == false) {
            g.setColor(Color.WHITE);//白色画笔
            //设置字体
            g.setFont(new Font("微软雅黑", Font.BOLD, 40));//设置字体
            g.drawString("按下Space开始游戏", 300, 300);
        }
        //游戏状态
        if (isFail) {
            g.setColor(Color.WHITE);//白色画笔
            //设置字体
            g.setFont(new Font("微软雅黑", Font.BOLD, 40));//设置字体
            g.drawString("失败,按下空格重新开始", 300, 300);
        }

    }

    //事件监听--需要通过固定事件来刷新,1s=10次
    @Override
    public void actionPerformed(ActionEvent e) {
        if (isStart && isFail == false) {//如果游戏是开始状态,就让小蛇动起来
            //吃食物
            if (snakeX[0] == foodx && snakey[0] == foody) {
                length++;//每次吃一个增加一次长度

                //分数+10
                score = score + 10;

                //再次随机食物
                foodx = 25 + 25 * random.nextInt(34);//边界内
                foody = 75 + 25 * random.nextInt(24);
            }

            //移动
            for (int i = length - 1; i > 0; i--) {
                snakeX[i] = snakeX[i - 1];//向前移动一节
                snakey[i] = snakey[i - 1];//向前移动一节
            }

            //走向
            if (fx.equals("R")) {
                snakeX[0] = snakeX[0] + 25;
                if (snakeX[0] > 850) {
                    snakeX[0] = 25;
                }
            } else if (fx.equals("L")) {
                snakeX[0] = snakeX[0] - 25;
                if (snakeX[0] < 25) {
                    snakeX[0] = 850;
                }//边界判断

            } else if (fx.equals("U")) {
                snakey[0] = snakey[0] - 25;
                if (snakey[0] < 75) {
                    snakey[0] = 650;
                }
            } else if (fx.equals("D")) {
                snakey[0] = snakey[0] + 25;
                if (snakey[0] > 650) {
                    snakey[0] = 75;
                }
            }
            //失败判断,撞到自己就是失败
            for (int i = 1; i < length; i++) {
                if (snakeX[0] == snakeX[i] && snakey[0] == snakey[i]) {
                    isFail = true;
                }
            }
            repaint();//重画页面
        }
        timer.start();//定时器开启!
    }

    //键盘监听事件
    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();//获得键盘按键是哪一个
        if (keyCode == KeyEvent.VK_SPACE) {//如果按下的是空格键
            if (isFail) {
                //重新开始
                isFail = false;
                init();
            } else {
                isStart = !isStart;//取反
            }
        }

        //小蛇移动
        if (keyCode == KeyEvent.VK_UP) {
            fx = "U";
        } else if (keyCode == KeyEvent.VK_DOWN) {
            fx = "D";
        } else if (keyCode == KeyEvent.VK_LEFT) {
            fx = "L";
        } else if (keyCode == KeyEvent.VK_RIGHT) {
            fx = "R";
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

}

package net.lyt.snake;

import javax.swing.*;

public class StartGame {
    public static void main(String[] args) {
        JFrame frmSnameGame = new JFrame();
        frmSnameGame.setBounds(10, 10, 900, 720);
        frmSnameGame.setResizable(false);
        frmSnameGame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frmSnameGame.add(new GamePanel());
        frmSnameGame.setLocationRelativeTo(null);
        frmSnameGame.setVisible(true);
    }
}

在这里插入图片描述

四、有志同道合的码友可加好友

QQ:2529165097
VX:lyt2250177503

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>