Creating a Bamboozle like game in SpriteKit with Swift

Many years ago there was a game on Teletext called Bamboozle! In the game, players had to correctly answer between 12 and 30 questions, in order correctly. Each question had 4 possible answers (selected using the Red, Green, Blue and Yellow buttons on the remote control) If a player got one question wrong they had to restart the entire game. For those of you who remember Teletext and how slow it was loading a page, this could be a frustrating process

This tutorial will help you build a game to replicate the functionality of the Bamboozle game

  1. Create a new sprite kit game for iPhone, using Swift, and call it Bamboozle.
  2. Delete the GameScene.sks and GameScene.swift files
  3. Inside the viewDidLoad method, modify the line of code that starts if let scene so it reads as show below (note the word 'if' is removed, along with the opening brace):
    let scene = GameScene(size: view.bounds.size) 
  4. Remove the extraneous closing } at the end of the method
  5. Create a new SKScene class called GameScene (use the Cocoa Touch template and use Swift as the language)
  6. If the first line of code in the GameScene file states import UIKit, change it to read import SpriteKit
  7. Add the following two methods as shown below. If you start by typing ‘init’, you can choose from the code suggestions to save typing the whole statements manually:
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
        
    override init(size: CGSize) {
        super.init(size: size)
    }
  8. Add the method shown below in the GameScene class (between the braces), once created, delete the ‘code’ placeholder:
    override func didMove(to view: SKView) {
    }
  9. Inside the didMoveToView method add the code below to create a label for the question:
    //set background color
    backgroundColor = SKColor.black
            
    //create a label node for the question
    let question = SKLabelNode(text: "What is 4 + 2?")
    question.fontName = "Menlo"
    question.position = CGPoint(x:frame.midX, y:frame.maxY-50)
            
    //add the question to the scene
    addChild(question)
  10. Run the game to check that the question is displayed.
    The next stage is to create the possible answers. The code below shows how to add the first answer.
  11. Add, then duplicate and modify the code so there are four possible answers with values of 2, 4, 6, and 8 using the colours red, green, yellow and blue. Align them roughly as shown in the screenshot above:
    let answer1 = SKLabelNode(fontNamed: "Menlo")
    answer1.text = "2"
    answer1.position = CGPoint(x:70, y:200)
    answer1.fontColor = SKColor.red
    addChild(answer1)
  12. Run the project to check the answers look like they are in an appropriate location
    We now need to ensure that if the user touches the correct answer they progress to the next screen, but as yet there is no ‘next’ screen.
  13. Create a new SKScene class, name the file Question2 and make it a subclass of SKScene
  14. Implement the same methods for this scene as you did in step 8
  15. Implement the didMoveToView method, and add code to set the background colour to blue
    Now we have an empty scene that we can transition to if the player selects the right answer.
  16. Return to the GameScene.swift file and under the line that starts class, and outside of any other function, add the following code:
    //implicitly unwrapped optional (denoted by the ! after SKLabelNode)
    private var rightAnswer : SKLabelNode!
    The ! after SKLabelNode denotes that this variable is optional, but it will be treated by default as if it has a value. (The alternative for an optional variable is to use a question mark ?). If we were to omit the ! or ?, then we would have to provide a default value for the right answer.
  17. Under the code where the last answer label is added to the screen add the following line of code:
    rightAnswer = answer3;
  18. The last stage is to write code which checks if the user has pressed the correct button, and transitions to the new scene if they have. Implement the method to handle touches – you can start typing touchesBegan and hit the tab key when the correct suggestion appears. Then add code to the method so the whole thing appears as shown below:
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
        let touch :UITouch = touches.first!
            
        let touchLocation = touch.location(in: self)
            
        if  rightAnswer.contains(touchLocation){
            let q2Scene = Question2(size:size)
            let fade = SKTransition.fade(withDuration: 0.5)
            view?.presentScene(q2Scene, transition: fade)
        }
        else
        {
            //on the first screen we just stay where we are (until the player gets the right answer)
            //for later scenes we will add code here to go right back to the first scene
        }
    }
    You should now be able to run the project. Nothing will happen if the incorrect button is pressed, but when the right answer is selected the scene will change.
  19. You should now be able to add questions in the Question2 scene, add code to send the player back to the start if they are wrong, using code adapted from that which you have already used.
  20. Continue the game until you have 5 sets of questions. Try and make the questions hard, and move the position of the correct answer.

You can download a completed version of the game from GitHub

Additional Tasks

Advanced Task

It should be possible to simply reuse one scene, loading the question and answer combination from a file. Carry out further research to identify how this might be done and attempt to implement it.