package com;
import java.util.Random;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;
import mainMid.MyBallMidlet;
public class Guan2Canvas extends GameCanvas implements Runnable{
protected MyBall m_Ball;
protected MyBar m_Bar;
private IronRingPre m_ironRing;
int ringX,ringY;
int ballLength;
protected int sWidth;
protected int sHeight;
protected boolean m_bRunning; //控制线程运行
protected int ballScore;
protected int guan;
protected boolean isLive;
protected Random rm;
private boolean isMove;
private MyBallMidlet mid;
public Guan2Canvas(MyBallMidlet mid){
super(false);
this.mid=mid;
isMove=false;
isLive=true;
sWidth=getWidth();
sHeight=getHeight();
ballScore=0;
guan=2;
rm=new Random();
m_ironRing=new IronRingPre();
m_ironRing.size=50;
m_ironRing.x=(sWidth-m_ironRing.size)/2+30;
m_ironRing.y=80;
ringX=m_ironRing.x+m_ironRing.size/2;
ringY=m_ironRing.y+m_ironRing.size/2;
m_Bar=new MyBar();
m_Ball=new MyBall();
m_Bar.width=50;
m_Bar.heigth=10;
m_Bar.x=(sWidth-m_Bar.width)/2;
m_Bar.y=sHeight-m_Bar.heigth-16;
m_Bar.step=2;
m_Ball.R=200;
m_Ball.G=240;
m_Ball.B=0;
m_Ball.size=20;
m_Ball.xStep=1;
m_Ball.yStep=1;
m_Ball.x=(sWidth-m_Ball.size)/2;
m_Ball.y=sHeight-m_Ball.size-m_Bar.heigth-16;
ballLength=(m_ironRing.size/2+m_Ball.size/2)*(m_ironRing.size/2+m_Ball.size/2);
Start();
}
public void Start(){
m_bRunning = true;
Thread thread = new Thread(this); //分配新线程
thread.start();
pp();//线程启动
}
public void run() { //新线程自动调用此方法
//获取系统当前时间,并将时间换算成以毫秒为单位的数
long T1 = System.currentTimeMillis();
long T2 = T1;
while(m_bRunning){
T2 = System.currentTimeMillis();
if( T2 - T1 > 15 ){ //间隔100毫秒
T1 = T2;
Input();
if(isMove)
Logic();
repaint();
}
}
}
public void Stop(){ //终止游戏
m_bRunning = false;
}
public void keyPressed(int code){
if(code==-5){
m_Ball.startMove();
try {
Thread.sleep(80);
isMove=true;
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
public void paint(Graphics g) {
//清屏
g.setColor(144,197,252);
g.fillRect(0, 20, getWidth(), getHeight()-20);
//信息
g.setColor(0);
g.fillRect(0, 0, getWidth(), 20);
g.setColor(255, 255, 255);
g.drawString("当前关数:"+guan, 20, 2, 0);
g.drawString("当前得分:"+ballScore, 150, 2, 0);
g.setColor(0);
g.drawString("第"+guan+"关", sWidth/2-40, 100, 0);
g.drawString("按中间键启动小球", sWidth/2-45, 130, 0);
g.drawString("获得7分即可过关", sWidth/2-60, 155, 0);
g.setColor(255, 0, 0);
g.drawString("注意:铁环会阻挡球哦!", sWidth/2-60, 167, 0);
if(!isLive) {
g.setColor(0);
g.drawString("Game Over", sWidth/2-25, 180, 0);
}
m_Bar.Paint(g);
g.setColor(m_Ball.R, m_Ball.G, m_Ball.B);
m_Ball.Paint(g);
m_ironRing.paint(g);
}
private void Input() {
if (isLive) {
int keyStates = getKeyStates(); //得到当前按键状态
//如果按下方向键的左键,则bar向左移动
if ((keyStates & GameCanvas.LEFT_PRESSED) != 0) {
if (m_Bar.x > 0) {
m_Bar.x -= m_Bar.step;
if (m_Ball.y == sHeight - m_Ball.size - m_Bar.heigth-16 && (m_Ball.x + m_Ball.size / 2 >= m_Bar.x || (m_Ball.x + m_Ball.size / 2 <= m_Bar.x + m_Bar.width))) {
m_Ball.x -= m_Bar.step;
}
}
}
//如果按下方向键的右键,则Bar向右移动
if ((keyStates & GameCanvas.RIGHT_PRESSED) != 0) {
if (m_Bar.x < sWidth - m_Bar.width) {
if (m_Ball.y == sHeight - m_Ball.size - m_Bar.heigth-16 && (m_Ball.x + m_Ball.size / 2 >= m_Bar.x || (m_Ball.x + m_Ball.size / 2 <= m_Bar.x + m_Bar.width))) {
m_Ball.x += m_Bar.step;
}
m_Bar.x += m_Bar.step;
}
}
}
}
//判断情况
public void pp() {
new Thread() {
public void run() {
while (true) {
int ballx = m_Ball.x + m_Ball.size / 2;
int bally = m_Ball.y + m_Ball.size / 2;
if ((ballx - ringX) * (ballx - ringX) + (bally - ringY) * (bally - ringY) < ballLength) {
m_Ball.xStep = -m_Ball.xStep;
m_Ball.yStep = -m_Ball.yStep;
}
}
}
}.start();
}
private void Logic() {
if (m_Ball.x <=0 || m_Ball.x >sWidth - m_Ball.size) {
m_Ball.xStep = -m_Ball.xStep;
}
if (m_Ball.y <= 20) {
m_Ball.yStep = -m_Ball.yStep;
}
if (m_Ball.y >sHeight - m_Ball.size) {
m_Ball.m_Running = false;
isLive = false;
}
if (m_Ball.y >= sHeight - m_Ball.size - m_Bar.heigth-16 && m_Ball.x + m_Ball.size / 2 >= m_Bar.x && (m_Ball.x + m_Ball.size / 2 <= m_Bar.x + m_Bar.width)) {
m_Ball.yStep = -m_Ball.yStep;
ballScore++;
m_Ball.R = rm.nextInt(256);
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
m_Ball.G = rm.nextInt(256);
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
m_Ball.B = rm.nextInt(256);
}
}
}