1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
//Everything is Initialized, Drawn, Ran and Cleared fom memory
//include the head file
#include "MyDirectX.h"
using namespace std;
//names the window
const string APPTITLE = "P0NG";
//sets the screen size
const int SCREENW = 1024;
const int SCREENH = 768;
//defining images from the SPRITE struct
SPRITE paddle1, paddle2, ball, background;
//setting up the font
LPD3DXFONT fontArial24 = NULL;
//setting up the pictures / animations
LPDIRECT3DTEXTURE9 imgBackground = NULL;
LPDIRECT3DTEXTURE9 imgBall = NULL;
LPDIRECT3DTEXTURE9 imgPaddle = NULL;
//the wave sound
CSound *sound_BallHit = NULL;
CSound *sound_Score = NULL;
CSound *sound_Win = NULL;
//initializes everything that is needed
bool Game_Init(HWND window)
{
//initialize Direct3D, and sets fullscreen or windowed mode
Direct3D_Init(window, SCREENW, SCREENH, false);
//initializes input devices
DirectInput_Init(window);
//initialize DirectSound
DirectSound_Init(window);
//creating the font
fontArial24 = MakeFont("Arial",24);
//load the sprite textures
imgPaddle = LoadTexture("paddle.png");
if (!imgPaddle) return false;
imgBall = LoadTexture("asteroid.tga");
if (!imgBall) return false;
imgBackground = LoadTexture("background.png");
if (!imgBackground) return false;
//set properties for the paddle1
paddle1.x = 50;
paddle1.y = 250;
paddle1.width = 42;
paddle1.height = 187;
paddle1.columns = 1;
paddle1.startframe = 0;
paddle1.endframe = 1;
paddle1.velx = 0.5f;
paddle1.vely = 0.5f;
//set properties for the paddle2
paddle2.x = SCREENW-100;
paddle2.y = 250;
paddle2.width = 42;
paddle2.height = 187;
paddle2.columns = 1;
paddle2.startframe = 0;
paddle2.endframe = 1;
paddle2.velx = 0.5f;
paddle2.vely = 0.5f;
//setting properties for asteroid 1
ball.x = 550;
ball.y = 400;
ball.width = ball.height = 60;
ball.columns = 8;
ball.startframe = 0;
ball.endframe = 63;
ball.velx = -1.0f;
ball.vely = -1.0f;
background.x = 0;
background.y = 0;
background.width = 1024;
background.height = 768;
//load BallHit wave file
sound_BallHit = LoadSound("BallHit.wav");
//load Score wave file
sound_Score = LoadSound("Score.wav");
//load YouWin Sound
sound_Win = LoadSound("YouWin.mid");
return true;
}
void Game_Run(HWND window)
{
//initializing variables for the hit counter, and the text's X location
//converting an INT to a STRING
static int hitCount=0;
string hits;
stringstream out;
out << hitCount;
hits=out.str();
//checking if the d3d device is running properly
if (!d3ddev) return;
//checking for keypresses/mouseclicks
DirectInput_Update();
//Sets the background color
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,255,255), 1.0f, 0);
//move the paddle2 up/down with W and S keys
if (Key_Down(DIK_UP))
{
paddle2.y -= 1.0f;
if (paddle2.y < 0) paddle2.y = 0;
}
//moves the paddle1 down if the key is pressed
if (Key_Down(DIK_DOWN))
{
paddle2.y += 1.0f;
if (paddle2.y > SCREENH - paddle2.height)
paddle2.y = SCREENH - paddle2.height;
}
//move the paddle1 up/down with arrow keys
if (KEY_DOWN('W'))
{
paddle1.y -= 1.0f;
if (paddle1.y < 0) paddle1.y = 0;
}
//moves the paddle1 down if the key is pressed
if (KEY_DOWN('S'))
{
paddle1.y += 1.0f;
if (paddle1.y > SCREENH - paddle1.height)
paddle1.y = SCREENH - paddle1.height;
}
if (Collision(paddle1, ball))
{
ball.velx *= -1;
ball.x += 2;
if (Collision(paddle1, ball))
{
ball.vely *= -1;
if (ball.y+ball.width/2 < paddle1.y+paddle1.width)
{
ball.y -= 4;
}
else
{
ball.y += 4;
}
}
hitCount++;
PlaySound(sound_BallHit);
}
//test for collisions on paddle1 + asteroid 1 and increases the hitcounter when it does
if (Collision(paddle2, ball))
{
ball.velx *= -1;
ball.x -= 2;
if (Collision(paddle2, ball))
{
ball.vely *= -1;
if (ball.y+ball.width/2 < paddle2.y+paddle2.width)
{
ball.y -= 4;
}
else
{
ball.y += 4;
}
}
hitCount++;
PlaySound(sound_BallHit);
}
//sets the direction and horizontal + vertical speeds and changes their direction when it goes off the screen
ball.x += ball.velx;
ball.y += ball.vely;
if (ball.x < 0)
ball.velx = 1.0f;
if (ball.x > SCREENW-ball.width)
ball.velx = -1.0f;
if (ball.y < 0)
{
ball.y = 1;
ball.vely = 1.0f;
}
if (ball.y > SCREENH-ball.height)
{
ball.y = SCREENH-ball.height;
ball.vely = -1.0f;
}
//sets animation properties
Sprite_Animate(ball.frame, ball.startframe, ball.endframe, ball.direction, ball.starttime, ball.delay);
//test for collisions on paddle1 + ball and increases the hitcounter when it does
//starts drawing all animations/pics and text
if (d3ddev->BeginScene())
{
//*** insert sprite code here ***
spriteobj->Begin(D3DXSPRITE_ALPHABLEND);
Sprite_Draw_Frame(imgBackground, background.x, background.y, background.frame, background.width, background.height, background.columns);
//Draws the sprites to the screen
Sprite_Draw_Frame(imgPaddle, paddle1.x, paddle1.y, paddle1.frame, paddle1.width, paddle1.height, paddle1.columns);
//Draws the sprites to the screen
Sprite_Draw_Frame(imgPaddle, paddle2.x, paddle2.y, paddle2.frame, paddle2.width, paddle2.height, paddle2.columns);
//Draws the sprites to the screen
Sprite_Draw_Frame(imgBall, ball.x, ball.y, ball.frame, ball.width, ball.height, ball.columns);
//draws the test for the hit counter
FontPrint(fontArial24, 550, 0, hits,D3DCOLOR_XRGB(0,0,255));
FontPrint(fontArial24, 425, 0, "Hit Counter =" ,D3DCOLOR_XRGB(0,0,255));
//stop drawing
spriteobj->End();
//stop rendering
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}
//detects keypresses and ends the game if detected
if (KEY_DOWN(VK_ESCAPE) || KEY_DOWN('Q')) gameover = true;
//controller Back button also ends
if (controllers[0].wButtons & XINPUT_GAMEPAD_BACK)
gameover = true;
}
//shuts everything down
void Game_End()
{
//free memory and shut down
if (imgPaddle) imgPaddle->Release();
if (imgBall) imgBall->Release();
if (imgBackground) imgBackground->Release();
if (fontArial24) fontArial24->Release();
//Stop Sounds
if (sound_BallHit) delete sound_BallHit;
if (sound_Score) delete sound_Score;
//function that shuts down the sound
DirectSound_Shutdown();
//function that shuts down the input devices
DirectInput_Shutdown();
//function that shuts down all drawn sprites etc.
Direct3D_Shutdown();
}
|