⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 play.java~1~

📁 俄罗斯方块功能
💻 JAVA~1~
字号:
package src;

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;

public class Play extends GameCanvas implements Runnable{
    private Graphics g;
    private boolean Pstate;
    private boolean press=false;         //避免连续落下
    private int presscount=0;         //避免连续落下
    private int drawX,drawY;
    private Random rand;
    private Font font;
    private int[] shapenumber;           //需要画图的方块数组
    private int[] shapenumberN;           //下一个出现的方块数组
    private Shape shape;                 //产生方块的对象
    private Shape shapeN;                 //产生下一个方块的对象
    private int angle=0;                //旋转的角度
    private boolean next=false;        //判断是否产生新的方块
    private int speed=1;               //方块下落的速度
    private boolean hit=false;        //判断是否碰到方块
    private boolean fullline=false;   //判断是否碰到方块
    private int count=0;               //消去的行数
    private int score=0;
    private int tmp;
    private int tmpX;
    private boolean direction=false;
    private boolean anglehit=false;
    private Displayable upinstance;
    private int[][] map={{0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0}};
    public Play(Displayable instance){
        super(false);
        g=this.getGraphics();
        this.upinstance=instance;
        shape=new Shape();
        shapeN=new Shape();
        rand=new Random();
        font=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_SMALL);
        g.setFont(font);
        this.setFullScreenMode(true);
        g.setColor(36,36,36);
        g.fillRect(0,0,this.getWidth(),this.getHeight());
        g.setColor(230,230,230);
        this.flushGraphics();
        g.translate(0,4);
        g.setClip(0,0,129,121);
        init();
        Thread thread=new Thread(this);
        thread.start();
    }

    public void init(){
        Pstate=true;
        drawX=48;
        drawY=50;
        next=true;
        shapeN.setnumber(createRandom(7));
        shapeN.setangle(0);
    }

    public void run(){
        while(Pstate){
            g.setColor(36,36,36);
            g.fillRect(0,0,128,121);
            g.setColor(230,230,230);
            g.drawRect(0,0,104,120);
            g.setColor(219,219,0);
            g.drawRect(0,0,128,120);
            if(score>300&&score<=600)
                speed=2;
            else if(score>600&&score<=1000)
                speed=3;
            else if(score>1000&&score<=1500)
                speed=4;
            else if(score>1500&&score<=2000)
                speed=5;
            else if(score>2000&&score<=2500)
                speed=6;
            else if(score>2500&&score<=3000)
                speed=7;
            else if(score>3000&&score<=3500)
                speed=8;
            else if(score>3500)
                speed=9;
            if(next){
                shape.setnumber(shapeN.getnumber());
                shape.setangle(0);
                shapeN.setnumber(createRandom(7));
                shapeN.setangle(0);
                shapenumberN=shapeN.getshape();
                hit=false;
                drawX=48;
                drawY=0;
                next=false;
            }
            if(press){
                if(direction){
                    if(presscount<1)
                        presscount++;
                    else{
                        presscount=0;
                        press=false;
                    }
                }else{
                    if(presscount<2)
                        presscount++;
                    else{
                        presscount=0;
                        press=false;
                    }
                }
            }
            direction=false;
            keypress();
            shape.setangle(angle);
            shapenumber=shape.getshape();
            if(checkcollision()){
                next=true;
                for(int i=0;i<4;i++){
                    map[drawY/8+shapenumber[i]/4][drawX/8+shapenumber[i]%4]=1;
                }
            }else{
                if(speed>6)
                    drawY=drawY+speed-5;
                else
                    drawY=drawY+1;
            }
            for(int i=0;i<4;i++){
                drawshape(drawX+shapenumber[i]%4*8,drawY+shapenumber[i]/4*8);
            }
            for(int i=0;i<4;i++){
                drawshapeN(106+shapenumberN[i]%4*5,20+shapenumberN[i]/4*5);
            }
            for(int i=0;i<15;i++)
                for(int j=0;j<13;j++){
                    if(map[i][j]==1)
                        drawshape(j*8,i*8);
                }
            checkfullline();
            try{
                if(speed>6)
                    Thread.sleep(25);
                else{
                    switch(speed){
                    case 1:
                        Thread.sleep(15);
                        break;
                    case 2:
                        Thread.sleep(35);
                        break;
                    case 3:
                        Thread.sleep(30);
                        break;
                    case 4:
                        Thread.sleep(25);
                        break;
                    case 5:
                        Thread.sleep(20);
                        break;
                    case 6:
                        Thread.sleep(15);
                        break;
                    }
                }
            }catch(Exception e){
                System.out.println("sleep error");
            }
            g.drawString("难度:",106,115-4*font.getHeight()-15,Graphics.LEFT|Graphics.TOP);
            g.drawString(String.valueOf(speed),106,115-3*font.getHeight()-10,Graphics.LEFT|Graphics.TOP);
            g.drawString("得分:",106,115-2*font.getHeight()-5,Graphics.LEFT|Graphics.TOP);
            g.drawString(String.valueOf(score),106,115-font.getHeight(),Graphics.LEFT|Graphics.TOP);
            for(int i=0;i<13;i++){
                if(map[0][i]==1){
                    Pstate=false;
                    break;
                }
            }
            this.flushGraphics();
            hit=false;
            if(!Pstate){
                try{
                    Thread.sleep(500);
                }catch(Exception e){
                    System.out.println("sleep error");
                }
                Alert a=new Alert("提示信息");
                a.setString("Game Over");
                a.setTimeout(2000);
                Display.getDisplay(BlockMidlet.instance).setCurrent(a,upinstance);
            }
            this.flushGraphics();
        }
    }

    public void drawshape(int x,int y){       //画一个方块的方法
        g.setColor(215,0,0);
        g.fillRect(x,y,8,8);
        g.setColor(230,230,230);
        g.drawRect(x,y,8,8);
    }

    public void drawshapeN(int x,int y){       //画一个方块的方法(预览图)
        g.setColor(215,0,0);
        g.fillRect(x,y,5,5);
        g.setColor(230,230,230);
        g.drawRect(x,y,5,5);
    }


    public boolean checkcollision(){                 //检测碰撞
        for(int i=0;i<4;i++){                        //检测是否碰到底
            if(drawY+shapenumber[i]/4*8>=112)
                return true;
        }
        for(int i=0;i<4;i++){                       //检测是否碰到方块
            if(map[(drawY+8)/8+shapenumber[i]/4][drawX/8+shapenumber[i]%4]==1)
                return true;
        }
        return false;
    }

    public int checkfullline(){            //检测是否有满行并消除满行
        count=0;
        for(int i=0;i<15;i++){
            fullline=true;
            for(int j=0;j<13;j++){
                if(map[i][j]!=1){
                    fullline=false;
                    break;
                }
            }
            if(fullline){
                for(int j=0;j<13;j++)
                    map[0][j]=0;
                for(int j=i;j>0;j--)
                    for(int k=0;k<13;k++){
                        map[j][k]=map[j-1][k];
                    }
                count++;
            }
        }
        switch(count){
        case 1:
            score=score+10;
            break;
        case 2:
            score=score+30;
            break;
        case 3:
            score=score+60;
            break;
        case 4:
            score=score+120;
            break;
        }
        return 0;
    }

    public int createRandom(int number){          //产生随机数
        int returnvalue=Math.abs(rand.nextInt()%number);
        return returnvalue;
    }

    public void keypress(){
        int keystate=this.getKeyStates();
        if((keystate&this.UP_PRESSED)!=0){
            if(!press&&presscount==0){
                if(!checkangle()){
                    if(angle<3)
                        angle++;
                    else
                        angle=0;
                }
                press=true;
            }
        }
        else if((keystate&this.LEFT_PRESSED)!=0){
            if(!press&&presscount==0){
                direction=true;
                for(int j=0;j<4;j++){
                    if((drawX/8+shapenumber[j]%4)==0){
                        hit=true;
                    }
                }
                if(!hit){                        //判断是否碰到方块
                    for(int i=0;i<4;i++){
                        if (map[drawY/8+shapenumber[i]/4][drawX/8+shapenumber[i]%4-1]==1){
                            hit=true;
                            break;
                        }
                    }
                    if(hit==false)
                        drawX=drawX-8;
                }
                press=true;
            }
        }
        else if((keystate&this.RIGHT_PRESSED)!=0){
            direction=true;
            if(!press&&presscount==0){
                for(int j=0;j<4;j++){
                    if((drawX/8+shapenumber[j]%4)==12){
                        hit=true;
                    }
                }
                if(!hit){                    //判断是否碰到方块
                    for(int i=0;i<4;i++){
                        if (map[drawY/8+shapenumber[i]/4][drawX/8+shapenumber[i]%4+1]==1){
                            hit=true;
                            break;
                        }
                    }
                    if(hit==false)
                        drawX=drawX+8;
                }
                press=true;
            }
        }
        else if((keystate&this.DOWN_PRESSED)!=0){
            if(!press&&presscount==0){
                while(!checkcollision()){
                    drawY=drawY+2;
                }
                press=true;
            }
        }
    }

    public boolean checkangle(){         //false为可以旋转
        tmp=angle;
        tmpX=drawX;
        if(tmp<3)
            tmp++;
        else
            tmp=0;
        shape.setangle(tmp);
        shapenumber=shape.getshape();
        for(int i=0;i<4;i++){
            if(!checkhit(tmpX+i*8,shapenumber)){
                drawX=tmpX+i*8;
                return false;
            }
            if(!checkhit(tmpX-i*8,shapenumber)){
                drawX=tmpX-i*8;
                return false;
            }
        }
        return true;
    }

    public boolean checkhit(int drawX,int[] shapenumber){
        for(int j=0;j<4;j++){
            if((drawX/8+shapenumber[j]%4)<0){
                return true;
            }
        }
        for(int j=0;j<4;j++){
            if((drawX/8+shapenumber[j]%4)>12){
                return true;
            }
        }
        for(int i=0;i<4;i++){                       //检测是否碰到方块
            if(map[(drawY+8)/8+shapenumber[i]/4][drawX/8+shapenumber[i]%4]==1)
                return true;
        }
        return false;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -