So this is something i was working on the other day. It needs a lot of work, so I was wondering if you guys had any comments or suggestions. The more the merrier.
Thanks,
Stu
Shape.h
#include <iostream>
#include <string>
#include <windows.h>
#include "Color.h"
using namespace std;
string ScreenBuffer;
class Shape
{
private:
int width;
int height;
bool intensity;
usint color;
public:
COORD POS;
int jump;
bool jumping;
/* Constructor */
Shape() {};
Shape(int h, int w, bool i, usint c, int x, int y)
{
height = h;
width = w;
intensity = i;
color = c;
POS.X = x;
POS.Y = y;
jump = 0;
jumping = false;
}
int getHeight() { return height; }
int getWidth() { return width; }
/* This Function draws squares and rectangles only */
void Draw()
{
//For place cursor position
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// Set Shape Color
cout << col(color, intensity);
// To add the x/y for loop and the coord position
COORD TEMP;
TEMP = POS;
for (int y = 0; y < height; y++)
{
TEMP.Y = POS.Y + y;
SetConsoleCursorPosition(hConsole, TEMP);
string line(width, ' ');
cout << line;
cout << endl;
}
//Reset Color
cout << col(RED|BG_WHITE,false);
}
};
Color.h
#include <iostream>
#include <windows.h>
using namespace std;
#define RED 0x0004
#define GREEN 0x0002
#define BLUE 0x0001
#define WHITE RED|GREEN|BLUE
#define YELLOW RED|GREEN
#define PINK RED|BLUE
#define TURQUOISE BLUE|GREEN
#define BG_RED 0x0040
#define BG_GREEN 0x0020
#define BG_BLUE 0x0010
#define BG_WHITE BG_RED|BG_GREEN|BG_BLUE
#define BG_YELLOW BG_RED|BG_GREEN
#define BG_PIN KBG_RED|BG_BLUE
#define BG_TURQUOISE BG_BLUE|BG_GREEN
typedef unsigned short int usint;
//---------------------------------------------------------------------------------
class ostreamHelper
{
private:
usint n_;
bool inten_;
ostream& (*f_)(ostream&,usint,bool);
public:
ostreamHelper(ostream&(*f)(ostream&,usint, bool), usint n, bool inten):f_(f),n_(n),inten_(inten) { }
friend ostream& operator<<(ostream& os,ostreamHelper helper)
{
return helper.f_(os,helper.n_,helper.inten_); }
};
//---------------------------------------------------------------------------------
ostream& ColHelper(ostream& os, usint col, bool inten)
{
HANDLE *hOut=new HANDLE;
*hOut=GetStdHandle(STD_OUTPUT_HANDLE);
if(inten==true)
col+=0x0008;
SetConsoleTextAttribute(*hOut,col);
delete hOut;
return os;
}
//--------------------------------------------------------------------------------
ostreamHelper col(usint col, bool intensiv)
{
return ostreamHelper(&ColHelper,col,intensiv);
}
//---------------------------------------------------------------------------------
//EXAMPLES HOW TO USE
//int main()
//{
/*
cout << col(RED,true) << "hi, I'm an intensiv Red :D\n";
cout << "I'm still red, couz u dont change it!\n";
cout << col(BLACK|BG_WHITE,true) << "hi, I'm yellow and my BG is greeen :D\n";
cout << col(YELLOW,false) << "now, my color is not intensiv :(\n";
cout << col(WHITE,false) << "now I have the standard color. :> great.\n";
system("pause");
return 0;
*/
//}
main.cpp
/**************************
* Draw Basic Shapes by *
* Stuart Larsen *
**************************/
#include <iostream>
#include <string>
#include <stdio.h>
#include "Shape.h"
using namespace std;
//Declarations
void ClearScreen();
int PlaceCursor(const int x, const int y);
void DrawScene();
//Objects
Shape box(2, 2, true, BG_RED, 2, 18);
Shape floor(5,80,false,BG_BLUE,0,20);
Shape sun(5, 8, false,BG_YELLOW,0,0);
Shape block(3, 20, false, BG_BLUE, 20, 12);
//To keep track of Objects in Object Array
enum Objects { PLAYER, FLOOR, SUN, BLOCK };
//All the shapes. In object array so we can hit detect.
Shape shapes[10];
int main()
{
//Assign object array values
shapes[PLAYER] = box;
shapes[FLOOR] = floor;
shapes[SUN] = sun;
shapes[BLOCK] = block;
//Console Settings
SetConsoleTitle("Draw Shapes");
cout << col(RED|BG_WHITE,false);
ClearScreen();
//Draw Scene
DrawScene();
//cin.get();
bool change = false;
while (true) {
//Fall down due to gravity
if (shapes[PLAYER].POS.Y < 15) {
shapes[PLAYER].POS.Y++;
change = true;
}
//User Inputs
if (GetAsyncKeyState (VK_LEFT)) {
shapes[PLAYER].POS.X--;
change = true;
DrawScene();
Sleep(100);
}
if (GetAsyncKeyState (VK_RIGHT)) {
shapes[PLAYER].POS.X++;
change = true;
DrawScene();
Sleep(100);
}
if (GetAsyncKeyState (VK_UP)) {
box.POS.Y--;
change = true;
DrawScene();
Sleep(100);
}
if (GetAsyncKeyState (VK_DOWN)) {
shapes[PLAYER].POS.Y++;
change = true;
DrawScene();
Sleep(100);
}
if (GetAsyncKeyState (VK_SPACE)) {
change = true;
shapes[PLAYER].jumping = true;
shapes[PLAYER].POS.Y -= 2;
DrawScene();
Sleep(100);
}
if (!GetAsyncKeyState (VK_SPACE)) {
shapes[PLAYER].jumping = false;
}
//Hit Detect
if (shapes[PLAYER].POS.Y + shapes[PLAYER].getHeight() - 1 == shapes[FLOOR].POS.Y && shapes[PLAYER].POS.X + shapes[PLAYER].getWidth() > shapes[FLOOR].POS.X && shapes[PLAYER].POS.X < shapes[FLOOR].POS.X + shapes[FLOOR].getWidth()) {
shapes[PLAYER].POS.Y = shapes[FLOOR].POS.Y - shapes[PLAYER].getHeight();
DrawScene();
Sleep(100);
}
if (shapes[PLAYER].POS.Y + shapes[PLAYER].getHeight() - 1 == shapes[BLOCK].POS.Y && shapes[PLAYER].POS.X + shapes[PLAYER].getWidth() > shapes[BLOCK].POS.X && shapes[PLAYER].POS.X < shapes[BLOCK].POS.X + shapes[BLOCK].getWidth()) {
shapes[PLAYER].POS.Y = shapes[BLOCK].POS.Y - shapes[PLAYER].getHeight();
DrawScene();
Sleep(100);
}
//Sleep(50);
}
}
/*****************************************************
* Right now it draws all the objects, but eventually *
* it will draw the screen buffer as a string. *
*****************************************************/
void DrawScene()
{
//Clear the Screen
ClearScreen();
//Draw Boxs
shapes[FLOOR].Draw();
shapes[PLAYER].Draw();
shapes[SUN].Draw();
shapes[BLOCK].Draw();
//Stats
PlaceCursor(1, 24);
cout << shapes[PLAYER].POS.X << ", " << shapes[PLAYER].POS.Y << " " << shapes[PLAYER].jumping;// << " Lives: (3 little boxs) " << endl;
}
//////////////////////////////////////////////////////
// Game Engine Stuff
//////////////////////////////////////////////////////
/* Clears the Screen in an more efficent manner than system("cls"); */
void ClearScreen ( void )
{
DWORD n; /* Number of characters written */
DWORD size; /* number of visible characters */
COORD coord = {0}; /* Top left screen position */
CONSOLE_SCREEN_BUFFER_INFO csbi;
/* Get a handle to the console */
HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
GetConsoleScreenBufferInfo ( h, &csbi );
/* Find the number of characters to overwrite */
size = csbi.dwSize.X * csbi.dwSize.Y;
/* Overwrite the screen buffer with whitespace */
FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
GetConsoleScreenBufferInfo ( h, &csbi );
FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );
/* Reset the cursor to the top left position */
SetConsoleCursorPosition ( h, coord );
}
int PlaceCursor(const int x, const int y) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD PlaceCursorHere;
PlaceCursorHere.X = x;
PlaceCursorHere.Y = y;
SetConsoleCursorPosition(hConsole, PlaceCursorHere);
return 0;
}
This post has been edited by poptarts4liffe: 11 March 2009 - 04:34 PM

New Topic/Question
Reply



MultiQuote



|