Back when I was still working at Amazon I decided to contribute to a DIY/Maker faire. I can’t remember the title of the event. I thought it would be fun to make a game that could use the buttons I had made in a previous build (see: Jeopardy Controllers are Easy). I thought a memory game would be fun and fairly easy to make in a week. I call it Sheep It! If it isn’t obvious, this is a play on words (or a bad pun) for both the moniker for code reviews at Amazon that pass (Ship It!) and the idea that one might be called a sheep for following a leader (which is what this game is all about).
I enjoy using QT and thought that I would like to learn how QML works. The code located at https://github.com/jjveleber/SheepIt is the result. I am sure number of hacks in this code is large. My only excuse is that I had little time to finish and was just having fun trying different things out. I was able to add animations fairly easily. For instance when a player scores there are some particle effects as well as a slide and sizing animations that all run in sequence.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
import Game 1.0 import QtQuick 2.2 import QtQuick.Layouts 1.1 import QtQuick.Particles 2.0 import QtMultimedia 5.0 Item { id:me property Game gameInstance Component.onCompleted: { yourScoreText.text = gameInstance.yourScore.scoreString(gameInstance.yourScore.scoreIndex); highScoreText.text = gameInstance.highScore.scoreString(gameInstance.highScore.scoreIndex); } SheepText { id:highScoreLable text: "High Score:" color: "#a01e01" anchors.left: parent.left anchors.top: parent.top anchors.topMargin: 20 anchors.leftMargin: 50 anchors.rightMargin: 50 anchors.bottomMargin: 50 } SheepText { id:highScoreText color: "#a01e01" anchors.left: highScoreLable.right anchors.verticalCenter: highScoreLable.verticalCenter anchors.leftMargin: 20 } SheepText { id:yourScoreLable text: "Your Score:" anchors.left: highScoreLable.left anchors.top: highScoreLable.bottom anchors.topMargin: 25 } SheepText { id:yourScoreText anchors.left: yourScoreLable.right anchors.verticalCenter: yourScoreLable.verticalCenter anchors.leftMargin: 20 } MouseArea { id:yourScoreTextMouseArea anchors.fill: yourScoreText } SheepText { id:yourNewScoreText anchors.left: yourScoreText.left opacity: 0.0 } states: [ State { name: "playerScored" }, State { name: "reset" } ] transitions: [ Transition { from: "*" to: "playerScored" animations: youScoredAnimation }, Transition { from: "*" to: "reset" animations: resetGameAnimation } ] SequentialAnimation { id: resetGameAnimation running: false // TODO: end of game screen reset after play ScaleAnimator { target: yourScoreText from: 1 to: 0 easing.type: Easing.InBounce; duration: 300 } PropertyAction { target: yourScoreText property: "text" value: "0" } ScaleAnimator { target: yourScoreText from: 0 to: 1 easing.type: Easing.OutBounce; duration: 300 } } SoundEffect{ id: slideSnd source: "qrc:/sndfx/whoosh" } SoundEffect{ id: clunkSnd source: "qrc:/sndfx/clunk" } SoundEffect{ id: chimeSnd source: "qrc:/sndfx/chime" } SoundEffect{ id:highScoreYay1Snd source: "qrc:/sndfx/yay1" } SoundEffect{ id:highScoreYay2Snd source: "qrc:/sndfx/yay2" } SequentialAnimation { id:youScoredAnimation running: false SequentialAnimation { ScriptAction { script: { yourScoreText.text = gameInstance.yourScore.scoreString(gameInstance.yourScore.scoreIndex -1); yourNewScoreText.text = gameInstance.yourScore.scoreString(gameInstance.yourScore.scoreIndex); } } ParallelAnimation { ScriptAction { script: { slideSnd.play(); } } YAnimator { id:yourNewScoreYAni target: yourNewScoreText; from: -me.y to: yourScoreText.y easing.type: Easing.InBack; duration: 500 } OpacityAnimator { target: yourNewScoreText from: 0.1 to: 1 easing.type: Easing.Linear; duration: 500 } ScaleAnimator { target: yourNewScoreText from: 0 to: 1 easing.type: Easing.OutBounce; duration: 300 } } ScriptAction { script: { clunkSnd.play(); chimeSnd.play(); shootingStarEmitter.burst(750); shootingStarBurst.pulse(300); } } ParallelAnimation { ScaleAnimator { target: yourNewScoreText from: 1 to: 0 easing.type: Easing.OutBounce; duration: 300 } ScriptAction { script: { yourScoreText.text = gameInstance.yourScore.scoreString(gameInstance.yourScore.scoreIndex); highScoreText.text = gameInstance.highScore.scoreString(gameInstance.highScore.scoreIndex); if(gameInstance.isHighScore) { function randomNumber(from, to) { return Math.floor(Math.random() * (to - from + 1) + from); } if(randomNumber(1,2) == 1) { highScoreYay1Snd.play(); } else { highScoreYay2Snd.play(); } } gameInstance.isPlayBack = true; } } } } PropertyAction { target: me properties: "state" value: "" } } ParticleSystem { id:yourScoreStarParticles anchors.fill: parent paused: false // Shooting star particles ImageParticle { source: "qrc:/pics/GoldStartParticle" color: "#ffefaf" colorVariation: 1.0 alpha: 0 entryEffect: ImageParticle.Scale rotationVariation: 180 rotationVelocity: 90 rotationVelocityVariation: 90 autoRotation: true } Emitter { id: shootingStarEmitter emitRate: 100 lifeSpan: 2000 x: yourScoreText.x y: yourScoreText.y width: yourScoreText.width height: yourScoreText.height velocity: PointDirection {xVariation: 50; yVariation: 50;} acceleration: PointDirection {xVariation: 120; yVariation: 120;} size: 32 sizeVariation: 16 shape: EllipseShape { fill: true } enabled: false } Emitter { id: shootingStarBurst emitRate: shootingStarEmitter.emitRate * 2 lifeSpan: 100 x: yourScoreText.x y: yourScoreText.y width: yourScoreText.width height: yourScoreText.height velocity: PointDirection {xVariation: 60; yVariation: 60;} acceleration: PointDirection {xVariation: 40; yVariation: 40;} size: 24 sizeVariation: 16 shape: EllipseShape { fill: true } enabled: false } } } |
This was a fun project. Some people were really good at playing Sheep It! There was a kid that played for about 20 minutes. It was impressive. I don’t think I could come close to beating his score. Speaking of scoring, I added another aspect to the game. This was a bit a social sub-game. I told the folks watching people play Sheep It! that I would give them one of the 555 PWM Controller kits that I had brought for sale if they could tell me how the scoring worked. It was fun. People created little teams and tried to figure it out. They would stand there and record scores trying to find a pattern. A few people got close and said that the numbers were all primes. Only one person got the correct answer of twin primes.
I think if I make another game like this I will use something like Unity or some other game specific platform. I would like to get some experience in this area and nothing beat having a project and a deadline to get that experience.