1. Trang chủ
  2. » Công Nghệ Thông Tin

Lập trình Android: Game lật hình pot

11 423 13

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 11
Dung lượng 121,5 KB

Nội dung

Trung tâm Tin học – ĐH KHTN Game đếm số Project name: GameHinhCuaDac Build Target: Android 2.3.3 Application name: GameHinhCuaDac Package name: com.dac Create Activity: GameHinhCuaDacActivity Trong file main.xml: <?xml version="1.0" encoding="utf-8"?> <TableLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:background="@drawable/bgmemory" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/TableLayout03"> <TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:visibility="visible" android:textSize="30sp" android:paddingTop="20sp" android:textColor="#EEFFAA" android:id="@+id/mainTitle" /> </TableRow> <TableRow android:id="@+id/TableRow02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_gravity="center" android:gravity="center" android:typeface="normal" android:bufferType="normal" android:textColor="#FFEEAA" /> </TableRow> <TableRow android:id="@+id/TableRow03" Lập trình Android – http://laptrinhdidong.vn Page 1 Trung tâm Tin học – ĐH KHTN android:paddingTop="30px" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center"> <ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> </LinearLayout> </TableRow> <TableLayout android:id="@+id/tableLayout1" android:gravity="bottom" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="match_parent"> <TableRow android:id="@+id/tableRow1" android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:id="@+id/btnnew" android:background="@drawable/iconhome" android:layout_width="40px" android:layout_height="40px"> </Button> </TableRow> </TableLayout> </TableLayout> Trong Class Card.Java: package com.dac; import android.widget.Button; public class Card { public int x; public int y; Lập trình Android – http://laptrinhdidong.vn Page 2 Trung tâm Tin học – ĐH KHTN public Button button; public Card(Button button, int x, int y){ this.x = x; this.y = y; this.button = button; } } Trong file Activity: package com.dac; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.graphics.drawable.Drawable; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.Gravity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TableLayout; import android.widget.TableRow; public class GamHinhCuaDacActivity extends Activity { private static int ROW_COUNT = -1; private static int COL_COUNT = -1; private Context context; private Drawable backImage; private int [] [] cards; private List<Drawable> images; private Card firstCard; private Card seconedCard; private ButtonListener buttonListener; private static Object lock = new Object(); Lập trình Android – http://laptrinhdidong.vn Page 3 Trung tâm Tin học – ĐH KHTN private TableLayout mainTable; private UpdateCardsHandler handler; private static final int NEW_MENU_ID = Menu.FIRST; private static final int EXIT_MENU_ID = Menu.FIRST+1; private int count =0; private MediaPlayer mp; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); progressDialog(); handler = new UpdateCardsHandler(); loadImages(); setContentView(R.layout.main); Button btnnew = (Button)findViewById(R.id.btnnew); btnnew.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub clickSound(); singleChoiceDialog(); } }); backImage = getResources().getDrawable(R.drawable.iconfruit); buttonListener = new ButtonListener(); mainTable = (TableLayout)findViewById(R.id.TableLayout03); context = mainTable.getContext(); newGame(4,4); } public void progressDialog(){ final ProgressDialog progressDialog = ProgressDialog.show(GamHinhCuaDacActivity.this, "", "Loading ", true); new Thread(new Runnable(){ public void run(){ try { Thread.sleep(300); progressDialog.dismiss(); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); } private void newGame(int c, int r) { ROW_COUNT = r; COL_COUNT = c; count =0; Lập trình Android – http://laptrinhdidong.vn Page 4 Trung tâm Tin học – ĐH KHTN cards = new int [COL_COUNT] [ROW_COUNT]; mainTable.removeView(findViewById(R.id.TableRow01)); mainTable.removeView(findViewById(R.id.TableRow02)); TableRow tr = ((TableRow)findViewById(R.id.TableRow03)); tr.removeAllViews(); mainTable = new TableLayout(context); tr.addView(mainTable); for (int y = 0; y < ROW_COUNT; y++) { mainTable.addView(createRow(y)); } firstCard=null; loadCards(); } private void loadImages() { images = new ArrayList<Drawable>(); images.add(getResources().getDrawable(R.drawable.card1)); images.add(getResources().getDrawable(R.drawable.card2)); images.add(getResources().getDrawable(R.drawable.card3)); images.add(getResources().getDrawable(R.drawable.card4)); images.add(getResources().getDrawable(R.drawable.card5)); images.add(getResources().getDrawable(R.drawable.card6)); images.add(getResources().getDrawable(R.drawable.card7)); images.add(getResources().getDrawable(R.drawable.card8)); images.add(getResources().getDrawable(R.drawable.card9)); images.add(getResources().getDrawable(R.drawable.card10)); images.add(getResources().getDrawable(R.drawable.card11)); images.add(getResources().getDrawable(R.drawable.card12)); images.add(getResources().getDrawable(R.drawable.card13)); images.add(getResources().getDrawable(R.drawable.card14)); images.add(getResources().getDrawable(R.drawable.card15)); images.add(getResources().getDrawable(R.drawable.card16)); images.add(getResources().getDrawable(R.drawable.card17)); images.add(getResources().getDrawable(R.drawable.card18)); images.add(getResources().getDrawable(R.drawable.card19)); images.add(getResources().getDrawable(R.drawable.card20)); images.add(getResources().getDrawable(R.drawable.card21)); } private void loadCards(){ int size = ROW_COUNT*COL_COUNT; Lập trình Android – http://laptrinhdidong.vn Page 5 Trung tâm Tin học – ĐH KHTN Log.i("loadCards()","size=" + size); ArrayList<Integer> list = new ArrayList<Integer>(); for(int i=0;i<size;i++){ list.add(new Integer(i)); } Random r = new Random(); for(int i=size-1;i>=0;i ){ int t=0; if(i>0){ t = r.nextInt(i); } t=list.remove(t).intValue(); cards[i%COL_COUNT][i/COL_COUNT]=t%(size/2); Log.i("loadCards()", "card["+(i%COL_COUNT)+ "]["+(i/COL_COUNT)+"]=" + cards[i%COL_COUNT] [i/COL_COUNT]); } } private TableRow createRow(int y){ TableRow row = new TableRow(context); row.setHorizontalGravity(Gravity.CENTER); for (int x = 0; x < COL_COUNT; x++) { row.addView(createImageButton(x,y)); } return row; } private View createImageButton(int x, int y){ Button button = new Button(context); button.setBackgroundDrawable(backImage); button.setId(100*x+y); button.setOnClickListener(buttonListener); return button; } class ButtonListener implements OnClickListener { @Override public void onClick(View v) { synchronized (lock) { if(firstCard!=null && seconedCard != null){ return; Lập trình Android – http://laptrinhdidong.vn Page 6 Trung tâm Tin học – ĐH KHTN } int id = v.getId(); int x = id/100; int y = id%100; turnCard((Button)v,x,y); } } private void turnCard(Button button,int x, int y) { button.setBackgroundDrawable(images.get(cards[x][y])); //playSelectedSound(); if(firstCard==null){ firstCard = new Card(button,x,y); } else{ if(firstCard.x == x && firstCard.y == y){ return; } seconedCard = new Card(button,x,y); TimerTask tt = new TimerTask() { @Override public void run() { //try{ synchronized (lock) { handler.sendEmptyMessage(0); } //} //catch (Exception e) { // Log.e("E1", e.getMessage()); //} } }; Timer t = new Timer(false); t.schedule(tt, 800); } } } class UpdateCardsHandler extends Handler{ Lập trình Android – http://laptrinhdidong.vn Page 7 Trung tâm Tin học – ĐH KHTN @Override public void handleMessage(Message msg) { synchronized (lock) { checkCards(); } } public void checkCards(){ if(cards[seconedCard.x][seconedCard.y] == cards[firstCard.x] [firstCard.y]){ playCorrectSound(); firstCard.button.setVisibility(View.INVISIBLE); seconedCard.button.setVisibility(View.INVISIBLE); count ++; if(count == (ROW_COUNT*COL_COUNT)/2){ winDialog(); } } else { seconedCard.button.setBackgroundDrawable(backImage); firstCard.button.setBackgroundDrawable(backImage); } firstCard=null; seconedCard=null; } } public void playSelectedSound(){ //mp = MediaPlayer.create(this, R.raw.click2); mp.start(); } public void playCorrectSound(){ mp = MediaPlayer.create(this, R.raw.correct); mp.start(); } public void playInCorrectSound(){ mp.start(); } public void stopSound(){ if(mp != null){ mp.stop(); mp.release(); mp = null; } } public void clickSound(){ stopSound(); Lập trình Android – http://laptrinhdidong.vn Page 8 Trung tâm Tin học – ĐH KHTN mp = MediaPlayer.create(this, R.raw.brickogg); mp.start(); } private void winDialog(){ AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setTitle("Ban co muon choi tiep ko"); alert.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub singleChoiceDialog(); } }) .setNegativeButton("Thoat", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.cancel(); } }) .show(); } public void singleChoiceDialog(){ final CharSequence[] items = {"DE", "Medium", "Hard", "Exelent"}; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Chon muc do choi"); builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int item) { // TODO Auto-generated method stub //Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); switch(item){ case 0: newGame(4,4); break; case 1: newGame(4,5); break; case 2: newGame(4,6); break; case 3: newGame(4,7); } Lập trình Android – http://laptrinhdidong.vn Page 9 Trung tâm Tin học – ĐH KHTN } }).setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); AlertDialog alert = builder.create(); alert.show(); } } Ngoài ra còn 1 số file hình ảnh, âm thanh khác các bạn download Project để tìm hiểu thêm. Lập trình Android – http://laptrinhdidong.vn Page 10 [...]...Trung tâm Tin học – ĐH KHTN Mọi ý kiến đóng góp các bạn vui lòng gữi bài về forum: www.forum.laptrinhdidong.vn Rất mong nhận được sự phản hồi từ các bạn Lập trình Android – http://laptrinhdidong.vn Page 11 . Toast.LENGTH_SHORT).show(); switch(item){ case 0: newGame(4,4); break; case 1: newGame(4,5); break; case 2: newGame(4,6); break; case 3: newGame(4,7); } Lập trình Android – http://laptrinhdidong.vn Page. android:layout_height="wrap_content" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_gravity="center" android:gravity="center" android:typeface="normal". android:typeface="normal" android:bufferType="normal" android:textColor="#FFEEAA" /> </TableRow> <TableRow android:id="@+id/TableRow03" Lập trình Android –

Ngày đăng: 14/08/2014, 11:20

TỪ KHÓA LIÊN QUAN

w