|
楼主 |
发表于 2016-8-10 10:25:49
|
显示全部楼层
咦没人用lejos吗。。。。为啥这么冷清勒。。。。算了我直接把代码贴出来
首先是主类Russian.java
控制总的游戏进程
- package ro;
- import lejos.nxt.Button;
- import lejos.nxt.Button.*;
- public class Russian {
-
- static GameStatue gs = new GameStatue();
-
- MyPanel mp = new MyPanel(gs);
-
- public Russian(){
-
- //Events
- Button.ENTER.addButtonListener(mp);
- Button.ESCAPE.addButtonListener(mp);
- Button.LEFT.addButtonListener(mp);
- Button.RIGHT.addButtonListener(mp);
-
- }
- private void gameStart(){
-
- }
-
- private void gameRun(){
- while(true){
- try{ //avoid overHeat
- Thread.sleep(1000);
- }catch(Exception e){
- e.printStackTrace();
- }
- while(gs.isGameRunning){
- try{ //wait the actions
- Thread.sleep(1000);
- }catch(Exception e){
- e.printStackTrace();
- }
-
- if(!gs.tryBlockDown()){
- gs.locateBlock();
- gs.tryRemoveRow();
- if(gs.isGameOver()){
- System.out.println("GAME OVER!!");
- gs.isGameRunning = false;
- return;
- }
- gs.refresh();
- }mp.paint();
-
- }
- }
- }
-
- private void gameEnd(){
- Button.waitForAnyPress(5000);
- }
-
- public static void main(String[] args) {
- Russian r = new Russian();
- r.gameStart();
- r.gameRun();
- r.gameEnd();
- }
- }
复制代码 然后是画图类MyPanel.java,实现在LCD上的动画效果- package ro;
- import lejos.nxt.Button;
- import lejos.nxt.ButtonListener;
- import lejos.nxt.LCD;
- import javax.microedition.lcdui.*;
- public class MyPanel implements ButtonListener{
-
- GameStatue gs;
- Graphics g = new Graphics();
-
- public MyPanel(GameStatue g){ //set Game Statue
- gs = g;
- }
-
- public void paint(){ //Draw
-
- LCD.clear();//refresh
-
- g.setFont(Font.getFont(0,0,Font.SIZE_SMALL));
-
- //Standard Line
- g.drawLine(64, 0, 64, 64);
-
- //moving Blocks
- for(int iy=0; iy<4; iy++){
- for(int ix=0; ix<4; ix++){
- if(gs.currentMap[ix][iy]){
- g.fillRect((gs.blockX+ix)*8, (gs.blockY+iy-4)*8, 8,8);
- }
- }
- }
- //next Blocks
- g.drawString("NEXT:", 64+2, 16, Graphics.LEFT|Graphics.TOP);
- for(int iy=0; iy<4; iy++){
- for(int ix=0; ix<4; ix++){
- if(gs.nextMap[ix][iy]){
- g.fillRect((ix)*6+gs.WIDTH*8+2, (iy)*6+24+2, 6,6);
- }
- }
- }
-
-
- //stationary Blocks
- for(int iy=4; iy<gs.HEIGHT+4; iy++){
- for(int ix=0; ix<gs.WIDTH; ix++){
- if(gs.isBlockExist[ix][iy]){
- g.fillRect(ix*8,(iy-4)*8, 8,8);
- }
- }
- }
-
- //Display score & etc
- g.drawString("SCORE:", 64+2, 0, Graphics.LEFT|Graphics.TOP);
- g.drawString(String.valueOf(gs.score),64+2,8,Graphics.LEFT|Graphics.TOP);
- }
-
- @Override
- public void buttonPressed(Button b) {
-
- if(Button.readButtons() == Button.ID_LEFT+Button.ID_RIGHT){
- gs.gameStartOrPause();
- }else{
- switch(b.getId()){
- case Button.ID_LEFT:
- gs.tryBlockLeft();
- break;
- case Button.ID_RIGHT:
- gs.tryBlockRight();
- break;
- case Button.ID_ENTER:
- gs.tryBlockRotate();
- break;
- case Button.ID_ESCAPE:
- gs.tryBlockDown();
- break;
- default:
- }
- }
- paint();
-
- }
- @Override
- public void buttonReleased(Button b) {
- // TODO Auto-generated method stub
- }
- }
复制代码 再然后是控制类GameStatue, 存储游戏的大部分数据以及控制方块移动,旋转的方法
最后是存储方块形状的类Blocks,用十六位的int来代表4*4棋盘上的方块数据
- package ro;
- public class Blocks {
- //block version,rotation
- public static final int map[][] = {
- {0x06c0,0x0462,0x06c0,0x0462},//反之字
- {0x0c60,0x04c8,0x0c60,0x04c8},//之字
- {0x0660,0x0660,0x0660,0x0660},//正方形
- {0x08e0,0x0644,0x00e2,0x044c},//左手拐
- {0x02e0,0x0446,0x00e8,0x0c44},//右手拐
- {0x4444,0x00f0,0x4444,0x00f0},//长条
- {0x04e0,0x0464,0x00e4,0x04c4}//三角
- };
- }
复制代码 显而易见我在Java se上的代码没怎么改就直接搬过来了。嗯,所以说Java的可移植性比G语言强无限大倍(G语言的可移植性大概是零吧。。。)
想转行lejos Java 的快上车啊啊啊!!
|
|