| 1 | unit umathgame;
|
|---|
| 2 |
|
|---|
| 3 | {$mode objfpc}{$H+}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses
|
|---|
| 8 | Forms, ExtCtrls, BGRAVirtualScreen, BGRALabel, BGRAImageButton, BGRABitmap;
|
|---|
| 9 |
|
|---|
| 10 | type
|
|---|
| 11 |
|
|---|
| 12 | { TfrmJuego }
|
|---|
| 13 |
|
|---|
| 14 | TfrmJuego = class(TForm)
|
|---|
| 15 | btnIgualdad: TBGRAImageButton;
|
|---|
| 16 | btnIniciar: TBGRAImageButton;
|
|---|
| 17 | btnOperador: TBGRAImageButton;
|
|---|
| 18 | btnResultado: TBGRAImageButton;
|
|---|
| 19 | btnVariable1: TBGRAImageButton;
|
|---|
| 20 | btnVariable2: TBGRAImageButton;
|
|---|
| 21 | lblAciertos: TBGRALabel;
|
|---|
| 22 | lblIntentos: TBGRALabel;
|
|---|
| 23 | n_aciertos: TBGRALabel;
|
|---|
| 24 | n_intentos: TBGRALabel;
|
|---|
| 25 | n_tiempo: TBGRALabel;
|
|---|
| 26 | Opcion1: TBGRAImageButton;
|
|---|
| 27 | Opcion2: TBGRAImageButton;
|
|---|
| 28 | Opcion3: TBGRAImageButton;
|
|---|
| 29 | Opcion4: TBGRAImageButton;
|
|---|
| 30 | Opcion5: TBGRAImageButton;
|
|---|
| 31 | temporizador: TTimer;
|
|---|
| 32 | vsBarraEcuacion: TBGRAVirtualScreen;
|
|---|
| 33 | vsBarraOpciones: TBGRAVirtualScreen;
|
|---|
| 34 | vsFondo: TBGRAVirtualScreen;
|
|---|
| 35 | procedure btnIniciar_Click(Sender: TObject);
|
|---|
| 36 | procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
|
|---|
| 37 | procedure FormCreate(Sender: TObject);
|
|---|
| 38 | procedure FormShow(Sender: TObject);
|
|---|
| 39 | procedure Opcion1Click(Sender: TObject);
|
|---|
| 40 | procedure Opcion2Click(Sender: TObject);
|
|---|
| 41 | procedure Opcion3Click(Sender: TObject);
|
|---|
| 42 | procedure Opcion4Click(Sender: TObject);
|
|---|
| 43 | procedure Opcion5Click(Sender: TObject);
|
|---|
| 44 | procedure temporizadorTimer(Sender: TObject);
|
|---|
| 45 | procedure vsBarraEcuacionRedraw(Sender: TObject; Bitmap: TBGRABitmap);
|
|---|
| 46 | procedure vsBarraOpcionesRedraw(Sender: TObject; Bitmap: TBGRABitmap);
|
|---|
| 47 | procedure vsFondoRedraw(Sender: TObject; Bitmap: TBGRABitmap);
|
|---|
| 48 | private
|
|---|
| 49 | intentos, aciertos: integer;
|
|---|
| 50 | opcioncorrecta: integer;
|
|---|
| 51 | tiempo: integer;
|
|---|
| 52 | var1, var2, resultado, operador: integer;
|
|---|
| 53 | procedure Chequear(valor: integer);
|
|---|
| 54 | procedure Generar;
|
|---|
| 55 | end;
|
|---|
| 56 |
|
|---|
| 57 | procedure OperacionAleatoria(var Avar1, Avar2, Aresultado, AOperador: integer);
|
|---|
| 58 | function OperadorAString(Aoperador: integer): string;
|
|---|
| 59 | procedure SumaAleatoria(var Avar1, Avar2, Aresultado: integer);
|
|---|
| 60 | procedure RestaAleatoria(var Avar1, Avar2, Aresultado: integer);
|
|---|
| 61 | procedure MultiplicacionAleatoria(var Avar1, Avar2, Aresultado: integer);
|
|---|
| 62 | procedure DivisionAleatoria(var Avar1, Avar2, Aresultado: integer);
|
|---|
| 63 |
|
|---|
| 64 | var
|
|---|
| 65 | frmJuego: TfrmJuego;
|
|---|
| 66 |
|
|---|
| 67 | resourcestring
|
|---|
| 68 | {txtOperacionesMatematicas = 'Operaciones Matemáticas v0.2';
|
|---|
| 69 | txtJuegoTerminado = 'Juego Terminado';
|
|---|
| 70 | txtAciertos = 'Aciertos';
|
|---|
| 71 | txtIntentos = 'Intentos';
|
|---|
| 72 | txtEficacia = 'Eficacia';
|
|---|
| 73 | txtIniciar = 'Iniciar';}
|
|---|
| 74 | txtOperacionesMatematicas = 'Math Operations';
|
|---|
| 75 | txtJuegoTerminado = 'Game Over';
|
|---|
| 76 | txtAciertos = 'Hits';
|
|---|
| 77 | txtIntentos = 'Attempts';
|
|---|
| 78 | txtEficacia = 'Effectiveness';
|
|---|
| 79 | txtIniciar = 'Start';
|
|---|
| 80 |
|
|---|
| 81 | implementation
|
|---|
| 82 |
|
|---|
| 83 | uses
|
|---|
| 84 | Math, SysUtils, BGRASamples, Controls, Dialogs;
|
|---|
| 85 |
|
|---|
| 86 | {$R *.lfm}
|
|---|
| 87 |
|
|---|
| 88 | { Random Procedures }
|
|---|
| 89 |
|
|---|
| 90 | procedure OperacionAleatoria(var Avar1, Avar2, Aresultado, AOperador: integer);
|
|---|
| 91 | var
|
|---|
| 92 | unrango: integer;
|
|---|
| 93 | begin
|
|---|
| 94 | unrango := RandomRange(0, 4);
|
|---|
| 95 | AOperador := unrango;
|
|---|
| 96 | case unrango of
|
|---|
| 97 | 0: SumaAleatoria(Avar1, Avar2, Aresultado);
|
|---|
| 98 | 1: RestaAleatoria(Avar1, Avar2, Aresultado);
|
|---|
| 99 | 2: MultiplicacionAleatoria(Avar1, Avar2, Aresultado);
|
|---|
| 100 | 3: DivisionAleatoria(Avar1, Avar2, Aresultado);
|
|---|
| 101 | end;
|
|---|
| 102 | end;
|
|---|
| 103 |
|
|---|
| 104 | function OperadorAString(Aoperador: integer): string;
|
|---|
| 105 | begin
|
|---|
| 106 | case Aoperador of
|
|---|
| 107 | 0: Result := '+';
|
|---|
| 108 | 1: Result := '-';
|
|---|
| 109 | 2: Result := '*';
|
|---|
| 110 | 3: Result := '/';
|
|---|
| 111 | end;
|
|---|
| 112 | end;
|
|---|
| 113 |
|
|---|
| 114 | procedure SumaAleatoria(var Avar1, Avar2, Aresultado: integer);
|
|---|
| 115 | begin
|
|---|
| 116 | Avar1 := RandomRange(1, 50);
|
|---|
| 117 | Avar2 := RandomRange(1, 50);
|
|---|
| 118 | AResultado := Avar1 + Avar2;
|
|---|
| 119 | end;
|
|---|
| 120 |
|
|---|
| 121 | procedure RestaAleatoria(var Avar1, Avar2, Aresultado: integer);
|
|---|
| 122 | begin
|
|---|
| 123 | Avar1 := RandomRange(1, 50);
|
|---|
| 124 | Avar2 := RandomRange(1, 50);
|
|---|
| 125 | AResultado := Avar1 - Avar2;
|
|---|
| 126 | end;
|
|---|
| 127 |
|
|---|
| 128 | procedure MultiplicacionAleatoria(var Avar1, Avar2, Aresultado: integer);
|
|---|
| 129 | begin
|
|---|
| 130 | Avar1 := RandomRange(1, 9);
|
|---|
| 131 | Avar2 := RandomRange(1, 9);
|
|---|
| 132 | AResultado := Avar1 * Avar2;
|
|---|
| 133 | end;
|
|---|
| 134 |
|
|---|
| 135 | procedure DivisionAleatoria(var Avar1, Avar2, Aresultado: integer);
|
|---|
| 136 | begin
|
|---|
| 137 | AResultado := RandomRange(1, 9);
|
|---|
| 138 | Avar2 := RandomRange(1, 9);
|
|---|
| 139 | Avar1 := Aresultado * Avar2;
|
|---|
| 140 | end;
|
|---|
| 141 |
|
|---|
| 142 | { TfrmJuego }
|
|---|
| 143 |
|
|---|
| 144 | procedure TfrmJuego.FormShow(Sender: TObject);
|
|---|
| 145 | begin
|
|---|
| 146 | Generar;
|
|---|
| 147 | end;
|
|---|
| 148 |
|
|---|
| 149 | procedure TfrmJuego.FormClose(Sender: TObject; var CloseAction: TCloseAction);
|
|---|
| 150 | begin
|
|---|
| 151 | temporizador.Enabled := False;
|
|---|
| 152 |
|
|---|
| 153 | tiempo := 0;
|
|---|
| 154 | n_tiempo.Caption := '00';
|
|---|
| 155 | end;
|
|---|
| 156 |
|
|---|
| 157 | procedure TfrmJuego.btnIniciar_Click(Sender: TObject);
|
|---|
| 158 | begin
|
|---|
| 159 | intentos := 0;
|
|---|
| 160 | aciertos := 0;
|
|---|
| 161 | tiempo := 60;
|
|---|
| 162 |
|
|---|
| 163 | n_intentos.Caption := IntToStr(intentos);
|
|---|
| 164 | n_aciertos.Caption := IntToStr(aciertos);
|
|---|
| 165 | n_tiempo.Caption := IntToStr(tiempo);
|
|---|
| 166 |
|
|---|
| 167 | Generar;
|
|---|
| 168 |
|
|---|
| 169 | temporizador.Enabled := True;
|
|---|
| 170 | end;
|
|---|
| 171 |
|
|---|
| 172 | procedure TfrmJuego.temporizadorTimer(Sender: TObject);
|
|---|
| 173 | var
|
|---|
| 174 | eficacia: integer;
|
|---|
| 175 | begin
|
|---|
| 176 | tiempo := tiempo - 1;
|
|---|
| 177 | n_tiempo.Caption := IntToStr(tiempo);
|
|---|
| 178 |
|
|---|
| 179 | if tiempo = 0 then
|
|---|
| 180 | begin
|
|---|
| 181 | temporizador.Enabled := False;
|
|---|
| 182 |
|
|---|
| 183 | if aciertos <> 0 then
|
|---|
| 184 | eficacia := Trunc(aciertos / intentos * 100)
|
|---|
| 185 | else
|
|---|
| 186 | eficacia := 0;
|
|---|
| 187 |
|
|---|
| 188 | ShowMessage(
|
|---|
| 189 | txtJuegoTerminado + '.' + #10 + IntToStr(aciertos) + ' ' +
|
|---|
| 190 | txtAciertos + '.' + #10 + IntToStr(intentos) + ' ' + txtIntentos +
|
|---|
| 191 | '.' + #10 + IntToStr(eficacia) + '%' + ' ' + txtEficacia + '.');
|
|---|
| 192 |
|
|---|
| 193 | n_tiempo.Caption := '00';
|
|---|
| 194 | end;
|
|---|
| 195 | end;
|
|---|
| 196 |
|
|---|
| 197 | procedure TfrmJuego.Generar;
|
|---|
| 198 |
|
|---|
| 199 | procedure AleatorizarOpciones(a, b, rand2, opcioncorrecta: integer);
|
|---|
| 200 | var
|
|---|
| 201 | i, j: integer;
|
|---|
| 202 | losnumeros: array [0..50] of boolean;
|
|---|
| 203 | isok: boolean;
|
|---|
| 204 | begin
|
|---|
| 205 |
|
|---|
| 206 | for i := 0 to 50 do
|
|---|
| 207 | begin
|
|---|
| 208 | losnumeros[i] := False;
|
|---|
| 209 | end;
|
|---|
| 210 |
|
|---|
| 211 | for i := 0 to 4 do
|
|---|
| 212 | begin
|
|---|
| 213 |
|
|---|
| 214 | if i <> rand2 then
|
|---|
| 215 | begin
|
|---|
| 216 |
|
|---|
| 217 | repeat
|
|---|
| 218 | isok := False;
|
|---|
| 219 |
|
|---|
| 220 | j := RandomRange(a, b);
|
|---|
| 221 |
|
|---|
| 222 | if (not losnumeros[j]) and (j <> opcioncorrecta) then
|
|---|
| 223 | begin
|
|---|
| 224 | losnumeros[j] := True;
|
|---|
| 225 | isok := True;
|
|---|
| 226 | end
|
|---|
| 227 | else
|
|---|
| 228 | isok := False;
|
|---|
| 229 |
|
|---|
| 230 | until isok;
|
|---|
| 231 |
|
|---|
| 232 | vsBarraOpciones.Controls[i].Caption := IntToStr(j);
|
|---|
| 233 | end;
|
|---|
| 234 |
|
|---|
| 235 | end;
|
|---|
| 236 | end;
|
|---|
| 237 |
|
|---|
| 238 | var
|
|---|
| 239 | rand1, rand2: integer;
|
|---|
| 240 | begin
|
|---|
| 241 | Self.BeginFormUpdate;
|
|---|
| 242 |
|
|---|
| 243 | OperacionAleatoria(var1, var2, resultado, operador);
|
|---|
| 244 |
|
|---|
| 245 | btnVariable1.Caption := IntToStr(var1);
|
|---|
| 246 | btnOperador.Caption := OperadorAString(operador);
|
|---|
| 247 | btnVariable2.Caption := IntToStr(var2);
|
|---|
| 248 | btnResultado.Caption := IntToStr(Resultado);
|
|---|
| 249 |
|
|---|
| 250 | rand1 := RandomRange(0, 3);
|
|---|
| 251 | rand2 := RandomRange(0, 4);
|
|---|
| 252 |
|
|---|
| 253 | case rand1 of
|
|---|
| 254 | 0:
|
|---|
| 255 | begin
|
|---|
| 256 | btnVariable1.Caption := '?';
|
|---|
| 257 | vsBarraOpciones.Controls[rand2].Caption := IntToStr(var1);
|
|---|
| 258 | opcioncorrecta := var1;
|
|---|
| 259 | end;
|
|---|
| 260 | 1:
|
|---|
| 261 | begin
|
|---|
| 262 | btnVariable2.Caption := '?';
|
|---|
| 263 | vsBarraOpciones.Controls[rand2].Caption := IntToStr(var2);
|
|---|
| 264 | opcioncorrecta := var2;
|
|---|
| 265 | end;
|
|---|
| 266 | 2:
|
|---|
| 267 | begin
|
|---|
| 268 | btnResultado.Caption := '?';
|
|---|
| 269 | vsBarraOpciones.Controls[rand2].Caption := IntToStr(resultado);
|
|---|
| 270 | opcioncorrecta := resultado;
|
|---|
| 271 | end;
|
|---|
| 272 | end;
|
|---|
| 273 |
|
|---|
| 274 | case operador of
|
|---|
| 275 | 0: AleatorizarOpciones(1, 50, rand2, opcioncorrecta);
|
|---|
| 276 | 1: AleatorizarOpciones(1, 50, rand2, opcioncorrecta);
|
|---|
| 277 | 2: AleatorizarOpciones(1, 9, rand2, opcioncorrecta);
|
|---|
| 278 | 3: AleatorizarOpciones(1, 9, rand2, opcioncorrecta);
|
|---|
| 279 | end;
|
|---|
| 280 |
|
|---|
| 281 | Self.EndFormUpdate;
|
|---|
| 282 | end;
|
|---|
| 283 |
|
|---|
| 284 | procedure TfrmJuego.Chequear(valor: integer);
|
|---|
| 285 | begin
|
|---|
| 286 | if valor = opcioncorrecta then
|
|---|
| 287 | begin
|
|---|
| 288 | aciertos := aciertos + 1;
|
|---|
| 289 | intentos := intentos + 1;
|
|---|
| 290 | n_aciertos.Caption := IntToStr(aciertos);
|
|---|
| 291 | n_intentos.Caption := IntToStr(intentos);
|
|---|
| 292 | end
|
|---|
| 293 | else
|
|---|
| 294 | begin
|
|---|
| 295 | intentos := intentos + 1;
|
|---|
| 296 | n_intentos.Caption := IntToStr(intentos);
|
|---|
| 297 | end;
|
|---|
| 298 | Generar;
|
|---|
| 299 | end;
|
|---|
| 300 |
|
|---|
| 301 | { Graphics }
|
|---|
| 302 |
|
|---|
| 303 | procedure TfrmJuego.FormCreate(Sender: TObject);
|
|---|
| 304 | begin
|
|---|
| 305 | Opcion1.BitmapLoadFromFile('android_14_normal.png');
|
|---|
| 306 |
|
|---|
| 307 | btnVariable1.Assign(Opcion1);
|
|---|
| 308 | btnOperador.Assign(Opcion1);
|
|---|
| 309 | btnVariable2.Assign(Opcion1);
|
|---|
| 310 | btnIgualdad.Assign(Opcion1);
|
|---|
| 311 | btnResultado.Assign(Opcion1);
|
|---|
| 312 |
|
|---|
| 313 | Opcion2.Assign(Opcion1);
|
|---|
| 314 | Opcion3.Assign(Opcion1);
|
|---|
| 315 | Opcion4.Assign(Opcion1);
|
|---|
| 316 | Opcion5.Assign(Opcion1);
|
|---|
| 317 |
|
|---|
| 318 | btnIniciar.Assign(Opcion1);
|
|---|
| 319 |
|
|---|
| 320 | Caption := txtOperacionesMatematicas;
|
|---|
| 321 | btnIniciar.Caption := txtIniciar;
|
|---|
| 322 | lblAciertos.Caption := txtAciertos;
|
|---|
| 323 | lblIntentos.Caption := txtIntentos;
|
|---|
| 324 | end;
|
|---|
| 325 |
|
|---|
| 326 | procedure TfrmJuego.vsBarraEcuacionRedraw(Sender: TObject; Bitmap: TBGRABitmap);
|
|---|
| 327 | begin
|
|---|
| 328 | DrawiOSElement(Bitmap, iOSBar);
|
|---|
| 329 | end;
|
|---|
| 330 |
|
|---|
| 331 | procedure TfrmJuego.vsBarraOpcionesRedraw(Sender: TObject; Bitmap: TBGRABitmap);
|
|---|
| 332 | begin
|
|---|
| 333 | DrawiOSToolBar(Bitmap, False);
|
|---|
| 334 | end;
|
|---|
| 335 |
|
|---|
| 336 | procedure TfrmJuego.vsFondoRedraw(Sender: TObject; Bitmap: TBGRABitmap);
|
|---|
| 337 | begin
|
|---|
| 338 | DrawiOSElement(Bitmap, iOSBackground);
|
|---|
| 339 | end;
|
|---|
| 340 |
|
|---|
| 341 | { Option Buttons }
|
|---|
| 342 |
|
|---|
| 343 | procedure TfrmJuego.Opcion1Click(Sender: TObject);
|
|---|
| 344 | begin
|
|---|
| 345 | Chequear(StrToInt(Opcion1.Caption));
|
|---|
| 346 | end;
|
|---|
| 347 |
|
|---|
| 348 | procedure TfrmJuego.Opcion2Click(Sender: TObject);
|
|---|
| 349 | begin
|
|---|
| 350 | Chequear(StrToInt(Opcion2.Caption));
|
|---|
| 351 | end;
|
|---|
| 352 |
|
|---|
| 353 | procedure TfrmJuego.Opcion3Click(Sender: TObject);
|
|---|
| 354 | begin
|
|---|
| 355 | Chequear(StrToInt(Opcion3.Caption));
|
|---|
| 356 | end;
|
|---|
| 357 |
|
|---|
| 358 | procedure TfrmJuego.Opcion4Click(Sender: TObject);
|
|---|
| 359 | begin
|
|---|
| 360 | Chequear(StrToInt(Opcion4.Caption));
|
|---|
| 361 | end;
|
|---|
| 362 |
|
|---|
| 363 | procedure TfrmJuego.Opcion5Click(Sender: TObject);
|
|---|
| 364 | begin
|
|---|
| 365 | Chequear(StrToInt(Opcion5.Caption));
|
|---|
| 366 | end;
|
|---|
| 367 |
|
|---|
| 368 | end.
|
|---|
| 369 |
|
|---|