iLMS知識社群歷程檔登入
位置: 網路程式設計 > 作業 > NetProg100_BN_課堂練習_01碰碰球_20120524
作業資訊  |  已交名單(30)
項目 內容
允許遲交
開放觀摩
屬性 個人作業
成績比重 2%
期限
2012-05-24 23:59
附件
描述
-- GaameView.java ----
package tw.edu.hust.bn98000;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class GameView extends View {

private Ball b1 = new Ball( 160, 240, 15, 5, 3, Color.GRAY);;
private Ball b2 = new Ball( 60, 240, 10, -3, 5, Color.BLUE);;
private Ball b3 = new Ball( 160, 40, 5, 6, -3, Color.RED);;

public GameView(Context context) {
super(context);
// TODO Auto-generated constructor stub
new Thread(){
public void run() {
while(true) {
b1.move( GameView.this.getWidth(), GameView.this.getHeight());
b2.move( GameView.this.getWidth(), GameView.this.getHeight());
b3.move( GameView.this.getWidth(), GameView.this.getHeight());
GameView.this.postInvalidate();
try {
sleep(30);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
}

public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}

public GameView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
    //
Paint wPen = new Paint();
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
wPen.setColor(Color.WHITE);
canvas.drawRect(0, 0, this.getWidth(), this.getHeight(), wPen);
b1.draw(canvas);
b2.draw(canvas);
b3.draw(canvas);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}



---- Ball..java ---

     public void move(int width, int height) {
x += vx;
y += vy;
// 微調
// 1. 左右
if (x-r < 0) {
x = r;
vx = -vx;
}
else if ( x+r > width ) {
x = width-r;
vx = -vx;
}
// 2. 上下
if (y-r < 0) {
y = r;
vy = -vy;
}
else if ( y+r > height ) {
y = height-r;
vy = -vy;
}
      }