本文介绍了请告诉我您是否认为我的代码有用.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class MainMIDlet extends MIDlet implements Runnable
{
Display display;
MainCanvas mainCanvas;
Thread thread;
public MainMIDlet()
{
display = Display.getDisplay(this);
mainCanvas = new MainCanvas(this);
thread = new Thread(this);
}
public void startApp()
{
try{
display.setCurrent(mainCanvas);
thread.start();
}catch(Exception e) {e.printStackTrace();}
}
public void run()
{
mainCanvas.DBricks();
while(!mainCanvas.gameover)
{
if(!mainCanvas.pause)
{
if(mainCanvas.startgame)
{
mainCanvas.gameCode();
mainCanvas.BrickBreak();
mainCanvas.repaint();
try{
Thread.sleep(mainCanvas.speed);//increase the speed of the ball by reducing the value.
}
catch(Exception e){}
}
else
{
mainCanvas.BallX=mainCanvas.BatX+25;mainCanvas.BallY=mainCanvas.BatY-mainCanvas.bsize;
mainCanvas.repaint();
try{Thread.sleep(mainCanvas.speed+mainCanvas.temp);}catch(Exception e){}
}
}
else
{
mainCanvas.repaint();
try{Thread.sleep(mainCanvas.speed+mainCanvas.temp);}catch(Exception e){}
}
}
while(true){
try{
Thread.sleep(150L);
//System.out.println("Continuous running");
mainCanvas.repaint();
}catch(Exception e) {}
}
}
public void pauseApp()
{
}
public void destroyApp(boolean uc)
{
notifyDestroyed();
}
public void exitApp()
{
destroyApp(false);
}
}
//
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
//import java.util.Vector;
import java.util.Random;
public class MainCanvas extends Canvas
{
/**
* MIDlet
*/
MainMIDlet midlet;
/**
* determines which key is being pressed
*/
int keyPress = 0;
//
//array to store the brick x,y and present bits of the brick.
public static int Bricks[][];
// varible for to chech the movement of the ball direction.
public static boolean BdirectionY=true;
public static boolean BdirectionX=true;
//variable to check gameover or not.
public static boolean gameover=false,startgame=false,pause=false;
//varibales for brick width and height.
public static int BWidth=30;
public static int BHeight=15;
//variable to specify the number of the bricks..
public static int size=23;
public int screenWidth=getWidth();
public int screenHeight=getHeight();
public static int BatX=120,BatY=280;
public static int BatW=50;
public static int bsize=10,life=3,score=0,win=0;
public static int temp=20;
public static int BallX=BatX+25,BallY=BatY-bsize;
public static int s=-5;
public static int speed=50;
int index=0;
private Image bomb;
private boolean bbool =false;
private int Bheight=140;
/**
* constructor having the MIDlet as the parameter
*/
public MainCanvas(MainMIDlet midlet)
{
this.midlet = midlet;
setFullScreenMode(true);
Bricks=new int [size][5];
try{
bomb = Image.createImage("/Bomb.png");
}catch (Exception e) {
// TODO: handle exception
}
}
/**
* paint Method which is an abstract method in Canvas
*/
public void paint(Graphics g)//
{
g.setColor(255,0,255);
g.fillRect(0,0,getWidth(),getHeight());
Ballmove(g);
if(bbool){
Bheight+=4;
g.drawImage(bomb, 70, Bheight, Graphics.TOP | Graphics.LEFT);
}
}
void BrickBreak()
{
// code for ball hitting bat.
if((BallX<=BatX+BatW&&BallX>=BatX&&BallY+bsize==BatY)||(BallX+bsize<=BatX+BatW&&BallX+bsize>=BatX&&BallY+bsize==BatY))
{
BdirectionY=true;
}
for(int i=0;i<size;i++)
{
//code for ball hitting the brick from bottom
if(BallX>=Bricks[i][0]&&BallX<=Bricks[i][0]+BWidth&&BallY==Bricks[i][1]+BHeight)
{
Bricks[i][0]=s;
Bricks[i][1]=s;
Bricks[i][2]=0;
Bricks[i][4]=0;
Bricks[i][3]=0;
if((Bricks[1][3]==0)&& bbool==false)
{
System.out.println("bomb");
bbool = true;
}
BdirectionY=false;
score+=10;
win+=1;
}
//code for ball hitting the brick from top
else if(BallX>=Bricks[i][0]&&BallX<=Bricks[i][0]+BWidth&&BallY+bsize==Bricks[i][1])
{
Bricks[i][0]=s;
Bricks[i][1]=s;
Bricks[i][2]=0;
Bricks[i][4]=0;
Bricks[i][3]=0;
BdirectionY=true;
score+=10;
win+=1;
}
//code for ball hitting the brick from left side.
else if((BallX+bsize==Bricks[i][0]&&BallY>=Bricks[i][1]&&BallY<=Bricks[i][1]+BHeight))
{
Bricks[i][0]=s;
Bricks[i][1]=s;
Bricks[i][2]=0;
Bricks[i][4]=0;
Bricks[i][3]=0;
BdirectionX=true;
score+=10;
win+=1;
}
//code for ballhitting the brick from right side
else if((BallX==Bricks[i][0]+BWidth&&BallY>=Bricks[i][1]&&BallY<=Bricks[i][1]+BHeight))
{
Bricks[i][0]=s;
Bricks[i][1]=s;
Bricks[i][2]=0;
Bricks[i][4]=0;
Bricks[i][3]=0;
BdirectionX=false;
score+=10;
win+=1;
}
}
if(win==size)
{
index =4;
}
}
void gameCode()
{
//code checking whether ball moving up or down
if(BdirectionY)
{
BallY-=5;
if(BallY<(temp+5)) //up
{
System.out.println("up collision");
BdirectionY=false;
}
}
else
{
if(Bheight>(screenHeight-50)&& bbool==true){
System.out.println("bomb collision");
bbool =false;
}
BallY+=5;
if(BallY>(screenHeight-((temp/3)-12)))
{
System.out.println("down collision");
BallY=BatY;
BallX=BatX+35;
BdirectionY=true;
startgame=false;
life-=1;
if(life==0)
{
index=2;
}
}
}
//code checking whether ball moving right ro left.
if(BdirectionX)
{
BallX-=5;
if(BallX<10)
{
BdirectionX=false;
}
}
else
{
BallX+=5;
if(BallX>(screenWidth-((temp/2)+10)))
{
BdirectionX=true;
}
}
}
void DBricks()
{
//System.out.println("Brick ");
int x=25;
int y=60;
//setting the brick positons.
for(int i=0;i<size;i++)
{
if(i==2)
{
y=20;
x=10;
}
/*if(i==13)
{
y-=temp;
x=(BWidth*2);
}
if(i==temp)
{
y=180;
x=80;
}*/
// if(i==12)
// {
// y-=temp;
// x=(BWidth*2);
// }
// if(i==15)
// {
// x=25+BWidth;
// y=160;
// }
// if(i==temp)
// {
// y=180;
// x=80;
// }
Bricks[i][0]=x;
Bricks[i][1]=y;
Bricks[i][2]=1;
Bricks[i][3]=BWidth;
Bricks[i][4]=BHeight;
x+=BWidth;
}
/*
//setting the brick positons.
for(int i=0;i<size;i++)
{
if(i==7)
{
y-=temp;
x=25+BWidth;
}
if(i==12)
{
y-=temp;
x=25+(BWidth*2);
}
if(i==15)
{
x=25+BWidth;
y=160;
}
if(i==temp)
{
y=180;
x=80;
}
Bricks[i][0]=x;
Bricks[i][1]=y;
Bricks[i][2]=1;
Bricks[i][3]=BWidth;
Bricks[i][4]=BHeight;
x+=BWidth;
}*/
}
void DrawBall(Graphics g)
{
g.setColor(255,0,0);
g.fillArc(BallX,BallY,bsize,bsize,0,360);
}
void DBat(Graphics g)
{
g.setColor(0,0,255);
g.fillRect(BatX,BatY,BatW,10);
}
//bat movements towards right.
void Rkeymove()
{
if(BatX<(getWidth()-((temp*3)+bsize)))
BatX+=15;
}
//bat movements towards left.
void Lkeymove()
{
if(BatX>(bsize)+5)
BatX-=15;
}
void DrawBricks(Graphics g)
{
for(int i=0;i<size;i++)
{
if(Bricks[i][2]==1)
{
g.setColor(0,0,0);
g.fillRect(Bricks[i][0],Bricks[i][1],Bricks[i][3]-2,Bricks[i][4]-2);
}
}
}
void gameOver(Graphics g)
{
// g.setColor(Color.red);
// g.fillRect(0,0,ScreenWidth,ScreenHeight);
g.setColor(0,0,255);
g.drawString("YOU LOSE",(screenWidth/3),screenHeight/2,Graphics.TOP | Graphics.HCENTER);
}
public void Ballmove(Graphics g)
{
DrawBall(g);
DBat(g);
DrawBricks(g);
g.drawRect((temp/7),20,screenWidth-((temp/2)-5),screenHeight-((temp/3)));
g.setColor(0x000000);
g.setStrokeStyle(Graphics.SOLID);
g.drawString("LIFE : "+life,screenWidth-10,3, (Graphics.TOP | Graphics.RIGHT));
g.drawString("SCORE: "+score,screenWidth/2+(temp),3,Graphics.TOP | Graphics.HCENTER);
g.drawString("press ''8'' to Pause",5,3,(Graphics.TOP | Graphics.LEFT));
switch (index) {
case 1: // pause
pauseGame(g);
break;
case 2: // gameover
gameOver(g);
index = 2;
break;
case 3: // palyAgian
playAgain(g);
break;
case 4:
gameWin(g);
break;
}
keyEvents();
}
//method to disply win screen
void gameWin(Graphics g)
{
g.setColor(0,0,255);
g.fillRect(0,0,screenWidth,screenHeight);
g.setColor(255,0,0);
g.drawString("YOU WIN",(screenWidth>>2),screenHeight>>2,Graphics.TOP | Graphics.HCENTER);
}
void pauseGame(Graphics g)
{
g.setColor(255,255,255);
g.fillRect(0,0,screenWidth,screenHeight);
g.setColor(255,0,0);
g.setStrokeStyle(Graphics.SOLID);
g.drawString("GamePaused Press ''8'' to Resume", 40, screenHeight>>1, Graphics.TOP | Graphics.LEFT);
// g.drawString("GamePaused Press ''P'' to Resume",(temp+3),ScreenHeight/2);
}
//method to display play again screen.
void playAgain(Graphics g)
{
// g.setColor(Color.pink);
// g.fillRect(0,0,ScreenWidth,ScreenHeight);
g.setColor(255,0,0);
g.drawString("Press S PalyAgain",(screenWidth>>1),screenHeight>>1,(Graphics.TOP | Graphics.HCENTER));
}
/**
* Gives the key being pressed. call-back method in Canvas
*/
public void keyPressed(int key)
{
switch(key)
{
case 48:
// reverse = !reverse;
break;
case -1:
keyPress |= 1;
startgame = true;
break;
case -2:
keyPress |= 2;
pause=!pause;
if(pause)
index=1;
else
index=0;
break;
case -3:
keyPress |= 4;
break;
case -4:
keyPress |= 8;
break;
case 52:
if(index==3)
{
life=3;
score=0;
DBricks();
index=0;
}
}
}
public void keyRepeated(int key)
{
switch(key)
{
case -1:
break;
case -2:
break;
case -3:
System.out.println("1111111111");
Lkeymove();
break;
case -4:
Rkeymove();
break;
}
}
/**
* Gives the key being Released. call-back method in Canvas
*/
public void keyReleased(int key)
{
// System.out.println(key);
switch(key)
{
case -1:
keyPress ^= 1;
break;
case -2:
keyPress ^= 2;
break;
case -3:
keyPress ^= 4;
break;
case -4:
keyPress ^= 8;
break;
}
}
/**
* controls the snake movements.
*/
public void keyEvents()
{
}
}
推荐答案
这篇关于请告诉我您是否认为我的代码有用.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!