Autor
Nachricht
Erhard Henkes
Mitglied
Benutzerprofil
Anmeldungsdatum: 25.04.2000
Beiträge: 11924
Erhard Henkes Mitglied
21:58:24 02.09.2008 Titel:
Tetris Clone zu Anfang als leichtes 2D-Projekt?
Zitieren
Wer kommt eigentlich immmer auf die Idee, Anfängern tetris als Einstiegsprojekt zu empfehlen? Das ist alles andere als einfach! Hier ein Irrlicht-Beispiel:
http://abusoft.g0dsoft.com/
Code: 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
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
#ifndef MAIN_H
#define MAIN_H
#define guiID_HSWin 100
#define guiID_HSTxt 101
#define guiID_HSBtn 102
#include <windows.h>
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
struct cubeDat
{
int Typ; // 1 - 7
int Rot; // Rotation (0 - 3)
position2d<int> Pos; // Position (0-10, 0-21)
int Map[4][4];
};
struct hsDat
{
int Score;
wchar_t Name[50];
};
class cGameIrrTris : public IEventReceiver
{
private:
IrrlichtDevice *device;
IVideoDriver* driver;
IGUIEnvironment* env;
ITexture* imgBack;
bool progDeath;
bool gameRunning;
bool showHighScores;
cubeDat cubeNow;
cubeDat cubeNext;
int Map[10][22];
int Score;
IGUIStaticText* txtScore;
long tmrInterval;
void loadHighScores();
void saveHighScores();
void enableHS();
void updateGame();
void drawMap();
void getNextCube();
void getPlayCube();
void setCubeToMap();
void getCubeMap(cubeDat* cb);
bool checkCubePos();
void addToHighScores();
void updateNewHSColor();
array<hsDat> HighScores;
int posNewScore;
IGUIStaticText* hsView[25];
public:
void runGame();
virtual bool OnEvent(const SEvent& event);
int t;
};
#endif // MAIN_H
Code: 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
#ifndef MAIN_H
#define MAIN_H
#define guiID_HSWin 100
#define guiID_HSTxt 101
#define guiID_HSBtn 102
#include <windows.h>
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
struct cubeDat
{
int Typ; // 1 - 7
int Rot; // Rotation (0 - 3)
position2d<int> Pos; // Position (0-10, 0-21)
int Map[4][4];
};
struct hsDat
{
int Score;
wchar_t Name[50];
};
class cGameIrrTris : public IEventReceiver
{
private:
IrrlichtDevice *device;
IVideoDriver* driver;
IGUIEnvironment* env;
ITexture* imgBack;
bool progDeath;
bool gameRunning;
bool showHighScores;
cubeDat cubeNow;
cubeDat cubeNext;
int Map[10][22];
int Score;
IGUIStaticText* txtScore;
long tmrInterval;
void loadHighScores();
void saveHighScores();
void enableHS();
void updateGame();
void drawMap();
void getNextCube();
void getPlayCube();
void setCubeToMap();
void getCubeMap(cubeDat* cb);
bool checkCubePos();
void addToHighScores();
void updateNewHSColor();
array<hsDat> HighScores;
int posNewScore;
IGUIStaticText* hsView[25];
public:
void runGame();
virtual bool OnEvent(const SEvent& event);
int t;
};
#endif // MAIN_H
Code: 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
#ifndef MAIN_H
#define MAIN_H
#define guiID_HSWin 100
#define guiID_HSTxt 101
#define guiID_HSBtn 102
#include <windows.h>
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
struct cubeDat
{
int Typ; // 1 - 7
int Rot; // Rotation (0 - 3)
position2d<int> Pos; // Position (0-10, 0-21)
int Map[4][4];
};
struct hsDat
{
int Score;
wchar_t Name[50];
};
class cGameIrrTris : public IEventReceiver
{
private:
IrrlichtDevice *device;
IVideoDriver* driver;
IGUIEnvironment* env;
ITexture* imgBack;
bool progDeath;
bool gameRunning;
bool showHighScores;
cubeDat cubeNow;
cubeDat cubeNext;
int Map[10][22];
int Score;
IGUIStaticText* txtScore;
long tmrInterval;
void loadHighScores();
void saveHighScores();
void enableHS();
void updateGame();
void drawMap();
void getNextCube();
void getPlayCube();
void setCubeToMap();
void getCubeMap(cubeDat* cb);
bool checkCubePos();
void addToHighScores();
void updateNewHSColor();
array<hsDat> HighScores;
int posNewScore;
IGUIStaticText* hsView[25];
public:
void runGame();
virtual bool OnEvent(const SEvent& event);
int t;
};
#endif // MAIN_H
Code: 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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
// IrrTriz.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung.
//
#include "stdafx.h"
////////////////////////////////////////////////////////////////////
///////// Version 2005-2006 by A. Buschhüter (abusoft@freenet.de) //
////////////////////////////////////////////////////////////////////
#include "main.h"
//#include "resourcen.h"
// linker commands
#pragma comment(lib, "irrlicht.lib")
#pragma warning( disable : 4996 ) // sprintf
int main()
{
cGameIrrTris Spiel;
Spiel.runGame();
return 0;
}
bool cGameIrrTris::OnEvent(const SEvent& event)
{
if(event.EventType == EET_GUI_EVENT)
{
s32 id = event.GUIEvent.Caller->getID();
switch(id)
{
case guiID_HSBtn:
{
//wchar_t nName[100];
hsDat hs;
swprintf(hs.Name, 100, L"%d - %s", Score, env->getRootGUIElement()->getElementFromId(guiID_HSTxt, true)->getText());
hs.Score = Score;
for(posNewScore = 0; HighScores[posNewScore].Score >= Score; posNewScore++);
HighScores.insert(hs, posNewScore);
HighScores.erase(25);
saveHighScores();
env->getRootGUIElement()->getElementFromId(guiID_HSWin, true)->remove();
showHighScores = true;
device->getCursorControl()->setVisible(false);
device->getCursorControl()->setPosition(400, 300);
return true;
}
break;
}
}
else if((event.EventType == EET_KEY_INPUT_EVENT) && !event.KeyInput.PressedDown)
{
switch(event.KeyInput.Key)
{
case KEY_ESCAPE:
{
if(gameRunning)
{
gameRunning = false;
}
else
{
progDeath = true;
}
return true;
}
break;
case KEY_RETURN:
{
if(env->getRootGUIElement()->getElementFromId(guiID_HSTxt, true))
{
//wchar_t nName[100];
hsDat hs;
swprintf(hs.Name, 100, L"%d - %s", Score,
env->getRootGUIElement()->getElementFromId(guiID_HSTxt, true)->getText());
hs.Score = Score;
for(posNewScore = 0; HighScores[posNewScore].Score >= Score; ++posNewScore);
HighScores.insert(hs, posNewScore);
HighScores.erase(25);
saveHighScores();
env->getRootGUIElement()->getElementFromId(guiID_HSWin, true)->remove();
showHighScores = true;
device->getCursorControl()->setVisible(false);
device->getCursorControl()->setPosition(400, 300);
}
}
break;
case KEY_LEFT:
{
if(gameRunning)
{
// left
cubeNow.Pos.X--;
if(!checkCubePos()) cubeNow.Pos.X++;
}
return true;
}
break;
case KEY_RIGHT:
{
if(gameRunning)
{
// right
cubeNow.Pos.X++;
if(!checkCubePos()) cubeNow.Pos.X--;
}
return true;
}
break;
case KEY_DOWN:
{
if(gameRunning)
{
// drop
while(checkCubePos()) cubeNow.Pos.Y++;
cubeNow.Pos.Y--;
setCubeToMap();
}
return true;
}
break;
case KEY_SPACE:
{
if(gameRunning)
{
// rotate
cubeNow.Rot = (cubeNow.Rot + 1) % 4;
getCubeMap(&cubeNow);
if(!checkCubePos()){
cubeNow.Rot--;
if(cubeNow.Rot < 0) cubeNow.Rot = 3;
getCubeMap(&cubeNow);
}
}
return true;
}
break;
case KEY_F1:
{
if(env->getRootGUIElement()->getElementFromId(guiID_HSWin, true)) return false;
if(!gameRunning) showHighScores = !showHighScores;
return true;
}
break;
case KEY_F2:
{
if(env->getRootGUIElement()->getElementFromId(guiID_HSWin, true)) return false;
if(!gameRunning)
{
showHighScores = false;
posNewScore = -1;
Score = 0;
// clear map
for(int x = 0; x < 10; x++)
for(int y = 0; y < 22; y++)
Map[x][y] = 0;
// get next cube
srand(device->getTimer()->getTime());
getNextCube();
getPlayCube();
tmrInterval = 1000; // 1 sec.
gameRunning = true;
}
return true;
}
break;
}
}
return false;
}
void cGameIrrTris::getCubeMap(cubeDat* cb)
{
switch(cb->Typ)
{
case 0:
{
if((cb->Rot == 0) || (cb->Rot == 2))
{ // 0°
// row 1
cb->Map[0][0] = 0;
cb->Map[1][0] = 0;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = cb->Typ + 1;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = 0;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if((cb->Rot == 1) || (cb->Rot == 3))
{ // 90°
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = 0;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = cb->Typ + 1;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
}
break;
case 1:
{
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = cb->Typ + 1;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = 0;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = 0;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
break;
case 2:
{
if(cb->Rot == 0)
{ // 0°
cb->Map[0][0] = 0;
cb->Map[1][0] = 0;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 1)
{ // 90°
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 2)
{ // 180°
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = 0;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 3)
{ // 270°
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = 0;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
}
break;
case 3:
{
if((cb->Rot == 0) || (cb->Rot == 2))
{ // 0°
cb->Map[0][0] = 0;
cb->Map[1][0] = 0;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = cb->Typ + 1;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if((cb->Rot == 1) || (cb->Rot == 3))
{ // 90°
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = cb->Typ + 1;
cb->Map[1][2] = 0;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
}
break;
case 4:
{
if((cb->Rot == 0) || (cb->Rot == 2))
{ // 0°
cb->Map[0][0] = 0;
cb->Map[1][0] = 0;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = 0;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = cb->Typ + 1;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if((cb->Rot == 1) || (cb->Rot == 3))
{ // 90°
cb->Map[0][0] = cb->Typ + 1;
cb->Map[1][0] = 0;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
}
break;
case 5:
{
if(cb->Rot == 0)
{ // 0°
cb->Map[0][0] = 0;
cb->Map[1][0] = 0;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = 0;
cb->Map[2][2] = cb->Typ + 1;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 1)
{ // 90°
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = 0;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = cb->Typ + 1;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 2)
{ // 180°
cb->Map[0][0] = cb->Typ + 1;
cb->Map[1][0] = 0;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = 0;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 3)
{ // 270°
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = cb->Typ + 1;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = 0;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
}
break;
case 6:
{
if(cb->Rot == 0)
{ // 0°
cb->Map[0][0] = 0;
cb->Map[1][0] = 0;
cb->Map[2][0] = cb->Typ + 1;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = 0;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 1)
{ // 90°
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = 0;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = cb->Typ + 1;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 2)
{ // 180°
cb->Map[0][0] = 0;
cb->Map[1][0] = 0;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = cb->Typ + 1;
cb->Map[1][2] = 0;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 3)
{ // 270°
cb->Map[0][0] = cb->Typ + 1;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = 0;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
}
break;
}
}
void cGameIrrTris::runGame()
{
progDeath = false;
gameRunning = false;
showHighScores = true;
posNewScore = -1;
SIrrlichtCreationParameters paramIrr;
paramIrr.Fullscreen = true;
paramIrr.AntiAlias = false;
paramIrr.Bits = 32;
paramIrr.DriverType = EDT_OPENGL;
paramIrr.EventReceiver = this;
paramIrr.Stencilbuffer = false;
paramIrr.Vsync = true;
paramIrr.WindowSize = dimension2d<s32>(800, 600);
device = createDeviceEx(paramIrr);
driver = device->getVideoDriver();
device->setWindowCaption(L"IrrTriz");
device->getCursorControl()->setVisible(false);
driver->setTextureCreationFlag(ETCF_ALWAYS_32_BIT, true);
driver->setTextureCreationFlag(ETCF_ALWAYS_16_BIT, false);
driver->setTextureCreationFlag(ETCF_OPTIMIZED_FOR_QUALITY, true);
driver->setTextureCreationFlag(ETCF_OPTIMIZED_FOR_SPEED, true);
driver->setTextureCreationFlag(ETCF_CREATE_MIP_MAPS, false);
device->getFileSystem()->addZipFileArchive("game.dat");
loadHighScores();
imgBack = driver->getTexture("map.bmp");
env = device->getGUIEnvironment();
IGUIFont* font = env->getFont("font.bmp");
txtScore = env->addStaticText(L"0", rect<s32>(650, 275, 799, 320), false, false);
txtScore->setOverrideFont(font);
txtScore->setOverrideColor(SColor(255, 0, 190, 190));
IGUIFont* fontS = env->getFont("font-sh-small.bmp");
IGUIFont* fontB = env->getFont("font-sh-big.bmp");
hsView[0] = env->addStaticText(L"", rect<s32>(290, 10, 510, 60), false, false);
hsView[0]->setOverrideColor(SColor(255, 0, 150, 250));
hsView[0]->setOverrideFont(fontB);
for(int t = 1; t < 25; t++)
{
hsView[t] = env->addStaticText(L"", rect<s32>(290, t * 20 + 40, 510, t * 20 + 60), false, false);
hsView[t]->setOverrideColor(SColor(255, 255, 255, 0));
hsView[t]->setOverrideFont(fontS);
}
// make GUI not transparent
for(s32 i=0; i<EGDC_COUNT ; ++i)
{
SColor col = env->getSkin()->getColor((EGUI_DEFAULT_COLOR)i);
col.setAlpha(255);
env->getSkin()->setColor((EGUI_DEFAULT_COLOR)i, col);
}
// clear map
for(int x = 0; x < 10; ++x)
for(int y = 0; y < 22; ++y)
Map[x][y] = 0;
while(device->run())
{
driver->beginScene(true, true, SColor(255,0,110,0));
driver->draw2DImage(imgBack, rect<s32>(0, 0, 800, 601), rect<s32>(0, 0, 800, 601), 0, 0, false);
if(gameRunning)
updateGame();
drawMap();
env->drawAll();
driver->endScene();
updateNewHSColor();
if(progDeath)
device->closeDevice();
Sleep(1);
}
device->drop();
}
void cGameIrrTris::getNextCube()
{
cubeNext.Typ = (rand() % 7);
cubeNext.Rot = 0;
cubeNext.Pos = position2d<int>(3, 0);
getCubeMap(&cubeNext);
}
void cGameIrrTris::getPlayCube()
{
cubeNow = cubeNext;
getNextCube();
if((cubeNow.Typ != 1) && (cubeNow.Typ != 6))
cubeNow.Pos.Y = -1;
if(!checkCubePos())
{
gameRunning = false;
if(Score > HighScores[24].Score)
addToHighScores();
}
}
void cGameIrrTris::setCubeToMap()
{
static int cubeX, cubeY;
static wchar_t wcScore[10];
for(cubeY = 0; cubeY < 4; cubeY++)
{
for(cubeX = 0; cubeX < 4; cubeX++)
{
if(cubeNow.Map[cubeX][cubeY] != 0)
{
Map[cubeX + cubeNow.Pos.X][cubeY + cubeNow.Pos.Y] = cubeNow.Map[cubeX][cubeY];
}
}
}
// check for complete rows
static bool rFull;
for(int y = 21; y >= 0; y--)
{
rFull = true;
for(int x = 0; x < 10; x++)
{
if(Map[x][y] == 0)
{
rFull = false;
break;
}
}
if(rFull)
{
Score += 100;
tmrInterval = 1000 - Score / 10;
if(tmrInterval < 1)
tmrInterval = 1;
swprintf(wcScore, 10, L"%d", Score);
txtScore->setText(wcScore);
// delete row
for(int y2 = y; y2 > 0; y2--)
{
for(int x = 0; x < 10; x++)
{
Map[x][y2] = Map[x][y2 - 1];
}
}
for(int x = 0; x < 10; x++)
{
Map[x][0] = 0;
}
y++;
}
}
// get next as play cube
getPlayCube();
}
void cGameIrrTris::drawMap()
{
static int cubeX, cubeY, ghostX, ghostY;
// draw next cube
if(gameRunning)
{
for(int y = 0; y < 4; y++)
{
for(int x = 0; x < 4; x++)
{
if(cubeNext.Map[x][y] != 0)
{
driver->draw2DImage(imgBack,
rect<s32>(629 + x * 26, 77 + y * 26, 654 + x * 26, 102 + y * 26),
rect<s32>(cubeNext.Typ * 30, 700, cubeNext.Typ * 30 + 26, 726), 0, 0, false);
}
}
}
// get ghost position
cubeY = cubeNow.Pos.Y;
while(checkCubePos()) cubeNow.Pos.Y++;
ghostY = (cubeNow.Pos.Y - 1) * 26;
cubeNow.Pos.Y = cubeY;
// draw current cube and hist ghost
cubeX = ghostX = 270 + cubeNow.Pos.X * 26;
cubeY = cubeNow.Pos.Y * 26;
for(int y = 0; y < 4; y++)
{
for(int x = 0; x < 4; x++)
{
if(cubeNow.Map[x][y] != 0)
{
// ghost stone
driver->draw2DImage(imgBack,
rect<s32>(ghostX + x * 26, ghostY + y * 26, ghostX + x * 26 + 25, ghostY + y * 26 + 25),
rect<s32>(0, 730, 25, 755), 0, 0, false);
// normal stone
driver->draw2DImage(imgBack,
rect<s32>(cubeX + x * 26, cubeY + y * 26, cubeX + x * 26 + 25, cubeY + y * 26 + 25),
rect<s32>(cubeNow.Typ * 30, 700, cubeNow.Typ * 30 + 26, 726), 0, 0, false);
}
}
}
// hide "press F2"
driver->draw2DImage(imgBack, rect<s32>(40, 280, 190, 350), rect<s32>(60, 230, 170, 280), 0, 0, false);
}
if(!showHighScores)
{
// draw map
cubeX = 270;
for(int y = 0; y < 22; y++)
{
for(int x = 0; x < 10; x++)
{
if(Map[x][y] != 0)
{
driver->draw2DImage(imgBack,
rect<s32>(cubeX + x * 26, y * 26, cubeX + x * 26 + 25, y * 26 + 25),
rect<s32>((Map[x][y] - 1) * 30, 700, (Map[x][y] - 1) * 30 + 26, 726), 0, 0, false);
}
}
}
}
enableHS();
}
bool cGameIrrTris::checkCubePos()
{
static int cubeX, cubeY, mapX, mapY;
for(cubeY = 0; cubeY < 4; cubeY++)
{
for(cubeX = 0; cubeX < 4; cubeX++)
{
if(cubeNow.Map[cubeX][cubeY] != 0)
{
mapX = cubeX + cubeNow.Pos.X;
mapY = cubeY + cubeNow.Pos.Y;
if((mapX < 0) || (mapY < 0)) return false;
if((mapX > 9) || (mapY > 21)) return false;
if(Map[mapX][mapY] != 0) return false;
}
}
}
return true;
}
void cGameIrrTris::updateGame()
{
static long tmrLast = 0, tmrNow;
tmrNow = GetTickCount();
if(tmrNow > (tmrLast + tmrInterval))
{
cubeNow.Pos.Y++;
if(!checkCubePos())
{
cubeNow.Pos.Y--;
setCubeToMap();
}
tmrLast = tmrNow;
}
}
void cGameIrrTris::enableHS()
{
static bool hsOld = false;
if(hsOld == showHighScores) return;
hsOld = showHighScores;
for(int t = 0; t < 25; t++)
{
hsView[t]->setText(HighScores[t].Name);
hsView[t]->setVisible(showHighScores);
}
}
void cGameIrrTris::saveHighScores()
{
char key[10], val[100];
for(int t = 0; t < 25; t++)
{
sprintf(key, "HS%d", t);
for(int r = 0; HighScores[t].Name[r] != '\0'; r++)
{
val[r] = HighScores[t].Name[r];
val[r + 1] = '\0';
}
WritePrivateProfileString(LPCWSTR(key), L"Name", LPCWSTR(val), L"IrrTriz.hs");
sprintf(val, "%d", HighScores[t].Score);
WritePrivateProfileString(LPCWSTR(key), L"Score", LPCWSTR(val), L"IrrTriz.hs");
}
}
void cGameIrrTris::loadHighScores()
{
char retVal[255], key[10], vor[100];
hsDat hsNew;
HighScores.clear();
for(int t = 0; t < 25; t++)
{
sprintf(key, "HS%d", t);
sprintf(vor, "%d - Acki", (25 - t) * 100);
GetPrivateProfileString(LPCWSTR(key), L"Name", LPCWSTR(vor), LPWSTR(retVal), 255, L"IrrTriz.hs");
for(int r = 0; retVal[r] != '\0'; r++)
{
hsNew.Name[r] = (unsigned char)retVal[r];
hsNew.Name[r + 1] = '\0';
}
sprintf(vor, "%d", (25 - t) * 100);
GetPrivateProfileString(LPCWSTR(key), L"Score", LPCWSTR(vor), LPWSTR(retVal), 255, L"IrrTriz.hs");
hsNew.Score = atoi(retVal);
HighScores.push_back(hsNew);
}
}
void cGameIrrTris::addToHighScores()
{
IGUIWindow* wnd = env->addWindow(rect<s32>(225, 100, 575, 315), true, L"Highscore - New Entry", 0, guiID_HSWin);
wnd->getElementFromPoint(position2d<s32>(570, 105))->setVisible(false);
IGUIFont* fontB = env->getFont("font-sh-big.bmp");
IGUIStaticText* txt = env->addStaticText(
L" !!! Congratulations !!!\n"
L"You have entered the Highscore-List!\n"
L" Please enter your name.",
rect<s32>(10,30,345,110), false, true, wnd);
txt->setOverrideFont(fontB);
txt->setOverrideColor(SColor(255, 0, 50, 200));
IGUIEditBox* box = env->addEditBox(L"Player", rect<s32>(50, 120, 300, 150), true, wnd, guiID_HSTxt);
box->setOverrideFont(fontB);
box->setOverrideColor(SColor(255, 0, 50, 100));
box->setMax(25);
IGUIButton* btn = env->addButton(rect<s32>(125, 165, 225, 195), wnd, guiID_HSBtn, L"OK");
btn->setOverrideFont(fontB);
device->getCursorControl()->setVisible(true);
device->getCursorControl()->setPosition(400, 300);
env->setFocus(box);
}
void cGameIrrTris::updateNewHSColor()
{
static s32 r = 0, g = 100, b = 200;
static s32 step1 = 10, step2 = 20, step3 = 30;
static SColor cNewHS(255, 0, 0, 255);
// add a funny color cycle to the new HighScore entry
if(!showHighScores || (posNewScore < 0)) return;
r += step1;
if((r > 255) || (r < 0))
{
step1 = -step1;
r += step1;
}
g += step2;
if((g > 255) || (g < 0))
{
step2 = -step2;
g += step2;
}
b += step3;
if((b > 255) || (b < 0))
{
step3 = -step3;
b += step3;
}
cNewHS.setRed(r);
cNewHS.setGreen(g);
hsView[posNewScore]->setOverrideColor(cNewHS);
}
Code: 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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
// IrrTriz.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung.
//
#include "stdafx.h"
////////////////////////////////////////////////////////////////////
///////// Version 2005-2006 by A. Buschhüter (abusoft@freenet.de) //
////////////////////////////////////////////////////////////////////
#include "main.h"
//#include "resourcen.h"
// linker commands
#pragma comment(lib, "irrlicht.lib")
#pragma warning( disable : 4996 ) // sprintf
int main()
{
cGameIrrTris Spiel;
Spiel.runGame();
return 0;
}
bool cGameIrrTris::OnEvent(const SEvent& event)
{
if(event.EventType == EET_GUI_EVENT)
{
s32 id = event.GUIEvent.Caller->getID();
switch(id)
{
case guiID_HSBtn:
{
//wchar_t nName[100];
hsDat hs;
swprintf(hs.Name, 100, L"%d - %s", Score, env->getRootGUIElement()->getElementFromId(guiID_HSTxt, true)->getText());
hs.Score = Score;
for(posNewScore = 0; HighScores[posNewScore].Score >= Score; posNewScore++);
HighScores.insert(hs, posNewScore);
HighScores.erase(25);
saveHighScores();
env->getRootGUIElement()->getElementFromId(guiID_HSWin, true)->remove();
showHighScores = true;
device->getCursorControl()->setVisible(false);
device->getCursorControl()->setPosition(400, 300);
return true;
}
break;
}
}
else if((event.EventType == EET_KEY_INPUT_EVENT) && !event.KeyInput.PressedDown)
{
switch(event.KeyInput.Key)
{
case KEY_ESCAPE:
{
if(gameRunning)
{
gameRunning = false;
}
else
{
progDeath = true;
}
return true;
}
break;
case KEY_RETURN:
{
if(env->getRootGUIElement()->getElementFromId(guiID_HSTxt, true))
{
//wchar_t nName[100];
hsDat hs;
swprintf(hs.Name, 100, L"%d - %s", Score,
env->getRootGUIElement()->getElementFromId(guiID_HSTxt, true)->getText());
hs.Score = Score;
for(posNewScore = 0; HighScores[posNewScore].Score >= Score; ++posNewScore);
HighScores.insert(hs, posNewScore);
HighScores.erase(25);
saveHighScores();
env->getRootGUIElement()->getElementFromId(guiID_HSWin, true)->remove();
showHighScores = true;
device->getCursorControl()->setVisible(false);
device->getCursorControl()->setPosition(400, 300);
}
}
break;
case KEY_LEFT:
{
if(gameRunning)
{
// left
cubeNow.Pos.X--;
if(!checkCubePos()) cubeNow.Pos.X++;
}
return true;
}
break;
case KEY_RIGHT:
{
if(gameRunning)
{
// right
cubeNow.Pos.X++;
if(!checkCubePos()) cubeNow.Pos.X--;
}
return true;
}
break;
case KEY_DOWN:
{
if(gameRunning)
{
// drop
while(checkCubePos()) cubeNow.Pos.Y++;
cubeNow.Pos.Y--;
setCubeToMap();
}
return true;
}
break;
case KEY_SPACE:
{
if(gameRunning)
{
// rotate
cubeNow.Rot = (cubeNow.Rot + 1) % 4;
getCubeMap(&cubeNow);
if(!checkCubePos()){
cubeNow.Rot--;
if(cubeNow.Rot < 0) cubeNow.Rot = 3;
getCubeMap(&cubeNow);
}
}
return true;
}
break;
case KEY_F1:
{
if(env->getRootGUIElement()->getElementFromId(guiID_HSWin, true)) return false;
if(!gameRunning) showHighScores = !showHighScores;
return true;
}
break;
case KEY_F2:
{
if(env->getRootGUIElement()->getElementFromId(guiID_HSWin, true)) return false;
if(!gameRunning)
{
showHighScores = false;
posNewScore = -1;
Score = 0;
// clear map
for(int x = 0; x < 10; x++)
for(int y = 0; y < 22; y++)
Map[x][y] = 0;
// get next cube
srand(device->getTimer()->getTime());
getNextCube();
getPlayCube();
tmrInterval = 1000; // 1 sec.
gameRunning = true;
}
return true;
}
break;
}
}
return false;
}
void cGameIrrTris::getCubeMap(cubeDat* cb)
{
switch(cb->Typ)
{
case 0:
{
if((cb->Rot == 0) || (cb->Rot == 2))
{ // 0°
// row 1
cb->Map[0][0] = 0;
cb->Map[1][0] = 0;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = cb->Typ + 1;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = 0;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if((cb->Rot == 1) || (cb->Rot == 3))
{ // 90°
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = 0;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = cb->Typ + 1;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
}
break;
case 1:
{
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = cb->Typ + 1;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = 0;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = 0;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
break;
case 2:
{
if(cb->Rot == 0)
{ // 0°
cb->Map[0][0] = 0;
cb->Map[1][0] = 0;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 1)
{ // 90°
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 2)
{ // 180°
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = 0;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 3)
{ // 270°
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = 0;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
}
break;
case 3:
{
if((cb->Rot == 0) || (cb->Rot == 2))
{ // 0°
cb->Map[0][0] = 0;
cb->Map[1][0] = 0;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = cb->Typ + 1;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if((cb->Rot == 1) || (cb->Rot == 3))
{ // 90°
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = cb->Typ + 1;
cb->Map[1][2] = 0;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
}
break;
case 4:
{
if((cb->Rot == 0) || (cb->Rot == 2))
{ // 0°
cb->Map[0][0] = 0;
cb->Map[1][0] = 0;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = 0;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = cb->Typ + 1;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if((cb->Rot == 1) || (cb->Rot == 3))
{ // 90°
cb->Map[0][0] = cb->Typ + 1;
cb->Map[1][0] = 0;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
}
break;
case 5:
{
if(cb->Rot == 0)
{ // 0°
cb->Map[0][0] = 0;
cb->Map[1][0] = 0;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = 0;
cb->Map[2][2] = cb->Typ + 1;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 1)
{ // 90°
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = 0;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = cb->Typ + 1;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 2)
{ // 180°
cb->Map[0][0] = cb->Typ + 1;
cb->Map[1][0] = 0;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = 0;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 3)
{ // 270°
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = cb->Typ + 1;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = 0;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
}
break;
case 6:
{
if(cb->Rot == 0)
{ // 0°
cb->Map[0][0] = 0;
cb->Map[1][0] = 0;
cb->Map[2][0] = cb->Typ + 1;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = 0;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 1)
{ // 90°
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = 0;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = cb->Typ + 1;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 2)
{ // 180°
cb->Map[0][0] = 0;
cb->Map[1][0] = 0;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = cb->Typ + 1;
cb->Map[1][2] = 0;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 3)
{ // 270°
cb->Map[0][0] = cb->Typ + 1;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = 0;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
}
break;
}
}
void cGameIrrTris::runGame()
{
progDeath = false;
gameRunning = false;
showHighScores = true;
posNewScore = -1;
SIrrlichtCreationParameters paramIrr;
paramIrr.Fullscreen = true;
paramIrr.AntiAlias = false;
paramIrr.Bits = 32;
paramIrr.DriverType = EDT_OPENGL;
paramIrr.EventReceiver = this;
paramIrr.Stencilbuffer = false;
paramIrr.Vsync = true;
paramIrr.WindowSize = dimension2d<s32>(800, 600);
device = createDeviceEx(paramIrr);
driver = device->getVideoDriver();
device->setWindowCaption(L"IrrTriz");
device->getCursorControl()->setVisible(false);
driver->setTextureCreationFlag(ETCF_ALWAYS_32_BIT, true);
driver->setTextureCreationFlag(ETCF_ALWAYS_16_BIT, false);
driver->setTextureCreationFlag(ETCF_OPTIMIZED_FOR_QUALITY, true);
driver->setTextureCreationFlag(ETCF_OPTIMIZED_FOR_SPEED, true);
driver->setTextureCreationFlag(ETCF_CREATE_MIP_MAPS, false);
device->getFileSystem()->addZipFileArchive("game.dat");
loadHighScores();
imgBack = driver->getTexture("map.bmp");
env = device->getGUIEnvironment();
IGUIFont* font = env->getFont("font.bmp");
txtScore = env->addStaticText(L"0", rect<s32>(650, 275, 799, 320), false, false);
txtScore->setOverrideFont(font);
txtScore->setOverrideColor(SColor(255, 0, 190, 190));
IGUIFont* fontS = env->getFont("font-sh-small.bmp");
IGUIFont* fontB = env->getFont("font-sh-big.bmp");
hsView[0] = env->addStaticText(L"", rect<s32>(290, 10, 510, 60), false, false);
hsView[0]->setOverrideColor(SColor(255, 0, 150, 250));
hsView[0]->setOverrideFont(fontB);
for(int t = 1; t < 25; t++)
{
hsView[t] = env->addStaticText(L"", rect<s32>(290, t * 20 + 40, 510, t * 20 + 60), false, false);
hsView[t]->setOverrideColor(SColor(255, 255, 255, 0));
hsView[t]->setOverrideFont(fontS);
}
// make GUI not transparent
for(s32 i=0; i<EGDC_COUNT ; ++i)
{
SColor col = env->getSkin()->getColor((EGUI_DEFAULT_COLOR)i);
col.setAlpha(255);
env->getSkin()->setColor((EGUI_DEFAULT_COLOR)i, col);
}
// clear map
for(int x = 0; x < 10; ++x)
for(int y = 0; y < 22; ++y)
Map[x][y] = 0;
while(device->run())
{
driver->beginScene(true, true, SColor(255,0,110,0));
driver->draw2DImage(imgBack, rect<s32>(0, 0, 800, 601), rect<s32>(0, 0, 800, 601), 0, 0, false);
if(gameRunning)
updateGame();
drawMap();
env->drawAll();
driver->endScene();
updateNewHSColor();
if(progDeath)
device->closeDevice();
Sleep(1);
}
device->drop();
}
void cGameIrrTris::getNextCube()
{
cubeNext.Typ = (rand() % 7);
cubeNext.Rot = 0;
cubeNext.Pos = position2d<int>(3, 0);
getCubeMap(&cubeNext);
}
void cGameIrrTris::getPlayCube()
{
cubeNow = cubeNext;
getNextCube();
if((cubeNow.Typ != 1) && (cubeNow.Typ != 6))
cubeNow.Pos.Y = -1;
if(!checkCubePos())
{
gameRunning = false;
if(Score > HighScores[24].Score)
addToHighScores();
}
}
void cGameIrrTris::setCubeToMap()
{
static int cubeX, cubeY;
static wchar_t wcScore[10];
for(cubeY = 0; cubeY < 4; cubeY++)
{
for(cubeX = 0; cubeX < 4; cubeX++)
{
if(cubeNow.Map[cubeX][cubeY] != 0)
{
Map[cubeX + cubeNow.Pos.X][cubeY + cubeNow.Pos.Y] = cubeNow.Map[cubeX][cubeY];
}
}
}
// check for complete rows
static bool rFull;
for(int y = 21; y >= 0; y--)
{
rFull = true;
for(int x = 0; x < 10; x++)
{
if(Map[x][y] == 0)
{
rFull = false;
break;
}
}
if(rFull)
{
Score += 100;
tmrInterval = 1000 - Score / 10;
if(tmrInterval < 1)
tmrInterval = 1;
swprintf(wcScore, 10, L"%d", Score);
txtScore->setText(wcScore);
// delete row
for(int y2 = y; y2 > 0; y2--)
{
for(int x = 0; x < 10; x++)
{
Map[x][y2] = Map[x][y2 - 1];
}
}
for(int x = 0; x < 10; x++)
{
Map[x][0] = 0;
}
y++;
}
}
// get next as play cube
getPlayCube();
}
void cGameIrrTris::drawMap()
{
static int cubeX, cubeY, ghostX, ghostY;
// draw next cube
if(gameRunning)
{
for(int y = 0; y < 4; y++)
{
for(int x = 0; x < 4; x++)
{
if(cubeNext.Map[x][y] != 0)
{
driver->draw2DImage(imgBack,
rect<s32>(629 + x * 26, 77 + y * 26, 654 + x * 26, 102 + y * 26),
rect<s32>(cubeNext.Typ * 30, 700, cubeNext.Typ * 30 + 26, 726), 0, 0, false);
}
}
}
// get ghost position
cubeY = cubeNow.Pos.Y;
while(checkCubePos()) cubeNow.Pos.Y++;
ghostY = (cubeNow.Pos.Y - 1) * 26;
cubeNow.Pos.Y = cubeY;
// draw current cube and hist ghost
cubeX = ghostX = 270 + cubeNow.Pos.X * 26;
cubeY = cubeNow.Pos.Y * 26;
for(int y = 0; y < 4; y++)
{
for(int x = 0; x < 4; x++)
{
if(cubeNow.Map[x][y] != 0)
{
// ghost stone
driver->draw2DImage(imgBack,
rect<s32>(ghostX + x * 26, ghostY + y * 26, ghostX + x * 26 + 25, ghostY + y * 26 + 25),
rect<s32>(0, 730, 25, 755), 0, 0, false);
// normal stone
driver->draw2DImage(imgBack,
rect<s32>(cubeX + x * 26, cubeY + y * 26, cubeX + x * 26 + 25, cubeY + y * 26 + 25),
rect<s32>(cubeNow.Typ * 30, 700, cubeNow.Typ * 30 + 26, 726), 0, 0, false);
}
}
}
// hide "press F2"
driver->draw2DImage(imgBack, rect<s32>(40, 280, 190, 350), rect<s32>(60, 230, 170, 280), 0, 0, false);
}
if(!showHighScores)
{
// draw map
cubeX = 270;
for(int y = 0; y < 22; y++)
{
for(int x = 0; x < 10; x++)
{
if(Map[x][y] != 0)
{
driver->draw2DImage(imgBack,
rect<s32>(cubeX + x * 26, y * 26, cubeX + x * 26 + 25, y * 26 + 25),
rect<s32>((Map[x][y] - 1) * 30, 700, (Map[x][y] - 1) * 30 + 26, 726), 0, 0, false);
}
}
}
}
enableHS();
}
bool cGameIrrTris::checkCubePos()
{
static int cubeX, cubeY, mapX, mapY;
for(cubeY = 0; cubeY < 4; cubeY++)
{
for(cubeX = 0; cubeX < 4; cubeX++)
{
if(cubeNow.Map[cubeX][cubeY] != 0)
{
mapX = cubeX + cubeNow.Pos.X;
mapY = cubeY + cubeNow.Pos.Y;
if((mapX < 0) || (mapY < 0)) return false;
if((mapX > 9) || (mapY > 21)) return false;
if(Map[mapX][mapY] != 0) return false;
}
}
}
return true;
}
void cGameIrrTris::updateGame()
{
static long tmrLast = 0, tmrNow;
tmrNow = GetTickCount();
if(tmrNow > (tmrLast + tmrInterval))
{
cubeNow.Pos.Y++;
if(!checkCubePos())
{
cubeNow.Pos.Y--;
setCubeToMap();
}
tmrLast = tmrNow;
}
}
void cGameIrrTris::enableHS()
{
static bool hsOld = false;
if(hsOld == showHighScores) return;
hsOld = showHighScores;
for(int t = 0; t < 25; t++)
{
hsView[t]->setText(HighScores[t].Name);
hsView[t]->setVisible(showHighScores);
}
}
void cGameIrrTris::saveHighScores()
{
char key[10], val[100];
for(int t = 0; t < 25; t++)
{
sprintf(key, "HS%d", t);
for(int r = 0; HighScores[t].Name[r] != '\0'; r++)
{
val[r] = HighScores[t].Name[r];
val[r + 1] = '\0';
}
WritePrivateProfileString(LPCWSTR(key), L"Name", LPCWSTR(val), L"IrrTriz.hs");
sprintf(val, "%d", HighScores[t].Score);
WritePrivateProfileString(LPCWSTR(key), L"Score", LPCWSTR(val), L"IrrTriz.hs");
}
}
void cGameIrrTris::loadHighScores()
{
char retVal[255], key[10], vor[100];
hsDat hsNew;
HighScores.clear();
for(int t = 0; t < 25; t++)
{
sprintf(key, "HS%d", t);
sprintf(vor, "%d - Acki", (25 - t) * 100);
GetPrivateProfileString(LPCWSTR(key), L"Name", LPCWSTR(vor), LPWSTR(retVal), 255, L"IrrTriz.hs");
for(int r = 0; retVal[r] != '\0'; r++)
{
hsNew.Name[r] = (unsigned char)retVal[r];
hsNew.Name[r + 1] = '\0';
}
sprintf(vor, "%d", (25 - t) * 100);
GetPrivateProfileString(LPCWSTR(key), L"Score", LPCWSTR(vor), LPWSTR(retVal), 255, L"IrrTriz.hs");
hsNew.Score = atoi(retVal);
HighScores.push_back(hsNew);
}
}
void cGameIrrTris::addToHighScores()
{
IGUIWindow* wnd = env->addWindow(rect<s32>(225, 100, 575, 315), true, L"Highscore - New Entry", 0, guiID_HSWin);
wnd->getElementFromPoint(position2d<s32>(570, 105))->setVisible(false);
IGUIFont* fontB = env->getFont("font-sh-big.bmp");
IGUIStaticText* txt = env->addStaticText(
L" !!! Congratulations !!!\n"
L"You have entered the Highscore-List!\n"
L" Please enter your name.",
rect<s32>(10,30,345,110), false, true, wnd);
txt->setOverrideFont(fontB);
txt->setOverrideColor(SColor(255, 0, 50, 200));
IGUIEditBox* box = env->addEditBox(L"Player", rect<s32>(50, 120, 300, 150), true, wnd, guiID_HSTxt);
box->setOverrideFont(fontB);
box->setOverrideColor(SColor(255, 0, 50, 100));
box->setMax(25);
IGUIButton* btn = env->addButton(rect<s32>(125, 165, 225, 195), wnd, guiID_HSBtn, L"OK");
btn->setOverrideFont(fontB);
device->getCursorControl()->setVisible(true);
device->getCursorControl()->setPosition(400, 300);
env->setFocus(box);
}
void cGameIrrTris::updateNewHSColor()
{
static s32 r = 0, g = 100, b = 200;
static s32 step1 = 10, step2 = 20, step3 = 30;
static SColor cNewHS(255, 0, 0, 255);
// add a funny color cycle to the new HighScore entry
if(!showHighScores || (posNewScore < 0)) return;
r += step1;
if((r > 255) || (r < 0))
{
step1 = -step1;
r += step1;
}
g += step2;
if((g > 255) || (g < 0))
{
step2 = -step2;
g += step2;
}
b += step3;
if((b > 255) || (b < 0))
{
step3 = -step3;
b += step3;
}
cNewHS.setRed(r);
cNewHS.setGreen(g);
hsView[posNewScore]->setOverrideColor(cNewHS);
}
Code: 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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
// IrrTriz.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung.
//
#include "stdafx.h"
////////////////////////////////////////////////////////////////////
///////// Version 2005-2006 by A. Buschhüter (abusoft@freenet.de) //
////////////////////////////////////////////////////////////////////
#include "main.h"
//#include "resourcen.h"
// linker commands
#pragma comment(lib, "irrlicht.lib")
#pragma warning( disable : 4996 ) // sprintf
int main()
{
cGameIrrTris Spiel;
Spiel.runGame();
return 0;
}
bool cGameIrrTris::OnEvent(const SEvent& event)
{
if(event.EventType == EET_GUI_EVENT)
{
s32 id = event.GUIEvent.Caller->getID();
switch(id)
{
case guiID_HSBtn:
{
//wchar_t nName[100];
hsDat hs;
swprintf(hs.Name, 100, L"%d - %s", Score, env->getRootGUIElement()->getElementFromId(guiID_HSTxt, true)->getText());
hs.Score = Score;
for(posNewScore = 0; HighScores[posNewScore].Score >= Score; posNewScore++);
HighScores.insert(hs, posNewScore);
HighScores.erase(25);
saveHighScores();
env->getRootGUIElement()->getElementFromId(guiID_HSWin, true)->remove();
showHighScores = true;
device->getCursorControl()->setVisible(false);
device->getCursorControl()->setPosition(400, 300);
return true;
}
break;
}
}
else if((event.EventType == EET_KEY_INPUT_EVENT) && !event.KeyInput.PressedDown)
{
switch(event.KeyInput.Key)
{
case KEY_ESCAPE:
{
if(gameRunning)
{
gameRunning = false;
}
else
{
progDeath = true;
}
return true;
}
break;
case KEY_RETURN:
{
if(env->getRootGUIElement()->getElementFromId(guiID_HSTxt, true))
{
//wchar_t nName[100];
hsDat hs;
swprintf(hs.Name, 100, L"%d - %s", Score,
env->getRootGUIElement()->getElementFromId(guiID_HSTxt, true)->getText());
hs.Score = Score;
for(posNewScore = 0; HighScores[posNewScore].Score >= Score; ++posNewScore);
HighScores.insert(hs, posNewScore);
HighScores.erase(25);
saveHighScores();
env->getRootGUIElement()->getElementFromId(guiID_HSWin, true)->remove();
showHighScores = true;
device->getCursorControl()->setVisible(false);
device->getCursorControl()->setPosition(400, 300);
}
}
break;
case KEY_LEFT:
{
if(gameRunning)
{
// left
cubeNow.Pos.X--;
if(!checkCubePos()) cubeNow.Pos.X++;
}
return true;
}
break;
case KEY_RIGHT:
{
if(gameRunning)
{
// right
cubeNow.Pos.X++;
if(!checkCubePos()) cubeNow.Pos.X--;
}
return true;
}
break;
case KEY_DOWN:
{
if(gameRunning)
{
// drop
while(checkCubePos()) cubeNow.Pos.Y++;
cubeNow.Pos.Y--;
setCubeToMap();
}
return true;
}
break;
case KEY_SPACE:
{
if(gameRunning)
{
// rotate
cubeNow.Rot = (cubeNow.Rot + 1) % 4;
getCubeMap(&cubeNow);
if(!checkCubePos()){
cubeNow.Rot--;
if(cubeNow.Rot < 0) cubeNow.Rot = 3;
getCubeMap(&cubeNow);
}
}
return true;
}
break;
case KEY_F1:
{
if(env->getRootGUIElement()->getElementFromId(guiID_HSWin, true)) return false;
if(!gameRunning) showHighScores = !showHighScores;
return true;
}
break;
case KEY_F2:
{
if(env->getRootGUIElement()->getElementFromId(guiID_HSWin, true)) return false;
if(!gameRunning)
{
showHighScores = false;
posNewScore = -1;
Score = 0;
// clear map
for(int x = 0; x < 10; x++)
for(int y = 0; y < 22; y++)
Map[x][y] = 0;
// get next cube
srand(device->getTimer()->getTime());
getNextCube();
getPlayCube();
tmrInterval = 1000; // 1 sec.
gameRunning = true;
}
return true;
}
break;
}
}
return false;
}
void cGameIrrTris::getCubeMap(cubeDat* cb)
{
switch(cb->Typ)
{
case 0:
{
if((cb->Rot == 0) || (cb->Rot == 2))
{ // 0°
// row 1
cb->Map[0][0] = 0;
cb->Map[1][0] = 0;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = cb->Typ + 1;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = 0;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if((cb->Rot == 1) || (cb->Rot == 3))
{ // 90°
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = 0;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = cb->Typ + 1;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
}
break;
case 1:
{
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = cb->Typ + 1;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = 0;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = 0;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
break;
case 2:
{
if(cb->Rot == 0)
{ // 0°
cb->Map[0][0] = 0;
cb->Map[1][0] = 0;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 1)
{ // 90°
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 2)
{ // 180°
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = 0;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 3)
{ // 270°
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = 0;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
}
break;
case 3:
{
if((cb->Rot == 0) || (cb->Rot == 2))
{ // 0°
cb->Map[0][0] = 0;
cb->Map[1][0] = 0;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = cb->Typ + 1;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if((cb->Rot == 1) || (cb->Rot == 3))
{ // 90°
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = cb->Typ + 1;
cb->Map[1][2] = 0;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
}
break;
case 4:
{
if((cb->Rot == 0) || (cb->Rot == 2))
{ // 0°
cb->Map[0][0] = 0;
cb->Map[1][0] = 0;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = 0;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = cb->Typ + 1;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if((cb->Rot == 1) || (cb->Rot == 3))
{ // 90°
cb->Map[0][0] = cb->Typ + 1;
cb->Map[1][0] = 0;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
}
break;
case 5:
{
if(cb->Rot == 0)
{ // 0°
cb->Map[0][0] = 0;
cb->Map[1][0] = 0;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = 0;
cb->Map[2][2] = cb->Typ + 1;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 1)
{ // 90°
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = 0;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = cb->Typ + 1;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 2)
{ // 180°
cb->Map[0][0] = cb->Typ + 1;
cb->Map[1][0] = 0;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = 0;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 3)
{ // 270°
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = cb->Typ + 1;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = 0;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
}
break;
case 6:
{
if(cb->Rot == 0)
{ // 0°
cb->Map[0][0] = 0;
cb->Map[1][0] = 0;
cb->Map[2][0] = cb->Typ + 1;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = 0;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 1)
{ // 90°
cb->Map[0][0] = 0;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = 0;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = cb->Typ + 1;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 2)
{ // 180°
cb->Map[0][0] = 0;
cb->Map[1][0] = 0;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = cb->Typ + 1;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = cb->Typ + 1;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = cb->Typ + 1;
cb->Map[1][2] = 0;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
else if(cb->Rot == 3)
{ // 270°
cb->Map[0][0] = cb->Typ + 1;
cb->Map[1][0] = cb->Typ + 1;
cb->Map[2][0] = 0;
cb->Map[3][0] = 0;
// row 2
cb->Map[0][1] = 0;
cb->Map[1][1] = cb->Typ + 1;
cb->Map[2][1] = 0;
cb->Map[3][1] = 0;
// row 3
cb->Map[0][2] = 0;
cb->Map[1][2] = cb->Typ + 1;
cb->Map[2][2] = 0;
cb->Map[3][2] = 0;
// row 4
cb->Map[0][3] = 0;
cb->Map[1][3] = 0;
cb->Map[2][3] = 0;
cb->Map[3][3] = 0;
}
}
break;
}
}
void cGameIrrTris::runGame()
{
progDeath = false;
gameRunning = false;
showHighScores = true;
posNewScore = -1;
SIrrlichtCreationParameters paramIrr;
paramIrr.Fullscreen = true;
paramIrr.AntiAlias = false;
paramIrr.Bits = 32;
paramIrr.DriverType = EDT_OPENGL;
paramIrr.EventReceiver = this;
paramIrr.Stencilbuffer = false;
paramIrr.Vsync = true;
paramIrr.WindowSize = dimension2d<s32>(800, 600);
device = createDeviceEx(paramIrr);
driver = device->getVideoDriver();
device->setWindowCaption(L"IrrTriz");
device->getCursorControl()->setVisible(false);
driver->setTextureCreationFlag(ETCF_ALWAYS_32_BIT, true);
driver->setTextureCreationFlag(ETCF_ALWAYS_16_BIT, false);
driver->setTextureCreationFlag(ETCF_OPTIMIZED_FOR_QUALITY, true);
driver->setTextureCreationFlag(ETCF_OPTIMIZED_FOR_SPEED, true);
driver->setTextureCreationFlag(ETCF_CREATE_MIP_MAPS, false);
device->getFileSystem()->addZipFileArchive("game.dat");
loadHighScores();
imgBack = driver->getTexture("map.bmp");
env = device->getGUIEnvironment();
IGUIFont* font = env->getFont("font.bmp");
txtScore = env->addStaticText(L"0", rect<s32>(650, 275, 799, 320), false, false);
txtScore->setOverrideFont(font);
txtScore->setOverrideColor(SColor(255, 0, 190, 190));
IGUIFont* fontS = env->getFont("font-sh-small.bmp");
IGUIFont* fontB = env->getFont("font-sh-big.bmp");
hsView[0] = env->addStaticText(L"", rect<s32>(290, 10, 510, 60), false, false);
hsView[0]->setOverrideColor(SColor(255, 0, 150, 250));
hsView[0]->setOverrideFont(fontB);
for(int t = 1; t < 25; t++)
{
hsView[t] = env->addStaticText(L"", rect<s32>(290, t * 20 + 40, 510, t * 20 + 60), false, false);
hsView[t]->setOverrideColor(SColor(255, 255, 255, 0));
hsView[t]->setOverrideFont(fontS);
}
// make GUI not transparent
for(s32 i=0; i<EGDC_COUNT ; ++i)
{
SColor col = env->getSkin()->getColor((EGUI_DEFAULT_COLOR)i);
col.setAlpha(255);
env->getSkin()->setColor((EGUI_DEFAULT_COLOR)i, col);
}
// clear map
for(int x = 0; x < 10; ++x)
for(int y = 0; y < 22; ++y)
Map[x][y] = 0;
while(device->run())
{
driver->beginScene(true, true, SColor(255,0,110,0));
driver->draw2DImage(imgBack, rect<s32>(0, 0, 800, 601), rect<s32>(0, 0, 800, 601), 0, 0, false);
if(gameRunning)
updateGame();
drawMap();
env->drawAll();
driver->endScene();
updateNewHSColor();
if(progDeath)
device->closeDevice();
Sleep(1);
}
device->drop();
}
void cGameIrrTris::getNextCube()
{
cubeNext.Typ = (rand() % 7);
cubeNext.Rot = 0;
cubeNext.Pos = position2d<int>(3, 0);
getCubeMap(&cubeNext);
}
void cGameIrrTris::getPlayCube()
{
cubeNow = cubeNext;
getNextCube();
if((cubeNow.Typ != 1) && (cubeNow.Typ != 6))
cubeNow.Pos.Y = -1;
if(!checkCubePos())
{
gameRunning = false;
if(Score > HighScores[24].Score)
addToHighScores();
}
}
void cGameIrrTris::setCubeToMap()
{
static int cubeX, cubeY;
static wchar_t wcScore[10];
for(cubeY = 0; cubeY < 4; cubeY++)
{
for(cubeX = 0; cubeX < 4; cubeX++)
{
if(cubeNow.Map[cubeX][cubeY] != 0)
{
Map[cubeX + cubeNow.Pos.X][cubeY + cubeNow.Pos.Y] = cubeNow.Map[cubeX][cubeY];
}
}
}
// check for complete rows
static bool rFull;
for(int y = 21; y >= 0; y--)
{
rFull = true;
for(int x = 0; x < 10; x++)
{
if(Map[x][y] == 0)
{
rFull = false;
break;
}
}
if(rFull)
{
Score += 100;
tmrInterval = 1000 - Score / 10;
if(tmrInterval < 1)
tmrInterval = 1;
swprintf(wcScore, 10, L"%d", Score);
txtScore->setText(wcScore);
// delete row
for(int y2 = y; y2 > 0; y2--)
{
for(int x = 0; x < 10; x++)
{
Map[x][y2] = Map[x][y2 - 1];
}
}
for(int x = 0; x < 10; x++)
{
Map[x][0] = 0;
}
y++;
}
}
// get next as play cube
getPlayCube();
}
void cGameIrrTris::drawMap()
{
static int cubeX, cubeY, ghostX, ghostY;
// draw next cube
if(gameRunning)
{
for(int y = 0; y < 4; y++)
{
for(int x = 0; x < 4; x++)
{
if(cubeNext.Map[x][y] != 0)
{
driver->draw2DImage(imgBack,
rect<s32>(629 + x * 26, 77 + y * 26, 654 + x * 26, 102 + y * 26),
rect<s32>(cubeNext.Typ * 30, 700, cubeNext.Typ * 30 + 26, 726), 0, 0, false);
}
}
}
// get ghost position
cubeY = cubeNow.Pos.Y;
while(checkCubePos()) cubeNow.Pos.Y++;
ghostY = (cubeNow.Pos.Y - 1) * 26;
cubeNow.Pos.Y = cubeY;
// draw current cube and hist ghost
cubeX = ghostX = 270 + cubeNow.Pos.X * 26;
cubeY = cubeNow.Pos.Y * 26;
for(int y = 0; y < 4; y++)
{
for(int x = 0; x < 4; x++)
{
if(cubeNow.Map[x][y] != 0)
{
// ghost stone
driver->draw2DImage(imgBack,
rect<s32>(ghostX + x * 26, ghostY + y * 26, ghostX + x * 26 + 25, ghostY + y * 26 + 25),
rect<s32>(0, 730, 25, 755), 0, 0, false);
// normal stone
driver->draw2DImage(imgBack,
rect<s32>(cubeX + x * 26, cubeY + y * 26, cubeX + x * 26 + 25, cubeY + y * 26 + 25),
rect<s32>(cubeNow.Typ * 30, 700, cubeNow.Typ * 30 + 26, 726), 0, 0, false);
}
}
}
// hide "press F2"
driver->draw2DImage(imgBack, rect<s32>(40, 280, 190, 350), rect<s32>(60, 230, 170, 280), 0, 0, false);
}
if(!showHighScores)
{
// draw map
cubeX = 270;
for(int y = 0; y < 22; y++)
{
for(int x = 0; x < 10; x++)
{
if(Map[x][y] != 0)
{
driver->draw2DImage(imgBack,
rect<s32>(cubeX + x * 26, y * 26, cubeX + x * 26 + 25, y * 26 + 25),
rect<s32>((Map[x][y] - 1) * 30, 700, (Map[x][y] - 1) * 30 + 26, 726), 0, 0, false);
}
}
}
}
enableHS();
}
bool cGameIrrTris::checkCubePos()
{
static int cubeX, cubeY, mapX, mapY;
for(cubeY = 0; cubeY < 4; cubeY++)
{
for(cubeX = 0; cubeX < 4; cubeX++)
{
if(cubeNow.Map[cubeX][cubeY] != 0)
{
mapX = cubeX + cubeNow.Pos.X;
mapY = cubeY + cubeNow.Pos.Y;
if((mapX < 0) || (mapY < 0)) return false;
if((mapX > 9) || (mapY > 21)) return false;
if(Map[mapX][mapY] != 0) return false;
}
}
}
return true;
}
void cGameIrrTris::updateGame()
{
static long tmrLast = 0, tmrNow;
tmrNow = GetTickCount();
if(tmrNow > (tmrLast + tmrInterval))
{
cubeNow.Pos.Y++;
if(!checkCubePos())
{
cubeNow.Pos.Y--;
setCubeToMap();
}
tmrLast = tmrNow;
}
}
void cGameIrrTris::enableHS()
{
static bool hsOld = false;
if(hsOld == showHighScores) return;
hsOld = showHighScores;
for(int t = 0; t < 25; t++)
{
hsView[t]->setText(HighScores[t].Name);
hsView[t]->setVisible(showHighScores);
}
}
void cGameIrrTris::saveHighScores()
{
char key[10], val[100];
for(int t = 0; t < 25; t++)
{
sprintf(key, "HS%d", t);
for(int r = 0; HighScores[t].Name[r] != '\0'; r++)
{
val[r] = HighScores[t].Name[r];
val[r + 1] = '\0';
}
WritePrivateProfileString(LPCWSTR(key), L"Name", LPCWSTR(val), L"IrrTriz.hs");
sprintf(val, "%d", HighScores[t].Score);
WritePrivateProfileString(LPCWSTR(key), L"Score", LPCWSTR(val), L"IrrTriz.hs");
}
}
void cGameIrrTris::loadHighScores()
{
char retVal[255], key[10], vor[100];
hsDat hsNew;
HighScores.clear();
for(int t = 0; t < 25; t++)
{
sprintf(key, "HS%d", t);
sprintf(vor, "%d - Acki", (25 - t) * 100);
GetPrivateProfileString(LPCWSTR(key), L"Name", LPCWSTR(vor), LPWSTR(retVal), 255, L"IrrTriz.hs");
for(int r = 0; retVal[r] != '\0'; r++)
{
hsNew.Name[r] = (unsigned char)retVal[r];
hsNew.Name[r + 1] = '\0';
}
sprintf(vor, "%d", (25 - t) * 100);
GetPrivateProfileString(LPCWSTR(key), L"Score", LPCWSTR(vor), LPWSTR(retVal), 255, L"IrrTriz.hs");
hsNew.Score = atoi(retVal);
HighScores.push_back(hsNew);
}
}
void cGameIrrTris::addToHighScores()
{
IGUIWindow* wnd = env->addWindow(rect<s32>(225, 100, 575, 315), true, L"Highscore - New Entry", 0, guiID_HSWin);
wnd->getElementFromPoint(position2d<s32>(570, 105))->setVisible(false);
IGUIFont* fontB = env->getFont("font-sh-big.bmp");
IGUIStaticText* txt = env->addStaticText(
L" !!! Congratulations !!!\n"
L"You have entered the Highscore-List!\n"
L" Please enter your name.",
rect<s32>(10,30,345,110), false, true, wnd);
txt->setOverrideFont(fontB);
txt->setOverrideColor(SColor(255, 0, 50, 200));
IGUIEditBox* box = env->addEditBox(L"Player", rect<s32>(50, 120, 300, 150), true, wnd, guiID_HSTxt);
box->setOverrideFont(fontB);
box->setOverrideColor(SColor(255, 0, 50, 100));
box->setMax(25);
IGUIButton* btn = env->addButton(rect<s32>(125, 165, 225, 195), wnd, guiID_HSBtn, L"OK");
btn->setOverrideFont(fontB);
device->getCursorControl()->setVisible(true);
device->getCursorControl()->setPosition(400, 300);
env->setFocus(box);
}
void cGameIrrTris::updateNewHSColor()
{
static s32 r = 0, g = 100, b = 200;
static s32 step1 = 10, step2 = 20, step3 = 30;
static SColor cNewHS(255, 0, 0, 255);
// add a funny color cycle to the new HighScore entry
if(!showHighScores || (posNewScore < 0)) return;
r += step1;
if((r > 255) || (r < 0))
{
step1 = -step1;
r += step1;
}
g += step2;
if((g > 255) || (g < 0))
{
step2 = -step2;
g += step2;
}
b += step3;
if((b > 255) || (b < 0))
{
step3 = -step3;
b += step3;
}
cNewHS.setRed(r);
cNewHS.setGreen(g);
hsView[posNewScore]->setOverrideColor(cNewHS);
}
Das sieht doch alles andere als einfach aus, oder?
_________________ OS-Development-, C++, Win32-API-, MFC-, Chemie-, Robotik- und Flugsimulator-Tutorials
http://www.henkessoft.de/index.htm
Zuletzt bearbeitet von Erhard Henkes am 22:06:44 02.09.2008, insgesamt 1-mal bearbeitet
floorball92
Mitglied
Benutzerprofil
Anmeldungsdatum: 14.03.2008
Beiträge: 79
floorball92 Mitglied
22:01:14 02.09.2008 Titel:
Zitieren
denke, dass das auch einfacher geht
_________________ MFG. Floorball92
Erhard Henkes
Mitglied
Benutzerprofil
Anmeldungsdatum: 25.04.2000
Beiträge: 11924
Erhard Henkes Mitglied
22:19:05 02.09.2008 Titel:
Zitieren
Zitat: denke, dass das auch einfacher geht
Dann gib mir bitte einen Link auf einen einfacheren Source.
EDIT: mit der Irrlicht Engine
_________________ OS-Development-, C++, Win32-API-, MFC-, Chemie-, Robotik- und Flugsimulator-Tutorials
http://www.henkessoft.de/index.htm
Zuletzt bearbeitet von Erhard Henkes am 00:02:31 04.09.2008, insgesamt 1-mal bearbeitet
rapso
Moderator
Benutzerprofil
Anmeldungsdatum: 17.06.2002
Beiträge: 7282
rapso Moderator
22:57:18 02.09.2008 Titel:
Zitieren
floorball92
Mitglied
Benutzerprofil
Anmeldungsdatum: 14.03.2008
Beiträge: 79
floorball92 Mitglied
23:01:34 02.09.2008 Titel:
Zitieren
Imho nicht zu vergleichen.
_________________ MFG. Floorball92
rapso
Moderator
Benutzerprofil
Anmeldungsdatum: 17.06.2002
Beiträge: 7282
rapso Moderator
02:41:35 06.09.2008 Titel:
Zitieren
hab gerade waehrend mein backup lief (und ich eh nichts produktives machen konnte) auch mal nen clone gemacht
C/C++ Code: 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
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
// Copyright (c) 2008, rapso (www.rapso.de)
// All rights reserved.
//
// BSD license ( http://en.wikipedia.org/wiki/BSD_license )
//
// constact at rapsoo@hotmail.com
// or
// http://www.c-plusplus.de/forum/viewforum-var-f-is-7.html
///////////////////////////////////////////////////////////////////////////////
#include <assert.h>
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <intrin.h>
#pragma intrinsic(_rotl16)
const unsigned int SIZE_X = 10+2; //orginal width + border
const unsigned int SIZE_Y = 20+1; //orginal height + border
const unsigned int GAME_SPEED = 250; //in ms
const unsigned int ROW_SCORE = 10; //random number
const unsigned int ROW_BORDER = 1|(1<<(SIZE_X-1)); //left and right border
const unsigned int ROW_FILLED = (1<<SIZE_X)-1; //1<<8 == 0x100;
//0x100-1 == 0xff;
typedef unsigned short tdTetrad;
enum EState
{
ES_START,
ES_RESETGAME,
ES_GAME,
ES_LOSE,
ES_END,
ES_QUIT
};
enum ETetrad
{
ET_INVALIDE = 0,
ET_I = 0x0F00,
ET_J = 0x0470,
ET_L = 0x02e0,
ET_O = 0x0660,
ET_S = 0x06C0,
ET_T = 0x04E0,
ET_Z = 0x0C60,
ET_COUNT = 7
};
unsigned int g_Score;
tdTetrad g_Tetrad;
unsigned int g_PosX;
unsigned int g_PosY;
unsigned short g_Field[SIZE_Y+3]; //too lazy to bound check in Valid(...)
unsigned int g_LastPressedKey;
void GotoPixel(unsigned int x,unsigned int y)
{
const HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE);
const COORD Pos = {x,y};
SetConsoleCursorPosition(Out,Pos);
}
EState ProcessReset(const bool LogicStep)
{
GotoPixel(0,0);
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ");//clear screen :P
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ");
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ");
for (unsigned int a=0;a<SIZE_Y-1;a++)
g_Field[a] = ROW_BORDER;
g_Field[SIZE_Y-1] = ~0;//floor
g_Score = 0;
g_Tetrad = ET_INVALIDE;
g_LastPressedKey = 0;
return ES_GAME;
}
EState ProcessStart(const bool LogicStep)
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ");//clear screen :P
GotoPixel(20,20);
printf("TetriXTM ");
GotoPixel(10,25);
printf("Press SPACE to Start or ESCAPE to quit ");
if (GetAsyncKeyState(VK_SPACE))
return ES_RESETGAME;
return GetAsyncKeyState(VK_ESCAPE)?ES_END:ES_START;
}
bool Valid(const unsigned int PosX,const unsigned int PosY,tdTetrad Tetrad)
{
const unsigned int Mask = (_rotl16((Tetrad&0xf000)>>12,PosX)&g_Field[PosY+0])|
(_rotl16((Tetrad&0x0f00)>> 8,PosX)&g_Field[PosY+1])|
(_rotl16((Tetrad&0x00f0)>> 4,PosX)&g_Field[PosY+2])|
(_rotl16((Tetrad&0x000f)>> 0,PosX)&g_Field[PosY+3]);
return Mask==0;
}
tdTetrad Rotate(const tdTetrad Tetrad)
{
tdTetrad Ret = 0;
for (unsigned int y=0;y<4;y++)
for (unsigned int x=0;x<4;x++)
Ret |= (Tetrad>>y+x*4&1)<<3-x+y*4;
return Ret;
}
void Merge(unsigned short * pField,const unsigned int PosX,const unsigned int PosY,const tdTetrad Tetrad)
{
for (unsigned int y=0;y<SIZE_Y;y++)
pField[y] = g_Field[y];
pField[PosY+0] = g_Field[PosY+0] | (((Tetrad&0xf000)>>12)<<PosX);
if (PosY+1<SIZE_Y)
pField[PosY+1] = g_Field[PosY+1] | (((Tetrad&0x0f00)>> 8)<<PosX);
if (PosY+2<SIZE_Y)
pField[PosY+2] = g_Field[PosY+2] | (((Tetrad&0x00f0)>> 4)<<PosX);
if (PosY+3<SIZE_Y)
pField[PosY+3] = g_Field[PosY+3] | (((Tetrad&0x000f)>> 0)<<PosX);
}
void CheckScore()
{
//start one row above the floor
//topmost row is not taken into account
for (unsigned int y=SIZE_Y-2;y>0;y--)
{
if (g_Field[y]==ROW_FILLED)
{
g_Score += ROW_SCORE;
//collaps stack
for (unsigned int y2=y;y2>0;y2--)
g_Field[y2] = g_Field[y2-1];
g_Field[0] = ROW_BORDER;
y++; //push by one to compensate the loop y--
}
}
}
EState ProcessGame(const bool LogicStep)
{
if (GetAsyncKeyState(VK_ESCAPE))
return ES_LOSE;
//spawn logic
if (g_Tetrad==ET_INVALIDE)
{
switch (rand()%ET_COUNT)
{
case 0:g_Tetrad = ET_I;break ;
case 1:g_Tetrad = ET_J;break ;
case 2:g_Tetrad = ET_L;break ;
case 3:g_Tetrad = ET_O;break ;
case 4:g_Tetrad = ET_S;break ;
case 5:g_Tetrad = ET_T;break ;
case 6:g_Tetrad = ET_Z;break ;
}
g_PosX = SIZE_X/2-2; //half is center of screen, sub 2 for half Tetrad width
g_PosY = 0;
if (!Valid(g_PosX,g_PosY,g_Tetrad))
return ES_LOSE;
}
//logic time step
if (LogicStep)
{
if (Valid(g_PosX,g_PosY+1,g_Tetrad))
g_PosY++;
else
{
Merge(g_Field,g_PosX,g_PosY,g_Tetrad);
CheckScore();
g_Tetrad=ET_INVALIDE;
}
}
else
{
//avoid same action while key still pressed
if (!g_LastPressedKey || !GetAsyncKeyState(g_LastPressedKey))
{
g_LastPressedKey = 0;
if (GetAsyncKeyState(VK_LEFT) && Valid(g_PosX-1,g_PosY,g_Tetrad))
{
g_PosX--;
g_LastPressedKey = VK_LEFT;
}
if (GetAsyncKeyState(VK_RIGHT) && Valid(g_PosX+1,g_PosY,g_Tetrad))
{
g_PosX++;
g_LastPressedKey = VK_RIGHT;
}
if (GetAsyncKeyState(VK_UP) && Valid(g_PosX,g_PosY,Rotate(g_Tetrad)))
{
g_Tetrad = Rotate(g_Tetrad);
g_LastPressedKey = VK_UP;
}
if (GetAsyncKeyState(VK_DOWN))
{
while (Valid(g_PosX,g_PosY+1,g_Tetrad))
g_PosY++;
CheckScore();
g_LastPressedKey = VK_DOWN;
}
}
}
//display
{
unsigned short DrawField[SIZE_Y];
Merge(DrawField,g_PosX,g_PosY,g_Tetrad);
for (unsigned int y=0;y<SIZE_Y;y++)
{
GotoPixel(20,y);
for (unsigned int x=0;x<SIZE_X;x++)
printf(DrawField[y]&(1<<x)?"ÌÌ ":" ");
}
}
GotoPixel(0,2);
printf("Score is:\n%8d ",g_Score);
return ES_GAME;
}
EState ProcessLose(const bool LogicStep)
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ");//clear screen :P
GotoPixel(20,20);
printf("Game Over ");
GotoPixel(20,21);
printf("your highscore was: ",g_Score);
GotoPixel(10,40);
printf("Press SPACE to Start or ESCAPE to quit ");
if (GetAsyncKeyState(VK_SPACE))
return ES_RESETGAME;
return GetAsyncKeyState(VK_ESCAPE)?ES_END:ES_LOSE;
}
EState ProcessEnd(const bool LogicStep)
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ");//clear screen :P
GotoPixel(20,20);
printf("Thx for playing TetriXTM ");
GotoPixel(15,40);
Sleep(2500);
return ES_QUIT;
}
int main(int argc,char * argv[])
{
const unsigned int StartTime = GetTickCount();
unsigned int LastTick = 0;
EState State = ES_START;
while (State!=ES_QUIT)
{
const unsigned int CurrentTick = (GetTickCount()-StartTime)/GAME_SPEED;
const bool LogicTick = LastTick!=CurrentTick;
LastTick = CurrentTick;
switch (State)
{
case ES_START: State = ProcessStart(LogicTick); break ;
case ES_RESETGAME: State = ProcessReset(LogicTick); break ;
case ES_GAME: State = ProcessGame(LogicTick); break ;
case ES_LOSE: State = ProcessLose(LogicTick); break ;
case ES_END: State = ProcessEnd(LogicTick); break ;
}
Sleep(20);//to reduce flickering
}
return 0;
}
C/C++ Code: 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
// Copyright (c) 2008, rapso (www.rapso.de)
// All rights reserved.
//
// BSD license ( http://en.wikipedia.org/wiki/BSD_license )
//
// constact at rapsoo@hotmail.com
// or
// http://www.c-plusplus.de/forum/viewforum-var-f-is-7.html
///////////////////////////////////////////////////////////////////////////////
#include <assert.h>
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <intrin.h>
#pragma intrinsic(_rotl16)
const unsigned int SIZE_X = 10+2; //orginal width + border
const unsigned int SIZE_Y = 20+1; //orginal height + border
const unsigned int GAME_SPEED = 250; //in ms
const unsigned int ROW_SCORE = 10; //random number
const unsigned int ROW_BORDER = 1|(1<<(SIZE_X-1)); //left and right border
const unsigned int ROW_FILLED = (1<<SIZE_X)-1; //1<<8 == 0x100;
//0x100-1 == 0xff;
typedef unsigned short tdTetrad;
enum EState
{
ES_START,
ES_RESETGAME,
ES_GAME,
ES_LOSE,
ES_END,
ES_QUIT
};
enum ETetrad
{
ET_INVALIDE = 0,
ET_I = 0x0F00,
ET_J = 0x0470,
ET_L = 0x02e0,
ET_O = 0x0660,
ET_S = 0x06C0,
ET_T = 0x04E0,
ET_Z = 0x0C60,
ET_COUNT = 7
};
unsigned int g_Score;
tdTetrad g_Tetrad;
unsigned int g_PosX;
unsigned int g_PosY;
unsigned short g_Field[SIZE_Y+3]; //too lazy to bound check in Valid(...)
unsigned int g_LastPressedKey;
void GotoPixel(unsigned int x,unsigned int y)
{
const HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE);
const COORD Pos = {x,y};
SetConsoleCursorPosition(Out,Pos);
}
EState ProcessReset(const bool LogicStep)
{
GotoPixel(0,0);
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ");//clear screen :P
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ");
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ");
for (unsigned int a=0;a<SIZE_Y-1;a++)
g_Field[a] = ROW_BORDER;
g_Field[SIZE_Y-1] = ~0;//floor
g_Score = 0;
g_Tetrad = ET_INVALIDE;
g_LastPressedKey = 0;
return ES_GAME;
}
EState ProcessStart(const bool LogicStep)
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ");//clear screen :P
GotoPixel(20,20);
printf("TetriXTM ");
GotoPixel(10,25);
printf("Press SPACE to Start or ESCAPE to quit ");
if (GetAsyncKeyState(VK_SPACE))
return ES_RESETGAME;
return GetAsyncKeyState(VK_ESCAPE)?ES_END:ES_START;
}
bool Valid(const unsigned int PosX,const unsigned int PosY,tdTetrad Tetrad)
{
const unsigned int Mask = (_rotl16((Tetrad&0xf000)>>12,PosX)&g_Field[PosY+0])|
(_rotl16((Tetrad&0x0f00)>> 8,PosX)&g_Field[PosY+1])|
(_rotl16((Tetrad&0x00f0)>> 4,PosX)&g_Field[PosY+2])|
(_rotl16((Tetrad&0x000f)>> 0,PosX)&g_Field[PosY+3]);
return Mask==0;
}
tdTetrad Rotate(const tdTetrad Tetrad)
{
tdTetrad Ret = 0;
for (unsigned int y=0;y<4;y++)
for (unsigned int x=0;x<4;x++)
Ret |= (Tetrad>>y+x*4&1)<<3-x+y*4;
return Ret;
}
void Merge(unsigned short * pField,const unsigned int PosX,const unsigned int PosY,const tdTetrad Tetrad)
{
for (unsigned int y=0;y<SIZE_Y;y++)
pField[y] = g_Field[y];
pField[PosY+0] = g_Field[PosY+0] | (((Tetrad&0xf000)>>12)<<PosX);
if (PosY+1<SIZE_Y)
pField[PosY+1] = g_Field[PosY+1] | (((Tetrad&0x0f00)>> 8)<<PosX);
if (PosY+2<SIZE_Y)
pField[PosY+2] = g_Field[PosY+2] | (((Tetrad&0x00f0)>> 4)<<PosX);
if (PosY+3<SIZE_Y)
pField[PosY+3] = g_Field[PosY+3] | (((Tetrad&0x000f)>> 0)<<PosX);
}
void CheckScore()
{
//start one row above the floor
//topmost row is not taken into account
for (unsigned int y=SIZE_Y-2;y>0;y--)
{
if (g_Field[y]==ROW_FILLED)
{
g_Score += ROW_SCORE;
//collaps stack
for (unsigned int y2=y;y2>0;y2--)
g_Field[y2] = g_Field[y2-1];
g_Field[0] = ROW_BORDER;
y++; //push by one to compensate the loop y--
}
}
}
EState ProcessGame(const bool LogicStep)
{
if (GetAsyncKeyState(VK_ESCAPE))
return ES_LOSE;
//spawn logic
if (g_Tetrad==ET_INVALIDE)
{
switch (rand()%ET_COUNT)
{
case 0:g_Tetrad = ET_I;break ;
case 1:g_Tetrad = ET_J;break ;
case 2:g_Tetrad = ET_L;break ;
case 3:g_Tetrad = ET_O;break ;
case 4:g_Tetrad = ET_S;break ;
case 5:g_Tetrad = ET_T;break ;
case 6:g_Tetrad = ET_Z;break ;
}
g_PosX = SIZE_X/2-2; //half is center of screen, sub 2 for half Tetrad width
g_PosY = 0;
if (!Valid(g_PosX,g_PosY,g_Tetrad))
return ES_LOSE;
}
//logic time step
if (LogicStep)
{
if (Valid(g_PosX,g_PosY+1,g_Tetrad))
g_PosY++;
else
{
Merge(g_Field,g_PosX,g_PosY,g_Tetrad);
CheckScore();
g_Tetrad=ET_INVALIDE;
}
}
else
{
//avoid same action while key still pressed
if (!g_LastPressedKey || !GetAsyncKeyState(g_LastPressedKey))
{
g_LastPressedKey = 0;
if (GetAsyncKeyState(VK_LEFT) && Valid(g_PosX-1,g_PosY,g_Tetrad))
{
g_PosX--;
g_LastPressedKey = VK_LEFT;
}
if (GetAsyncKeyState(VK_RIGHT) && Valid(g_PosX+1,g_PosY,g_Tetrad))
{
g_PosX++;
g_LastPressedKey = VK_RIGHT;
}
if (GetAsyncKeyState(VK_UP) && Valid(g_PosX,g_PosY,Rotate(g_Tetrad)))
{
g_Tetrad = Rotate(g_Tetrad);
g_LastPressedKey = VK_UP;
}
if (GetAsyncKeyState(VK_DOWN))
{
while (Valid(g_PosX,g_PosY+1,g_Tetrad))
g_PosY++;
CheckScore();
g_LastPressedKey = VK_DOWN;
}
}
}
//display
{
unsigned short DrawField[SIZE_Y];
Merge(DrawField,g_PosX,g_PosY,g_Tetrad);
for (unsigned int y=0;y<SIZE_Y;y++)
{
GotoPixel(20,y);
for (unsigned int x=0;x<SIZE_X;x++)
printf(DrawField[y]&(1<<x)?"ÌÌ ":" ");
}
}
GotoPixel(0,2);
printf("Score is:\n%8d ",g_Score);
return ES_GAME;
}
EState ProcessLose(const bool LogicStep)
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ");//clear screen :P
GotoPixel(20,20);
printf("Game Over ");
GotoPixel(20,21);
printf("your highscore was: ",g_Score);
GotoPixel(10,40);
printf("Press SPACE to Start or ESCAPE to quit ");
if (GetAsyncKeyState(VK_SPACE))
return ES_RESETGAME;
return GetAsyncKeyState(VK_ESCAPE)?ES_END:ES_LOSE;
}
EState ProcessEnd(const bool LogicStep)
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ");//clear screen :P
GotoPixel(20,20);
printf("Thx for playing TetriXTM ");
GotoPixel(15,40);
Sleep(2500);
return ES_QUIT;
}
int main(int argc,char * argv[])
{
const unsigned int StartTime = GetTickCount();
unsigned int LastTick = 0;
EState State = ES_START;
while (State!=ES_QUIT)
{
const unsigned int CurrentTick = (GetTickCount()-StartTime)/GAME_SPEED;
const bool LogicTick = LastTick!=CurrentTick;
LastTick = CurrentTick;
switch (State)
{
case ES_START: State = ProcessStart(LogicTick); break ;
case ES_RESETGAME: State = ProcessReset(LogicTick); break ;
case ES_GAME: State = ProcessGame(LogicTick); break ;
case ES_LOSE: State = ProcessLose(LogicTick); break ;
case ES_END: State = ProcessEnd(LogicTick); break ;
}
Sleep(20);//to reduce flickering
}
return 0;
}
C/C++ Code: 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
// Copyright (c) 2008, rapso (www.rapso.de)
// All rights reserved.
//
// BSD license ( http://en.wikipedia.org/wiki/BSD_license )
//
// constact at rapsoo@hotmail.com
// or
// http://www.c-plusplus.de/forum/viewforum-var-f-is-7.html
///////////////////////////////////////////////////////////////////////////////
#include <assert.h>
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <intrin.h>
#pragma intrinsic(_rotl16)
const unsigned int SIZE_X = 10+2; //orginal width + border
const unsigned int SIZE_Y = 20+1; //orginal height + border
const unsigned int GAME_SPEED = 250; //in ms
const unsigned int ROW_SCORE = 10; //random number
const unsigned int ROW_BORDER = 1|(1<<(SIZE_X-1)); //left and right border
const unsigned int ROW_FILLED = (1<<SIZE_X)-1; //1<<8 == 0x100;
//0x100-1 == 0xff;
typedef unsigned short tdTetrad;
enum EState
{
ES_START,
ES_RESETGAME,
ES_GAME,
ES_LOSE,
ES_END,
ES_QUIT
};
enum ETetrad
{
ET_INVALIDE = 0,
ET_I = 0x0F00,
ET_J = 0x0470,
ET_L = 0x02e0,
ET_O = 0x0660,
ET_S = 0x06C0,
ET_T = 0x04E0,
ET_Z = 0x0C60,
ET_COUNT = 7
};
unsigned int g_Score;
tdTetrad g_Tetrad;
unsigned int g_PosX;
unsigned int g_PosY;
unsigned short g_Field[SIZE_Y+3]; //too lazy to bound check in Valid(...)
unsigned int g_LastPressedKey;
void GotoPixel(unsigned int x,unsigned int y)
{
const HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE);
const COORD Pos = {x,y};
SetConsoleCursorPosition(Out,Pos);
}
EState ProcessReset(const bool LogicStep)
{
GotoPixel(0,0);
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ");//clear screen :P
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ");
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ");
for (unsigned int a=0;a<SIZE_Y-1;a++)
g_Field[a] = ROW_BORDER;
g_Field[SIZE_Y-1] = ~0;//floor
g_Score = 0;
g_Tetrad = ET_INVALIDE;
g_LastPressedKey = 0;
return ES_GAME;
}
EState ProcessStart(const bool LogicStep)
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ");//clear screen :P
GotoPixel(20,20);
printf("TetriXTM ");
GotoPixel(10,25);
printf("Press SPACE to Start or ESCAPE to quit ");
if (GetAsyncKeyState(VK_SPACE))
return ES_RESETGAME;
return GetAsyncKeyState(VK_ESCAPE)?ES_END:ES_START;
}
bool Valid(const unsigned int PosX,const unsigned int PosY,tdTetrad Tetrad)
{
const unsigned int Mask = (_rotl16((Tetrad&0xf000)>>12,PosX)&g_Field[PosY+0])|
(_rotl16((Tetrad&0x0f00)>> 8,PosX)&g_Field[PosY+1])|
(_rotl16((Tetrad&0x00f0)>> 4,PosX)&g_Field[PosY+2])|
(_rotl16((Tetrad&0x000f)>> 0,PosX)&g_Field[PosY+3]);
return Mask==0;
}
tdTetrad Rotate(const tdTetrad Tetrad)
{
tdTetrad Ret = 0;
for (unsigned int y=0;y<4;y++)
for (unsigned int x=0;x<4;x++)
Ret |= (Tetrad>>y+x*4&1)<<3-x+y*4;
return Ret;
}
void Merge(unsigned short * pField,const unsigned int PosX,const unsigned int PosY,const tdTetrad Tetrad)
{
for (unsigned int y=0;y<SIZE_Y;y++)
pField[y] = g_Field[y];
pField[PosY+0] = g_Field[PosY+0] | (((Tetrad&0xf000)>>12)<<PosX);
if (PosY+1<SIZE_Y)
pField[PosY+1] = g_Field[PosY+1] | (((Tetrad&0x0f00)>> 8)<<PosX);
if (PosY+2<SIZE_Y)
pField[PosY+2] = g_Field[PosY+2] | (((Tetrad&0x00f0)>> 4)<<PosX);
if (PosY+3<SIZE_Y)
pField[PosY+3] = g_Field[PosY+3] | (((Tetrad&0x000f)>> 0)<<PosX);
}
void CheckScore()
{
//start one row above the floor
//topmost row is not taken into account
for (unsigned int y=SIZE_Y-2;y>0;y--)
{
if (g_Field[y]==ROW_FILLED)
{
g_Score += ROW_SCORE;
//collaps stack
for (unsigned int y2=y;y2>0;y2--)
g_Field[y2] = g_Field[y2-1];
g_Field[0] = ROW_BORDER;
y++; //push by one to compensate the loop y--
}
}
}
EState ProcessGame(const bool LogicStep)
{
if (GetAsyncKeyState(VK_ESCAPE))
return ES_LOSE;
//spawn logic
if (g_Tetrad==ET_INVALIDE)
{
switch (rand()%ET_COUNT)
{
case 0:g_Tetrad = ET_I;break ;
case 1:g_Tetrad = ET_J;break ;
case 2:g_Tetrad = ET_L;break ;
case 3:g_Tetrad = ET_O;break ;
case 4:g_Tetrad = ET_S;break ;
case 5:g_Tetrad = ET_T;break ;
case 6:g_Tetrad = ET_Z;break ;
}
g_PosX = SIZE_X/2-2; //half is center of screen, sub 2 for half Tetrad width
g_PosY = 0;
if (!Valid(g_PosX,g_PosY,g_Tetrad))
return ES_LOSE;
}
//logic time step
if (LogicStep)
{
if (Valid(g_PosX,g_PosY+1,g_Tetrad))
g_PosY++;
else
{
Merge(g_Field,g_PosX,g_PosY,g_Tetrad);
CheckScore();
g_Tetrad=ET_INVALIDE;
}
}
else
{
//avoid same action while key still pressed
if (!g_LastPressedKey || !GetAsyncKeyState(g_LastPressedKey))
{
g_LastPressedKey = 0;
if (GetAsyncKeyState(VK_LEFT) && Valid(g_PosX-1,g_PosY,g_Tetrad))
{
g_PosX--;
g_LastPressedKey = VK_LEFT;
}
if (GetAsyncKeyState(VK_RIGHT) && Valid(g_PosX+1,g_PosY,g_Tetrad))
{
g_PosX++;
g_LastPressedKey = VK_RIGHT;
}
if (GetAsyncKeyState(VK_UP) && Valid(g_PosX,g_PosY,Rotate(g_Tetrad)))
{
g_Tetrad = Rotate(g_Tetrad);
g_LastPressedKey = VK_UP;
}
if (GetAsyncKeyState(VK_DOWN))
{
while (Valid(g_PosX,g_PosY+1,g_Tetrad))
g_PosY++;
CheckScore();
g_LastPressedKey = VK_DOWN;
}
}
}
//display
{
unsigned short DrawField[SIZE_Y];
Merge(DrawField,g_PosX,g_PosY,g_Tetrad);
for (unsigned int y=0;y<SIZE_Y;y++)
{
GotoPixel(20,y);
for (unsigned int x=0;x<SIZE_X;x++)
printf(DrawField[y]&(1<<x)?"ÌÌ ":" ");
}
}
GotoPixel(0,2);
printf("Score is:\n%8d ",g_Score);
return ES_GAME;
}
EState ProcessLose(const bool LogicStep)
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ");//clear screen :P
GotoPixel(20,20);
printf("Game Over ");
GotoPixel(20,21);
printf("your highscore was: ",g_Score);
GotoPixel(10,40);
printf("Press SPACE to Start or ESCAPE to quit ");
if (GetAsyncKeyState(VK_SPACE))
return ES_RESETGAME;
return GetAsyncKeyState(VK_ESCAPE)?ES_END:ES_LOSE;
}
EState ProcessEnd(const bool LogicStep)
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ");//clear screen :P
GotoPixel(20,20);
printf("Thx for playing TetriXTM ");
GotoPixel(15,40);
Sleep(2500);
return ES_QUIT;
}
int main(int argc,char * argv[])
{
const unsigned int StartTime = GetTickCount();
unsigned int LastTick = 0;
EState State = ES_START;
while (State!=ES_QUIT)
{
const unsigned int CurrentTick = (GetTickCount()-StartTime)/GAME_SPEED;
const bool LogicTick = LastTick!=CurrentTick;
LastTick = CurrentTick;
switch (State)
{
case ES_START: State = ProcessStart(LogicTick); break ;
case ES_RESETGAME: State = ProcessReset(LogicTick); break ;
case ES_GAME: State = ProcessGame(LogicTick); break ;
case ES_LOSE: State = ProcessLose(LogicTick); break ;
case ES_END: State = ProcessEnd(LogicTick); break ;
}
Sleep(20);//to reduce flickering
}
return 0;
}
so, n8
_________________Kilo Byte=1000,Kilobyte=1024 ANSI/IEEE Standard 1084-1986
rapso
-Mod im Spiele-/Grafikprogrammierung| rapsoo@hotmail.com | #dionysos irc.quakenet.org | amazon stole my PS3
floorball92
Mitglied
Benutzerprofil
Anmeldungsdatum: 14.03.2008
Beiträge: 79
floorball92 Mitglied
09:56:55 06.09.2008 Titel:
Zitieren
ich bin beeindruckt. Habe noch nie ein Konsolenbasiertes Tetris gesehen. Sind aber noch bug drinne
_________________ MFG. Floorball92
Erhard Henkes
Mitglied
Benutzerprofil
Anmeldungsdatum: 25.04.2000
Beiträge: 11924
Erhard Henkes Mitglied
13:24:10 06.09.2008 Titel:
Zitieren
Code::Blocks 8.02 scheitert an C/C++ Code: #include <intrin.h>
C/C++ Code: #include <intrin.h>
C/C++ Code: #include <intrin.h>
MSVC++ 2008 schafft es mit zwei Warnmeldungen:
Zitat: 1>d:\irrlicht engine\rapso - tetris\tetris_by_rapso\tetris_by_rapso\tetris_by_rapso.cpp(117) : warning C4554: '>>': Prüfen Sie Operatorrangfolge auf mögliche Fehler; verwenden Sie runde Klammern, um den Vorrang zu verdeutlichen
1>d:\irrlicht engine\rapso - tetris\tetris_by_rapso\tetris_by_rapso\tetris_by_rapso.cpp(117) : warning C4554: '<<': Prüfen Sie Operatorrangfolge auf mögliche Fehler; verwenden Sie runde Klammern, um den Vorrang zu verdeutlichen
Alle Achtung! Bin beeindruckt.
Übrigens W4:
Zitat: 1>d:\irrlicht engine\rapso - tetris\tetris_by_rapso\tetris_by_rapso\tetris_by_rapso.cpp(68) : warning C4244: 'Initialisierung': Konvertierung von 'unsigned int' in 'SHORT', möglicher Datenverlust
1>d:\irrlicht engine\rapso - tetris\tetris_by_rapso\tetris_by_rapso\tetris_by_rapso.cpp(68) : warning C4244: 'Initialisierung': Konvertierung von 'unsigned int' in 'SHORT', möglicher Datenverlust
1>d:\irrlicht engine\rapso - tetris\tetris_by_rapso\tetris_by_rapso\tetris_by_rapso.cpp(81) : warning C4245: "=": Konvertierung von "int" in "unsigned short", signed/unsigned-Konflikt.
1>d:\irrlicht engine\rapso - tetris\tetris_by_rapso\tetris_by_rapso\tetris_by_rapso.cpp(72) : warning C4100: 'LogicStep': Unreferenzierter formaler Parameter
1>d:\irrlicht engine\rapso - tetris\tetris_by_rapso\tetris_by_rapso\tetris_by_rapso.cpp(90) : warning C4100: 'LogicStep': Unreferenzierter formaler Parameter
1>d:\irrlicht engine\rapso - tetris\tetris_by_rapso\tetris_by_rapso\tetris_by_rapso.cpp(105) : warning C4244: 'Argument': Konvertierung von 'const unsigned int' in 'unsigned char', möglicher Datenverlust
1>d:\irrlicht engine\rapso - tetris\tetris_by_rapso\tetris_by_rapso\tetris_by_rapso.cpp(106) : warning C4244: 'Argument': Konvertierung von 'const unsigned int' in 'unsigned char', möglicher Datenverlust
1>d:\irrlicht engine\rapso - tetris\tetris_by_rapso\tetris_by_rapso\tetris_by_rapso.cpp(107) : warning C4244: 'Argument': Konvertierung von 'const unsigned int' in 'unsigned char', möglicher Datenverlust
1>d:\irrlicht engine\rapso - tetris\tetris_by_rapso\tetris_by_rapso\tetris_by_rapso.cpp(108) : warning C4244: 'Argument': Konvertierung von 'const unsigned int' in 'unsigned char', möglicher Datenverlust
1>d:\irrlicht engine\rapso - tetris\tetris_by_rapso\tetris_by_rapso\tetris_by_rapso.cpp(117) : warning C4554: '>>': Prüfen Sie Operatorrangfolge auf mögliche Fehler; verwenden Sie runde Klammern, um den Vorrang zu verdeutlichen
1>d:\irrlicht engine\rapso - tetris\tetris_by_rapso\tetris_by_rapso\tetris_by_rapso.cpp(117) : warning C4554: '<<': Prüfen Sie Operatorrangfolge auf mögliche Fehler; verwenden Sie runde Klammern, um den Vorrang zu verdeutlichen
1>d:\irrlicht engine\rapso - tetris\tetris_by_rapso\tetris_by_rapso\tetris_by_rapso.cpp(237) : warning C4100: 'LogicStep': Unreferenzierter formaler Parameter
1>d:\irrlicht engine\rapso - tetris\tetris_by_rapso\tetris_by_rapso\tetris_by_rapso.cpp(252) : warning C4100: 'LogicStep': Unreferenzierter formaler Parameter
1>d:\irrlicht engine\rapso - tetris\tetris_by_rapso\tetris_by_rapso\tetris_by_rapso.cpp(264) : warning C4100: 'argv': Unreferenzierter formaler Parameter
1>d:\irrlicht engine\rapso - tetris\tetris_by_rapso\tetris_by_rapso\tetris_by_rapso.cpp(264) : warning C4100: 'argc': Unreferenzierter formaler Parameter
_________________ OS-Development-, C++, Win32-API-, MFC-, Chemie-, Robotik- und Flugsimulator-Tutorials
http://www.henkessoft.de/index.htm
Zuletzt bearbeitet von Erhard Henkes am 13:28:49 06.09.2008, insgesamt 1-mal bearbeitet
Erhard Henkes
Mitglied
Benutzerprofil
Anmeldungsdatum: 25.04.2000
Beiträge: 11924
Erhard Henkes Mitglied
13:37:50 06.09.2008 Titel:
Zitieren
Tetris hat als Konsolen-Programm begonnen:
http://www.colinfahey.com/tetris/tetris_de.html
http://www.colinfahey.com/tetris/original_tetris_start_game02.jpg
rapso kommt also ca. 20 Jahre zu spät.
Schafft einer dieses Programm zum Laufen zu bekommen?
http://homepages.cwi.nl/~tromp/tetris.html
Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
long h[4];t(){h[3]-=h[3]/3000;setitimer(0,h,0);}c,d,l,v[]={(int)t,0,2},w,s,I,K
=0,i=276,j,k,q[276],Q[276],*n=q,*m,x=17,f[]={7,-13,-12,1,8,-11,-12,-1,9,-1,1,
12,3,-13,-12,-1,12,-1,11,1,15,-1,13,1,18,-1,1,2,0,-12,-1,11,1,-12,1,13,10,-12,
1,12,11,-12,-1,1,2,-12,-1,12,13,-12,12,13,14,-11,-1,1,4,-13,-12,12,16,-11,-12,
12,17,-13,1,-1,5,-12,12,11,6,-12,12,24};u(){for(i=11;++i<264;)if((k=q[i])-Q[i]
){Q[i]=k;if(i-++I||i%12<1)printf("\033[%d;%dH",(I=i)/12,i%12*2+28);printf(
"\033[%dm "+(K-k?0:5),k);K=k;}Q[263]=c=getchar();}G(b){for(i=4;i--;)if(q[i?b+
n[i]:b])return 0;return 1;}g(b){for(i=4;i--;q[i?x+n[i]:x]=b);}main(C,V,a)char*
*V,*a;{h[3]=1000000/(l=C>1?atoi(V[1]):2);for(a=C>2?V[2]:"jkl pq";i;i--)*n++=i<
25||i%12<2?7:0;srand(getpid());system("stty cbreak -echo stop u");sigvec(14,v,
0);t();puts("\033[H\033[J");for(n=f+rand()%7*4;;g(7),u(),g(0)){if(c<0){if(G(x+
12))x+=12;else{g(7);++w;for(j=0;j<252;j=12*(j/12+1))for(;q[++j];)if(j%12==10){
for(;j%12;q[j--]=0);u();for(;--j;q[j+12]=q[j]);u();}n=f+rand()%7*4;G(x=17)||(c
=a[5]);}}if(c==*a)G(--x)||++x;if(c==a[1])n=f+4**(m=n),G(x)||(n=m);if(c==a[2])G
(++x)||--x;if(c==a[3])for(;G(x+12);++w)x+=12;if(c==a[4]||c==a[5]){s=sigblock(
8192);printf("\033[H\033[J\033[0m%d\n",w);if(c==a[5])break;for(j=264;j--;Q[j]=
0);while(getchar()-a[4]);puts("\033[H\033[J\033[7m");sigsetmask(s);}}d=popen(
"stty -cbreak echo stop \023;sort -mnr -o HI - HI;cat HI","w");fprintf(d,
"%4d from level %1d by %s\n",w,l,getlogin());pclose(d);}
Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
long h[4];t(){h[3]-=h[3]/3000;setitimer(0,h,0);}c,d,l,v[]={(int)t,0,2},w,s,I,K
=0,i=276,j,k,q[276],Q[276],*n=q,*m,x=17,f[]={7,-13,-12,1,8,-11,-12,-1,9,-1,1,
12,3,-13,-12,-1,12,-1,11,1,15,-1,13,1,18,-1,1,2,0,-12,-1,11,1,-12,1,13,10,-12,
1,12,11,-12,-1,1,2,-12,-1,12,13,-12,12,13,14,-11,-1,1,4,-13,-12,12,16,-11,-12,
12,17,-13,1,-1,5,-12,12,11,6,-12,12,24};u(){for(i=11;++i<264;)if((k=q[i])-Q[i]
){Q[i]=k;if(i-++I||i%12<1)printf("\033[%d;%dH",(I=i)/12,i%12*2+28);printf(
"\033[%dm "+(K-k?0:5),k);K=k;}Q[263]=c=getchar();}G(b){for(i=4;i--;)if(q[i?b+
n[i]:b])return 0;return 1;}g(b){for(i=4;i--;q[i?x+n[i]:x]=b);}main(C,V,a)char*
*V,*a;{h[3]=1000000/(l=C>1?atoi(V[1]):2);for(a=C>2?V[2]:"jkl pq";i;i--)*n++=i<
25||i%12<2?7:0;srand(getpid());system("stty cbreak -echo stop u");sigvec(14,v,
0);t();puts("\033[H\033[J");for(n=f+rand()%7*4;;g(7),u(),g(0)){if(c<0){if(G(x+
12))x+=12;else{g(7);++w;for(j=0;j<252;j=12*(j/12+1))for(;q[++j];)if(j%12==10){
for(;j%12;q[j--]=0);u();for(;--j;q[j+12]=q[j]);u();}n=f+rand()%7*4;G(x=17)||(c
=a[5]);}}if(c==*a)G(--x)||++x;if(c==a[1])n=f+4**(m=n),G(x)||(n=m);if(c==a[2])G
(++x)||--x;if(c==a[3])for(;G(x+12);++w)x+=12;if(c==a[4]||c==a[5]){s=sigblock(
8192);printf("\033[H\033[J\033[0m%d\n",w);if(c==a[5])break;for(j=264;j--;Q[j]=
0);while(getchar()-a[4]);puts("\033[H\033[J\033[7m");sigsetmask(s);}}d=popen(
"stty -cbreak echo stop \023;sort -mnr -o HI - HI;cat HI","w");fprintf(d,
"%4d from level %1d by %s\n",w,l,getlogin());pclose(d);}
Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
long h[4];t(){h[3]-=h[3]/3000;setitimer(0,h,0);}c,d,l,v[]={(int)t,0,2},w,s,I,K
=0,i=276,j,k,q[276],Q[276],*n=q,*m,x=17,f[]={7,-13,-12,1,8,-11,-12,-1,9,-1,1,
12,3,-13,-12,-1,12,-1,11,1,15,-1,13,1,18,-1,1,2,0,-12,-1,11,1,-12,1,13,10,-12,
1,12,11,-12,-1,1,2,-12,-1,12,13,-12,12,13,14,-11,-1,1,4,-13,-12,12,16,-11,-12,
12,17,-13,1,-1,5,-12,12,11,6,-12,12,24};u(){for(i=11;++i<264;)if((k=q[i])-Q[i]
){Q[i]=k;if(i-++I||i%12<1)printf("\033[%d;%dH",(I=i)/12,i%12*2+28);printf(
"\033[%dm "+(K-k?0:5),k);K=k;}Q[263]=c=getchar();}G(b){for(i=4;i--;)if(q[i?b+
n[i]:b])return 0;return 1;}g(b){for(i=4;i--;q[i?x+n[i]:x]=b);}main(C,V,a)char*
*V,*a;{h[3]=1000000/(l=C>1?atoi(V[1]):2);for(a=C>2?V[2]:"jkl pq";i;i--)*n++=i<
25||i%12<2?7:0;srand(getpid());system("stty cbreak -echo stop u");sigvec(14,v,
0);t();puts("\033[H\033[J");for(n=f+rand()%7*4;;g(7),u(),g(0)){if(c<0){if(G(x+
12))x+=12;else{g(7);++w;for(j=0;j<252;j=12*(j/12+1))for(;q[++j];)if(j%12==10){
for(;j%12;q[j--]=0);u();for(;--j;q[j+12]=q[j]);u();}n=f+rand()%7*4;G(x=17)||(c
=a[5]);}}if(c==*a)G(--x)||++x;if(c==a[1])n=f+4**(m=n),G(x)||(n=m);if(c==a[2])G
(++x)||--x;if(c==a[3])for(;G(x+12);++w)x+=12;if(c==a[4]||c==a[5]){s=sigblock(
8192);printf("\033[H\033[J\033[0m%d\n",w);if(c==a[5])break;for(j=264;j--;Q[j]=
0);while(getchar()-a[4]);puts("\033[H\033[J\033[7m");sigsetmask(s);}}d=popen(
"stty -cbreak echo stop \023;sort -mnr -o HI - HI;cat HI","w");fprintf(d,
"%4d from level %1d by %s\n",w,l,getlogin());pclose(d);}
_________________ OS-Development-, C++, Win32-API-, MFC-, Chemie-, Robotik- und Flugsimulator-Tutorials
http://www.henkessoft.de/index.htm
Zuletzt bearbeitet von Erhard Henkes am 13:41:47 06.09.2008, insgesamt 1-mal bearbeitet
Nächstes Thema anzeigen
Vorheriges Thema anzeigen
Sie können Beiträge in dieses Forum schreiben. Sie können auf Beiträge in diesem Forum antworten. Sie können Ihre Beiträge in diesem Forum nicht bearbeiten. Sie können Ihre Beiträge in diesem Forum nicht löschen. Sie können an Umfragen in diesem Forum nicht mitmachen.