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
|
// Module: RectangleClass.h
// Project: CIS023_F2016_Lab_14 Montana Burr
// Developer: Montana Burr
// Date: December 8, 2016
// Purpose: Header file for RectangleClass
#pragma once
class RectangleClass
{
public:
RectangleClass();
void Draw(HWND, HDC); // Draws rectangle on main window.
~RectangleClass();
RectangleClass *nextNode; // Next rectangle in linked list
private:
POINT pLocation; // Initial location of rectangle.
POINT pSize; // Size of rectangle.
COLORREF color; // Color of rectangle.
int direction; // Direction of animation.
int speed; // Speed of animation.
};
// Module: RectangleClass.cpp
// Project: CIS023_F2016_Lab_14 Montana Burr
// Developer: Montana Burr
// Date: December 8, 2016
// Purpose: Defines functions for RectangleClass class
#include "stdafx.h"
#include "RectangleClass.h"
RectangleClass::RectangleClass()
{
// Seed the random number generator with the current time.
srand(time(NULL));
// Initialize member variables to random values.
pLocation.x = rand() % 100 + 1;
pLocation.y = rand() % 100 + 1;
pSize.x = rand() % 100 + 1;
pSize.y = rand() % 100 + 1;
int red = rand() % 255;
int green = rand() % 255;
int blue = rand() % 255;
color = RGB(red, green, blue);
speed = rand() % 20 + 10;
direction = rand() % 7;
nextNode = nullptr;
}
void RectangleClass::Draw(HWND mainWindow, HDC hdc)
{
// Get the rectangle that contains the window.
RECT rClient;
GetWindowRect(mainWindow,&rClient);
// Animate pLocation
switch (direction)
{
case UP:
{
// Move rectangle up
pLocation.y -= speed;
if (pLocation.y <= rClient.top)
// Bounce rectangle off top border of window
direction = DOWN;
};
break;
case DOWN:
{
// Move rectangle down.
pLocation.y += speed;
if (pLocation.y + pSize.y >= rClient.bottom)
// Bounce rectangle off bottom border of window
direction = UP;
};
break;
case LEFT:
{
// Move rectangle to the left.
pLocation.x -= speed;
if (pLocation.x <= rClient.left)
// Bounce rectangle off left border of window
direction = RIGHT;
};
break;
case RIGHT:
{
// Move rectangle to the right.
pLocation.x += speed;
if (pLocation.x + pSize.x >= rClient.right)
// Bounce rectangle off right border of window
direction = LEFT;
};
break;
case UPLEFT:
{
// Move rectangle up and to the left.
pLocation.y -= speed;
pLocation.x -= speed;
if (pLocation.y <= rClient.top)
// Bounce rectangle off top border of window
direction = DOWNLEFT;
else if (pLocation.x <= rClient.left)
// Bounce rectangle off left border of window
direction = UPRIGHT;
};
break;
case UPRIGHT:
{
// Move rectangle up and to the right.
pLocation.y -= speed;
pLocation.x += speed;
if (pLocation.x >= rClient.right)
// Bounce rectangle off right border of window
direction = UPLEFT;
else if (pLocation.y <= rClient.top)
// Bounce rectangle off top border of window
direction = DOWNRIGHT;
};
break;
case DOWNLEFT:
{
// Move rectangle down and to the left.
pLocation.y += speed;
pLocation.x -= speed;
if (pLocation.y + pSize.y >= rClient.bottom)
// Bounce rectangle off bottom border of window
direction = UPLEFT;
else if (pLocation.x <= rClient.left)
// Bounce rectangle off left border of window
direction = DOWNRIGHT;
};
break;
case DOWNRIGHT:
{
// Move rectangle down and to the right.
pLocation.y += speed;
pLocation.x -= speed;
if (pLocation.y + pSize.y >= rClient.bottom)
// Bounce rectangle off bottom border of window.
direction = UPRIGHT;
if (pLocation.x + pSize.x <= rClient.right)
// Bounce rectangle off right border of window.
direction = DOWNLEFT;
};
break;
default:
break;
}
// Draw the rectangle.
RECT mainRectangle = { pLocation.x, pLocation.y, pSize.x + pLocation.x, pSize.y + pLocation.y };
HBRUSH brush = CreateSolidBrush(color); // Set fill color of rectangle.
HPEN pen = CreatePen(PS_SOLID, 1, color);
SelectObject(hdc, pen); // Select pen for drawing border.
FillRect(hdc, &mainRectangle, brush);
}
RectangleClass::~RectangleClass()
{
}
|