Code: Select all
if (left_mobile < right_fixed &&
right_mobile > left_fixed &&
top_mobile < bottom_fixed &&
bottom_mobile > top_fixed)
{
colliding = true;
}
else
{
colliding = false;
}
Code: Select all
if (left_mobile < right_fixed &&
right_mobile > left_fixed &&
top_mobile < bottom_fixed &&
bottom_mobile > top_fixed)
{
colliding = true;
}
else
{
colliding = false;
}
Here is the entire code. If this is not sufficient let me know... Thanks Chili!
Code: Select all
#include "MainWindow.h"
#include "Game.h"
Game::Game( MainWindow& wnd )
:
wnd( wnd ),
gfx( wnd )
{
}
void Game::Go()
{
gfx.BeginFrame();
UpdateModel();
ComposeFrame();
gfx.EndFrame();
}
void Game::UpdateModel()
{
if (wnd.kbd.KeyIsPressed(VK_UP)) //key is pressed
{
if (inhibitUp) //checks inhibition
{
}
else //finds no inhibition
{
vy = vy - 1;
inhibitUp = true; //maintains velocity while key is pressed
}
}
else
{
inhibitUp = false; //once key is released
}
if (wnd.kbd.KeyIsPressed(VK_DOWN))
{
if (inhibitDown)
{
}
else
{
vy = vy + 1;
inhibitDown = true;
}
}
else
{
inhibitDown = false;
}
if (wnd.kbd.KeyIsPressed(VK_LEFT))
{
if (inhibitLeft)
{
}
else
{
vx = vx - 1;
inhibitLeft = true;
}
}
else
{
inhibitLeft = false;
}
if (wnd.kbd.KeyIsPressed(VK_RIGHT))
{
if (inhibitRight)
{
}
else
{
vx = vx + 1;
inhibitRight = true;
}
}
else
{
inhibitRight = false;
}
if (wnd.kbd.KeyIsPressed(VK_UP))
{
reticle_y = reticle_y - 1;
}
if (wnd.kbd.KeyIsPressed(VK_DOWN))
{
reticle_y = reticle_y + 1;
}
if (wnd.kbd.KeyIsPressed(VK_LEFT))
{
reticle_x = reticle_x - 1;
}
if (wnd.kbd.KeyIsPressed(VK_RIGHT))
{
reticle_x = reticle_x + 1;
}
reticle_x = reticle_x + vx;
reticle_y = reticle_y + vy;
if (reticle_x + 6 >= gfx.ScreenWidth)
{
reticle_x = gfx.ScreenWidth - 7;
vx = 0;
}
if (reticle_x - 6 < 0)
{
reticle_x = 6;
vx = 0;
}
if (reticle_y + 6 >= gfx.ScreenHeight)
{
reticle_y = gfx.ScreenHeight - 7;
vy = 0;
}
if(reticle_y - 6 < 0)
{
reticle_y = 6;
vy = 0;
}
const int left_mobile = reticle_x - 5;
const int right_mobile = reticle_x + 5;
const int top_mobile = reticle_y - 5;
const int bottom_mobile = reticle_y + 5;
const int left_fixed = x_boxFixed - 5;
const int right_fixed = x_boxFixed + 5;
const int top_fixed = y_boxFixed - 5;
const int bottom_fixed = y_boxFixed + 5;
if (left_mobile < right_fixed &&
right_mobile > left_fixed &&
top_mobile < bottom_fixed &&
bottom_mobile > top_fixed)
{
colliding = true;
}
else
{
colliding = false;
}
shapeIsChanged = wnd.kbd.KeyIsPressed(VK_SHIFT);
}
void Game::ComposeFrame()
{
if (colliding)
{
reticle(reticle_x, reticle_y, 255, 0, 0);
}
else
{
reticle(reticle_x, reticle_y, 255, 255, 255);
}
}
void Game::DrawBox(int x_boxFixed, int y_boxFixed, int r, int g, int b)
{
gfx.PutPixel(x_boxFixed + 50, y_boxFixed - 50, r, g, b);
gfx.PutPixel(x_boxFixed + 50, y_boxFixed + 50, r, g, b);
gfx.PutPixel(x_boxFixed - 50, y_boxFixed + 50, r, g, b);
gfx.PutPixel(x_boxFixed - 50, y_boxFixed - 50, r, g, b);
gfx.PutPixel(x_boxFixed + 50, y_boxFixed, r, g, b);
gfx.PutPixel(x_boxFixed - 50, y_boxFixed, r, g, b);
gfx.PutPixel(x_boxFixed, y_boxFixed + 50, r, g, b);
gfx.PutPixel(x_boxFixed, y_boxFixed - 50, r, g, b);
}
void Game::reticle(int reticle_x, int reticle_y, int r_mob, int g_mob, int b_mob)
{
gfx.PutPixel(reticle_x + 6, reticle_y, r_mob, g_mob, b_mob);
gfx.PutPixel(reticle_x, reticle_y - 6, r_mob, g_mob, b_mob);
gfx.PutPixel(reticle_x - 6, reticle_y, r_mob, g_mob, b_mob);
gfx.PutPixel(reticle_x - 6, reticle_y + 6, r_mob, g_mob, b_mob);
gfx.PutPixel(reticle_x - 6, reticle_y - 6, r_mob, g_mob, b_mob);
gfx.PutPixel(reticle_x + 6, reticle_y - 6, r_mob, g_mob, b_mob);
gfx.PutPixel(reticle_x, reticle_y + 6, r_mob, g_mob, b_mob);
gfx.PutPixel(reticle_x + 6, reticle_y + 6, r_mob, g_mob, b_mob);
gfx.PutPixel(reticle_x, reticle_y, r_mob, g_mob, b_mob);
}
Code: Select all
void Game::ComposeFrame()
{
DrawBox(200, 250, 200, 30, 60);
if (colliding)
{
reticle(reticle_x, reticle_y, 255, 0, 0);
}
else
{
reticle(reticle_x, reticle_y, 255, 255, 255);
}
}
Code: Select all
void Game::DrawBox(int x_boxFixed, int y_boxFixed, int r, int g, int b)
{
gfx.PutPixel(x_boxFixed + 50, y_boxFixed - 50, r, g, b);
gfx.PutPixel(x_boxFixed + 50, y_boxFixed + 50, r, g, b);
gfx.PutPixel(x_boxFixed - 50, y_boxFixed + 50, r, g, b);
gfx.PutPixel(x_boxFixed - 50, y_boxFixed - 50, r, g, b);
gfx.PutPixel(x_boxFixed + 50, y_boxFixed, r, g, b);
gfx.PutPixel(x_boxFixed - 50, y_boxFixed, r, g, b);
gfx.PutPixel(x_boxFixed, y_boxFixed + 50, r, g, b);
gfx.PutPixel(x_boxFixed, y_boxFixed - 50, r, g, b);
}