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
|
#include "Pacman.h"
#include <sstream>
Pacman::Pacman(int argc, char* argv[]) : Game(argc, argv), _cPacmanSpeed(0.1f), _cPacmanFrameTime(250), _cMunchieFrameTime(500)
{
_frameCount = 0;
_paused = false;
_pKeyDown = false;
_rKeyDown = false;
//Initialise important Game aspects
Graphics::Initialise(argc, argv, this, 1024, 768, false, 25, 25, "Pacman", 60);
Input::Initialise();
_pacman = new Player();
_munchie = new Enemy();
_pacman->direction = 0;
_pacman->currentFrameTime = 0;
_pacman->frame = 0;
_pacman->speedMultiplier = 1.0f;
// Start the Game Loop - This calls Update and Draw in game loop
Graphics::StartGameLoop();
}
Pacman::~Pacman()
{
delete _pacman->texture;
delete _pacman->sourceRect;
delete _pacman->position;
delete _pacman;
delete _munchie->BlueTexture;
delete _munchie->InvertedTexture;
delete _munchie->Rect;
}
void Pacman::LoadContent()
{
// Load Pacman
_pacman->texture = new Texture2D(); (This is where i get the nullptr error)
_pacman->texture->Load("Textures/Pacman.tga", false);
_pacman->position = new Vector2(350.0f, 350.0f);
_pacman->sourceRect = new Rect(0.0f, 0.0f, 32, 32);
// Load Munchie
_munchie->BlueTexture = new Texture2D();
_munchie->BlueTexture->Load("Textures/Munchie.tga", true);
_munchie->InvertedTexture = new Texture2D();
_munchie->InvertedTexture->Load("Textures/MunchieInverted.tga", true);
_munchie->Rect = new Rect(100.0f, 450.0f, 12, 12);
// Set string position
_stringPosition = new Vector2(10.0f, 25.0f);
//Set Menu Paramters
_menuBackground = new Texture2D();
_menuBackground->Load("Textures/Transparency.png", false);
_menuRectangle = new Rect(0.0f, 0.0f, Graphics::GetViewportWidth(),
Graphics::GetViewportHeight());
_menuStringPosition = new Vector2(Graphics::GetViewportWidth() / 2.0f,
Graphics::GetViewportHeight() / 2.0f);
}
//Methods
void Pacman::Input(int elapsedTime, Input::KeyboardState* keyboardstate, Input::MouseState* mousestate)
{
float pacmanSpeed = _cPacmanSpeed * elapsedTime * _pacman->speedMultiplier;
if (keyboardstate->IsKeyDown(Input::Keys::LEFTSHIFT)) { _pacman->speedMultiplier = 3.0f; }
else { _pacman->speedMultiplier = 1.0f; }
//checks if pause button is pressed
if (keyboardstate->IsKeyDown(Input::Keys::P) && !_pKeyDown)
{
_pKeyDown = true;
_paused = !_paused;
}
if (keyboardstate->IsKeyUp(Input::Keys::P))
_pKeyDown = false;
if (!_paused)
{
_frameCount++;
// Checks if D key is pressed
if (keyboardstate->IsKeyDown(Input::Keys::D))
{
_pacman->position->X += pacmanSpeed; //Moves Pacman across X axis
_pacman->direction = 0;
}
// Checks if A key is pressed
if (keyboardstate->IsKeyDown(Input::Keys::A))
{
_pacman->position->X -= pacmanSpeed; //Moves Pacman across X axis
_pacman->direction = 2;
}
// Checks if W key is pressed
if (keyboardstate->IsKeyDown(Input::Keys::W))
{
_pacman->position->Y -= pacmanSpeed; //Moves Pacman across Y axis
_pacman->direction = 3;
}
// Checks if S key is pressed
if (keyboardstate->IsKeyDown(Input::Keys::S))
{
_pacman->position->Y += pacmanSpeed; //Moves Pacman across Y axis
_pacman->direction = 1;
}
//Checks if R key is pressed
if (keyboardstate->IsKeyDown(Input::Keys::R)&& !_rKeyDown)//boolean value for pressing R
{
int x = rand() % Graphics::GetViewportWidth();
int y = rand() % Graphics::GetViewportHeight();
_munchie->Rect->X = x;
_munchie->Rect->Y = y;
_rKeyDown = true;
}
if (keyboardstate->IsKeyUp(Input::Keys::R))
_rKeyDown = false;
//checks for boost
if (keyboardstate->IsKeyDown(Input::Keys::LEFTSHIFT))
{
_pacman->speedMultiplier = 2.0f;
}
else
{
_pacman->speedMultiplier = 1.0f;
}
//checks if mouse button is pressed
if (mousestate->LeftButton == Input::ButtonState::PRESSED) // This is an enum
{
_munchie->Rect->X = mousestate->X;
_munchie->Rect->Y = mousestate->Y;
}
}
}
void Pacman::CheckPaused(Input::KeyboardState* state, Input::Keys pauseKey)
{
}
void Pacman::CheckViewportCollision()
{
//checks if pacman is trying to disappear
if (_pacman->position->X - _pacman->sourceRect->Width > Graphics::GetViewportWidth()) //1024 is game width
{// pacman hit right wall - reset his position
_pacman->position->X = 0;
}
//checks if pacman is trying to disappear
if (_pacman->position->X + _pacman->sourceRect->Width < 0) //1024 is game width
{// pacman hit left wall - reset his position
_pacman->position->X = Graphics::GetViewportWidth();
}
//checks if pacman is trying to disappear
if (_pacman->position->Y - _pacman->sourceRect->Height > Graphics::GetViewportHeight()) //1024 is game width
{// pacman hit top wall - reset his position
_pacman->position->Y = 0;
}
//checks if pacman is trying to disappear
if (_pacman->position->Y + _pacman->sourceRect->Height < 0) //1024 is game width
{// pacman hit bottom wall - reset his position
_pacman->position->Y = Graphics::GetViewportHeight();
}
}
void Pacman::UpdatePacman(int elapsedTime)
{
_pacman->currentFrameTime += elapsedTime;
if (_pacman->currentFrameTime > _cPacmanFrameTime)
{
_pacman->frame++;
if (_pacman->frame >= 2)
_pacman->frame = 0;
_pacman->currentFrameTime = 0;
}
_pacman->sourceRect->Y = _pacman->sourceRect->Height * _pacman->direction;
_pacman->sourceRect->X = _pacman->sourceRect->Width * _pacman->frame;
}
void Pacman::UpdateMunchie(int elapsedTime)
{
}
void Pacman::Update(int elapsedTime)
{
float pacmanSpeed = _cPacmanSpeed * elapsedTime * _pacman->speedMultiplier;
// Gets the current state of the keyboard
Input::KeyboardState* keyboardState = Input::Keyboard::GetState();
Input::MouseState* mousestate = Input::Mouse::GetState();
Input(elapsedTime, keyboardState,mousestate);
//random code
//changing spritesheet
UpdatePacman(elapsedTime);
//checks if pacman is trying to disappear
CheckViewportCollision();
}
void Pacman::Draw(int elapsedTime)
{
// Allows us to easily create a string
std::stringstream stream;
stream << "Pacman X: " << _pacman->position->X << " Y: " << _pacman->position->Y;
SpriteBatch::BeginDraw(); // Starts Drawing
SpriteBatch::Draw(_pacman->texture, _pacman->position, _pacman->sourceRect); // Draws Pacman
if (_munchie->frameCount < 30)
{
// Draws Red Munchie
SpriteBatch::Draw(_munchie->InvertedTexture, _munchie->Rect, nullptr, Vector2::Zero, 1.0f, 0.0f, Color::White, SpriteEffect::NONE);
//moved code from here
}
else
{
// Draw Blue Munchie
SpriteBatch::Draw(_munchie->BlueTexture, _munchie->Rect, nullptr, Vector2::Zero, 1.0f, 0.0f, Color::White, SpriteEffect::NONE);
_frameCount++;
if (_frameCount >= 60)
_frameCount = 0;
}
// Draws String
SpriteBatch::DrawString(stream.str().c_str(), _stringPosition, Color::Green);
if (_paused) {
std::stringstream menuStream;
menuStream << "PAUSED!";
SpriteBatch::Draw(_menuBackground, _menuRectangle, nullptr);
SpriteBatch::DrawString(menuStream.str().c_str(), _menuStringPosition,
Color::Red);
}
SpriteBatch::EndDraw();
}
|