Monday, September 10, 2012

Game đổ xúc xắc đánh cược

Mình vừa làm xong bài đổ xúc xắc nên đem lên cho mọi người góp ý. Bài này mình làm được những phần sau:

- Hiệu ứng đổ xúc xắc - cho con xúc xắc quay ban đầu nhanh rồi chậm dần
- Đánh cược.
- Bắt lỗi phần đánh cược.

Dưới đây là màn hình ban đầu:


Còn này là màn hình chơi:


Màn hình bắt lỗi:



Kết thúc trò chơi:



Ánh xạ:



Code của file .m


PHP Code:
//
//  NhatNgheViewController.m
//  Xucxac
//
//  Created by Duy Lee on 9/8/12.
//  Copyright (c) 2012 Duy Lee. All rights reserved.
//

#import "NhatNgheViewController.h"
@interface NhatNgheViewController ()

@
end
@implementation NhatNgheViewController

int nameIMG
; int flag; // xac dinh thoi diem xuc xac tang hay giam toc do - (void)viewDidLoad {
    [
super viewDidLoad];
    
// Do any additional setup after loading the view, typically from a nib.
    
nameIMG = 1;
}

- (
void)viewDidUnload {
    [
imgDice release];
    
imgDice = nil;
    [
segBetting release];
    
segBetting = nil;
    [
lblResult release];
    
lblResult = nil;
    [
lblMyMoney release];
    
lblMyMoney = nil;
    [
txtBetting release];
    
txtBetting = nil;
    [
imgFace release];
    
imgFace = nil;
    [
btnBet release];
    
btnBet = nil;
    [
super viewDidUnload];
    
// Release any retained subviews of the main view. }

- (BOOL)
shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (
interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (
void)dealloc {
    [
imgDice release];
    [
segBetting release];
    [
lblResult release];
    [
lblMyMoney release];
    [
txtBetting release];
    [
imgFace release];
    [
btnBet release];
    [
super dealloc];
}
- (
IBAction)btnPlay:(id)sender {
    
flag = 0;
    
lblResult.text = @"";
    
imgFace.image = [UIImage imageNamed:@"big_smile.png"];
    
segBetting.enabled = false;
    
btnBet.enabled = false;
    
    if ([
txtBetting.text intValue] < 0) {
        
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Illegal" message:@"Please, put a number bigger than 0" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [
alert show];
        [
alert release];
        return;
    }
    if ([
txtBetting.text isEqualToString:@""]) {
        
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Illegal" message:@"Please, put a number" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [
alert show];
        [
alert release];
        return;
    }
    if ([
txtBetting.text intValue] > [lblMyMoney.text intValue]) {
        
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Illegal" message:@"Please, put a number smaller than your money" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [
alert show];
        [
alert release];
        return;
    }
    
    
    
timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(toDice) userInfo:nil repeats:YES];
    
timerStop = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(stop) userInfo:nil repeats:NO];
}
- (
void)toDice{
    
    
nameIMG = arc4random() % 6 + 1;;
    
    
NSString *str = [NSString stringWithFormat:@"%i.gif",nameIMG];
    
imgDice.image = [UIImage imageNamed:str];
    
    
float tm = timer.timeInterval;
    [
timer invalidate];
    
float minus = arc4random() % 10 + 1;

    
// thoi diem de giam toc do xuc xac( 3 giai doan giam)
    
if (timer.timeInterval > 0.2) {
        
flag = 1;
    }
    if (
timer.timeInterval > 0.5) {
        
flag = 2;
    }
    if (
flag == 0) {
        
timer = [NSTimer scheduledTimerWithTimeInterval:tm + minus/1000 target:self selector:@selector(toDice) userInfo:nil repeats:YES];
    }
    if (
flag == 1) {
        
timer = [NSTimer scheduledTimerWithTimeInterval:tm + minus/100 target:self selector:@selector(toDice) userInfo:nil repeats:YES];
    }
    if (
flag == 2) {
        
timer = [NSTimer scheduledTimerWithTimeInterval:tm + minus/10 target:self selector:@selector(toDice) userInfo:nil repeats:YES];
    }
    
}
- (
void)stop{
    [
timer invalidate];
    
segBetting.enabled = true;
    
btnBet.enabled = true;
    
    if ((
segBetting.selectedSegmentIndex == 0 && nameIMG % 2 == 0) || (segBetting.selectedSegmentIndex == 1 && nameIMG % 2 == 1) ) {
        
        
lblResult.text = [NSString stringWithFormat:@"You win %i$",[txtBetting.text intValue]];
        
imgFace.image = [UIImage imageNamed:@"money-icon.png"];
        
int money = [lblMyMoney.text intValue] + [txtBetting.text intValue];
        
lblMyMoney.text = [NSString stringWithFormat:@"%i",money];
    }
    else {
        
lblResult.text = [NSString stringWithFormat:@"You lose %i$",[txtBetting.text intValue]];
        
imgFace.image = [UIImage imageNamed:@"cry-icon.png"];
        
int money = [lblMyMoney.text intValue] - [txtBetting.text intValue];
        
lblMyMoney.text = [NSString stringWithFormat:@"%i",money];
        
        if (
money == 0) {
            
lblResult.text = @"Game over!";
            
txtBetting.text = @"0";
            
txtBetting.enabled = false;
            
btnBet.enabled = false;
        }
    }
}
@
end 
Mong mọi người xem thử và góp ý cho bài mình thêm hoàn thiện nhá!

link down: nhấn vào đây
link youtube: http://youtu.be/5DxqGKu_Pg8

No comments:

Post a Comment