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 use Android to realize the game of finding hearts An and flipping cards

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

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use Android to find An of hearts game, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian with you to understand.

There is a game in which, when we were young, we would often play with our friends and take out several playing cards, including hearts A. there are two identities in the game, shuffle and guess, shuffle the order by shuffling, and then turn the back of all the cards toward the guesser, which is chosen by the guesser. If hearts An is pulled out, the guess succeeds, otherwise it fails. This should be the simplest poker game. So how do you write this game function in an Android program? In this blog, I will complete the card game of finding An of hearts for this requirement.

Logical analysis:

1. Cards should be reflected in the ImageView control, in the guess, the unified display of the back of the cards, guess before the random change of the position of the cards, guess after the cards will be flipped, reminding the user success or failure.

two。 The contents of the cards can be placed in a container for unified management, and then the location of the elements in the container can be changed randomly.

3. When the user clicks to select a card, set three cards to flip to the front to see if hearts An is selected.

So we implement this requirement logic in the code.

First write the layout file: activity_main.xml

Next, the code that handles the logic is written into the activity interface.

Import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.*;public class MainActivity extends Activity {private ImageView myImageView; private ImageView myImageView2; private ImageView myImageView3; private Button myButton; private boolean isPlay = false; / / to determine whether it is the flag bit of guessing game process private int [] myImage= {R.mipmap.p01, R.mipmap.p02, R.mipmap.p03,} / * * set the click event for each ImageView * / View.OnClickListener myimageClistener = new View.OnClickListener () {@ Override public void onClick (View v) {if (! isPlay) {/ / judge that it can be clicked switch (v.getId ()) {case R.id.mImage01: setImage (0MagneMyImageView); break; case R.id.mImage02: setImage (1memmyImageView2); break; case R.id.mImage03: setImage (2memmyImageView3); break; default: break;} Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); myImageView= (ImageView) findViewById (R.id.mImage01); myImageView2= (ImageView) findViewById (R.id.mImage02); myImageView3= (ImageView) findViewById (R.id.mImage03); myButton= (Button) findViewById (R.id.mButton); random (); myImageView.setOnClickListener (myimageClistener); myImageView2.setOnClickListener (myimageClistener); myImageView3.setOnClickListener (myimageClistener) MyButton.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {if (isPlay) {myImageView.setImageResource (R.mipmap.p04); myImageView2.setImageResource (R.mipmap.p04); myImageView3.setImageResource (R.mipmap.p04); myImageView.setAlpha; myImageView2.setAlpha; myImageView3.setAlpha; random (); / / randomly generate the card position isPlay = false;} again) } private void setImage (int temp, ImageView ss) {/ / reveal digital myImageView.setImageResource (myImage [0]); myImageView2.setImageResource (myImage [1]); myImageView3.setImageResource (myImage [2]); myImageView.setAlpha (100) hidden behind cards; / / set transparency to translucent myImageView2.setAlpha (100); myImageView3.setAlpha (100); ss.setAlpha (255) / / set the view clicked to be fully visible if (myImage [temp] = R.mipmap.p01) {/ / determine whether the image corresponding to the selected view is A Toast.makeText of hearts (this, "Congratulations you guessed correctly", Toast.LENGTH_SHORT). Show ();} else {Toast.makeText (this, "you can't guess", Toast.LENGTH_SHORT). Show ();} isPlay = true / / the setting cannot be clicked at this time} / * * randomly change the position of the elements in the array to ensure that the cards change * / private void random () {for (int itemo witi)

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