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
|
// Brain Party
// Copyright (C) 2010 Paul Hudson (http://www.tuxradar.com/brainparty)
// Brain Party is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "numbersnake.h"
#include "Minigame.h"
#include <sstream>
#include <iostream>
#include <algorithm>
#include <iterator>
BPMiniGame_NumberSnake::BPMiniGame_NumberSnake(BPGame* game) : BPMiniGame(game) {
sfcBackground = TheGame->LoadBitmap("numbersnake", 320, 416);
sfcBlack = TheGame->LoadBitmap("numbersnake_black", 320, 128);
CurrentLevel = 0;
LastStateChange = LastQuestionChange = -1;
NumCorrect = 0;
sfcQuestionParts = NULL;
Answer = 0;
QuestionPos = 0;
QuestionSpeed = 1250;
TimeStarted = 0;
GameTitle = "Number Snake";
GameHelp = "Watch carefully as simple mathematics questions flash on your screen, then see if you can figure out the answers!";
GameHelp2 = "This is a game that tests your ability to perform simple sums very quickly. In fact, if you're not quick then you won't have finished the first sum by the time the second sum appears, so there really is no time to hang around!";
MiniGameType = PUZZLE;
NumStrings.Add("zero");
NumStrings.Add("one");
NumStrings.Add("two");
NumStrings.Add("three");
NumStrings.Add("four");
NumStrings.Add("five");
NumStrings.Add("six");
NumStrings.Add("seven");
NumStrings.Add("eight");
NumStrings.Add("nine");
}
BPMiniGame_NumberSnake::~BPMiniGame_NumberSnake() {
SAFE_DELETE(sfcBackground);
SAFE_DELETE(sfcBlack);
NumStrings.Clear();
if (sfcQuestionParts != NULL) {
for (int i = 0; i < QuestionLength; ++i) {
SAFE_DELETE(sfcQuestionParts[i]);
}
delete[] sfcQuestionParts;
}
}
void BPMiniGame_NumberSnake::OnMouseDown() {
}
void BPMiniGame_NumberSnake::OnMouseMove() {
}
void BPMiniGame_NumberSnake::OnMouseUp() {
if (TouchEvent.Y < 100) {
// possible click on score
if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, 0, 4, 52, 47)) SubmitAnswer(0);
if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, 66, 4, 52, 47)) SubmitAnswer(1);
if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, 134, 4, 52, 47)) SubmitAnswer(2);
if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, 201, 4, 52, 47)) SubmitAnswer(3);
if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, 272, 4, 52, 47)) SubmitAnswer(4);
if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, 0, 52, 52, 47)) SubmitAnswer(5);
if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, 66, 52, 52, 47)) SubmitAnswer(6);
if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, 134, 52, 52, 47)) SubmitAnswer(7);
if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, 201, 52, 52, 47)) SubmitAnswer(8);
if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, 272, 52, 52, 47)) SubmitAnswer(9);
}
}
void BPMiniGame_NumberSnake::Start() {
TimeStarted = TheGame->TickCount;
LevelUp();
}
int BPMiniGame_NumberSnake::GetWeight() {
return MinMax(100 * NumCorrect);
}
void BPMiniGame_NumberSnake::Render() {
TheGame->DrawImage(sfcBackground, 0, 0);
if (GameState == SHOWING) {
if (QuestionPos == QuestionLength - 1) {
float fadeamount = min(0.65f, Fader);
glColor4f(1.0f, 1.0f, 1.0f, fadeamount);
TheGame->DrawImage(sfcBlack, 0, 165);
} else {
glColor4f(1.0f, 1.0f, 1.0f, 0.65f);
TheGame->DrawImage(sfcBlack, 0, 165);
}
glColor4f(1.0f, 1.0f, 1.0f, Fader);
TheGame->DrawString(sfcQuestionParts[QuestionPos], 0, 170);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}
if (GameState == CORRECT) {
RenderCorrect();
} else if (GameState == WRONG) {
RenderWrong();
}
}
void BPMiniGame_NumberSnake::Tick() {
if (GameState == SHOWING) {
if (LastQuestionChange + QuestionSpeed < TheGame->TickCount) {
++QuestionPos;
LastQuestionChange = TheGame->TickCount;
if (QuestionPos == QuestionLength) {
GameState = WAITING;
}
}
float start = TheGame->TickCount + QuestionSpeed;
float end = LastQuestionChange + QuestionSpeed;
float alpha = (start - end) / QuestionSpeed;
Fader = TheGame->SmoothStep(1.0f, 0.0f, alpha);
}
if (LastStateChange != -1 && LastStateChange + 500 < TheGame->TickCount) {
if (!MarathonMode && CurrentLevel >= 5) {
Success();
} else {
LevelUp();
}
}
}
void BPMiniGame_NumberSnake::SubmitAnswer(int answer) {
if (GameState != WAITING) return;
if (answer == Answer) {
TheGame->PlaySound("correct");
GameState = CORRECT;
++NumCorrect;
} else {
TheGame->PlaySound("wrong");
GameState = WRONG;
}
LastStateChange = TheGame->TickCount;
}
void BPMiniGame_NumberSnake::LevelUp() {
++CurrentLevel;
QuestionPos = 0;
LastQuestionChange = TheGame->TickCount;
QuestionSpeed -= 50;
BPList<int> Numbers;
Answer = TheGame->RandomRange(1, 6);
ostringstream PrintMe;
PrintMe << "What is " << NumStrings[Answer];
for (int i = 0; i < 4 + CurrentLevel; ++i) {
Numbers.Add(TheGame->RandomRange(1, 6));
}
int RandNum;
for (int i = 0; i < Numbers.Count; ++i) {
int num = Numbers[i];
// we want to allow subtraction only if it doesn't dip below zero!
if (Answer - num > 0) {
// only allow multiplication if the total doesn't go above 25
if (Answer * num < 26) {
RandNum = TheGame->RandomRange(0, 2);
} else {
RandNum = TheGame->RandomRangeExcept(0, 2, 1);
}
} else {
// only allow multiplication if the total doesn't go above 25
if (Answer * num < 26) {
RandNum = TheGame->RandomRange(0, 1);
} else {
RandNum = 0;
}
}
switch (RandNum) {
case 0:
// add
PrintMe << " plus " << NumStrings[num];
Answer += num;
break;
case 1:
PrintMe << " times " << NumStrings[num];
Answer *= num;
break;
case 2:
// subtract
PrintMe << " minus " << NumStrings[num];
Answer -= num;
break;
}
}
while (Answer >= 10) {
int newnum = TheGame->RandomRange(5, 9);
PrintMe << " minus " << NumStrings[newnum];
Answer -= newnum;
}
PrintMe << " ?";
istringstream iss(PrintMe.str());
vector<string> parts;
copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter<vector<string> >(parts));
if (sfcQuestionParts != NULL) {
for (int i = 0; i < QuestionLength; ++i) {
SAFE_DELETE(sfcQuestionParts[i]);
}
delete[] sfcQuestionParts;
}
QuestionLength = parts.size();
sfcQuestionParts = new SpriteFont*[QuestionLength];
int i = 0;
for (int i = 0; i < parts.size(); ++i) {
sfcQuestionParts[i] = NULL;
TheGame->AllocString(&sfcQuestionParts[i], parts[i].c_str(), EPIC, 320, 227, CENTRED);
}
GameState = SHOWING;
LastStateChange = -1;
}
void BPMiniGame_NumberSnake::SetMarathon() {
MarathonMode = true;
GameHelp = "Watch carefully as simple mathematics questions flash on your screen, then see if you can figure out the answers!";
}
|