Brick Wall Programming Exercise Games & Graphics in C++ Tony Gaddis

1637
Views

Games & Graphics in C++ Using the DarkGDK library
Brick Wall Ch. 6.

This code looks at the Brick Wall exercise on pp. 278.

Find out where your website ranks with your chosen keywords in Google, Yahoo and Bing.
14 day free trial. No credit card required.

C++ and DarkGDK

Premium WooCommerce Extensions, WordPress Plugins & Themes Only $10 or Free with Membership

This program utilizes the for statement with a count controlled loop and an if-else statement

This program has twists to it:

1. It uses a prototype function for practice
2. It also uses dbRND to make some bricks blue
3. Utilizes some of the RandomBricksLoop.cpp code structure on pp. 243

Premium WooCommerce Extensions, WordPress Plugins & Themes Only $10 or Free with Membership

The function prototype is not necessary to draw the brick wall. The whole thing can be included in the main function void DarkGDK(). It was added on purpose to practice with a function prototype.

********************************************************

#include “DarkGDK.h”

// Function prototype
void drawBrick();
/*
Constant global variables. Added as a global is not necessary. It could have been added locally in the drawBrick() function. This is for practice with global variables. Setting constant variables could have been set as simply int variables locally in the drawBrick() function, but was done for practice using const in variables. Note that the textbook uses DWORD to declare the color variable as a 32 bit unsigned integer. You can use int or DWORD. Both work. I keep is simple and use int.
*/
const int red = dbRGB(255,0,0);
const int black = dbRGB(0,0,0);
const int blue = dbRGB(0,0,255);
//******************************************************
// DarkGDK function *
//******************************************************

void DarkGDK ()
{

// Seed the random number generator dbRND located in the drawBrick() function. dbRandomize( dbTimer() );

// Call the drawBrick function
drawBrick();

// After drawBrick() is finished, pause the program until the user presses a key.
dbWaitKey();

}

/********************************************************************
The drawBrick function draws red and blue bricks.
The function draws bricks 40 pixels wide and 20 pixels high, *
allowing for one pixel border around the bricks
**********************************************************************/

void drawBrick()

{

for(int x=0; x<=800; x+=40) /* as long as x = to 0 and less than or equal 800, draw some bricks 40 pixels wide below in dbBox*/

{

for(int y=0; y<=600; y+=20) /* as long as y = to 0 and less than or equal 600, draw some bricks 20 pixels high below in dbBox*/

{

// Draw blue bricks randomly; number for a is less than random number for b on the pixel grid*/

int a = dbRND(20);
int b = dbRND(50);
if(a<b)
{

dbInk(blue,black);
dbBox(x+1,y+1,x+39,y+19);

}

else // otherwise draw the red bricks at the dimensions above
{

//draw red bricks
dbInk(red,black);
dbBox(x+1,y+1,x+39,y+19);
}
}
}
}

Recommended C++ Course

A Rookies Guide to C++

(1637)

Brick Wall Programming Exercise Games & Graphics in C++ Tony Gaddis

Articles, C & C++, Programming | 0 Comments

Share your thoughts!