首页 / IOS / ios-UI-汤姆猫德游戏实现
ios-UI-汤姆猫德游戏实现
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了ios-UI-汤姆猫德游戏实现,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6100字,纯文字阅读大概需要9分钟。
内容图文
![ios-UI-汤姆猫德游戏实现](/upload/InfoBanner/zyjiaocheng/1316/b7ec6c9c2cc3458b87364926bb62db48.jpg)
//
// ViewController.m
// UI-猜拳游戏
//
// Created by jzq_mac on 15/7/15.
// Copyright (c) 2015年 jzq_mac. All rights reserved.
//
#import "ViewController.h"
#define TIME 61
@interface ViewController ()
{
UILabel *timeLable;
UILabel *scoreLable;
UILabel *observeLable;
UIImageView *imgeView;
UIImageView *imgeView1;
UIButton *button;
int score;
int time;
int number;
NSTimer *timer;
NSArray *imageList;
NSArray *buttonImageList;
NSArray *faceList;
BOOL Start;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[ super viewDidLoad];
// 设置背景图
UIImageView *imageView = [[UIImageViewalloc]initWithFrame:[UIScreenmainScreen].bounds];
imageView.image = [UIImageimageNamed:@"钱.png"];
[self.viewaddSubview:imageView];
imageList = @[@"石头.png",@"剪刀.png",@"布.png"];
buttonImageList = @[@"石头1.png",@"剪刀1.png",@"布1.png"];
faceList = @[@"哭.png",@"笑.png",@"挣扎.png"];
//创建笑脸视图
imgeView1 = [[UIImageViewalloc]initWithFrame:CGRectMake(135, 320, 100, 100)];
imgeView1 .image = [UIImageimageNamed:@"美女1.png"];
[ self .viewaddSubview:imgeView1];
time = TIME;
// 调用视图
[ self creatView];
[ self flashView];
[ self creatButon];
// 定时器
timer = [NSTimerscheduledTimerWithTimeInterval:1 target:self selector:@selector(flashView) userInfo: nil repeats:YES];
[[NSRunLoopcurrentRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
timer .fireDate = [NSDatedistantFuture];
timeLable .text = [NSStringstringWithFormat:@"时间: %d 秒",time];
}
//创建视图
- (void)creatView
{
scoreLable = [[UILabelalloc]initWithFrame:CGRectMake(0, 20, 100, 50)];
[ self .viewaddSubview:scoreLable];
scoreLable .text = [NSStringstringWithFormat:@"得分: %d 分",score];
scoreLable .textColor = [UIColorblueColor];
scoreLable .backgroundColor = [UIColorgrayColor];
scoreLable .textAlignment = NSTextAlignmentCenter;
scoreLable.alpha = 0.4;
timeLable = [[UILabelalloc]initWithFrame:CGRectMake(275, 20, 100, 50)];
[ self .viewaddSubview:timeLable];
timeLable .backgroundColor = [UIColorgrayColor];
timeLable .textColor = [UIColorblueColor];
timeLable .textAlignment = NSTextAlignmentCenter;
timeLable .text = [NSStringstringWithFormat:@"时间: %d 秒",time];
timeLable.alpha = 0.4;
observeLable = [[UILabelalloc]initWithFrame:CGRectMake(0, 450, CGRectGetHeight([UIScreenmainScreen].bounds), 50)];
[ self .viewaddSubview:observeLable];
observeLable .backgroundColor = [UIColorgrayColor];
observeLable.alpha = 0.6;
// observeLable.textAlignment = NSTextAlignmentCenter;
observeLable .font = [UIFontboldSystemFontOfSize:30];
observeLable . text = @" ~~~人 机猜 拳大 战~~~ ";
}
//闪烁图片
- (void)flashView
{
time--;
timeLable .text = [NSStringstringWithFormat:@"时间: %d 秒",time];
for (int i = 0; i < 1; i++) {
imgeView = [[UIImageViewalloc]initWithFrame:CGRectMake(135 + 100*i, 80, 100, 100)];
imgeView .image = [UIImageimageNamed:@"美女.png"];
[self.viewaddSubview:imgeView ];
int r = arc4random()%imageList.count;
number = r;
imgeView .image = [UIImageimageNamed:imageList[r]];
}
if (time == 0) {
timer .fireDate = [NSDatedistantFuture];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"大家一起猜" message:@"时间到" delegate:self cancelButtonTitle:nil otherButtonTitles:@"不服就再来啊", nil];
[alert show];
}
}
//创建button
- (void)creatButon
{
for (int i = 0; i < 3; i++) {
button = [[UIButtonalloc]initWithFrame:CGRectMake(35 + 100*i, 200, 100, 100)];
[self.viewaddSubview:button];
[ button setBackgroundImage:[UIImageimageNamed:buttonImageList[i]] forState:UIControlStateNormal];
button .backgroundColor = [UIColorredColor];
button .layer.cornerRadius = 50;
button .layer.masksToBounds = YES;
button.alpha = 0.6;
button.tag = i + 1;
[buttonaddTarget:self action:@selector(selectButton:) forControlEvents:UIControlEventTouchUpInside];
}
UIButton *startButton = [[UIButtonalloc]initWithFrame:CGRectMake(160, 550, 80, 80)];
[self.viewaddSubview:startButton];
[startButton setBackgroundImage:[UIImageimageNamed:@"開始.png"] forState:UIControlStateNormal];
[startButton setTitle:@"開始" forState:UIControlStateNormal];
[startButton setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
[startButton addTarget:self action:@selector(startGame:) forControlEvents:UIControlEventTouchUpInside];
}
//開始游戏
- (void)startGame:(UIButton *)sender
{
if (sender.selected !=YES) {
timer .fireDate = [NSDatedistantPast];
[sender setTitle:@"暂停" forState:UIControlStateNormal];
sender.selected = YES;
Start = YES;
}else
{
timer .fireDate = [NSDatedistantFuture];
[sender setTitle:@"開始" forState:UIControlStateNormal];
sender.selected = NO;
Start = NO;
}
}
//选择button的触发事件
- (void)selectButton:(UIButton *)sender
{
if (Start != NO) {
// sender.showsTouchWhenHighlighted = YES;
if (sender.tag == 1) {
if (number == 0) {
imgeView1.image = [UIImageimageNamed:faceList[2]];
}elseif(number == 1){
imgeView1.image = [UIImageimageNamed:faceList[1]];
score ++;
scoreLable .text = [NSStringstringWithFormat:@"总分: %d 分",score];
}elseif (number == 2){
imgeView1.image = [UIImageimageNamed:faceList[0]];
}
}
if (sender.tag == 2) {
if (number == 0) {
imgeView1.image = [UIImageimageNamed:faceList[0]];
}elseif(number == 1){
imgeView1.image = [UIImageimageNamed:faceList[2]];
}elseif (number == 2){
imgeView1.image = [UIImageimageNamed:faceList[1]];
score ++;
scoreLable .text = [NSStringstringWithFormat:@"总分: %d 分",score];
}
}
if (sender.tag == 3) {
if (number == 0) {
imgeView1.image = [UIImageimageNamed:faceList[1]];
score ++;
scoreLable .text = [NSStringstringWithFormat:@"总分: %d 分",score];
}elseif(number == 1){
imgeView1.image = [UIImageimageNamed:faceList[0]];
}elseif (number == 2){
imgeView1.image = [UIImageimageNamed:faceList[2]];
}
}
}
}
//弹窗事件
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
score = 0;
time = TIME;
scoreLable .text = [NSStringstringWithFormat:@"总分: %d 分",score];
timeLable .text = [NSStringstringWithFormat:@"时间:%d 秒",time];
timer .fireDate = [NSDatedistantPast];
}else{
timer .fireDate = [NSDatedistantPast];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
原文:http://www.cnblogs.com/liguangsunls/p/7144138.html
内容总结
以上是互联网集市为您收集整理的ios-UI-汤姆猫德游戏实现全部内容,希望文章能够帮你解决ios-UI-汤姆猫德游戏实现所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。