📄 bubblebreakercanvas.java
字号:
package org.bubblebreaker.view;
import org.bubblebreaker.model.*;
import org.bubblebreaker.config.*;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import java.io.IOException;
public class BubbleBreakerCanvas extends GameCanvas{
int cRows= 10;
int cColumns = 10;
int nColors= 5;
int colWidth = 18;
int rowHeight = 18;
boolean keeprunning = true;
Model m_model;
BlinkBall ballBlinker;
public BubbleBreakerCanvas() {
super(false);//check later
}
public void start() {
Ball.createBalls(Settings.getBallsType());
rowHeight=colWidth=Ball.REDBALL.getDiameter();
nColors=Ball.balls.length;
cColumns = (getWidth())/colWidth; //if last column is 75% of ball width we take it
cRows = getHeight()/rowHeight;
m_model = Model.createModel(cRows,cColumns,nColors, Settings.getModelBuilder());
Graphics g=getGraphics();
if(ballBlinker != null)
{
ballBlinker.stop();
}
ballBlinker =new BlinkBall(g,m_model);
updateGameScreen(g, true);
}
void drawBall(Graphics g, int row, int col, Ball ball)
{
ball.draw(g,row,col,rowHeight,colWidth);
}
void updateGameScreen(Graphics g, boolean all) {
if(all)
{
//create the background
g.setColor(Color.GREY.getCode());
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.WHITE.getCode());
g.fillRect(0,0,colWidth*cColumns,rowHeight*cRows);
//draw balls
for(int row=0;row<cRows;row++)
for(int col=0;col<cColumns;col++)
{
drawBall(g, row, col, m_model.currentBoard.data[row][col]);
}
}
//write score and time
long timeElapsedMillis=System.currentTimeMillis() - m_model.startTimeInMillis;
System.out.println("Time elapsed : "+timeElapsedMillis);
String timeBuf;
//ms
timeBuf=""+timeElapsedMillis%1000;
timeElapsedMillis/=1000;
//seconds, minutes, hours
for(int k=0;k<3;k++)
{
if(timeElapsedMillis==0)
{
break;
}
timeBuf=timeElapsedMillis%60+":"+timeBuf;
timeElapsedMillis/=60;
}
//days
if(timeElapsedMillis!=0)
{
timeBuf=timeElapsedMillis+":"+timeBuf;
}
setTitle("Bubble Breaker ("+m_model.getScore()+") "+timeBuf);
/*
g.setColor(Color.BLACK.getCode());
System.out.println("writing score at "+colWidth/2+","+rowHeight/2);
Font font=g.getFont();
font = Font.getFont(font.getFace(),Font.STYLE_BOLD,Font.SIZE_LARGE);
g.setFont(font);
g.drawString(""+m_model.getScore(),5,5,g.LEFT|g.TOP);
*/
if(m_model.isGameEnded())
{
ballBlinker.stop();
Font font=g.getFont();
font = Font.getFont(font.getFace(),Font.STYLE_BOLD,Font.SIZE_LARGE);
g.setFont(font);
g.drawString("Final Score : "+m_model.getScore(),getWidth()/2-40,getHeight()/2-10,g.LEFT|g.TOP);
}
flushGraphics();
}
public final static int UP =1;
public final static int DOWN =2;
public final static int LEFT =3;
public final static int RIGHT =4;
public void keyPressed(int keyCode) {
if(m_model.isGameEnded())
return;
Graphics g= getGraphics();
System.out.println("key pressed : "+keyCode);
//g.setColor(Color.BLACK.getCode());
//g.drawString("key pressed : "+keyCode,colWidth/2,0,g.LEFT|g.TOP);
boolean alreadyInFiredState=false;
if(null!=m_model.currentBoard.selectedCells)
{
alreadyInFiredState=true;
}
switch (getGameAction(keyCode)) {
case Canvas.UP:
m_model.move(UP);
break;
case Canvas.DOWN:
m_model.move(DOWN);
break;
case Canvas.LEFT:
m_model.move(LEFT);
break;
case Canvas.RIGHT:
m_model.move(RIGHT);
break;
case Canvas.FIRE:
if(alreadyInFiredState)
{
m_model.deleteCells();
updateGameScreen(getGraphics(),true);
playMusic();
}else
{
m_model.fire();
}
break;
case 0:
// There is no game action.. Use keypad constants instead
switch (keyCode) {
case Canvas.KEY_NUM2:
m_model.move(UP);
break;
case Canvas.KEY_NUM8:
m_model.move(DOWN);
break;
case Canvas.KEY_NUM4:
m_model.move(LEFT);
break;
case Canvas.KEY_NUM6:
m_model.move(RIGHT);
break;
case Canvas.KEY_NUM5:
if(alreadyInFiredState)
{
m_model.deleteCells();
updateGameScreen(getGraphics(),true);
playMusic();
}else
{
m_model.fire();
}
break;
}
break;
}
System.out.println(""+m_model.currentBoard.currentCell.row+","+m_model.currentBoard.currentCell.col);
ballBlinker.restart();
updateGameScreen(getGraphics(), false);
System.out.println("done");
}
private void playMusic()
{
if(Settings.isMusicOff())
{
return;
}
try {
for(int i=60; i < 70; i+=2){
Manager.playTone(i, 50, 100);
}
} catch (MediaException me) { }
}
int m_level=5;
public void setLevel(int level)
{
m_level=level;
/*
cRows=10*(m_level/5) + m_level%5;
cColumns=cRows;
*/
System.out.println("new level : "+m_level+","+cRows+","+cColumns);
}
public int getLevel()
{
return m_level;
}
public void undo()
{
m_model.undo();
updateGameScreen(getGraphics(),true);
}
public void changeBall()
{
m_model.changeBall();
updateGameScreen(getGraphics(),true);
}
class BlinkBall extends java.util.TimerTask{
java.util.Timer timer;
Graphics g;
Model model;
boolean blinkStateShow=false;
java.util.Vector cellsToRedraw=new java.util.Vector();
public BlinkBall(Graphics g, Model model)
{
this.g=g;
this.model=model;
blinkStateShow=false;
timer=new java.util.Timer();
timer.scheduleAtFixedRate(this,0,1500);
}
public void stop()
{
if(null!=timer)
timer.cancel();
redrawCells();
}
public void redrawCells()
{
int size=cellsToRedraw.size();
System.out.println("Redrawing "+size+" cells");
for(int i=0;i<size;i++)
{
Cell cell = (Cell)cellsToRedraw.elementAt(i);
drawBall(g, cell.row, cell.col, model.currentBoard.data[cell.row][cell.col]);
}
cellsToRedraw.removeAllElements();
}
void blinkBall(Cell cell)
{
if(blinkStateShow)
{
model.currentBoard.data[cell.row][cell.col].draw(g,cell.row,cell.col);
cellsToRedraw.removeElement(cell);
}else
{
Ball.FUNNYBALL.draw(g,cell.row,cell.col);
/*g.setColor(Color.WHITE.getCode());
g.fillRect(
cell.col*colWidth,
cell.row*rowHeight,
Ball.REDBALL.getDiameter(), Ball.REDBALL.getDiameter());
*/
cellsToRedraw.addElement(cell);
}
}
public void restart()
{
blinkStateShow=false;
run();
}
public void run()
{
redrawCells();
if(model.currentBoard.selectedCells!=null)
{
int size=model.currentBoard.selectedCells.length;
for(int i=0;i<size;i++)
{
Cell cell=model.currentBoard.selectedCells[i];
blinkBall(cell);
}
}else
{
Cell cell=model.currentBoard.currentCell;
blinkBall(cell);
}
flushGraphics();
blinkStateShow=!blinkStateShow;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -