Code:
void NewGame()
{
GAMESTARTED = false;
for(int xmi = 0; xmi < SCREEN_WIDTH; ++ xmi)
for(int ymi = 0; ymi < 60; ++ymi)
{
if(ymi < 10)
MAP[xmi][ymi] = REDBAR;
else
MAP[xmi][ymi] = BLACKBAR;
DrawBars(xmi,ymi,MAP[xmi][ymi]);
}
void DrawPaddle(int _X, int _Y)
{ //checks to see if there was any previous ball movement
if(Paddle_Move == true)
BitBlt(MapImage,PrePaddle_X,PrePaddle_Y,8,1,Paddle,0,1,SRCCOPY);
//updates iamge
BitBlt(MapImage,_X,_Y,8,1,Paddle,0,0,SRCCOPY);
};
the whole main code is this
Code:
#include "ball.h"
#include "ImageClass.h"
#include<windows.h>
//size of bars
#define BAR_SIZE 8
#define SCREEN_WIDTH 20
#define SCREEN_HEIGHT 30
//color bars
#define REDBAR 1
//to erase bars
#define BLACKBAR 0
//map array fir bars to be place in
int MAP[SCREEN_WIDTH ][SCREEN_HEIGHT];
// value ot use when to check the game is finish
bool GAMESTARTED;
// check if paddle is moving
bool Paddle_Move = false;
//gets time in milli seconds
// Started time
DWORD timerstart;
// difference of start and current time
DWORD timercurrent;
//images for each visible component
//ball
ImageClass BallImage;
//map
ImageClass MapImage;
//bars
ImageClass BarImage;
//paddle
ImageClass Paddle;
//global handle
HWND ghWnd;
// a class to calculate ball's movements
BallClass ball;
//current ball's coordiantes
int BALL_X;
int BALL_Y;
//preious coordinates
int PREBALL_X;
int PREBALL_Y;
//current paddle coordinates
int Paddle_X =0;
int Paddle_Y =0;
// previous paddle coordinates
int PrePaddle_X;
int PrePaddle_Y;
//sets the new game
void NewGame();
// draws bar image
void DrawBars(int x, int y, int title);
// checks for bar and ball collision (still working on it )
void collision();
// draw paddle image
void DrawPaddle( int _X, int _Y);
// init paddle
void PaddleInit();
// game loop
void UpDateGame();
// game init
void GameInit();
LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{// gets speed desired by the user
int N = 0;
switch( uMsg)
{
case WM_KEYDOWN:
{
switch(wParam)
{
case VK_RETURN:
GAMESTARTED = true;
break;
// moves paddle right
case VK_RIGHT:
Paddle_X -= 8;
Paddle_Move = true;
break;
//moves paddle left
case VK_LEFT:
Paddle_X += 8;
Paddle_Move = true;
break;
//increases ball speed
case VK_UP:
N += 1;
ball.Speed(N);
break;
//decreases ball speed
case VK_DOWN:
N -= 1;
break;
}
}
//copys entire buffer onto the main client's
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(ghWnd,&ps);
BitBlt(hdc,0,0,SCREEN_WIDTH * BAR_SIZE,SCREEN_HEIGHT * BAR_SIZE,MapImage,0,0,SRCCOPY);
EndPaint(ghWnd,&ps);
return (0);
}
break;
case WM_DESTROY:
{
PostQuitMessage(0);
return(0);
}
break;
default :
return (DefWindowProc(hwnd,uMsg,wParam,lParam));
}
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
timerstart = GetTickCount();
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_VREDRAW|CS_HREDRAW;
wc.lpfnWndProc = (WNDPROC)WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon =LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground =(HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "Blizt game";
wc.hIconSm = NULL;
RegisterClassEx(&wc);
ghWnd = CreateWindowEx(WS_EX_WINDOWEDGE,"Blizt game","Demo game",WS_SYSMENU|WS_CAPTION |WS_BORDER,0,0,160,240,NULL,NULL,hInstance,NULL);
GameInit();
MSG msg;
for( ; ; )
{// updates every second
timercurrent = GetTickCount() - timerstart;
if(timercurrent > 1000.0)
{ UpDateGame();
timerstart = timercurrent;
} //checks for quit
if(PeekMessage(&msg,ghWnd,0,0,PM_REMOVE))
{ if(msg.message == WM_QUIT) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
void DrawBars(int x, int y, int title)
{
BitBlt(MapImage,x* BAR_SIZE,y* BAR_SIZE,BAR_SIZE,BAR_SIZE,BarImage,title * BAR_SIZE, title * BAR_SIZE,SRCAND );
BitBlt(MapImage,x* BAR_SIZE,y* BAR_SIZE,BAR_SIZE,BAR_SIZE,BarImage,title * BAR_SIZE, title * BAR_SIZE + BAR_SIZE,SRCPAINT );
}
void PaddleInit()
{
Paddle_X = 8;
Paddle_Y = 208;
DrawPaddle( Paddle_X, Paddle_Y);
}
void DrawPaddle(int _X, int _Y)
{ //checks to see if there was any previous ball movement
if(Paddle_Move == true)
BitBlt(MapImage,PrePaddle_X,PrePaddle_Y,8,1,Paddle,0,1,SRCCOPY);
//updates iamge
BitBlt(MapImage,_X,_Y,8,1,Paddle,0,0,SRCCOPY);
};
//erases previous image then makes new iamge movement for ball(also paddle)
void DrawBall()
{
if(GAMESTARTED == true)
BitBlt(BallImage,PREBALL_X,PREBALL_Y,8,8,BallImage,2 * 8, 0, SRCCOPY);
BitBlt(BallImage,BALL_X,BALL_Y,8,8,BallImage,0,0,SRCAND);
BitBlt(BallImage,BALL_X,BALL_Y,8,8,BallImage,2 * 8, 2*8, SRCPAINT);
}
void collision()
{
if(Paddle_X > SCREEN_WIDTH)
Paddle_X -= 1;
if(Paddle_X < 0)
Paddle_X += 1;
if(ball.GetX_val() > SCREEN_WIDTH)
ball. Newdirection( 8,0);
if(ball.GetY_val() < 0 )
ball.Newdirection(0,-8);
if(ball.GetY_val() == 206)
/* end of round */
if (ball.GetY_val()== Paddle_X - 1)
ball.Newdirection(8,0);
}
void UpDateGame()
{
if( GAMESTARTED == true)
{
collision();
PREBALL_X = BALL_X;
PREBALL_Y = BALL_Y;
BALL_X = ball.GetX_val();
BALL_Y = ball.GetY_val();
ball.Move();
DrawPaddle(Paddle_X, Paddle_Y);
}
InvalidateRect(ghWnd,NULL,false);
}
void NewGame()
{
GAMESTARTED = false;
for(int xmi = 0; xmi < SCREEN_WIDTH; ++ xmi)
for(int ymi = 0; ymi < 60; ++ymi)
{
if(ymi < 10)
MAP[xmi][ymi] = REDBAR;
else
MAP[xmi][ymi] = BLACKBAR;
DrawBars(xmi,ymi,MAP[xmi][ymi]);
}
ball.Startpos( SCREEN_WIDTH / 2, 120 );
PaddleInit();
UpDateGame();
}
void GameInit()
{
RECT rtemp;
HDC hdcc =GetDC(ghWnd);
SetRect(&rtemp,0,0,SCREEN_WIDTH * BAR_SIZE,SCREEN_HEIGHT * BAR_SIZE);
AdjustWindowRect(&rtemp,WS_CAPTION|WS_VISIBLE|WS_SYSMENU,false);
SetWindowPos(ghWnd,NULL,0,0,SCREEN_WIDTH * BAR_SIZE,SCREEN_HEIGHT * BAR_SIZE,SWP_NOMOVE);
MapImage.CreateImage(hdcc,"gamescreen.bmp");
BarImage.CreateImage(NULL,"Bar.bmp");
BallImage.CreateImage(NULL,"ball.bmp");
Paddle.CreateImage(NULL,"Paddle.bmp");
ReleaseDC(ghWnd,hdcc);
NewGame();
}