Swift Game Programming for Absolute Beginners by Arjan Egges

Swift Game Programming for Absolute Beginners by Arjan Egges

Author:Arjan Egges
Language: eng
Format: epub, pdf
Publisher: Apress, Berkeley, CA


Maintaining a Score

Scores are often a very effective way to motivate players to continue playing. High scores work especially well in that regard because they introduce a competitive factor into the game: you want to be better than AAA or XYZ (many early arcade games allowed only three characters for each name in the high-score list, leading to very imaginative names). High scores are so motivating that third-party systems exist to incorporate them into games. These systems let users compare themselves against thousands of other players around the world. In the Painter game, you keep it simple and add the property score to the GameWorld class in which to store the current score:

var score = 0

The player starts with a score of zero. Each time a paint can falls outside the screen, the score is updated. If a can of the correct color falls out of the screen, 10 points are added to the score. If a can isn’t the right color, the player loses a life.

The score is a part of what is called the economy of a game. The game’s economy basically describes the different costs and merits in the game and how they interact. When you make your own game, it’s always useful to think about its economy. What do things cost, and what are the gains of executing different actions as a player? And are these two things in balance with each other?

You update the score in the PaintCan class, where you can check whether the can falls outside the screen. If so, you check whether it has the right color, and update the score and the number of player lives accordingly. Then you hide the PaintCan object so that it can fall again, like so:

let top = CGPoint(x: self.position.x, y: self.position.y + red.size.height/2)

if GameScene.world.isOutsideWorld(top) {

if color != targetColor {

GameScene.world.lives -= 1

} else {

GameScene.world.score += 10

collectPointsSound.play()

}

self.hidden = true

}

In this code, you can also see that whenever a can of the right color falls off the screen, you play a sound effect stored in the collectPointsSound property.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.