Java se入门练手升级版小游戏俄罗斯方块(2021-12-05)

比较传统俄罗斯方块升级的地方

① 加入最高分记录;
② 加入随机位置产生方块增加游戏难度;
③ 加入开局随机产生方块增加游戏难度;
④ 加入游戏难度设置,可选择方块下降速度,根据不同下架速度匹配不同的消除每行分数也不同;

准备工作

①素材准备
②JDK8.0
③IntelliJ IDEA

升级模块代码

①在资源区新建Max文件,通过DataOutputStream和DataInputStream流进行读写加入最高分记录;

   public void writeMax(){
        DataOutputStream dos= null;
        try {
            dos = new DataOutputStream(new FileOutputStream(Thread.currentThread().getContextClassLoader().getResource("Max").getPath()));
            dos.writeInt(max);
            dos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                dos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void readMax(){
        DataInputStream dis = null;
        try {
            dis = new DataInputStream(new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("Max").getPath()));
            max=dis.readInt();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                dis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

②新建随机开局RandomOpen的方块类,在主类中new出来之后进行paint绘制再将遍历的每个方块嵌入wall中:

public class RandomOpen {
    protected List<Cell> cells=new ArrayList<>();
    public RandomOpen() {
        for (int i=12;i<19;i++){
            Random random = new Random();
            int randomOtherTwo = random.nextInt(9);
            int randomOtherThree = random.nextInt(9);
            int randomOtherFour = random.nextInt(9);
            int randomOtherFife = random.nextInt(9);
            for (int j=0;j<9;j++){
                int randomOtherOne = random.nextInt(7);
                    if (j!=randomOtherFour&&j!=randomOtherFife&&j!=randomOtherThree&&j!=randomOtherTwo){
                        switch (randomOtherOne){
                            case 0:
                                cells.add(new Cell(i,j,Tetris.L));
                                break;
                            case 1:
                                cells.add(new Cell(i,j,Tetris.I));
                                break;
                            case 2:
                                cells.add(new Cell(i,j,Tetris.J));
                                break;
                            case 3:
                                cells.add(new Cell(i,j,Tetris.S));
                                break;
                            case 4:
                                cells.add(new Cell(i,j,Tetris.Z));
                                break;
                            case 5:
                                cells.add(new Cell(i,j,Tetris.T));
                                break;
                            case 6:
                                cells.add(new Cell(i,j,Tetris.L));
                                break;
                            default:
                        }
                }
            }
        }
    }
}

    private void paintRandomOpen(Graphics g) {
            for (Cell cell:randomOpen.cells
            ) {
                int y = cell.getRow()*CELL_SIZE;
                int x = cell.getCol()*CELL_SIZE;
                g.drawImage(cell.getCellImage(),x,y,null);
                int row = cell.getRow();
                int col = cell.getCol();
                wall[row][col] =cell;
            }
        }

③加入开局随机产生方块,即在每个四格方块定义处使用随机数Random;

public I() {
        Random random = new Random();
        int r = random.nextInt(7);
        while (r==0){
            r=random.nextInt(7);
        }
        getCells()[0] = new Cell(0,r,Tetris.I);
        getCells()[1] = new Cell(0,r-1,Tetris.I);
        getCells()[2] = new Cell(0,r+1,Tetris.I);
        getCells()[3] = new Cell(0,r+2,Tetris.I);
        }

④设置游戏难度,添加按键改变方块下降速度,根据不同下架速度匹配不同的消除每行分数也不同;

//变量
int[] scores_pool = {0,1,2,5,10};
public static final int KUNNAN = 2;
public static final int PUTONG = 1;
public static final int JIANDAN = 0;
private int game_diffcuty;
String[] show_diffcuty = {"SPEED: ★ ","SPEED: ★ ★ ★","SPEED: ★ ★ ★ ★ ★"};
int [] randomDiffcuty = {1,3,6};
int [] speedDiffcuty = {6,4,2};
//绘制
    private void paintDiffcuty(Graphics g) {
        if(game_diffcuty == JIANDAN){
            Font font =new Font(Font.SANS_SERIF,Font.BOLD,20);
            g.setFont(font);
            g.drawString(show_diffcuty[game_diffcuty],10,480);
        }else if (game_diffcuty == PUTONG){
            Font font =new Font(Font.SANS_SERIF,Font.BOLD,20);
            g.setFont(font);
            g.drawString(show_diffcuty[game_diffcuty],10,480);
        }else if (game_diffcuty == KUNNAN){
            Font font =new Font(Font.SANS_SERIF,Font.BOLD,20);
            g.setFont(font);
            g.drawString(show_diffcuty[game_diffcuty],10,480);
        }
    }
    //在监听器中添加按键
    case KeyEvent.VK_C:
                        if (game_state !=PLAYING) {
                            Random random = new Random();
                            int diff = random.nextInt(3);
                            game_diffcuty = diff;
                        }
                         break;
    //方块下降速度设置
    while (true){
            if (game_state==PLAYING){
                int tab = speedDiffcuty[game_diffcuty];
                try {
                    Thread.sleep(100*tab);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (canDrop()){
                    currentOne.softDrop();
                }else{
                    landTOowall();
                    destroyLine();
                    if (isGameOver()){
                        game_state = GAMEOVER;
                        if (max<totalScore){
                            max=totalScore;
                            writeMax();
                        }
                    }else{
                        currentOne = nextOne;
                        nextOne = Tetromino.randomOne();
                    }
                }
            }
            repaint();
        }

让我们玩一下

开局吧!!!
绿色为按键提醒,黄色为最高记录,蓝色为最高分,左侧方块为即将出现的方块,游戏页面下方为游戏速度等级
目前1⭐难度,按O【Open】开始!!!

结束1分记录最高记录
退出窗口,重新进入,读取之前记录,再玩一把五星⭐⭐⭐⭐⭐刷新记录
在这里插入图片描述

第一个练手小游戏还有很多要学,冲冲冲。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值