Skip to content

Instantly share code, notes, and snippets.

@FarisR99
Last active August 29, 2015 14:05
Show Gist options
  • Save FarisR99/2055fc98e91bbf759fb4 to your computer and use it in GitHub Desktop.
Save FarisR99/2055fc98e91bbf759fb4 to your computer and use it in GitHub Desktop.
BlockClicker game for iOS.
//
// ViewController.swift
// Block Clicker
//
// Created by Faris on 19/08/2014.
// Copyright (c) 2014 KingFaris10. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet var imgBlock: UIImageView = nil;
@IBOutlet var btnReset: UIButton = nil;
@IBOutlet var btnImage: UIButton
@IBOutlet var lblScore: UILabel = nil;
@IBOutlet var lblWon: UILabel = nil;
var userScore:Int = 0;
var userWon:Bool = false;
override func viewDidLoad() {
super.viewDidLoad();
self.userScore = 0;
self.userWon = false;
self.lblScore.text = "\(self.userScore)";
self.lblWon.text = "";
self.lblWon.hidden = true;
self.imgBlock.image = UIImage(named: "1");
self.imgBlock.animationImages = self.getAnimations(6);
}
func getAnimations(size:Int) -> [UIImage] {
var animations:[UIImage] = [];
for index in 1...size {
animations.append(UIImage(named: "\(index)"));
}
for index in 0...(size - 1) {
animations.append(animations[(size - index) - 1]);
}
return animations;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning();
}
@IBAction func onButtonPress(sender:UIButton) {
switch sender.tag {
case 0:self.onButtonReset();
case 3:self.onImageTapped();
default:return;
}
}
func onButtonReset() {
self.userScore = 0;
self.userWon = false;
self.lblScore.text = "\(self.userScore)";
self.lblWon.text = "";
self.lblWon.hidden = true;
if (self.imgBlock.isAnimating()) {
self.imgBlock.stopAnimating();
}
}
func onImageTapped() {
if (!self.userWon) {
self.imgBlock.animationRepeatCount = 1;
self.imgBlock.startAnimating();
var scoreIncrementer:Int = 1;
switch self.userScore {
case 0...49:scoreIncrementer = 1;
case 50...99:scoreIncrementer = 2;
case 100...199:scoreIncrementer = 5;
case 200...299:scoreIncrementer = 10;
case 300...499:scoreIncrementer = 20;
case 500...1499:scoreIncrementer = 50;
default:scoreIncrementer = 100;
}
self.userScore += scoreIncrementer;
self.lblScore.text = "\(self.userScore)";
if (self.userScore >= 50000) {
self.endGame();
}
}
}
func endGame() {
self.userWon = true;
self.lblWon.text = "You reached \(self.userScore) and beat the game! Well done.";
self.lblWon.hidden = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment