Help Creating High Scores

Hello All,

So, I just started trying to learn C++ about 3 days ago as a hobby. So far I am really enjoying it and have learned a lot. But anyways, I am posting because I found the guide for the Tetris clone at devmaster and put it together and I even figured out how to add a score. I have everything working fine and am just looking to build upon the program a little more. Next I would like to try and include a way to keep track of high scores, and I was wondering if any of you could maybe help get me started.
So far I have looked at tutorials and guides on the internet and haven't been able to find anything that I could use.
For now I would be satisfied with maybe some way of recording the score when a player loses in a text file, and then I could try and go from there to get the information into a seperate window that could be accessed from the menu bar. So maybe if anyone could tell me what kind of functions I should try looking further into or just any advice, I would really appreciate it.

So here is my code so far. If anyone sees anywhere I could improve it please let me know I would really appreciate any feedback. Thanks.

I separated the 3 files with this line so you could hopefully read it easier
/********************************************************************

//main.cpp
#define WIN32_LEAN_AND_MEAN
#include
#include
#include "bitmapobject.h"
#define WINDOWCLASS "My Awesome Game!"
#define WINDOWTITLE "My Awesome Game!"

const int TILESIZE=16;
const int MAPWIDTH=13;
const int MAPHEIGHT=30;
const int GREY=8;
const int TILENODRAW=-1;
const int TILEBLACK=0;
const int TILEGREY=1;
const int TILEBLUE=2;
const int TILERED=3;
const int TILEGREEN=4;
const int TILEYELLOW=5;
const int TILEWHITE=6;
const int TILESTEEL=7;
const int TILEPURPLE=8;
const int TILEZERO=9;
const int TILEONE=10;
const int TILETWO=11;
const int TILETHREE=12;
const int TILEFOUR=13;
const int TILEFIVE=14;
const int TILESIX=15;
const int TILESEVEN=16;
const int TILEEIGHT=17;
const int TILENINE=18;
int score = 00000;
bool GameInit();
void GameLoop();
void GameDone();
void DrawTile(int x, int y, int tile);
void DrawMap();
void NewBlock();
void RotateBlock();
void Move(int x, int y);
int CollisionTest(int nx, int ny);
void RemoveRow(int row);
void NewGame();

HINSTANCE hInstMain=NULL;
HWND hWndMain=NULL;

int Map[MAPWIDTH][MAPHEIGHT+1];

struct Piece {
int size[4][4];
int x;
int y;
};

Piece sPrePiece;
Piece sPiece;

DWORD start_time;
bool GAMESTARTED=false;

BitMapObject bmoMap;
BitMapObject bmoBlocks;

LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg)
{
case WM_KEYDOWN:
{
if(wParam==VK_ESCAPE)
{
DestroyWindow(hWndMain);
return(0);
}
else if(wParam==VK_DOWN)
{
Move(0,1);
return(0);
}
else if(wParam==VK_UP)
{
RotateBlock();
return(0);
}
else if(wParam==VK_LEFT)
{
Move(-1,0);
return(0);
}
else if(wParam==VK_RIGHT)
{
Move(1,0);
return(0);
}
}break;
case WM_DESTROY:
{
PostQuitMessage(0);
return(0);
}break;
case WM_PAINT:
{
PAINTSTRUCT ps;

HDC hdc=BeginPaint(hwnd,&ps);
BitBlt(hdc,0,0,TILESIZE*MAPWIDTH+TILESIZE*GREY,TILESIZE*MAPHEIGHT,bmoMap,0,0,SRCCOPY);

EndPaint(hwnd,&ps);

return(0);
}break;
}
return(DefWindowProc(hwnd,uMsg,wParam,lParam));
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
hInstMain=hInstance;

WNDCLASSEX wcx;

wcx.cbSize=sizeof(WNDCLASSEX);

wcx.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;

wcx.lpfnWndProc=TheWindowProc;

wcx.cbClsExtra=0;

wcx.cbWndExtra=0;

wcx.hInstance=hInstMain;

wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);

wcx.hCursor=LoadCursor(NULL,IDC_ARROW);

wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);

wcx.lpszMenuName=NULL;

wcx.lpszClassName=WINDOWCLASS;

wcx.hIconSm=NULL;

if(!RegisterClassEx(&wcx)) return(0);

hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_BORDER | WS_SYSMENU | WS_CAPTION| WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);

if(!hWndMain) return(0);

if(!GameInit()) return(0);

MSG msg;

for( ; ; )
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message==WM_QUIT) break;

TranslateMessage(&msg);

DispatchMessage(&msg);
}
GameLoop();
}
GameDone();

return(msg.wParam);
}

bool GameInit()
{
RECT rcTemp;
SetRect(&rcTemp,0,0,MAPWIDTH*TILESIZE+TILESIZE*GREY,MAPHEIGHT*TILESIZE);//160x480 client area
AdjustWindowRect(&rcTemp,WS_BORDER | WS_SYSMENU | WS_CAPTION| WS_VISIBLE,FALSE);//adjust the window size based on desired client area
SetWindowPos(hWndMain,NULL,0,0,rcTemp.right-rcTemp.left,rcTemp.bottom-rcTemp.top,SWP_NOMOVE);//set the window width and height

HDC hdc=GetDC(hWndMain);
bmoMap.Create(hdc,MAPWIDTH*TILESIZE+TILESIZE*GREY,MAPHEIGHT*TILESIZE);
FillRect(bmoMap,&rcTemp,(HBRUSH)GetStockObject(BLACK_BRUSH));
ReleaseDC(hWndMain,hdc);

bmoBlocks.Load(NULL,"blocks.bmp");
NewGame();

return(true);
}

void GameDone()
{
}

void GameLoop()
{
if( (GetTickCount() - start_time) > 1000)
{
Move(0,1);
start_time=GetTickCount();
}
}

void NewGame()
{
start_time=GetTickCount();
GAMESTARTED=false;

for(int x=0;x< MAPWIDTH;x++)
{
for(int y=0;y< MAPHEIGHT+1;y++)
{
if(y==MAPHEIGHT) //makes Y-collision easier.
Map[x][y]=TILEGREY;
else
Map[x][y]=TILEBLACK;
}
}
NewBlock();

DrawMap();
}

void DrawTile(int x,int y,int tile)//put a tile
{
BitBlt(bmoMap,x*TILESIZE,y*TILESIZE,TILESIZE,TILESIZE,bmoBlocks,tile*TILESIZE,TILESIZE,SRCAND);
BitBlt(bmoMap,x*TILESIZE,y*TILESIZE,TILESIZE,TILESIZE,bmoBlocks,tile*TILESIZE,0,SRCPAINT);
}

void DrawMap()
{
int xmy, ymx;
int temp = score;

for(xmy=MAPWIDTH; xmy< MAPWIDTH+GREY; xmy++)
for(ymx=0; ymx< MAPHEIGHT; ymx++)
DrawTile(xmy, ymx, TILEGREY);

for(xmy=0; xmy<4; xmy++)
for(ymx=0; ymx<4; ymx++)
if(sPrePiece.size[xmy][ymx] != TILENODRAW)
DrawTile(sPrePiece.x+xmy, sPrePiece.y+ymx, sPrePiece.size[xmy][ymx]);

for(xmy=0;xmy< MAPWIDTH;xmy++)
for(ymx=0;ymx< MAPHEIGHT;ymx++)
DrawTile(xmy,ymx,Map[xmy][ymx]);

for(xmy=0; xmy<4; xmy++)
for(ymx=0; ymx<4; ymx++)
if(sPiece.size[xmy][ymx] != TILENODRAW)
DrawTile(sPiece.x+xmy, sPiece.y+ymx, sPiece.size[xmy][ymx]);

DrawTile(MAPWIDTH+1,GREY,(9+temp/100000));
if(temp >= 100000)
temp-=(100000*(temp/100000));
DrawTile(MAPWIDTH+2,GREY,(9+temp/10000));
if(temp >= 10000)
temp-=(10000*(temp/10000));
DrawTile(MAPWIDTH+3,GREY,(9+temp/1000));
if(temp >= 1000)
temp-=(1000*(temp/1000));
DrawTile(MAPWIDTH+4,GREY,(9+temp/100));
if(temp >= 100)
temp-=(100*(temp/100));
DrawTile(MAPWIDTH+5,GREY,(9+temp/10));
if(temp >= 10)
temp-=(10*(temp/10));
DrawTile(MAPWIDTH+6,GREY,(9+temp));

InvalidateRect(hWndMain,NULL,FALSE);
}

void NewBlock()
{
int newblock;
int i,j;
// 0 1 2 3 4 5 6
// X These
// X XX X XX XX XX XX are
// X XX XXX XX XX X X block
// X X X types

srand(GetTickCount());

for(i=0; i<4; i++)
for(j=0; j<4; j++)
sPiece.size[ i ][j]=TILENODRAW;
score = (++score);

sPiece.x=MAPWIDTH/2-2;
sPiece.y=-1;

if(GAMESTARTED == false)
{
GAMESTARTED=true;

newblock=rand()%7;

switch (newblock)
{
case 0:
{
sPiece.size[1][0]=TILERED;
sPiece.size[1][1]=TILERED;
sPiece.size[1][2]=TILERED;
sPiece.size[1][3]=TILERED;
sPiece.y=0;
}break;
case 1:
{
sPiece.size[1][1]=TILEBLUE;
sPiece.size[1][2]=TILEBLUE;
sPiece.size[2][1]=TILEBLUE;
sPiece.size[2][2]=TILEBLUE;
}break;
case 2:
{
sPiece.size[1][1]=TILESTEEL;
sPiece.size[0][2]=TILESTEEL;
sPiece.size[1][2]=TILESTEEL;
sPiece.size[2][2]=TILESTEEL;
}break;
case 3:
{
sPiece.size[0][1]=TILEYELLOW;
sPiece.size[1][1]=TILEYELLOW;
sPiece.size[1][2]=TILEYELLOW;
sPiece.size[2][2]=TILEYELLOW;
}break;
case 4:
{
sPiece.size[2][1]=TILEGREEN;
sPiece.size[1][1]=TILEGREEN;
sPiece.size[1][2]=TILEGREEN;
sPiece.size[0][2]=TILEGREEN;
}break;
case 5:
{
sPiece.size[1][1]=TILEWHITE;
sPiece.size[2][1]=TILEWHITE;
sPiece.size[2][2]=TILEWHITE;
sPiece.size[2][3]=TILEWHITE;
}break;
case 6:
{
sPiece.size[2][1]=TILEPURPLE;
sPiece.size[1][1]=TILEPURPLE;
sPiece.size[1][2]=TILEPURPLE;
sPiece.size[1][3]=TILEPURPLE;
}break;
}
}
else
{
for(i=0; i<4; i++)
for(j=0; j<4; j++)
sPiece.size[ i ][j]=sPrePiece.size[ i ][j];

}

newblock=rand()%7;

for(i=0; i<4; i++)
for(j=0; j<4; j++)
sPrePiece.size[ i ][j]=TILENODRAW;

sPrePiece.x=MAPWIDTH+GREY/4;
sPrePiece.y=GREY/4;

switch (newblock)
{
case 0:
{
sPrePiece.size[1][0]=TILERED;
sPrePiece.size[1][1]=TILERED;
sPrePiece.size[1][2]=TILERED;
sPrePiece.size[1][3]=TILERED;
}break;
case 1:
{
sPrePiece.size[1][1]=TILEBLUE;
sPrePiece.size[1][2]=TILEBLUE;
sPrePiece.size[2][1]=TILEBLUE;
sPrePiece.size[2][2]=TILEBLUE;
}break;
case 2:
{
sPrePiece.size[1][1]=TILESTEEL;
sPrePiece.size[0][2]=TILESTEEL;
sPrePiece.size[1][2]=TILESTEEL;
sPrePiece.size[2][2]=TILESTEEL;
}break;
case 3:
{
sPrePiece.size[0][1]=TILEYELLOW;
sPrePiece.size[1][1]=TILEYELLOW;
sPrePiece.size[1][2]=TILEYELLOW;
sPrePiece.size[2][2]=TILEYELLOW;
}break;
case 4:
{
sPrePiece.size[2][1]=TILEGREEN;
sPrePiece.size[1][1]=TILEGREEN;
sPrePiece.size[1][2]=TILEGREEN;
sPrePiece.size[0][2]=TILEGREEN;
}break;
case 5:
{
sPrePiece.size[1][1]=TILEWHITE;
sPrePiece.size[2][1]=TILEWHITE;
sPrePiece.size[2][2]=TILEWHITE;
sPrePiece.size[2][3]=TILEWHITE;
}break;
case 6:
{
sPrePiece.size[2][1]=TILEPURPLE;
sPrePiece.size[1][1]=TILEPURPLE;
sPrePiece.size[1][2]=TILEPURPLE;
sPrePiece.size[1][3]=TILEPURPLE;
}break;
}

DrawMap();
}

void RotateBlock()
{
int i, j, temp[4][4];

for(i=0; i<4; i++)
for(j=0; j<4; j++)
temp[3-j][ i ]=sPiece.size[ i ][j];

for(i=0; i<4; i++)
for(j=0; j<4; j++)
if(temp[ i ][j] != TILENODRAW)
if(sPiece.x + i < 0 || sPiece.x + i > MAPWIDTH - 1 ||
sPiece.y + j < 0 || sPiece.y + j > MAPHEIGHT - 1)
return;

for(int x=0; x< MAPWIDTH; x++)
for(int y=0; y< MAPHEIGHT; y++)
if(x >= sPiece.x && x < sPiece.x + 4)
if(y >= sPiece.y && y < sPiece.y +4)
if(Map[x][y] != TILEBLACK)
if(temp[x - sPiece.x][y - sPiece.y] != TILENODRAW)
return;

for(i=0; i<4; i++)
for(j=0; j<4; j++)
sPiece.size[ i ][j]=temp[ i ][j];

DrawMap();

return;
}

void Move(int x, int y)
{
if(CollisionTest(x, y))
{
if(y == 1)
{
if(sPiece.y<1)
{
score = 00000;
NewGame();
}
else
{
bool killblock=false;
int i,j;
for(i=0; i<4; i++)
for(j=0; j<4; j++)
if(sPiece.size[ i ][j] != TILENODRAW)
Map[sPiece.x+i][sPiece.y+j] = sPiece.size[ i ][j];

for(j=0; j< MAPHEIGHT; j++)
{
bool filled=true;
for(i=0; i< MAPWIDTH; i++)
if(Map[ i ][j] == TILEBLACK)
filled=false;

if(filled)
{
RemoveRow(j);
score = (score + 10);
killblock=true;
}
}

if(killblock)
{
for(i=0; i<4; i++)
for(j=0; j<4; j++)
sPiece.size[ i ][j]=TILENODRAW;

}
NewBlock();
}
}

}
else
{
sPiece.x+=x;
sPiece.y+=y;
}

DrawMap();
}

int CollisionTest(int nx, int ny)
{
int newx=sPiece.x+nx;
int newy=sPiece.y+ny;

int i,j,x,y;

for(i=0; i< 4; i++)
for(j=0; j< 4; j++)
if(sPiece.size[ i ][j] != TILENODRAW)
if(newx + i < 0 || newx + i > MAPWIDTH - 1 ||
newy + j < 0 || newy + j > MAPHEIGHT - 1)
return 1;

for(x=0; x< MAPWIDTH; x++)
for(y=0; y< MAPHEIGHT; y++)
if(x >= newx && x < newx + 4)
if(y >= newy && y < newy +4)
if(Map[x][y] != TILEBLACK)
if(sPiece.size[x - newx][y - newy] != TILENODRAW)
return 1;
return 0;
}

void RemoveRow(int row)
{
int x,y;
int counter=0;

for(x=0; x< MAPWIDTH; x++)
for(y=row; y>0; y--)
Map[x][y]=Map[x][y-1];

}



/*************************************************************************
//BitMapObject.h
#ifndef BITMAPOBJECT_H
#define BITMAPOBJECT_H
#pragma once
#include

class BitMapObject
{
private:
HDC hdcMemory;
HBITMAP hbmNewBitMap;
HBITMAP hbmOldBitMap;
int iWidth;
int iHeight;

public:
BitMapObject();

~BitMapObject();

void Load(HDC hdcCompatible,LPCTSTR lpszFilename);

void Create(HDC hdcCompatible, int width, int height);

void Destroy();

int GetWidth();

int GetHeight();

operator HDC();
};

#endif


/********************************************************************

//BitMapObject.cpp
#include "bitmapobject.h"

BitMapObject::BitMapObject()
{
hdcMemory=NULL;
hbmNewBitMap=NULL;
hbmOldBitMap=NULL;
iWidth=0;
iHeight=0;
}

BitMapObject::~BitMapObject()
{
if(hdcMemory)
Destroy();
}

void BitMapObject::Load(HDC hdcCompatible, LPCTSTR lpszFilename)
{
if(hdcMemory)
Destroy();

hdcMemory=CreateCompatibleDC(hdcCompatible);
hbmNewBitMap=(HBITMAP)LoadImage(NULL,lpszFilename,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
hbmOldBitMap=(HBITMAP)SelectObject(hdcMemory,hbmNewBitMap);
BITMAP bmp;
GetObject(hbmNewBitMap,sizeof(BITMAP),(LPVOID)&bmp);
iWidth=bmp.bmWidth;
iHeight=bmp.bmHeight;
}

void BitMapObject::Create(HDC hdcCompatible, int width, int height)
{
if(hdcMemory)
Destroy();

hdcMemory=CreateCompatibleDC(hdcCompatible);
hbmNewBitMap=CreateCompatibleBitmap(hdcCompatible, width, height);
hbmOldBitMap=(HBITMAP)SelectObject(hdcMemory, hbmNewBitMap);
iWidth=width;
iHeight=height;
}

void BitMapObject::Destroy()
{
SelectObject(hdcMemory, hbmOldBitMap);
DeleteObject(hbmNewBitMap);
DeleteDC(hdcMemory);
hdcMemory=NULL;
hbmNewBitMap=NULL;
hbmOldBitMap=NULL;
iWidth=0;
iHeight=0;
}

BitMapObject::operator HDC()
{
return(hdcMemory);
}

int BitMapObject::GetWidth()
{
return(iWidth);
}

int BitMapObject::GetHeight()
{
return(iHeight);
}

/********************************************************************

Thanks For Taking The Time To Look It Over.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories