Hướng dẫn làm game android Informational

Trong hướng dẫn hôm nay, chúng ta sẽ khám phá cách tạo Slot Game cho Android với phần mềm Android Studio. Slot game hay dân chơi thường gọi là nổ hũ, là một kiểu mô phỏng của máy đánh bạc ở các sòng casino. Một slot game thường có ba cuộn quay trở lên và sẽ quay khi nhấn nút. Bạn có thể tìm thấy nhiều ví dụ trò chơi này tại đây: Slot Game Mới.

Bạn có thể tham khảo video youtube dưới đây:

Máy quay số của chúng ta sẽ có ba cuộn hiển thị các hình ảnh trái cây sau đây:

Để bắt đầu, chúng ta viết mã bố trí các hình ảnh trên của slot game. Bố cục bao gồm ba hình ảnh cho mỗi cuộn, một nút để bắt đầu quay các cuộn phim và TextView để hiển thị thông báo cho người dùng như: Bạn giành được giải thưởng lớn! ---- <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.ssaurel.slotmachine.MainActivity" android:background="

FFFFFF">

<LinearLayout

android:id="@+id/imgs" android:layout_width="wrap_content" android:layout_height="wrap_content" android:eek:rientation="horizontal" android:layout_centerHorizontal="true"> <ImageView android:id="@+id/img1" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginTop="50dp" android:src="@drawable/slot5"/> <ImageView

android:id="@+id/img2" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginTop="50dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:src="@drawable/slot5"/> <ImageView android:id="@+id/img3" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginTop="50dp" android:src="@drawable/slot5"/> </LinearLayout> <TextView android:id="@+id/msg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:layout_below="@id/imgs" android:layout_centerHorizontal="true"/> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start" android:layout_marginTop="50dp" android:layout_below="@id/msg" android:layout_centerHorizontal="true"/> </RelativeLayout> ----- Khi một hình ảnh mới được hiển thị, chúng tôi thông báo cho đối tượng WheelListener đã đăng ký vào Wheel bằng cách gọi phương thức newImage. Điều đó cho chúng ta mã sau đây cho Bánh xe: ----- public class Wheel extends Thread { interface WheelListener { void newImage(int img); } private static int[] imgs = {R.drawable.slot1, R.drawable.slot2, R.drawable.slot3, R.drawable.slot4, R.drawable.slot5, R.drawable.slot6}; public int currentIndex; private WheelListener wheelListener; private long frameDuration; private long startIn; private boolean isStarted; public Wheel(WheelListener wheelListener, long frameDuration, long startIn) { this.wheelListener = wheelListener; this.frameDuration = frameDuration; this.startIn = startIn; currentIndex = 0; isStarted = true; } public void nextImg() { currentIndex++; if (currentIndex == imgs.length) { currentIndex = 0; } } @override public void run() { try { Thread.sleep(startIn); } catch (InterruptedException e) { } while(isStarted) { try { Thread.sleep(frameDuration); } catch (InterruptedException e) { } nextImg(); if (wheelListener != null) { wheelListener.newImage(imgs[currentIndex]); } } } public void stopWheel() { isStarted = false; } } ----- Cuối cùng, chúng ta khởi động các bánh xe tại một thời điểm khác bằng cách sử dụng phương pháp RandomLong. Mã của Hoạt động chính sẽ như thế: --- public class MainActivity extends AppCompatActivity { private TextView msg; private ImageView img1, img2, img3; private Wheel wheel1, wheel2, wheel3; private Button btn; private boolean isStarted; public static final Random RANDOM = new Random(); public static long randomLong(long lower, long upper) { return lower + (long) (RANDOM.nextDouble() * (upper - lower)); } @override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img1 = (ImageView) findViewById(R.id.img1); img2 = (ImageView) findViewById(R.id.img2); img3 = (ImageView) findViewById(R.id.img3); btn = (Button) findViewById(R.id.btn); msg = (TextView) findViewById(R.id.msg); btn.setOnClickListener(new View.OnClickListener() { @override public void onClick(View view) { if (isStarted) { wheel1.stopWheel(); wheel2.stopWheel(); wheel3.stopWheel(); if (wheel1.currentIndex == wheel2.currentIndex && wheel2.currentIndex == wheel3.currentIndex) { msg.setText("You win the big prize"); } else if (wheel1.currentIndex == wheel2.currentIndex || wheel2.currentIndex == wheel3.currentIndex || wheel1.currentIndex == wheel3.currentIndex) { msg.setText("Little Prize"); } else { msg.setText("You lose"); } btn.setText("Start"); isStarted = false; } else { wheel1 = new Wheel(new Wheel.WheelListener() { @override public void newImage(final int img) { runOnUiThread(new Runnable() { @override public void run() { img1.setImageResource(img); } }); } }, 200, randomLong(0, 200)); wheel1.start(); wheel2 = new Wheel(new Wheel.WheelListener() { @override public void newImage(final int img) { runOnUiThread(new Runnable() { @override public void run() { img2.setImageResource(img); } }); } }, 200, randomLong(150, 400)); wheel2.start(); wheel3 = new Wheel(new Wheel.WheelListener() { @override public void newImage(final int img) { runOnUiThread(new Runnable() { @override public void run() { img3.setImageResource(img); } }); } }, 200, randomLong(150, 400)); wheel3.start(); btn.setText("Stop"); msg.setText(""); isStarted = true; } } }); } } -----

Nguồn: Sylvain Saurel