我是 android 应用程序的新手,我刚开始写一个简单的游戏。我使用一个类扩展视图并使用一些线程来制作一个简单的游戏我想问我是否走错了方向?我应该使用其他方式吗?我认为我在这里使用线程是错误的,对吗?我只是用
Game gameview;
gameview= new Game(this);
setContentView(gameview);
在扩展活动的类中调用它
public class Game extends View {
Bitmap background;
Bitmap nomouthbear;
Bitmap nomouthbearget;
Bitmap apple;
float bearX;
float bearY;
List<Apple> appleList;
int appleCount = 0;
float appleX;
float appleY;
float applescore;
float applespeed;
int totalscore = 0;
float canvasheight;
float canvaswidth;
float randomNumber;// random number for apple appear in X
float randomNumber2;// random number for score and speed
float randomNumber3;
int applecombo = 0; // count for how many apple that the bear eat in combo
boolean checkbeareat; // for check if bear eat any of apple or not.
boolean bearEating = false;
Thread thread2;
boolean ineatingthread = false;
int time = 20; // game time =60sec
public Game(Context context) {
super(context);
// setup image source
nomouthbear = BitmapFactory.decodeResource(getResources(),
R.drawable.nomouthbear_net);
apple = BitmapFactory.decodeResource(getResources(), R.drawable.apple);
nomouthbearget = BitmapFactory.decodeResource(getResources(),
R.drawable.nomouthbearget);
background = BitmapFactory.decodeResource(getResources(),
R.drawable.background);
// init bear x,y
bearX=0;
bearY =0;
// setup background
if (background != null) {
setBackgroundDrawable(new BitmapDrawable(background));
}
// open a list which hold all apple
appleList = new ArrayList<Apple>();
// make a thread to creat apple every 2 sec
Thread thread = new Thread() {
@Override
public void run() {
try {
while (true) {
sleep(2000);
randomNumber = (float) Math.random();
randomNumber2 = (float) Math.random();
// avoid start at edge point
appleX = canvaswidth * randomNumber;
if (appleX >= canvaswidth - apple.getWidth() / 2)
appleX = canvaswidth - apple.getWidth() / 2;
if (appleX <= nomouthbear.getWidth() / 2)
appleX = nomouthbear.getWidth();
applescore = 1000 * randomNumber2;
// check if speed too low
applespeed = 10 * randomNumber2;
if (applespeed <= 3) {
applespeed = 3;
}
// add new apple in the apple list
appleList.add(new Apple(appleX, 65, applescore,
applespeed));
appleCount++;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
// start the thread
thread.start();
// thread for timer
Thread thread1 = new Thread() {
@Override
public void run() {
try {
while (true)
{
sleep(1000);
if(time>=0)
time--;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread1.start();
//thread for controlling the bear eat pic appear time
Thread thread2 = new Thread() {
@Override
public void run() {
try {
while (true)
{
if (bearEating == true) {
sleep(200);
bearEating = false;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread2.start();
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
if (time <= 0) {
Paint textPaint = new Paint();
textPaint.setColor(Color.BLACK);
textPaint.setTextAlign(Align.LEFT);
textPaint.setTextSize(65);
canvas.drawText(
"Game Over!",
canvas.getWidth() / 4, canvas.getHeight() / 2, textPaint);
canvas.drawText(
"Your Score is ",
canvas.getWidth() / 4, canvas.getHeight() / 2+65, textPaint);
canvas.drawText(
Integer.toString(totalscore),
canvas.getWidth() / 4, canvas.getHeight() / 2+130, textPaint);
} else {
checkbeareat = false;
canvasheight = canvas.getHeight();
canvaswidth = canvas.getWidth();
// draw score text
Paint textPaint = new Paint();
textPaint.setColor(Color.BLACK);
textPaint.setTextAlign(Align.LEFT);
textPaint.setTextSize(65);
canvas.drawText("Score: " + Integer.toString(totalscore), 50, 50,
textPaint);
canvas.drawText("Time left: " + Integer.toString(time), 50, 120,
textPaint);
// if have apple
if (appleCount != 0) {
for (int i = 0; i < appleCount; i++) {
if (appleList.get(i).checkstate()) {
// make every apple fall down
if ((appleList.get(i).getAppleY() + apple.getHeight() / 2) <= canvas
.getHeight()) {
appleList.get(i).setAppleY(
appleList.get(i).getAppleY()
+ appleList.get(i).getspeed());
} else {
// appleList.get(i).setAppleY(appleList.get(i).getAppleY());
applecombo = 0;
appleList.get(i).destoryApple();
}
// check if bear eat the apple
if (bearX + nomouthbear.getWidth() / 2 >= appleList
.get(i).getAppleX()
&& bearX <= appleList.get(i).getAppleX()
&& bearY>= appleList
.get(i).getAppleY()
&& bearY-nomouthbear.getHeight()/2 <= appleList.get(i).getAppleY()) {
// add score
totalscore += appleList.get(i).getAppleScore();
// change the state of apple to false so wont draw
// it
// again
appleList.get(i).destoryApple();
// draw bear ate
canvas.drawBitmap(nomouthbearget, bearX
- nomouthbear.getWidth() / 2, bearY
- nomouthbear.getHeight(), null);
checkbeareat = true;
bearEating = true;
applecombo++;
}
// draw apple
if (appleList.get(i).checkstate()) {
canvas.drawBitmap(
apple,
appleList.get(i).getAppleX()
- (apple.getWidth() / 2),
appleList.get(i).getAppleY()
- (apple.getHeight() / 2), null);
}
}
}
}
// draw bear
// canvas.drawBitmap(nomouthbear, 0, 0, null);
if (bearEating == false) {
if (bearX == 0 && bearY == 0){
canvas.drawBitmap(nomouthbear, 0, canvas.getHeight()- nomouthbear.getHeight()*2, null);}
else{
canvas.drawBitmap(nomouthbear,
bearX - nomouthbear.getWidth() / 2, bearY
- nomouthbear.getHeight(), null);}
} else {
canvas.drawBitmap(nomouthbearget,
bearX - nomouthbear.getWidth() / 2,
bearY - nomouthbear.getHeight(), null);
}
invalidate();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (event.getX() >= nomouthbear.getWidth() / 2
&& event.getX() <= canvaswidth - nomouthbear.getWidth() / 2) {
bearX = event.getX();
} else if (event.getX() >= canvaswidth - nomouthbear.getWidth() / 2)
bearX = canvaswidth - nomouthbear.getWidth() / 2;
else
bearX = nomouthbear.getWidth() / 2;
bearY = canvasheight - nomouthbear.getHeight();
return true;
}
}