📄 gamescreen.java
字号:
//import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import java.io.IOException;
import javax.microedition.lcdui.Font;
import java.lang.Math;
import java.util.Random;
import com.nokia.mid.ui.FullCanvas;
public class GameScreen extends FullCanvas implements Runnable
{
Font font=Font.getDefaultFont();
Graphics g;
private BeatBrick theMidlet;
private static final int MAX_CPS=50;
private static final int MS_PER_FRAME=1000/MAX_CPS;
private Image offScreenBuffer;
private Random random;
private Random p;
private Image bricks[];
private Image pills[];
private Image ball;
private Image bat;
private Image Lbat;
private Image Sbat;
final int numlines=7;
final int numpills=9;
final int kindspill=4;
final int brickwidth=16;
final int brickheight=10;
final int scoreheight=font.getHeight();
final int batheight=8;
int batwidth=25;//宽度要随着挡板大小的变化而变化
final int pillwidth=10;
final int pillheight=5;
final int startline=20;
final int ballsize=4;
final int screenHeight=getHeight();
final int screenWidth=getWidth();
final int baty=screenHeight-scoreheight-batheight;
final int lastlevel=5;
int randnum;
int bricksperline=8;
int batx;
int ballx;
int bally;
int pillx[];
int pilly[];
int pilldy;
int batdpos=0;
int balldx=0, balldy=0;
int step;
int ballsleft;
int score;
int level;
boolean ingame=false;
boolean showbrick[][];
boolean pill[][][];
boolean can[];
boolean goon=false;
boolean nobricks;
boolean newlevel=true;
boolean normal=true;
boolean large=false;
boolean small=false;
public GameScreen(BeatBrick midlet)
{
theMidlet=midlet;
init();
Thread t=new Thread(this);
t.start();
}
private void init()
{
offScreenBuffer=Image.createImage(screenWidth,screenHeight);
try{
bricks=new Image[numlines];
for(int i=0;i<numlines;i++)
bricks[i]=Image.createImage("/b"+i+".png");
pills=new Image[kindspill];
for(int i=0;i<kindspill;i++)
pills[i]=Image.createImage("/p"+i+".png");
ball=Image.createImage("/ball.png");
bat=Image.createImage("/bat.png");
Lbat=Image.createImage("/Lbat.png");
Sbat=Image.createImage("/Sbat.png");
}
catch(IOException ioe){
System.out.println("unable to load image");
}
score=0;
ballsleft=3;
level=1;
initGame();
}
public void initGame()
{
step=3;
batdpos=0;
batx=(screenWidth-batwidth)/2;
Random random=new Random();
balldx=Math.abs(random.nextInt()%3+1);
balldy=-step;
ballx=batx+(batwidth-ballsize)/2;
bally=screenHeight-scoreheight-batheight-2*ballsize;
pill=new boolean[numpills][numlines][bricksperline];
for(int i=0;i<numpills;i++)
for(int j=0;j<numlines;j++)
for(int k=0;k<bricksperline;k++)
pill[i][j][k]=false;
pill[0][1][4]=true;
pill[1][1][0]=true;
pill[2][2][7]=true;
pill[3][3][2]=true;
pill[4][4][6]=true;
pill[5][5][1]=true;
pill[6][5][4]=true;
pill[7][6][2]=true;
pill[8][6][7]=true;
pillx=new int[numpills];
pilly=new int[numpills];
for(int i=0;i<numpills;i++)
for(int j=0;j<numlines;j++)
for(int k=0;k<bricksperline;k++)
if(pill[i][j][k]){
pillx[i]=k*brickwidth;
pilly[i]=startline+j*brickheight;
}
can=new boolean[numpills];
for(int i=0;i<numpills;i++)
can[i]=false;
initBricks();
}
public void initBricks()
{
bricksperline=screenWidth/brickwidth;
showbrick=new boolean[numlines][bricksperline];
for(int i=0;i<numlines;i++)
for(int j=0;j<bricksperline;j++)
showbrick[i][j]=true;
CheckLevel();
}
protected void paint(Graphics graphics)
{
renderWorld();
graphics.drawImage(offScreenBuffer,0,0,Graphics.LEFT|Graphics.TOP);
}
private void renderWorld()
{
Graphics g=offScreenBuffer.getGraphics();
String s;
g.setColor(255,153,204);
g.fillRect(0,0,screenWidth,screenHeight);
nobricks=true;
for(int i=0;i<numlines;i++)
for(int j=0;j<bricksperline;j++)
if(showbrick[i][j]){
nobricks=false;
g.drawImage(bricks[i],j*brickwidth,startline+i*brickheight,Graphics.LEFT|Graphics.TOP);
}
//第四关比后两关简单,只要最下两排掉药丸
if(can[0]){g.drawImage(pills[3],pillx[0],pilly[0],Graphics.LEFT|Graphics.TOP);can[0]=false;}
if(can[1]){g.drawImage(pills[2],pillx[1],pilly[1],Graphics.LEFT|Graphics.TOP);can[1]=false;}
if(can[2]){g.drawImage(pills[3],pillx[2],pilly[2],Graphics.LEFT|Graphics.TOP);can[2]=false;}
if(can[3]){g.drawImage(pills[2],pillx[3],pilly[3],Graphics.LEFT|Graphics.TOP);can[3]=false;}
if(can[4]){g.drawImage(pills[2],pillx[4],pilly[4],Graphics.LEFT|Graphics.TOP);can[4]=false;}
if(can[5]){g.drawImage(pills[0],pillx[5],pilly[5],Graphics.LEFT|Graphics.TOP);can[5]=false;}
if(can[6]){g.drawImage(pills[1],pillx[6],pilly[6],Graphics.LEFT|Graphics.TOP);can[6]=false;}
if(can[7]){g.drawImage(pills[1],pillx[7],pilly[7],Graphics.LEFT|Graphics.TOP);can[7]=false;}
if(can[8]){g.drawImage(pills[0],pillx[8],pilly[8],Graphics.LEFT|Graphics.TOP);can[8]=false;}
g.drawImage(ball,ballx,bally,Graphics.LEFT|Graphics.TOP);
if(large) g.drawImage(Lbat,batx,baty,Graphics.LEFT|Graphics.TOP);
if(small) g.drawImage(Sbat,batx,baty,Graphics.LEFT|Graphics.TOP);
if(normal)g.drawImage(bat,batx,baty,Graphics.LEFT|Graphics.TOP);
g.setFont(font);
g.setColor(255,255,255);
s="得分: "+score;
g.drawString(s,10,screenHeight-scoreheight,Graphics.LEFT|Graphics.TOP);
s="球数: "+ballsleft;
g.drawString(s,screenWidth-10-font.stringWidth(s),screenHeight-scoreheight,Graphics.LEFT|Graphics.TOP);
if(newlevel&&!goon){
switch(level){
case(1):{s="LEVEL 1";break;}
case(2):{s="LEVEL 2";break;}
case(3):{s="LEVEL 3";break;}
case(4):{s="LEVEL 4";break;}
case(5):{s="LEVEL 5";break;}
case(6):{s="LEVEL 6";break;}
}
g.drawString(s,(screenWidth-font.stringWidth(s))/2,(screenHeight-scoreheight)/2,Graphics.LEFT|Graphics.TOP);
}
GetReady();
if (nobricks){
goon=false;
if(ingame){
if(level==lastlevel)//注意:判断相等的应在前面。如果先level++了,后面自然会相等。
theMidlet.activateGameOver(ingame,level,score);
if(level<lastlevel){
level++;
newlevel=true;
GetReady();
score+=100;
}
}
initGame();//如果在前面执行了,后面又gameOver(),那么执行就没有意义了。
}
}
public void GetReady()
{
if(!goon)return;
newlevel=false;
PlayGame();
}
public void PlayGame()
{
MoveBall();
CheckBat();
CheckBounce();
CheckBricks();
CheckPill();
}
protected void keyPressed(int keyCode)
{
if(ingame){
switch(getGameAction(keyCode)){
case LEFT:batdpos=-4;break;
case RIGHT:batdpos=4;break;
case UP:goon=true;break;
}
}
else{
if(getGameAction(keyCode)==FIRE){
ingame=true;
init();
}
}
}
protected void keyReleased(int keyCode)
{
if(getGameAction(keyCode)==LEFT||getGameAction(keyCode)==RIGHT)
batdpos=0;
}
public void CheckLevel()
{
switch(level){
case(1):{ for(int j=0;j<bricksperline;j++){
showbrick[1][j]=false;
showbrick[3][j]=false;
showbrick[5][j]=false;
}break;
}
case(2):{ int k=1;
for(int i=1;i<numlines;i++){
for(int j=0;j<k;j++)
showbrick[i][j]=false;
k++;
}break;
}
default:}
}
public void CheckPill()
{
for(int i=0;i<numpills;i++)
for(int j=0;j<numlines;j++)
for(int k=0;k<bricksperline;k++)
if(pill[i][j][k])
if(!showbrick[j][k]){
if(pilly[i]<=screenHeight)pilly[i]+=2;
if(level==4)
if(j>4){
if((pilly[i]<baty+batheight/2 && pillx[i]<=batx+batwidth && pillx[i]>=batx-pillwidth)||!(pillx[i]<=batx+batwidth && pillx[i]>=batx-pillwidth))
can[i]=true;
if(pillx[i]<=batx+batwidth && pillx[i]>=batx-pillwidth && pilly[i]>=baty-pillheight && pilly[i]<=baty+batheight/2)
switch(i){
case 5:{large=true;batwidth=33;normal=false;small=false;break;}
case 6:{small=true;batwidth=21;normal=false;large=false;break;}
case 7:{small=true;batwidth=21;normal=false;large=false;break;}
case 8:{large=true;batwidth=33;normal=false;small=false;break;}
}
}
if(level==5||level==6){
if((pilly[i]<baty+batheight/2 && pillx[i]<=batx+batwidth && pillx[i]>=batx-pillwidth)||!(pillx[i]<=batx+batwidth && pillx[i]>=batx-pillwidth))
can[i]=true;
if(pillx[i]<=batx+batwidth && pillx[i]>=batx-pillwidth && pilly[i]>=baty-pillheight && pilly[i]<=baty+batheight/2)
switch(i){
case 0:{ballsleft+=1;break;}
case 1:{score+=20;break;}
case 2:{ballsleft+=1;break;}
case 3:{score+=20;break;}
case 4:{score+=20;break;}
case 5:{large=true;batwidth=33;normal=false;small=false;break;}
case 6:{small=true;batwidth=21;normal=false;large=false;break;}
case 7:{small=true;batwidth=21;normal=false;large=false;break;}
case 8:{large=true;batwidth=33;normal=false;small=false;break;}
}
}
}
if(ballsleft>5 && ballsleft<=8) ballsleft-=4;
if(ballsleft>=10 && ballsleft<=13) ballsleft-=8;
}
public void MoveBall()
{
ballx+=balldx;
bally+=balldy;
if (bally<=0){
balldy=-balldy;
bally=0;
}
if (bally>=baty){
batx=(screenWidth-batwidth)/2;
ballx=batx+(batwidth-ballsize)/2;
bally=baty;
balldy=step;
balldx=0;
if(ingame)
{
ballsleft--;
if(ballsleft<=0){
ingame=false;
theMidlet.activateGameOver(ingame,level,score);
}
goon=false;
}
}
if (ballx>=(screenWidth-ballsize)){
balldx=-balldx;
ballx=screenWidth-ballsize;
}
if (ballx<=0){
balldx=-balldx;
ballx=0;
}
}
public void CheckBat()
{
batx+=batdpos;
if (batx<0)
batx=0;
else if (batx>(screenWidth-batwidth))
batx=screenWidth-batwidth;
}
public void CheckBounce()
{
if (bally>=baty-2*ballsize && bally<baty+batheight/2 &&
ballx>=batx-2*ballsize && ballx<=batx+batwidth)
{
bally=baty-2*ballsize;
balldy=-step;
Bounce();
}
}
public void Bounce()
{
if(batx+ballsize<=ballx&&ballx<=batx+batwidth-ballsize){
if(batdpos!=0){
if(balldx*batdpos>0){
balldx=3/2*balldx;
balldy=3/2*balldy;
}
if(balldx*batdpos<0){
balldx=batdpos/2;
}
}
if(batdpos==0){
if(balldx==0){
Random random=new Random();
randnum=Math.abs(random.nextInt()%3+1);
if (randnum>2)
balldx=randnum;
else
balldx=-randnum;
balldx=1*balldx;
}
}
}
if((ballx<batx+ballsize)||(ballx>batx+batwidth-ballsize)){
balldx=3/2*balldx;
}
}
public void CheckBricks()
{
int i,j,x,y;
int xspeed=balldx;
if (xspeed<0) xspeed=-xspeed;
int ydir=balldy;
int count[];
count=new int[bricksperline];
for(int k=0;k<bricksperline;k++)
count[k]=0;
if (bally<(startline-ballsize) || bally>(startline+numlines*(brickheight)))
return;
for (j=0; j<numlines; j++){
for (i=0; i<bricksperline; i++){
if (showbrick[j][i]){
y=startline+j*(brickheight);
x=i*(brickwidth);
if (bally>=(y-ballsize) && bally<(y+brickheight) &&
ballx>=(x-ballsize) && ballx<(x+brickwidth)){
/*if(level==lastlevel){
if(j==6){
count[i]++;
if(count[i]==2)showbrick[6][i]=false;
}
else showbrick[j][i]=false;
}
else*/
showbrick[j][i]=false;
if (ingame)
score+=(numlines-j);// 敲击砖块的地点
if (ballx>=(x-ballsize) && ballx<=(x-ballsize+3)){ // 左边
balldx=-xspeed;
}
else if (ballx<=(x+brickwidth-1) && ballx>=(x+brickwidth-4)){ // 右边
balldx=xspeed;
}
balldy=-ydir;
}
}
}
}
}
public void run()
{
long starttime;
while(true){
starttime=System.currentTimeMillis();
try{
repaint();
starttime+=20;
Thread.sleep(Math.max(0,starttime-System.currentTimeMillis()));
}
catch(InterruptedException e){break;}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -