简易弹球游戏

您所在的位置:网站首页 弹球小游戏简介文案 简易弹球游戏

简易弹球游戏

2024-03-14 13:06| 来源: 网络整理| 查看: 265

简单弹球游戏

一.小游戏功能描述 功能一:进入小游戏界面,点击play,游戏开始,点击exit可继续上一局游戏。 功能二:小球和球拍分别以圆形区域和矩形区域代替,小球开始以随机速度向下运动,遇到上方的障碍或下方的球拍时小球反弹,球拍由用户控制,球拍按左右键时,球拍会左右移动。 功能三:若没有接住球,游戏失败,则屏幕显示goodbye。

二.过程及重要代码 1.定义屏幕、球拍、球、障碍并设置属性

//屏幕宽高 int screenWidth = 0; int screenHeight = 0; //障碍挡板宽 public int baffleWith; //每份宽 int perWidth = 0; //间隔宽 int InterWidth = 0; //每份高 int perHeight = 0; //间隔高 int InterHeight = 0; //挡板层数 public int baffleLev = 8; //每层挡板数 public int baffleNum = 5; //挡板颜色 public int[] colors = {Color.BLUE, Color.RED, Color.GREEN, Color.YELLOW, Color.BLUE, Color.RED, Color.GREEN, Color.YELLOW}; //挡板总数 public int baffleTotle = baffleLev * baffleNum; //障碍物X坐标 int baffleX [] = null; //障碍物Y坐标 int baffleY [] = null; //障碍物标签 int baffleArr[][] = null; /**画障碍物*/ public void drawBaffle(Canvas canvas){ //屏幕宽高 screenWidth = screenWidth != 0 ? screenWidth : MainActivity.tableWidth; screenHeight = screenHeight != 0 ? screenHeight: MainActivity.tableHeight; //宽 perWidth = perWidth != 0 ? perWidth : screenWidth/(baffleNum+2); //间隔宽 InterWidth = InterWidth != 0 ? InterWidth : perWidth*2/(baffleNum+1); int tempLev = baffleLev/2; //每份高 perHeight = perHeight != 0 ? perHeight : screenHeight/2/(colors.length+tempLev+2); //间隔高 InterHeight = InterHeight !=0 ? InterHeight : perHeight/2; int tempHeight = 0; int tempH = 0; int tempWidht = 0; int tempW = 0; //屏幕宽 public static int tableWidth; //屏幕高 public static int tableHeight; //常亮属性 PowerManager powerManager = null; WakeLock wakeLock = null; MediaPlayer media = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //设置常亮 this.powerManager = (PowerManager)this.getSystemService(Context.POWER_SERVICE); this.wakeLock = this.powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Lock"); //获取窗口管理器 WindowManager windowManager = getWindowManager(); Display display = windowManager.getDefaultDisplay(); DisplayMetrics metrics = new DisplayMetrics(); display.getMetrics(metrics); //获取屏幕宽和高 tableWidth = metrics.widthPixels; tableHeight = metrics.heightPixels;

2.设置按钮属性

//按钮宽和高 int botwidth = (int) ((int) tableWidth/2.5); int botheight = (int) ((int) tableHeight/8); //设置play ImageButton playbot =(ImageButton) findViewById(R.id.play); ViewGroup.LayoutParams playbotn = (ViewGroup.LayoutParams) playbot.getLayoutParams(); playbotn.width = botwidth; playbotn.height = botheight; playbot.setOnClickListener(new OnClickListener() { @Override public void onClick(View source) { Intent intent = new Intent(MainActivity.this, PlayActivity.class); startActivity(intent); } }); //设置 exit ImageButton exitbot =(ImageButton) findViewById(R.id.exit); ViewGroup.LayoutParams exitbotn = (ViewGroup.LayoutParams) exitbot.getLayoutParams(); exitbotn.width = botwidth; exitbotn.height = botheight;

3.设置游戏

final Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { //结束判断 if( (playView.circleY + playView.radius) > (playView.paiY + playView.paiHeight) ){ timer.cancel(); playView.isOver = true; }else{ //碰到墙壁时X的改变 if( playView.circleX = playView.screenWidth - playView.radius ){ playView.speedX = -playView.speedX; } //碰到墙壁时Y的改变 if( playView.circleY = playView.paiX && playView.circleX = playView.paiY && (playView.circleY + playView.radius)


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3