Now guys you gotta think iv been coding c++ for two years now and i still havnt master it yet now what i going to show is a little game called snakes what you do it you move the snake to eat the apple now this is just in ones section remember theres 5 section just like this one of code now as you can see this is a really simple game but an RTS game have like 50 of these folders with lines and lines of coding so just remember its really hard to do something like thses game in a short matter of time and theose big game like STARCRAFT2 they had like 20 prefestional coders working on the game
include "stdafx.h"
#include "snake.h"
#include <ctime>
#define MAX_LOADSTRING 100
//set up enum/struct
enum index
{
GRASS_INDEX,
SNAKE_INDEX,
APPLE_INDEX,
WALL_INDEX
};
enum direction
{
up,
down,
left,
right
};
struct pos
{
int x;
int y;
};
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
bool bOne = true;
char map[13][13] = {
{3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 3},
{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{3, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{3, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0, 0, 3},
{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{3, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3}
};
pos coord[144];
int bodySize = 3;
pos oldCoord[100];
int points = 0;
int level = 6;
int levelMap = 6;
char headDirection = 5;
void move(char direction, HWND hWnd);
void putApple(HWND hWnd);
void gameOver(HWND hWnd);
void newLevel(HWND hWnd);
void randomWall();
wchar_t text[3];
int GAME_SPEED = 150;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_SNAKE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SNAKE));
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage are only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SNAKE));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_SNAKE);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_GECKO));
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_SYSMENU,
CW_USEDEFAULT, 0, 800, 708, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
void LoadAndBlitBitmap(LPCWSTR szFileName, HDC hWinDC, int width, int height, int sizeX, int sizeY)
{
HDC MemDCExercising;
HBITMAP bmpExercising;
// Load the bitmap from the resource
bmpExercising = LoadBitmap(hInst, MAKEINTRESOURCE(szFileName));
// Create a memory device compatible with the above DC variable
MemDCExercising = CreateCompatibleDC(hWinDC);
// Select the new bitmap
SelectObject(MemDCExercising, bmpExercising);
// Copy the bits from the memory DC into the current dc
BitBlt(hWinDC, width, height, sizeX, sizeY, MemDCExercising, 0, 0, SRCCOPY);
// Restore the old bitmap
DeleteDC(MemDCExercising);
DeleteObject(bmpExercising);
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//set position and coord only once during 1 life
if(bOne)
{
bOne = false;
coord[0].x = 2;
coord[0].y = 2;
coord[1].x = 2;
coord[1].y = 3;
coord[2].x = 2;
coord[2].y = 4;
oldCoord[0].x = 2;
oldCoord[0].y = 2;
oldCoord[1].x = 2;
oldCoord[1].y = 3;
oldCoord[2].x = 2;
oldCoord[2].y = 4;
}
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
//set up timer
SetTimer(hWnd, 1, (GAME_SPEED / level*3), NULL);
switch (message)
{
case WM_TIMER:
{
move(headDirection, hWnd);
InvalidateRect(hWnd, 0, FALSE);
break;
}
case WM_KEYUP:
{
switch(wParam)
{
//check if key was pressed and change direction
move(headDirection, hWnd);
case VK_UP:
if(headDirection != down)
{
headDirection = up;
move(headDirection, hWnd);
InvalidateRect(hWnd, 0, FALSE);
}
break;
case VK_DOWN:
if(headDirection != up)
{
headDirection = down;
move(headDirection, hWnd);
InvalidateRect(hWnd, 0, FALSE);
}
break;
case VK_LEFT:
if(headDirection != right && headDirection != 5)
{
headDirection = left;
move(headDirection, hWnd);
InvalidateRect(hWnd, 0, FALSE);
}
break;
case VK_RIGHT:
if(headDirection != left)
{
headDirection = right;
move(headDirection, hWnd);
InvalidateRect(hWnd, 0, FALSE);
}
break;
case 'Q':
DestroyWindow(hWnd);
break;
}
}
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
//choosing level
case IDM_LEVEL1:
levelMap = 1;
newLevel(hWnd);
putApple(hWnd);
points = points - 50;
break;
case IDM_LEVEL2:
levelMap = 2;
newLevel(hWnd);
putApple(hWnd);
points = points - 50;
break;
case IDM_LEVEL3:
levelMap = 3;
newLevel(hWnd);
putApple(hWnd);
points = points - 50;
break;
case IDM_LEVEL4:
levelMap = 4;
newLevel(hWnd);
putApple(hWnd);
points = points - 50;
break;
case IDM_LEVEL5:
levelMap = 5;
newLevel(hWnd);
putApple(hWnd);
points = points - 50;
break;
//choosing speed
case IDM_SLOW:
GAME_SPEED = 200;
break;
case IDM_NORMAL:
GAME_SPEED = 150;
break;
case IDM_FAST:
GAME_SPEED = 100;
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here..
for(int y = 0; y < 13; ++y)
{
for(int x = 0; x < 13; ++x)
{
//painting the map boxes
if(map[y][x] == GRASS_INDEX)
{
LoadAndBlitBitmap((MAKEINTRESOURCE(IDB_GRASS)), hdc, 50 * x, 50 * y, 50, 50);
}
else if(map[y][x] == SNAKE_INDEX)
{
if(y == coord[bodySize-1].x && x == coord[bodySize-1].y)
{
//check head direction
if(headDirection == up)
{
LoadAndBlitBitmap((MAKEINTRESOURCE(IDB_HEADUP)), hdc, 50 * x, 50 * y, 50, 50);
}
if(headDirection == down)
{
LoadAndBlitBitmap((MAKEINTRESOURCE(IDB_HEADDOWN)), hdc, 50 * x, 50 * y, 50, 50);
}
if(headDirection == left)
{
LoadAndBlitBitmap((MAKEINTRESOURCE(IDB_HEADLEFT)), hdc, 50 * x, 50 * y, 50, 50);
}
if(headDirection == right || headDirection == 5)
{
LoadAndBlitBitmap((MAKEINTRESOURCE(IDB_HEADRIGHT)), hdc, 50 * x, 50 * y, 50, 50);
}
}
else if(y == coord[0].x && x == coord[0].y)
{
//check the next box to the tail
if(coord[0].x-1 == coord[1].x)
{
LoadAndBlitBitmap((MAKEINTRESOURCE(IDB_TAILUP)), hdc, 50 * x, 50 * y, 50, 50);
}
if(coord[0].x+1 == coord[1].x)
{
LoadAndBlitBitmap((MAKEINTRESOURCE(IDB_TAILDOWN)), hdc, 50 * x, 50 * y, 50, 50);
}
if(coord[0].y-1 == coord[1].y)
{
LoadAndBlitBitmap((MAKEINTRESOURCE(IDB_TAILLEFT)), hdc, 50 * x, 50 * y, 50, 50);
}
if(coord[0].y+1 == coord[1].y)
{
LoadAndBlitBitmap((MAKEINTRESOURCE(IDB_TAILRIGHT)), hdc, 50 * x, 50 * y, 50, 50);
}
}
else
{
LoadAndBlitBitmap((MAKEINTRESOURCE(IDB_BODY)), hdc, 50 * x, 50 * y, 50, 50);
}
}
else if(map[y][x] == APPLE_INDEX)
{
LoadAndBlitBitmap((MAKEINTRESOURCE(IDB_APPLE)), hdc, 50 * x, 50 * y, 50, 50);
}
else if(map[y][x] == WALL_INDEX)
{
LoadAndBlitBitmap((MAKEINTRESOURCE(IDB_WALL)), hdc, 50 * x, 50 * y, 50, 50);
}
}
}
//printing out the score, size, level and map
wsprintf(text, __T("Score: %i"), points);
TextOut(hdc, 650, 50, text, 11);
wsprintf(text, __T("Size: %i"), bodySize);
TextOut(hdc, 650, 100, text,

;
wsprintf(text, __T("Level: %i"), level);
TextOut(hdc, 650, 150, text,

;
wsprintf(text, __T("Map: %i"), levelMap);
TextOut(hdc, 650, 200, text, 6);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
void move(char direction, HWND hWnd)
{
//make sure old coord is equal to the new coord
for(int i = 0; i < bodySize; i++)
{
oldCoord[i].x = coord[i].x;
oldCoord[i].y = coord[i].y;
}
if(direction == up)
{
if(map[(coord[bodySize-1].x)-1][coord[bodySize-1].y] == GRASS_INDEX)
{
//if we are on the grass than move
map[coord[bodySize-1].x-1][coord[bodySize-1].y] = SNAKE_INDEX;
coord[bodySize-1].x--;
map[coord[0].x][coord[0].y] = GRASS_INDEX;
for(int i = 0; i < bodySize-1; i++)
{
coord[i].x = oldCoord[i+1].x;
coord[i].y = oldCoord[i+1].y;
}
}
else if(map[(coord[bodySize-1].x)-1][coord[bodySize-1].y] == APPLE_INDEX)
{
//if head will be on the apple at the next move than make us longer + put an apple + play sound
PlaySound((MAKEINTRESOURCE(IDR_SOUNDX)), NULL, SND_RESOURCE | SND_ASYNC);
map[coord[bodySize-1].x-1][coord[bodySize-1].y] = SNAKE_INDEX;
bodySize++;
coord[bodySize-1].x = coord[bodySize-2].x - 1;
coord[bodySize-1].y = coord[bodySize-2].y;
putApple(hWnd);
}
else
{
//if head will be in the wall at the next move than restart + play sound
PlaySound((MAKEINTRESOURCE(IDR_SOUNDWALL)), NULL, SND_RESOURCE | SND_ASYNC);
gameOver(hWnd);
levelMap = 1;
}
}
//same for other directions
if(direction == down)
{
if(map[(coord[bodySize-1].x)+1][coord[bodySize-1].y] == GRASS_INDEX)
{
map[coord[bodySize-1].x+1][coord[bodySize-1].y] = SNAKE_INDEX;
coord[bodySize-1].x++;
map[coord[0].x][coord[0].y] = GRASS_INDEX;
for(int i = 0; i < bodySize-1; i++)
{
coord[i].x = oldCoord[i+1].x;
coord[i].y = oldCoord[i+1].y;
}
}
else if(map[(coord[bodySize-1].x)+1][coord[bodySize-1].y] == APPLE_INDEX)
{
PlaySound((MAKEINTRESOURCE(IDR_SOUNDX)), NULL, SND_RESOURCE | SND_ASYNC);
map[coord[bodySize-1].x+1][coord[bodySize-1].y] = SNAKE_INDEX;
bodySize++;
coord[bodySize-1].x = coord[bodySize-2].x + 1;
coord[bodySize-1].y = coord[bodySize-2].y;
putApple(hWnd);
}
else
{
PlaySound((MAKEINTRESOURCE(IDR_SOUNDWALL)), NULL, SND_RESOURCE | SND_ASYNC);
gameOver(hWnd);
levelMap = 1;
}
}
if(direction == left)
{
if(map[(coord[bodySize-1].x)][coord[bodySize-1].y-1] == GRASS_INDEX)
{
map[coord[bodySize-1].x][coord[bodySize-1].y-1] = SNAKE_INDEX;
coord[bodySize-1].y--;
map[coord[0].x][coord[0].y] = GRASS_INDEX;
for(int i = 0; i < bodySize-1; i++)
{
coord[i].x = oldCoord[i+1].x;
coord[i].y = oldCoord[i+1].y;
}
}
else if(map[(coord[bodySize-1].x)][coord[bodySize-1].y-1] == APPLE_INDEX)
{
PlaySound((MAKEINTRESOURCE(IDR_SOUNDX)), NULL, SND_RESOURCE | SND_ASYNC);
map[coord[bodySize-1].x][coord[bodySize-1].y-1] = SNAKE_INDEX;
bodySize++;
coord[bodySize-1].x = coord[bodySize-2].x;
coord[bodySize-1].y = coord[bodySize-2].y - 1;
putApple(hWnd);
}
else
{
PlaySound((MAKEINTRESOURCE(IDR_SOUNDWALL)), NULL, SND_RESOURCE | SND_ASYNC);
gameOver(hWnd);
levelMap = 1;
}
}
if(direction == right)
{
if(map[(coord[bodySize-1].x)][coord[bodySize-1].y+1] == GRASS_INDEX)
{
map[coord[bodySize-1].x][coord[bodySize-1].y+1] = SNAKE_INDEX;
coord[bodySize-1].y++;
map[coord[0].x][coord[0].y] = GRASS_INDEX;
for(int i = 0; i < bodySize-1; i++)
{
coord[i].x = oldCoord[i+1].x;
coord[i].y = oldCoord[i+1].y;
}
}
else if(map[(coord[bodySize-1].x)][coord[bodySize-1].y+1] == APPLE_INDEX)
{
PlaySound((MAKEINTRESOURCE(IDR_SOUNDX)), NULL, SND_RESOURCE | SND_ASYNC);
map[coord[bodySize-1].x][coord[bodySize-1].y+1] = SNAKE_INDEX;
bodySize++;
coord[bodySize-1].x = coord[bodySize-2].x;
coord[bodySize-1].y = coord[bodySize-2].y + 1;
putApple(hWnd);
}
else
{
PlaySound((MAKEINTRESOURCE(IDR_SOUNDWALL)), NULL, SND_RESOURCE | SND_ASYNC);
gameOver(hWnd);
levelMap = 1;
}
}
}
void putApple(HWND hWnd)
{
points = points + 50;
if(points == 5000)
{
::MessageBox(hWnd, __T("OMG you have done it! Good Job!!"), __T("Snake"), MB_OK);
gameOver(hWnd);
}
if(points/level == 250 || (points-1000)/level == 250 || (points-2000)/level == 250 || (points-3000)/level == 250 || (points-4000)/level == 250)
{
level++;
}
if(points/levelMap == 1000)
{
levelMap++;
newLevel(hWnd);
}
srand((unsigned int)time(0));
pos rn;
rn.x = rand()%13;
rn.y = rand()%13;
while(map[rn.x][rn.y] != GRASS_INDEX)
{
rn.x = rand()%13;
rn.y = rand()%13;
}
map[rn.x][rn.y] = APPLE_INDEX;
}
void gameOver(HWND hWnd)
{
level = 1;
bodySize = 3;
coord[0].x = 2;
coord[0].y = 2;
coord[1].x = 2;
coord[1].y = 3;
coord[2].x = 2;
coord[2].y = 4;
oldCoord[0].x = 2;
oldCoord[0].y = 2;
oldCoord[1].x = 2;
oldCoord[1].y = 3;
oldCoord[2].x = 2;
oldCoord[2].y = 4;
headDirection = 5;
level = 1;
for(int x = 1; x < 12; x++)
{
for(int y = 1; y < 12; y++)
{
map[x][y] = GRASS_INDEX;
}
}
//putting snake
map[2][2] = SNAKE_INDEX;
map[2][3] = SNAKE_INDEX;
map[2][4] = SNAKE_INDEX;
putApple(hWnd);
points = 0;
level = 1;
}
void newLevel(HWND hWnd)
{
gameOver(hWnd);
points = ((levelMap * 1000) - 1000);
srand((unsigned int)time(0));
pos rn;
rn.x = rand()%13;
rn.y = rand()%13;
while(map[rn.x][rn.y] != APPLE_INDEX)
{
rn.x = rand()%13;
rn.y = rand()%13;
}
map[rn.x][rn.y] = GRASS_INDEX;
for(int i = 0; i < (3*(points/1000)); i++)
{
randomWall();
}
}
void randomWall()
{
srand((unsigned int)time(0));
pos rn;
rn.x = rand()%13;
rn.y = rand()%13;
while(map[rn.x][rn.y] != GRASS_INDEX)
{
rn.x = rand()%13;
rn.y = rand()%13;
}
map[rn.x][rn.y] = WALL_INDEX;
}