安卓游戏开发_基础环境获取代码
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); } } } }
Rust编程语言群 1036955113
java新手自学群 626070845
java/springboot/hadoop/JVM 群 4915800
Hadoop/mongodb(搭建/开发/运维)Q群481975850
GOLang Q1群:6848027
GOLang Q2群:450509103
GOLang Q3群:436173132
GOLang Q4群:141984758
GOLang Q5群:215535604
C/C++/QT群 1414577
单片机嵌入式/电子电路入门群群 306312845
MUD/LIB/交流群 391486684
Electron/koa/Nodejs/express 214737701
大前端群vue/js/ts 165150391
操作系统研发群:15375777
汇编/辅助/破解新手群:755783453
大数据 elasticsearch 群 481975850
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
java新手自学群 626070845
java/springboot/hadoop/JVM 群 4915800
Hadoop/mongodb(搭建/开发/运维)Q群481975850
GOLang Q1群:6848027
GOLang Q2群:450509103
GOLang Q3群:436173132
GOLang Q4群:141984758
GOLang Q5群:215535604
C/C++/QT群 1414577
单片机嵌入式/电子电路入门群群 306312845
MUD/LIB/交流群 391486684
Electron/koa/Nodejs/express 214737701
大前端群vue/js/ts 165150391
操作系统研发群:15375777
汇编/辅助/破解新手群:755783453
大数据 elasticsearch 群 481975850
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。