// Beginning Game Programming, Second Edition
// Chapter 10
// Paddle_Game program source code file
#include "game.h"
//background image
LPDIRECT3DSURFACE9 back;
//sprite handler
LPD3DXSPRITE sprite_handler;
//ball sprite
LPDIRECT3DTEXTURE9 ball_image;
SPRITE ball;
//paddle sprite
LPDIRECT3DTEXTURE9 paddle_image;
SPRITE paddle;
//paddle sprite
LPDIRECT3DTEXTURE9 P2_image;
SPRITE P2;
//the wave sound
CSound *sound_bounce;
CSound *sound_hit;
//misc
long start = GetTickCount();
HRESULT result;
//initializes the game
int Game_Init(HWND hwnd)
{
//set random number seed
srand(time(NULL));
//initialize mouse
if (!Init_Mouse(hwnd))
{
MessageBox(hwnd, "Error initializing the mouse", "Error", MB_OK);
return 0;
}
//initialize keyboard
if (!Init_Keyboard(hwnd))
{
MessageBox(hwnd, "Error initializing the keyboard", "Error", MB_OK);
return 0;
}
//create sprite handler object
result = D3DXCreateSprite(d3ddev, &sprite_handler);
if (result != D3D_OK)
return 0;
//load the background image
back = LoadSurface("pongboard.png", NULL);
if (back == NULL)
return 0;
//load the ball sprite
ball_image = LoadTexture("ball.bmp", D3DCOLOR_XRGB(255,0,255));
if (ball_image == NULL)
return 0;
//set the ball's properties**************************Change ball.movex and ball.movey to make move more horizontal//Tom
ball.x = 400;
ball.y = 200;
ball.width = 12;
ball.height = 12;
ball.movex = 10;
ball.movey = -10;
//load the paddle sprite
paddle_image = LoadTexture("paddle.bmp", D3DCOLOR_XRGB(255,0,255));
if (paddle_image == NULL)
return 0;
//Load second paddle//S.Zielinski
P2_image = LoadTexture("P2.bmp", D3DCOLOR_XRGB(255,0,255));
if (paddle_image == NULL)
return 0;
//set paddle properties**********************Reverse paddle.width and paddle.height to match dimensions of sprite//Tom
paddle.x = 760;
paddle.y = SCREEN_HEIGHT - 300;
paddle.width = 26;
paddle.height = 90;
//Set second paddle properties ****************************** S.Zielinski
P2.x = 10;
P2.y = SCREEN_HEIGHT - 300;
P2.width = 26;
P2.height = 90;
//load bounce wave file
sound_bounce = LoadSound("bounce.wav");
if (sound_bounce == NULL)
return 0;
//load the hit wave file
sound_hit = LoadSound("hit.wav");
if (sound_hit == NULL)
return 0;
//return okay
return 1;
}
int Collision(SPRITE sprite1, SPRITE sprite2)
{
RECT rect1;
rect1.left = sprite1.x+1;
rect1.top = sprite1.y+1;
rect1.right = sprite1.x + sprite1.width-1;
rect1.bottom = sprite1.y + sprite1.height-1;
RECT rect2;
rect2.left = sprite2.x+1;
rect2.top = sprite2.y+1;
rect2.right = sprite2.x + sprite2.width-1;
rect2.bottom = sprite2.y + sprite2.height-1;
RECT dest;
return IntersectRect(&dest, &rect1, &rect2);
}
//the main game loop
void Game_Run(HWND hwnd)
{
//ball position vector
D3DXVECTOR3 position(0,0,0);
//make sure the Direct3D device is valid
if (d3ddev == NULL)
return;
//update mouse and keyboard
Poll_Mouse();
Poll_Keyboard();
//after short delay, ready for next frame?
//this keeps the game running at a steady frame rate
if (GetTickCount() - start >= 30)
{
//reset timing
start = GetTickCount();
//move the ball sprite
ball.x += ball.movex;
ball.y += ball.movey;
//bounce the ball at screen edges
if (ball.x > SCREEN_WIDTH - ball.width)
{
ball.x -= ball.width;
ball.movex *= -1;
PlaySound(sound_bounce);
}
else if (ball.x < 0)
{
ball.x += ball.width;
ball.movex *= -1;
PlaySound(sound_bounce);
}
if (ball.y > SCREEN_HEIGHT - ball.height)
{
ball.y -= ball.height;
ball.movey *= -1;
PlaySound(sound_bounce);
}
else if (ball.y < 0)
{
ball.y += ball.height;
ball.movey *= -1;
PlaySound(sound_bounce);
}
//move the paddle*************************Changed x's to y's to recognize top and bottom of screen as limits//Rosalio
paddle.y += Mouse_X();
if (paddle.y > SCREEN_HEIGHT - paddle.height)
paddle.y = SCREEN_HEIGHT - paddle.height;
else if (paddle.y < 0)
paddle.y = 0;
//move second paddle ****************************** S.Zielinski
P2.y += Mouse_X();
if (P2.y > SCREEN_HEIGHT - P2.height)
P2.y = SCREEN_HEIGHT - P2.height;
else if (P2.y < 0)
P2.y = 0;
//check for left arrow*******************Sped up and up instead of left
// if (Key_Down(DIK_UP))
// paddle.y -= 15;
//check for right arrow******************Sped up and down instead of right
//if (Key_Down(DIK_DOWN))
// paddle.y += 15;
//check for W key for up
if (Key_Down(DIK_W))
P2.y -= 15;
//check for S key for down
if (Key_Down(DIK_S))
P2.y += 15;
//if (ball.x > 0 && ball.y > 250) ///////////////NEED WORK
// paddle.y = ball.y;
// if(ball.x > 0 && ball.y < 250)
// paddle.y = ball.y;
//if(ball.x <400)
while(paddle.y < 200 && ball.x < 400)
{
paddle.y++;
//Sleep(5000);
}
while(paddle.y > 200 && ball.x < 400)
{
paddle.y--;
//system("Sleep 5000");
}
if (ball.x > 400) // check to see if ball went pass the center line on x
{
if ( ball.y < 200) // check to see if ball is in upper quadrant
{
// assign paddle to ball
paddle.y = ball.y;
}
if ( ball.y > 200) // check to see if ball is in lower quadrant
{
paddle.y = ball.y;
}
// if (CurrentPPos <= 300)
}
//see if ball hit the paddle
if (Collision(paddle, ball)|| Collision(P2, ball))
{
//**********************************Changed y's to x's to recognize side of paddle instead of top and sides//Rosalio
ball.x -= ball.movex;
ball.movex *= -1;
PlaySound(sound_hit);
}
if (Collision(paddle, ball)|| Collision(P2, ball))
{
//**********************************Original collision for ball
ball.y -= ball.movey;
ball.movey *= -1;
PlaySound(sound_hit);
}
}
//start rendering
if (d3ddev->BeginScene())
{
//erase the entire background
d3ddev->StretchRect(back, NULL, backbuffer, NULL, D3DTEXF_NONE);
//start sprite handler
sprite_handler->Begin(D3DXSPRITE_ALPHABLEND);
//draw the ball
position.x = (float)ball.x;
position.y = (float)ball.y;
sprite_handler->Draw(
ball_image,
NULL,
NULL,
&position,
D3DCOLOR_XRGB(255,255,255));
//draw the paddle
position.x = (float)paddle.x;
position.y = (float)paddle.y;
sprite_handler->Draw(
paddle_image,
NULL,
NULL,
&position,
D3DCOLOR_XRGB(255,255,255));
//draw second paddle
position.x = (float)P2.x;
position.y = (float)P2.y;
sprite_handler->Draw(
P2_image,
NULL,
NULL,
&position,
D3DCOLOR_XRGB(255,255,255));
//stop drawing
sprite_handler->End();
//stop rendering
d3ddev->EndScene();
}
//display the back buffer on the screen
d3ddev->Present(NULL, NULL, NULL, NULL);
//check for mouse button (to exit program)
if (Mouse_Button(0))
PostMessage(hwnd, WM_DESTROY, 0, 0);
//check for escape key (to exit program)
if (Key_Down(DIK_ESCAPE))
PostMessage(hwnd, WM_DESTROY, 0, 0);
}
//frees memory and cleans up before the game ends
void Game_End(HWND hwnd)
{
if (ball_image != NULL)
ball_image->Release();
if (paddle_image != NULL)
paddle_image->Release();
if (back != NULL)
back->Release();
if (sprite_handler != NULL)
sprite_handler->Release();
//if (sound_bounce != NULL)
// delete sound_bounce;
//if (sound_hit != NULL)
// delete sound_hit;
}
*** MOD EDIT: Added code tags. Please
This post has been edited by JackOfAllTrades: 23 July 2009 - 01:04 PM

New Topic/Question
Reply




MultiQuote





|