我的第一个Apple Watch小游戏——猜数字(Swift)

这是一个在AppleWatch上实现的一个小型App,开发语言为Swift。是一个猜数字的游戏,屏幕上会出现不同数字的滚动,并能控制游戏的开始结束,让别人来猜数字。是不是很有意思。还可以多个人来玩这个游戏,比大家谁最后的数字大。 该应用我已经上传至 https://github.com/chenyufeng1991/GuessNumber
  。

由于该应用我主要是在Watch上实现的,所以在手机上不会有任何的效果,只会有一个白色的界面而已。实现步骤如下:

(1)新建一个iOS中的Apple Watch应用,如图:

(2)然后导入3张图片,分别标示1,2,3. 在Interface.storyboard中设计如下:

(3)在InterfaceController.swift中实现如下:

import WatchKit
import Foundation

class InterfaceController: WKInterfaceController {

  @IBOutlet var image: WKInterfaceImage!
  @IBOutlet var button: WKInterfaceButton!

  //定时器;
  var timer:NSTimer!

  //判断是开始还是结束轮播;
  var isStart:Bool = true

  //图片下标;
  var index:Int = 1

  override func awakeWithContext(context: AnyObject?) {
    super.awakeWithContext(context)

    // Configure interface objects here.
  }

  override func willActivate() {
    // This method is called when watch view controller is about to be visible to user
    super.willActivate()
  }

  override func didDeactivate() {
    // This method is called when watch view controller is no longer visible
    super.didDeactivate()
  }

  @IBAction func buttonClicked() {

    if(isStart){//开始滚动图片

      addTimer()
      button.setTitle("结束")

    }else{//停止滚动图片
      self.timer.invalidate()
      self.timer = nil
      button.setTitle("开始")

    }

    isStart = !isStart
  }

  func addTimer(){   //图片轮播的定时器;
    self.timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "nextImage:", userInfo: nil, repeats: true)
  }

  func nextImage(sender:AnyObject!){

    let str = "img" + String(index)

    image.setImageNamed(str)

    index++
    if(index == 4){

      index = 1
    }

  }

}

(4)实现效果如下:

github主页:https://github.com/chenyufeng1991  。欢迎大家访问!

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-28 06:47:54

我的第一个Apple Watch小游戏——猜数字(Swift)的相关文章

python写的第一个简单小游戏-猜数字

1 #Filename:game1.py 2 3 guess=10 4 running=True 5 while running: 6 try: 7 answer=int(raw_input('Guess what i think:')) 8 except: 9 print 'Please input interga\n' 10 continue 11 12 if answer<guess: 13 print 'Your answer is too small\n' 14 continue 15

小游戏-猜数字

效果图: 游戏说明: 浏览器随机生成0-100以内的一个数字,在输入框中填写你猜测的数字,猜测范围是0-100以内的正整数哦! 有十次机会猜测,且在这十次猜测中都会对每次的猜测数字进行提示.. 代码 html 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>猜数字小游戏</title>

弱智python小游戏猜数字

from random import randintnum = randint(0,100)print("Guess what I think:?")bingo = Falsewhile bingo == False: answer = int(input()) if answer < num: print("too small!") if answer > num: print("too big") if answer == num

java小游戏——猜数字

import java.util.ArrayList; import java.util.List; import java.util.Random; public class Num01 { static List<Integer> numList = new ArrayList<>(); public static void createNum(){ List<Integer> list = new ArrayList<>(); for(int i =

CASIO 5800P计算器游戏--猜数字游戏

CASIO 5800P 计算器游戏--猜数字游戏原代码 我编的计算器小游戏--猜数字游戏 LbI I "xxGUESS NUMBERxx xPROGRAMMER:JCHx ---------------- START>>>>>>>[EXE]"◢ LbI Q "xxxDIFFICULTYxxx [1EASY] [2MIDDLE] [3HARD]"?→N: N=1=>GOTO N:N=2=>GOTO O: N=3=&

Cocos2d-X开发一个简单的小游戏

学了这么久Cocos2d-X,今天终于可以做出一个简单的小游戏了,游戏非常简单,通过菜单项控制精灵运动 在做游戏前,先学一个新概念 调度器(scheduler): Cocos2d-x调度器为游戏提供定时事件和定时调用服务.所有Node对象都知道如何调度和取消调度事件,使用调度器有几个好处: 每当Node不再可见或已从场景中移除时,调度器会停止. Cocos2d-x暂停时,调度器也会停止.当Cocos2d-x重新开始时,调度器也会自动继续启动. Cocos2d-x封装了一个供各种不同平台使用的调度

【C语言探索之旅】 第一部分第八课:第一个C语言小游戏

? 内容简介 1.课程大纲 2.第一部分第八课:第一个C语言小游戏 3.第一部分第九课预告: 函数 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言编写三个游戏. C语言编程基础知识 什么是编程? 工欲善其事,必先利其器 你的第一个程序 变量的世界 运算那点事 条件表达式 循环语句 实战:第一个C语言小游戏 函数 练习题 习作:完善第一个C语言小游戏 C语言高级技术 模块化编程 进击的指针,C语言王牌 数组 字符串 预处理 创建你自己的变量类型 文

html5面向对象做一个贪吃蛇小游戏

canvas加面向对象方式的贪吃蛇 2016-08-25 这个小游戏可以增加对面向对象的理解,可以加强js逻辑能力,总之认真自己敲一两遍收获还是不少啊!!适合刚学canvas的同学练习!! 废话不多说,直接来讲思路和代码. ----------------------------------------------------------------------------------------------------------------- 开发思路:首先要有蛇吃的食物,就是一个个canv

第一个c语言实现的猜数字游戏

#include <stdio.h> #include <Windows.h> #include <stdlib.h> #include <stdio.h> #include <Windows.h> #include <stdlib.h> #include <time.h> void output() { printf("*********************  欢迎玩猜数字游戏   ************