| CODE |
// hittheball // made by Xception #include <windows.h> #include <stdio.h> #include <stdlib.h> // Global Variables HBRUSH red_brush=CreateSolidBrush(0x2020f0); HDC game,backbuffer; HBITMAP back_bmp = NULL; HBITMAP old_bmp = NULL; RECT rt; int iScore,iTime; bool game_over=false; class Ball { private: int x; // horizontal position int y; // vertical position int hspeed; // horizontal speed int vspeed; // vertical speed public: Ball(void); void draw(HDC hdc); void move(void); bool Collision(int mx, int my); void SetRandomPos(void); }; void Ball::SetRandomPos(void) { x=rand()%340;y=rand()%240; } Ball::Ball(void) { SetRandomPos(); hspeed=3; vspeed=3; } void Ball::draw(HDC hdc) { SelectObject(hdc,red_brush); Ellipse(hdc,x,y,x+25,y+25); } bool Ball::Collision(int mx, int my) { if ((abs((mx)-(x+12))<(12))&&(abs((my)-(y+12))<(12))) return true; return false; } void Ball::move(void) { x+=hspeed; y+=vspeed; if ((x>rt.right-25) || (x<0)) hspeed*=-1; if ((y>rt.bottom-25) || (y<0)) vspeed*=-1; } Ball* ball1; Ball* ball2; LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM ); int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { HWND hwnd; MSG msg; WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_WINLOGO); wcex.hCursor = LoadCursor(NULL, IDC_CROSS); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL; wcex.lpszClassName = "MainWnd"; wcex.hIconSm = LoadIcon(hInstance, (LPCTSTR)IDI_WINLOGO); // Register the window class, and if it fails quit the program if (!RegisterClassEx (&wcex)) return 0; // Create the window hwnd = CreateWindowEx ( 0, // no extended window style "MainWnd", // Window Classname "Hit The Ball", // Window Title Text WS_SYSMENU, // Window style: Systemmenu (int)GetSystemMetrics(SM_CXFULLSCREEN)/2-200, // center window horizontally (int)GetSystemMetrics(SM_CYFULLSCREEN)/2-150, // and vertically 400, // Window width 300, // Window height NULL, // Window has no parent window NULL, // no menu hInstance, // Window Instance handle NULL); // No Window Creation data ShowWindow (hwnd, nCmdShow); // Show the window game=GetDC(hwnd); backbuffer=CreateCompatibleDC(game); // Create backbuffer back_bmp = CreateCompatibleBitmap(game, 400, 300); // for doublebuffering old_bmp = (HBITMAP)SelectObject(backbuffer, back_bmp); // select backbuffer SetTimer(hwnd,1,10,NULL); // timer for balls SetTimer(hwnd,2,1000,NULL); // timer for time // messageloop while( GetMessage(&msg, NULL, 0, 0) ) { { TranslateMessage( &msg ); DispatchMessage( &msg ); } } return msg.wParam; } // Window Procedure LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; char tempstr[12]; switch( message ) { case WM_CREATE: srand(GetTickCount()); // seed the random numbers iTime=30; iScore=0; ball1=new Ball; ball2=new Ball; // create balls break; case WM_TIMER: switch(wParam) { case 1: // move balls and draw stuff GetClientRect( hwnd, &rt ); FillRect(backbuffer,&rt,(HBRUSH) GetStockObject(WHITE_BRUSH)); sprintf(tempstr,"SCORE: %d", iScore); TextOut(backbuffer,20,0,tempstr,strlen(tempstr)); sprintf(tempstr,"Time: %d", iTime); TextOut(backbuffer,320,0,tempstr,strlen(tempstr)); if (game_over) { TextOut(backbuffer,160,100,"Game Over",9); TextOut(backbuffer,110,120,"Press <SPACE> to restart!",25);} else { ball1->move(); ball2->move();} ball1->draw(backbuffer); ball2->draw(backbuffer); BitBlt(game, 0, 0, 400, 300, backbuffer, 0, 0, SRCCOPY); break; case 2: // decrease time if (iTime>0) iTime--; else game_over=true; break; } case WM_KEYDOWN: if (wParam==VK_ESCAPE) DestroyWindow(hwnd); // Escape=Close Game if ((game_over) && (wParam==VK_SPACE)) { // Space=Restart Game iTime=30;iScore=0; game_over=false;} break; case WM_LBUTTONDOWN: if (!game_over) { // Check if mouse hits balls if (ball1->Collision(LOWORD(lParam),HIWORD(lParam))) { iScore++; Beep(500,20); ball1->SetRandomPos(); } if (ball2->Collision(LOWORD(lParam),HIWORD(lParam))) { iScore++; Beep(400,20); ball2->SetRandomPos(); }} break; case WM_PAINT: hdc = BeginPaint (hwnd, &ps); EndPaint( hwnd, &ps ); break; case WM_DESTROY: // cleanup! delete ball1, ball2; KillTimer(hwnd,1); KillTimer(hwnd,2); SelectObject(backbuffer,old_bmp); DeleteObject(back_bmp); DeleteDC(backbuffer); DeleteObject(red_brush); PostQuitMessage( 0 ); break; default: return DefWindowProc( hwnd, message, wParam, lParam ); } return 0; } |
| QUOTE |
Does it mean that if I don't use srand(GetTickCount()); I would get the same numbers every time the application starts? |
| CODE |
| case WM_PAINT: hdc = BeginPaint (hwnd, &ps); EndPaint( hwnd, &ps ); break; |
| QUOTE | ||
Xception I don't understand one thing of what use is this code:
|
| QUOTE (Gamer09 @ Jul 14 2003, 10:07 PM) | ||||
I think it is supposed to draw everything. |
| QUOTE |
| LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main Debug/Hit_the_Ball.exe : fatal error LNK1120: 1 unresolved externals |