从最简单的源代码开始,切勿眼高手低---(第一波)
从正式学习安卓到现在,差不多整整一年了,去年暑假,大约也就是6,7月份的样子,从图书馆借了好多书,安卓的,java的,假期里算是把李刚的<疯狂安卓讲义>完完整整的看完了,当时就只顾着看书,很少敲代码,也没做笔记,凭着脑子看,看完感觉收获还是挺大的,又看了mars老师的视频,没看完,大约看了第一季十几集差不多,终归停留在好像懂了的层面,也没做什么东西.
暑假过后,断断续续的学一点,发现java有点薄弱,毕竟没系统的学过,寒假之后到前段时间系统的把java过了一遍.到这时候我开始意识到做笔记和敲代码的重要性了,不做笔记,很快就会把当时明白的知识点遗忘掉,不敲代码,等真正用到的时候不知道怎么下手.
直到前段时间,自己亲手写了个小游戏,我才真正体会到学习的乐趣.还是要多实际的去操作才行.期末了,忙着各种考试,也有点迷茫接下来该怎么继续学习,感觉自己挺懂了,其实大多还是眼高手低.前天,碰巧在安卓巴士上找到一个别人分享的文件,里面是二十来个游戏的源代码,有点如获至宝的感觉啊.
于是按照从小到大的顺序排列了下来,打算现在开始加上暑假,挨着看一遍,自己实现一遍.
最小的一个便是下面这个.界面就是下面一个挡板,可控制左右移动,把下落的球弹上去.很常见的一个案例.看了一遍源代码,大体自己重写了一遍.收货真的很大,以后写完一个贴上来一个,也算是给代码做个笔记.
如果你也是新手,一定要多写.自己多写.亲手写.
下面是源码
public class PinBall extends Activity{ private int tableHeight; private int tableWidth; Random random = new Random(); //挡板大小和坐标 private final int racket_width = 70; private final int racket_height = 20; private int racketY; private int racketX; //球体大小,速度,坐标 private final int ballR = 12; private int ballX; private int ballY; private int Vy = 3; //生成一个比率(-0.5~0.5之间),用于产生随机方向 private double xyRate = random.nextDouble() - 0.5; private int Vx = (int) (Vy * 2 * xyRate); //游戏进行时标志 private boolean isLose = false; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); //全屏无标题 requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //设置游戏界面 final GameView game = new GameView(this); setContentView(game); //获取桌面大小 WindowManager manager = getWindowManager(); Display display = manager.getDefaultDisplay(); tableHeight = display.getHeight(); tableWidth = display.getWidth(); //根据获取的桌面大小初始化坐标 initXY(); //重绘 final Handler handler = new Handler(){ public void handleMessage(Message message){ if(message.what == 0x123){ game.invalidate(); } } }; final Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { //左右边框 if(ballX < 0 || (ballX + ballR > tableWidth)){ Vx = -Vx; } //超过下边界且未被球拍接住 if((ballY + ballR > racketY) && (ballX + ballR < racketX || ballX > racketX + racket_width)){ timer.cancel(); isLose = true; } //被球拍打回或者反弹时到达顶部 if(ballY < 0 || (ballY + ballR > racketY && ballX > racketX && ballX < racketX + racket_width )){ Vy = -Vy; } ballX += Vx; ballY += Vy; handler.sendEmptyMessage(0x123); } }, 0, 10); //给游戏view加监听器 game.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { int x = (int) event.getX(); int mid = tableWidth/2; switch (x > mid ? 1 : 2) { case 1: if(racketX + racket_width < tableWidth){ racketX += 10; } break; case 2: if(racketX > 0){ racketX -= 10; } break; } game.invalidate(); return true; } }); } private void initXY(){ racketY = tableHeight - 80; racketX = random.nextInt(tableWidth); ballX = random.nextInt(tableWidth); ballY = random.nextInt(10) + 20; } class GameView extends View{ private static final float TEXT_SIZE = 40; public GameView(Context context) { super(context); // TODO Auto-generated constructor stub setFocusable(true); } @Override protected void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL); //游戏结束 if (isLose) { paint.setTextSize(TEXT_SIZE); paint.setColor(Color.RED); canvas.drawText("Game Over!", tableWidth/2 - paint.getTextSize()*2, tableHeight/2 - paint.getTextSize()/2, paint); } //游戏继续 else { paint.setColor(Color.BLUE); canvas.drawCircle(ballX, ballY, ballR, paint); paint.setColor(Color.GREEN); canvas.drawRect(racketX, racketY, racketX + racket_width, racketY + racket_height, paint); } } } }