Important alert: (current site time 7/15/2013 5:29:08 PM EDT)
 

VB icon

[ A Cool Game ]

Email
Submitted on: 1/28/2005 9:37:53 PM
By: Brenton Andrew Saunders 
Level: Beginner
User Rating: By 2 Users
Compatibility: Microsoft Visual C++
Views: 23838
(About the author)
 
     A cool game, incomplete though. Try it and vote for it PLEASE.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: [ A Cool Game ]
// Description:A cool game, incomplete though. Try it and vote for it PLEASE.
// By: Brenton Andrew Saunders
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=8873&lngWId=3//for details.//**************************************

#include <windows.h>
#include <stdlib.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
HINSTANCE hInst;
CHAR szAppName[] = "Happy Ball";
HDChdc;
HDChdcBuffer;
HBITMAPhbmBuffer;
HPEN hpnRed;
HPEN hpnGreen;
HPEN hpnBlue;
HPEN hpnCyan;
HPEN hpnPurple;
HPEN hpnYellow;
HBRUSHhbrRed;
RECT rcClient;
intnBallX;
intnBallY;
intnInvertLineStartX;
intnInvertLineStartY;
intnInvertLineEndX;
intnInvertLineEndY;
intnAvailablePoints;
intnTotalPoints;
intnPointsX;
intnPointsY;
BOOL bInvertLineLeft;
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT Msg, WPARAM wParam,
							 LPARAM lParam)
{
	PAINTSTRUCT ps;
	CHARszBuffer[80];
	TEXTMETRIC tm;
	switch(Msg)
	{
	case WM_PAINT:
		BeginPaint(hWnd, &ps);
		FillRect(hdcBuffer, &rcClient, (HBRUSH)GetStockObject(WHITE_BRUSH));
		SelectObject(hdcBuffer, hpnGreen);
		MoveToEx(hdcBuffer, 0, ((rcClient.bottom / 2) - (5 / 2)), NULL);
		LineTo(hdcBuffer, rcClient.right, ((rcClient.bottom / 2) - (5 / 2)));
		SelectObject(hdcBuffer, hpnRed);
		MoveToEx(hdcBuffer, 0, (((rcClient.bottom / 2) - (rcClient.bottom / 4)) - (5 / 2)), NULL);
		LineTo(hdcBuffer, rcClient.right, (((rcClient.bottom / 2) - (rcClient.bottom / 4)) - (5 / 2)));
		SelectObject(hdcBuffer, hpnBlue);
		MoveToEx(hdcBuffer, 0, (((rcClient.bottom / 2) + (rcClient.bottom / 4)) - (5 / 2)), NULL);
		LineTo(hdcBuffer, rcClient.right, (((rcClient.bottom / 2) + (rcClient.bottom / 4)) - (5 / 2)));
		if(nInvertLineStartY == ((rcClient.bottom / 2) - (5 / 2)))
		{
			SelectObject(hdcBuffer, hpnPurple);
		}
		if(nInvertLineStartY == (((rcClient.bottom / 2) - (rcClient.bottom / 4)) - (5 / 2)))
		{
			SelectObject(hdcBuffer, hpnCyan);
		}
		if(nInvertLineStartY == (((rcClient.bottom / 2) + (rcClient.bottom / 4)) - (5 / 2)))
		{
			SelectObject(hdcBuffer, hpnYellow);
		}
		MoveToEx(hdcBuffer, nInvertLineStartX, nInvertLineStartY, (LPPOINT)NULL);
		LineTo(hdcBuffer, nInvertLineEndX, nInvertLineEndY);
		wsprintf(szBuffer, "%i", nAvailablePoints);
		TextOut(hdcBuffer, nPointsX, nPointsY, szBuffer, lstrlen(szBuffer));
		wsprintf(szBuffer, "Score: %i", nTotalPoints);
		TextOut(hdcBuffer, 0, 0, szBuffer, lstrlen(szBuffer));
		SelectObject(hdcBuffer, (HPEN)GetStockObject(BLACK_PEN));
		SelectObject(hdcBuffer, hbrRed);
		Ellipse(hdcBuffer, nBallX, nBallY, (nBallX + 40), (nBallY + 40));
		BitBlt(hdc, 0, 0, rcClient.right, rcClient.bottom, hdcBuffer,
			0, 0, SRCCOPY);
		EndPaint(hWnd, &ps);
		break;
	case WM_CREATE:
		srand(timeGetTime());
		GetClientRect(hWnd, &rcClient);
		
		hdc= GetDC(hWnd);
		hdcBuffer = CreateCompatibleDC(hdc);
		hbmBuffer = CreateCompatibleBitmap(hdc, rcClient.right,
			rcClient.bottom);
		hpnRed= CreatePen(PS_SOLID, 5, RGB(255, 0, 0));
		hpnGreen = CreatePen(PS_SOLID, 5, RGB(0, 255, 0));
		hpnBlue= CreatePen(PS_SOLID, 5, RGB(0, 0, 255));
		hpnCyan= CreatePen(PS_SOLID, 5, RGB(0, 255, 255));
		hpnPurple = CreatePen(PS_SOLID, 5, RGB(255, 0, 255));
		hpnYellow = CreatePen(PS_SOLID, 5, RGB(255, 255, 0));
		hbrRed= CreateSolidBrush(RGB(255, 0, 0));
		SelectObject(hdcBuffer, hbmBuffer);
		SetTextColor(hdcBuffer, RGB(128, 64, 0));
		SetBkMode(hdcBuffer, TRANSPARENT);
		FillRect(hdcBuffer, &rcClient, (HBRUSH)GetStockObject(WHITE_BRUSH));
		int nRandNum;
		nRandNum = (rand() % 2) + 1;
		if(nRandNum == 1)
		{
			nInvertLineStartX = rcClient.right;
			nInvertLineEndX= rcClient.right;
			bInvertLineLeft= TRUE;
		}
		if(nRandNum == 2)
		{
			nInvertLineStartX = 0;
			nInvertLineEndX= 0;
			bInvertLineLeft= FALSE;
		}
		nRandNum = (rand() % 3) + 1;
		if(nRandNum == 1)
		{
			nInvertLineStartY = (((rcClient.bottom / 2) - (rcClient.bottom / 4)) - (5 / 2));
			nInvertLineEndY= (((rcClient.bottom / 2) - (rcClient.bottom / 4)) - (5 / 2));
		}
		if(nRandNum == 2)
		{
			nInvertLineStartY = ((rcClient.bottom / 2) - (5 / 2));
			nInvertLineEndY= ((rcClient.bottom / 2) - (5 / 2));
		}
		if(nRandNum == 3)
		{
			nInvertLineStartY = (((rcClient.bottom / 2) + (rcClient.bottom / 4)) - (5 / 2));
			nInvertLineEndY= (((rcClient.bottom / 2) + (rcClient.bottom / 4)) - (5 / 2));
		}
		nAvailablePoints = (5 * ((rand() % 20) + 1));
		nBallX = ((rcClient.right / 2) - 20);
		nBallY = (((rcClient.bottom / 2) - (5 / 2)) - 20);
		nPointsX = (rand() % (rcClient.right - 16));
		nRandNum = (rand() % 3) + 1;
		if(nRandNum == 1)
		{
			nPointsY = ((((rcClient.bottom / 2) - (rcClient.bottom / 4)) - (5 / 2)) - 7);
		}
		if(nRandNum == 2)
		{
			nPointsY = (((rcClient.bottom / 2) - (5 / 2)) - 7);
		}
		if(nRandNum == 3)
		{
			nPointsY = ((((rcClient.bottom / 2) + (rcClient.bottom / 4)) - (5 / 2)) - 7);
		}
		SetTimer(hWnd, 1, 11, (TIMERPROC)NULL);
		SetTimer(hWnd, 2, 25, (TIMERPROC)NULL);
		SetTimer(hWnd, 3, 100, (TIMERPROC)NULL);
		break;
	case WM_TIMER:
		switch(wParam)
		{
		case 1:
			InvalidateRect(hWnd, (CONST RECT*)NULL, FALSE);
			break;
		case 2:
			if(!bInvertLineLeft)
			{
				nInvertLineEndX += 20;
				if(nInvertLineEndX >= rcClient.right)
				{
					int nRandNum;
					nRandNum = (rand() % 2) + 1;
					if(nRandNum == 1)
					{
						nInvertLineStartX = rcClient.right;
						nInvertLineEndX= rcClient.right;
						bInvertLineLeft= TRUE;
					}
					if(nRandNum == 2)
					{
						nInvertLineStartX = 0;
						nInvertLineEndX= 0;
						bInvertLineLeft= FALSE;
					}
					nRandNum = (rand() % 3) + 1;
					if(nRandNum == 1)
					{
						nInvertLineStartY = (((rcClient.bottom / 2) - (rcClient.bottom / 4)) - (5 / 2));
						nInvertLineEndY= (((rcClient.bottom / 2) - (rcClient.bottom / 4)) - (5 / 2));
					}
					if(nRandNum == 2)
					{
						nInvertLineStartY = ((rcClient.bottom / 2) - (5 / 2));
						nInvertLineEndY= ((rcClient.bottom / 2) - (5 / 2));
					}
					if(nRandNum == 3)
					{
						nInvertLineStartY = (((rcClient.bottom / 2) + (rcClient.bottom / 4)) - (5 / 2));
						nInvertLineEndY= (((rcClient.bottom / 2) + (rcClient.bottom / 4)) - (5 / 2));
					}
				}
			}
			if(bInvertLineLeft)
			{
				nInvertLineEndX -= 20;
				if(nInvertLineEndX <= 0)
				{
					int nRandNum;
					nRandNum = (rand() % 2) + 1;
					if(nRandNum == 1)
					{
						nInvertLineStartX = rcClient.right;
						nInvertLineEndX= rcClient.right;
						bInvertLineLeft= TRUE;
					}
					if(nRandNum == 2)
					{
						nInvertLineStartX = 0;
						nInvertLineEndX= 0;
						bInvertLineLeft= FALSE;
					}
					nRandNum = (rand() % 3) + 1;
					if(nRandNum == 1)
					{
						nInvertLineStartY = (((rcClient.bottom / 2) - (rcClient.bottom / 4)) - (5 / 2));
						nInvertLineEndY= (((rcClient.bottom / 2) - (rcClient.bottom / 4)) - (5 / 2));
					}
					if(nRandNum == 2)
					{
						nInvertLineStartY = ((rcClient.bottom / 2) - (5 / 2));
						nInvertLineEndY= ((rcClient.bottom / 2) - (5 / 2));
					}
					if(nRandNum == 3)
					{
						nInvertLineStartY = (((rcClient.bottom / 2) + (rcClient.bottom / 4)) - (5 / 2));
						nInvertLineEndY= (((rcClient.bottom / 2) + (rcClient.bottom / 4)) - (5 / 2));
					}
				}
			}
			break;
		case 3:
			if(nInvertLineEndX >= nBallX && nInvertLineEndX <= (nBallX + 40) && (nInvertLineEndY) == (nBallY + 20))
			{
				Sleep(2000);
				
				DestroyWindow(hWnd);
			}
			if(GetAsyncKeyState(VK_LEFT) & 0x8000)
			{
				nBallX -= 20;
			}
			if(GetAsyncKeyState(VK_RIGHT) & 0x8000)
			{
				nBallX += 20;
			}
			if(GetAsyncKeyState(VK_UP) & 0x8000)
			{
				if(nBallY != ((((rcClient.bottom / 2) - (rcClient.bottom / 4)) - (5 / 2)) - 20))
				{
					if(nBallY == (((rcClient.bottom / 2) - (5 / 2)) - 20))
					{
						 nBallY = ((((rcClient.bottom / 2) - (rcClient.bottom / 4)) - (5 / 2) - 20));
					}
					if(nBallY == ((((rcClient.bottom / 2) + (rcClient.bottom / 4)) - (5 / 2)) - 20))
					{
						 nBallY = (((rcClient.bottom / 2) - (5 / 2)) - 20);
					}
				}
			}
			if(GetAsyncKeyState(VK_DOWN) & 0x8000)
			{
				if(nBallY != ((((rcClient.bottom / 2) + (rcClient.bottom / 4)) - (5 / 2)) - 20))
				{
					if(nBallY == (((rcClient.bottom / 2) - (5 / 2)) - 20))
					{
						 nBallY = ((((rcClient.bottom / 2) + (rcClient.bottom / 4)) - (5 / 2)) - 20);
					}
					if(nBallY == ((((rcClient.bottom / 2) - (rcClient.bottom / 4)) - (5 / 2)) - 20))
					{
						 nBallY = (((rcClient.bottom / 2) - (5 / 2)) - 20);
					}
				}
			}
			if(nPointsX >= nBallX && (nPointsX + 16) <= (nBallX + 40) && (nBallY + 20) == (nPointsY + 7))
			{
				nTotalPoints += nAvailablePoints;
				nAvailablePoints = (5 * ((rand() % 20) + 1));
				nPointsX = (rand() % (rcClient.right - 16));
				nRandNum = (rand() % 3) + 1;
				if(nRandNum == 1)
				{
					nPointsY = ((((rcClient.bottom / 2) - (rcClient.bottom / 4)) - (5 / 2)) - 7);
				}
				if(nRandNum == 2)
				{
					nPointsY = (((rcClient.bottom / 2) - (5 / 2)) - 7);
				}
				if(nRandNum == 3)
				{
					nPointsY = ((((rcClient.bottom / 2) + (rcClient.bottom / 4)) - (5 / 2)) - 7);
				}
			}
			break;
		}
		break;
	case WM_CLOSE:
		DestroyWindow(hWnd);
		break;
	case WM_DESTROY:
		DeleteDC(hdcBuffer);
		DeleteObject(hbmBuffer);
		ReleaseDC(hWnd, hdc);
		DeleteObject(hpnRed);
		DeleteObject(hpnGreen);
		DeleteObject(hpnBlue);
		DeleteObject(hpnCyan);
		DeleteObject(hpnPurple);
		DeleteObject(hpnYellow);
		DeleteObject(hbrRed);
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, Msg, wParam, lParam);
	}
	return NULL;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevINstance,
				LPSTR lpCmdLine, int nShowCmd)
{
	HWND	 hWnd;
	WNDCLASS wc;
	MSG msg;
	wc.cbClsExtra	 = 0;
	wc.cbWndExtra	 = 0;
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wc.hCursor= LoadCursor((HINSTANCE)NULL, IDC_ARROW);
	wc.hIcon = LoadIcon((HINSTANCE)NULL, IDI_APPLICATION);
	wc.hInstance = hInstance;
	wc.lpfnWndProc= MainWndProc;
	wc.lpszClassName = szAppName;
	wc.lpszMenuName = (LPCTSTR)NULL;
	wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	if(!RegisterClass(&wc))
	{
		return NULL;
	}
	hInst = hInstance;
	hWnd = CreateWindow(szAppName, szAppName, WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, (HWND)NULL, (HMENU)NULL,
		hInstance, (LPVOID)NULL);
	if(!hWnd)
	{
		return NULL;
	}
	ShowWindow(hWnd, nShowCmd);
	UpdateWindow(hWnd);
	while(GetMessage(&msg, (HWND)NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return (int)msg.wParam;
}


Other 46 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

6/20/2005 10:55:33 PM

i got 33 errors in it...
(If this comment was disrespectful, please report it.)

 
11/23/2005 7:46:47 PMBryan Learn

Apparently not since it works on his computer. You just are using a different complier.

I say, good job for a beginner.
(If this comment was disrespectful, please report it.)

 
10/4/2006 10:55:19 AMTuyền

Thanks forshare
(If this comment was disrespectful, please report it.)

 
3/14/2008 8:26:33 AMGOWTHAM

DAI
(If this comment was disrespectful, please report it.)

 
4/3/2008 10:29:56 AMsanthoshkumar

very useful
(If this comment was disrespectful, please report it.)

 
10/1/2008 8:16:19 AM

wa kay claro
(If this comment was disrespectful, please report it.)

 
2/7/2009 11:29:14 PMjohn dep

compiler without a single error
runs on computer but anbother computer
says unhandled exception whats that mean?
by the way no isntructions
(If this comment was disrespectful, please report it.)

 
3/19/2009 5:43:57 AMapril

tnx for this program haha i have a pwoject nah huhu
(If this comment was disrespectful, please report it.)

 
8/9/2009 12:49:41 PMJ P

the code doesn't work
i got 26 erors
(If this comment was disrespectful, please report it.)

 
9/8/2009 5:44:20 AMMartin

Doestn work. Much errors.
(If this comment was disrespectful, please report it.)

 
11/1/2010 6:17:06 AMzaid

Thank youuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu uuuuuuuuuuuuuu
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.