Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Huarong Road Mini Game by Android

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the Android how to achieve Huarong Road Mini Game related knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that everyone after reading this Android how to achieve Huarong Road Mini Game article will have a harvest, let's take a look.

Effect:

Ideas and implementation

First of all, we should consider how to achieve this effect. When you think about it, it's actually a bit like the previous 2048, but not all the time. As a result, there was another wave of trouble. This time, refer to the previous 2048 for layout and content items, and put the code below:

To customize a frame layout, let's draw the numbers in it first:

Private void initial () {label = new TextView (getContext ()); label.setTextSize (32); label.setBackgroundColor (0x33ff0033); label.setTextColor (0x330D0D0D); label.setGravity (Gravity.CENTER); LayoutParams lp = new LayoutParams (LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); lp.setMargins (10,10,0,0); addView (label, lp); setNum (0);} public int getNum () {return num } @ SuppressLint ("SetTextI18n") public void setNum (int num) {this.num = num; if (the absolute value of num Y, it moves left and right, otherwise it moves up and down, and the coordinate of the upper left corner is (0Magne0) if (Math.abs (changeX) > Math.abs (changeY)) {if (changeX).

< -PADDING) { left(); } else if (changeX >

PADDING) {right ();}} else {if (changeY)

< -PADDING) { up(); } else if (changeY >

PADDING) {down ();}} break; default:} return true;}})

But we need to make all the random numbers before we slide. That is to say, 1 Mel N generates N random numbers.

Public int [] randomCommon (int max, int n) {if (n > (max + 1) | | max

< 0) { return null; } int[] result = new int[n]; int count = 0; while (count < n) { int num = (int) (Math.random() * max) + 1; boolean flag = true; for (int j = 0; j < n; j++) { if (num == result[j]) { flag = false; break; } } if (flag) { result[count] = num; count++; } } return result; } 数字生成完成之后,我们需要把数据放入之前写的Card并且add到现在的GridLayout中。 private void addCard(int cardWidth, int cardHeight) { Card card; int sum = 0; for (int x = 0; x < addNumber; x++) { for (int y = 0; y < addNumber; y++) { card = new Card(getContext()); card.setNum(number[sum] - 1); addView(card, cardWidth, cardHeight); point[x][y] = card; sum++; } } } 数据生成了,内容也显示了,接下来我们需要做的就是对方向的逻辑处理。这边我放一个,其他的同理: for (int x = 0; x < addNumber; x++) { for (int y = 0; y < addNumber; y++) { if (x - 1 >

= 0) {if (point [x-1] [y] .getNum () = 0) {point [x-1] [y] .setNum (point[ x] [y] .setNum ()); point [x] [y] .setNum (0); isFinish (); return;}}

Anyone who has seen the previous 2048 will find that the judgment here is easier, and then every time we slide to the end, we need to determine whether the game is over or not. If the game is over, give it a hint of whether to start over or go straight to the next level:

Int number = 1; for (int x = 0; x < addNumber; x +) {for (int y = 0; y < addNumber; yearly +) {if (number = = addNumber * addNumber) {MainActivity.getMainActivity () .stop () New AlertDialog.Builder (getContext ()) .setTitle ("Game over!") .setMessage ("your time is:" + MainActivity.getMainActivity (). GetTimer ()) .setPositiveButton ("start over", new DialogInterface.OnClickListener () {public void onClick (DialogInterface dialog, int which) {MainActivity.getMainActivity () .clear (); start () }) .setNegativeButton ("next pass", new DialogInterface.OnClickListener () {public void onClick (DialogInterface dialog, int which) {MainActivity.getMainActivity (). Clear (); addNumber ();}}) .show (); return;} if (point [x] [y] .getNum () = = number) {number++ This is the end of the article on "how to realize Huarong Road Mini Game by Android". Thank you for reading! I believe you all have a certain understanding of "how to achieve Huarong Road Mini Game" knowledge. If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report