• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

蛇和 梯@@子 游戏,分别用oc和swift开发

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

蛇和 梯@@子 游戏,分别用oc和swift开发

心血来潮想学学swift,看了一下教程和开发文档,发现其实swift的很多类都是基于oc的,比如swift的UIButton是基于oc的UIButton。所以我想其实学过oc再学swift并不是像学新语言一样难,只是把一种方言翻译成另一种方言而已。基于这个思路我试着写了一个oc的蛇和 梯@@子 的游戏,然后再一句一句翻译成swift,竟然成功了,仰天大笑

先介绍一下这个简单的游戏:一个25宫格的地图,上面有若干个 梯@@子 和蛇,摇骰子走到 梯@@子 下面就能顺 梯@@子 爬上去,走到蛇头处,要顺着蛇滑下来,走到25格即为成功。


用到的类:UIImageView、UIButton、UILabel、CAKeyframeAnimation、NSTimer

下面这个表格是我的翻译词典:

OC

swift
category extension:可以写在类外面
- (float) check: (int) count { } func check: (count: Int) -> Float { }

int n = 0

n ++

var n: Int = 0

n += 1

NSString *str = @"你好"

str = [NSString stringwithformat:@"%s%d", str, n];

var str: String = "你好"

str += String(n)

NSArray * ary = [NSArray array]

ary = @[
              [NSValue valueWithCGPoint:CGPointMake(0.9, 0.1)],
              [NSValue valueWithCGPoint:CGPointMake(9.8, 9.7)]
            ];

var ary = [NSValue]()

ary = [

            NSValue(cgPoint: CGPoint(x: 0.1, y: 0.9)), 

            NSValue(cgPoint: CGPoint(x: 9.8, y: 9.7))

          ]

ary.append(NSValue(cgPoint: CGPoint(x: 0.1, y: 0.9)))

NSDictionary *dic

var dic = [Int: CGPoint]() 

字典可以用Int类型做key

dic[n] = CGPoint(x:x, y:y)添加值

没有NSMutableDictionary和NSDictionary 区分

UIColor *white =  [UIColor whiteColor] let white:Color = UIColor.white
UIView

let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))

self.view.addSubview(view)

UILabel

var countLabel = UILabel(frame: CGRect(x:CellHeight, y:CGFloat(CellHeight*3+wh), width:CGFloat(ViewWidth-CellHeight*2), height:(CellHeight)))

countLabel.textColor = UIColor.blue
countLabel.layer.cornerRadius = 10
countLabel.layer.borderColor = UIColor.blue.cgColor
countLabel.layer.borderWidth = 1
countLabel.textAlignment = NSTextAlignment.center
countLabel.text = "您已走了0步"
self.view.addSubview(countLabel)
UIButton

var diceV = UIButton(type: UIButtonType.custom)

diceV.frame = CGRect(x:CGFloat((ViewWidth-shaiziWH)/2), y:CGFloat(CellHeight*5+wh), width:shaiziWH, height:shaiziWH)

diceV.setBackgroundImage(UIImage(named: "1"), for: UIControlState.normal)
diceV.adjustsImageWhenDisabled = false
diceV.addTarget(self, action: #selector(self.shakeDice), for: UIControlEvents.touchUpInside)

self.view.addSubview(diceV)

//添加的点击事件

@objc func shakeDice() { }

[NSTimer scheduledTimerWithTimeInterval:0.2 repeats:YES block:^(NSTimer * _Nonnull timer) { // }]; Timer.scheduledTimer(withTimeInterval: TimeInterval(0.2), repeats: false, block: { // })
CAKeyframeAnimation *runAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
runAnimation.values = [NSArray arrayWithArray:ary];
runAnimation.fillMode = kCAFillModeForwards;
runAnimation.removedOnCompletion = NO;// 是否在动画完成后从 Layer 层上移除  回到最开始状态
runAnimation.duration = 1.0f;
runAnimation.repeatCount = 0;
peopleV.layer.anchorPoint = CGPointMake(0, 0);
[peopleV.layer addAnimation:runAnimation forKey:nil];
let runAnimation:CAKeyframeAnimation = CAKeyframeAnimation(keyPath: "position")//只能写position
runAnimation.values = [NSValue(cgPoint: geziDic[start]!), NSValue(cgPoint: geziDic[end]!)]
runAnimation.duration = 1.0
runAnimation.repeatCount = 0
runAnimation.isRemovedOnCompletion = false
runAnimation.fillMode = kCAFillModeForwards
peopleV.layer.anchorPoint = CGPoint(x: 0, y: 0)//默认是(0.5, 0.5)

peopleV.layer.add(runAnimation, forKey: nil)

最后,代码已上传到Git,地址:

点击打开链接获取OC代码


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift计算文本宽高发布时间:2022-07-13
下一篇:
iOS-Swift相比Objective-C有哪些优缺点发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap