📄 cutoffcanvas.java
字号:
//Author Jeremiah McLeod xdebugxpackage cutOff;import java.util.*;import javax.microedition.lcdui.*;import cutOff.About;//import javax.microedition.lcdui.game.*;public class cutOffCanvas extends Canvas { static int CORNER_X; static int CORNER_Y; static int width; static int height; Display myDisplay; cutOff myMiniGames; Random myRandom; private int ker; private boolean ballToggle[]; private boolean abouted; public boolean pausedAbouted;boolean wasArrowRight=false; boolean wasArrowLeft=false; boolean wasArrowUp=false; boolean wasArrowDown=false; static int speed; int scorePrint; boolean myInitialized; private Font FONT; boolean polyAorB; private long adjustTime; int arrowX,arrowY,arrowSize,arrowD; int totalArea; static int bottom; private int score,lives; boolean arrowMoveUp,arrowMoveDown,arrowMoveLeft,arrowMoveRight; boolean notBuffered; Image imgDoubleBuffer; Graphics doubleBuffer; public boolean paused,go,gameOver; private long time1; //private boolean foundd=false; private boolean myImagesLoaded; private int cutPointsX [],cutPointsY []; private int numCutPoints; private Image ballImg1,ballImg2; private int tempScore,numPlaces; private int bounderPointsX [],bounderPointsY []; private int numBounderPoints; private int polyAX[],polyAY[],polyBX[],polyBY[]; private boolean toggle; private int numPolyAPoints,numPolyBPoints; private int [] fillerRectX,fillerRectY,fillerRectWidth,fillerRectHeight; private int numFillerRects; private int startCutPointX,startCutPointY; private boolean resting; private int [] ballX,ballY,ballDX,ballDY,ballMX,ballMY,ballDM,ballMA,ballRandom; private int numBalls; private int [] dontRandom; private int level; private int scorePrint2; private String [] numString; private static int ballWidth,ballHeight; private char ballC1,ballC2,ballC3,ballC4; public cutOffCanvas(cutOff midlet) { myDisplay = Display.getDisplay(midlet); myMiniGames = midlet; myInitialized=false; go=false; notBuffered=true; } void start() { myDisplay.setCurrent(this); repaint(); } void reset() { } //------------------------------------------------------- // graphics methods /** * paint the game graphic on the screen. */ public void paint(Graphics g) { int p,t,np; int k,a; int small=0; Graphics saved=null; // perform the calculations if necessary: if(!myInitialized) { if( !isDoubleBuffered()) notBuffered=true; else notBuffered=false; //notBuffered=true; g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL)); FONT=Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL); go=false; paused=false; gameOver=false; totalArea=0; cutPointsX = new int [150]; cutPointsY = new int [150]; bounderPointsX= new int [250]; bounderPointsY= new int [250]; polyAX=new int [250]; polyBX=new int [250]; polyAY=new int [250]; polyBY=new int [250]; fillerRectX=new int [300]; fillerRectY=new int [300]; fillerRectWidth=new int [300]; fillerRectHeight=new int [300]; ker=0; ballX=new int [30]; ballY=new int [30]; ballDX=new int [30]; ballDY=new int [30]; ballMX=new int [30]; ballMY=new int [30]; ballDM=new int [30]; ballMA=new int [30]; ballRandom=new int [30]; dontRandom = new int [30]; numString = new String [10]; numString[0]="0"; numString[1]="1"; numString[2]="2"; numString[3]="3"; numString[4]="4"; numString[5]="5"; numString[6]="6"; numString[7]="7"; numString[8]="8"; numString[9]="9"; ballToggle = new boolean [100]; for (p=0;p<100;p++) ballToggle[p]=true; CORNER_X = g.getClipX(); CORNER_Y = g.getClipY(); width = g.getClipWidth(); height = g.getClipHeight(); toggle=false; if (width<=height) small=width; else small=height; arrowSize=1+(small/40); scorePrint=width-(FONT.stringWidth("Score 55555"))-5; scorePrint2=width-(FONT.stringWidth("55555"))-5; ballWidth=(1+(small/28))*2; ballHeight=(1+(small/28))*2;speed=25; if (notBuffered==true) { imgDoubleBuffer=Image.createImage (width,height); doubleBuffer=imgDoubleBuffer.getGraphics (); } abouted=false; pausedAbouted=false; bottom=height-FONT.getHeight()-6; myRandom = new Random(System.currentTimeMillis()); go=false; gameOver=false; myImagesLoaded=false; g.setColor (255,255,255); g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL)); FONT=Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL); g.fillRect (0,0,width,height); g.setColor (0,0,0); g.drawString ("Loading Images",(width/2)-(FONT.stringWidth ("Loading Images")/2),height/2,CORNER_X|CORNER_Y); myInitialized = true; } else { if (myImagesLoaded==false) { loadImages (); myImagesLoaded=true; } if (notBuffered==true) { saved=g; g=imgDoubleBuffer.getGraphics(); } g.setFont (FONT); if (go==false && gameOver==false && paused==false && myImagesLoaded==true) { g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL)); FONT=Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL); g.setColor (0xffffffff); g.fillRect (0,0,width,height); g.setColor (0x00000000); if (abouted==false) { About.showAbout(Display.getDisplay(myMiniGames)); abouted=true; } else { g.drawString ("Capture",(width/2)-(FONT.stringWidth ("Capture")/2),(FONT.getHeight()*1),CORNER_X|CORNER_Y); g.drawString ("New Game to Start.",(width/2)-(FONT.stringWidth ("New Game to Start.")/2),height-(height/5),CORNER_X|CORNER_Y); } } if (paused==true) { g.setColor (0xffffffff); g.fillRect (0,0,width,height); g.setColor (0x00000000); g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL)); FONT=Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL); if (pausedAbouted==false) { About.showAbout(Display.getDisplay(myMiniGames)); pausedAbouted=true; } else { g.drawString ("Capture",(width/2)-(FONT.stringWidth ("Capture")/2),(FONT.getHeight()*1),CORNER_X|CORNER_Y); g.drawString ("Go to Continue",(width/2)-(FONT.stringWidth ("Go to Continue.")/2),height-(height/5),CORNER_X|CORNER_Y); }} if ((go==true || gameOver==true) && paused==false) { g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL)); FONT=Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL); g.setColor (0,0,0); g.fillRect (0,0,width,height); //g.fillRect (0,bottom,width,height-bottom); //drawCutPointLines g.setColor (255,100,50); for (p=0;p<numCutPoints;p++) { g.drawLine (cutPointsX[p],cutPointsY[p],cutPointsX[p+1],cutPointsY[p+1]); } g.setColor (50,100,255); g.drawLine (0,0,width-1,0); g.drawLine (0,0,0,bottom); g.drawLine (width-1,0,width-1,bottom); g.drawLine (0,bottom,width-1,bottom); /*for (p=0;p<numBounderPoints;p++) { np=p+1; if (np==numBounderPoints) np=0; g.drawLine (bounderPointsX[p],bounderPointsY[p],bounderPointsX[np],bounderPointsY[np]); } */ g.setColor (50,100,255); for (p=0;p<numFillerRects;p++) g.fillRect (fillerRectX[p],fillerRectY[p],fillerRectWidth[p],fillerRectHeight[p]); g.setColor (255,0,70); for (p=0;p<numBalls;p++) { if (toggle==true) { g.drawImage (ballImg1,ballX[p],ballY[p],0); } else { g.drawImage (ballImg2,ballX[p],ballY[p],0); } } ker++; if (ker==5) { ker=0; toggle=!toggle; } //try { if (score>999999) score=999999; if (lives>99) lives=99; g.setColor (255,255,255); g.drawString ("Lives ",0,bottom+3,0); if (lives<10) { // if (lives<0) System.out.println (" lives<0"); g.drawString (numString[lives],FONT.stringWidth ("Lives "),bottom+3,0); } else { //if (lives/10<0 || lives/10>10) System.out.println ("liiives1: "+(lives/10)); g.drawString (numString[lives/10],FONT.stringWidth ("Lives "),bottom+3,0);// if (lives-((lives/10)*10)<0 || lives-((lives/10)*10)>10) System.out.println ("liiiives2: "+(lives-((lives/10)*10))); g.drawString (numString[lives-((lives/10)*10)],FONT.stringWidth ("Lives 5"),bottom+3,0); } tempScore=score; a=10; numPlaces=0; for (p=1;p<8;p++) { if (tempScore-a<0) { numPlaces=p; p=9; } a=a*10; } for (p=1;p<numPlaces;p++) { k=1; a=0; for (t=0;t<numPlaces-p;t++) k=k*10; //System.out.println ("score 1 "+score); a=tempScore/k; //System.out.println ("score "+score+" aaaaaaaaaaaa"+a+" kk "+k); g.drawString (numString[a],scorePrint2+(p*FONT.stringWidth ("5")),bottom+3,0); tempScore=tempScore-(a*k); if (tempScore>=k) while (tempScore>=k) tempScore=tempScore-k; //System.out.println ("score2 "+score+ " a "+a+ " k "+k); } g.drawString ("Score",scorePrint,bottom+3,0); if (tempScore>=10) while(tempScore>=10) tempScore=tempScore-10; //if (score>10 || score<0) System.out.println ("sooooore"+score); g.drawString (numString[tempScore],scorePrint2+(p*FONT.stringWidth ("5")),bottom+3,0); //drawArrow g.setColor (255,100,100); if (arrowD==0) { g.drawLine (arrowX,arrowY,arrowX+arrowSize,arrowY+arrowSize); g.drawLine (arrowX+arrowSize,arrowY+arrowSize,arrowX-arrowSize,arrowY+arrowSize); g.drawLine (arrowX-arrowSize,arrowY+arrowSize,arrowX,arrowY); } if (arrowD==1) { g.drawLine (arrowX,arrowY,arrowX+arrowSize,arrowY-arrowSize); g.drawLine (arrowX+arrowSize,arrowY-arrowSize,arrowX-arrowSize,arrowY-arrowSize); g.drawLine (arrowX-arrowSize,arrowY-arrowSize,arrowX,arrowY); } if (arrowD==3) { g.drawLine (arrowX,arrowY,arrowX-arrowSize,arrowY-arrowSize); g.drawLine (arrowX-arrowSize,arrowY-arrowSize,arrowX-arrowSize,arrowY+arrowSize); g.drawLine (arrowX-arrowSize,arrowY+arrowSize,arrowX,arrowY); } if (arrowD==2) { g.drawLine (arrowX,arrowY,arrowX+arrowSize,arrowY-arrowSize); g.drawLine (arrowX+arrowSize,arrowY-arrowSize,arrowX+arrowSize,arrowY+arrowSize); g.drawLine (arrowX+arrowSize,arrowY+arrowSize,arrowX,arrowY); } /*g.setColor (0,244,0); for (p=0;p<numPolyBPoints;p++) { np=p+1; if (np==numPolyBPoints) np=0; g.drawLine (polyBX[p],polyBY[p],polyBX[np],polyBY[np]); } g.setColor (255,255,255); for (p=0;p<numBounderPoints;p++) { g.drawString (""+p,bounderPointsX[p],bounderPointsY[p],0); } */ //} catch (Exception e) {System.out.println (""+e); //} }//gotrue if (notBuffered==true) saved.drawImage (imgDoubleBuffer,0,0,CORNER_X|CORNER_Y); }//else } //paint void advance() { repaint (); if (go==true && paused==false && gameOver==false) { advan (); advan (); advan (); }}//advanceprivate final void advan () { int p,np,g,t; int i,j; int k,l; boolean foundd=false; boolean found=false; int areaA=0; int areaB=0; int x1,x2,y1,y2; ///balls for (p=numBalls-1;p>-1;p--) { for (t=0;t<numBounderPoints;t++) { np=t+1; if (np>=numBounderPoints) np=0; if (bounderPointsX[t]<=bounderPointsX[np]) { x1=bounderPointsX[t]; x2=bounderPointsX[np]; } else { x1=bounderPointsX[np]; x2=bounderPointsX[t]; } if (bounderPointsY[t]<=bounderPointsY[np]) { y1=bounderPointsY[t]; y2=bounderPointsY[np]; }else { y1=bounderPointsY[np]; y2=bounderPointsY[t]; } if (x1==x2) { y1=y1+1; y2=y2-1; } if (y1==y2) { x1=x1+1; x2=x2-1; } if (ballX[p]+ballWidth>=x1 && ballX[p]<=x2 && ballY[p]+ballHeight>=y1 && ballY[p]<=y2) { dontRandom[p]=20; if (y1==y2) { if (ballDY[p]==0) { ballDY[p]=1; ballY[p]-=2; } else { ballDY[p]=0; ballY[p]+=2; } } if (x1==x2) { if (ballDX[p]==0) { ballDX[p]=1; ballX[p]-=2; } else { ballDX[p]=0; ballX[p]+=2; } } } } if (dontRandom[p]<1) { ballRandom[p]--; if (ballRandom[p]<=0) { ballRandom[p]=getRandomInt (12); if (getRandomInt(2)==0) { ballX[p]+=(getRandomInt (3)-1); } else { ballY[p]+=(getRandomInt (3)-1); } for (t=0;t<numBounderPoints;t++) { np=t+1; if (np>=numBounderPoints) np=0; if (bounderPointsX[t]<=bounderPointsX[np]) { x1=bounderPointsX[t]; x2=bounderPointsX[np]; } else { x1=bounderPointsX[np]; x2=bounderPointsX[t]; } if (bounderPointsY[t]<=bounderPointsY[np]) { y1=bounderPointsY[t]; y2=bounderPointsY[np]; }else { y1=bounderPointsY[np]; y2=bounderPointsY[t]; } if (x1==x2) { y1=y1+1; y2=y2-1; } if (y1==y2) { x1=x1+1; x2=x2-1; } if (ballX[p]+ballWidth>=x1 && ballX[p]<=x2 && ballY[p]+ballHeight>=y1 && ballY[p]<=y2) { dontRandom[p]=8; if (y1==y2) { if (ballDY[p]==0) { ballDY[p]=1; ballY[p]-=2; } else { ballDY[p]=0; ballY[p]+=2; } } if (x1==x2) { if (ballDX[p]==0) { ballDX[p]=1; ballX[p]-=2; } else { ballDX[p]=0; ballX[p]+=2; } } } } } } dontRandom[p]--; if (dontRandom[p]<-500) dontRandom[p]=0; }//p for (p=0;p<numBalls;p++) { ballC1=contains (ballX[p]+2,ballY[p]+2,bounderPointsX,bounderPointsY,numBounderPoints); ballC2=contains (ballX[p]+ballWidth-2,ballY[p]+2,bounderPointsX,bounderPointsY,numBounderPoints); ballC3=contains (ballX[p]+2,ballY[p]+ballHeight-2,bounderPointsX,bounderPointsY,numBounderPoints); ballC4=contains (ballX[p]+ballWidth-2,ballY[p]+ballHeight-2,bounderPointsX,bounderPointsY,numBounderPoints); if ((ballC1!='i' && ballC1!='v') || (ballC2!='i' && ballC2!='v') || (ballC3!='i' && ballC3!='v') || (ballC4!='i' && ballC4!='v')) { for (t=p;t<numBalls-1;t++) { ballX[t]=ballX[t+1]; ballY[t]=ballY[t+1]; ballDX[t]=ballDX[t+1]; ballDY[t]=ballDY[t+1]; ballMX[t]=ballMX[t+1]; ballMY[t]=ballMY[t+1]; ballDM[t]=ballDM[t+1]; ballMA[t]=ballMA[t+1]; ballRandom[t]=ballRandom[t+1]; }//t numBalls--; lives++; score=score+100; } }//p for (p=numBalls-1;p>-1;p--) { if (ballDM[p]==0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -