Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / 2d breakout problem

Author
Message
RobBrown
15
Years of Service
User Offline
Joined: 23rd Nov 2009
Location:
Posted: 23rd Nov 2009 11:22
Hello,
I know there are many breakout tutorials on this site, however i have not found any that pertain directly to Dark GDK or the problem i am having. That being said here is the issue i am dealing with at the moment. I need to initialize an array of brick structures. this is my current structure.


struct Brick{
int id;
int x;
int y;
bool isAlive;
}
[\code]

id (the unique sprite id), x, y (the position vector), isAlive (bool to specify if the brick is visible).

I am having a hard time with a for loop to initialize the bricks. Currently i am just trying to setup one line of 5 bricks.


Brick Bricks[5]
for (i = 0; i < 5; i++){
Bricks[i].x = ?; //How do i place the bricks in a row??
Bricks[i].y = 50;
Bricks[i].id = i + 1
Bricks[i].isAlive = true;
}

I have a program that initializes and destroys one brick. I am using this template to add the array of brick structures and then in turn to destroy all the bricks. that is what "bool isAlive" is for. Once i figure out the array issue i will move onto the isAlive stuff, anyway here is the code i need to modify with the array of brick structures.


// whenever using Dark GDK you must ensure you include the header file
#include "DarkGDK.h"

struct Block{
int id;
int x;
int y;
bool isAlive;
};

// Set the game window size
const int WINDOW_WIDTH = 456;
const int WINDOW_HEIGHT = 360;
// Gray is a setting of 100 for each of red, green, and blue (out of 255).
const DWORD GRAY = dbRGB(100, 100, 100);
// give a unique id to each image
const int IMG_BALL = 1;
const int IMG_PADDLE = 2;
const int IMG_BLOCK = 3;
// give a unique id to each sprite
const int SPR_BALL = 1;
const int SPR_PADDLE = 2;
const int SPR_BLOCK = 3;

// the main entry point for the application is this function
void DarkGDK ()
{
// Set the game window size
dbSetDisplayMode(WINDOW_WIDTH, WINDOW_HEIGHT, dbScreenDepth());
dbSyncOn ();
// display 60 frames a second
dbSyncRate (60);
// a call is made to this function so we can stop the GDK from
// responding to the escape key, we can then add in some code in our
// main loop so we can control what happens when the escape key is pressed
dbDisableEscapeKey ();
// now we will set the random seed value to the timer, this will
// help us to get more random values each time we run the program
dbRandomize (dbTimer());
// Set the transparent color to black (red = 0, green = 0, blue = 0)
dbSetImageColorKey(0, 0, 0);
// Load the ball image and identify it with the constant IMG_BALL
dbLoadImage("Ball.bmp", IMG_BALL);
// Set the transparent color for the paddle
dbSetImageColorKey(66, 65, 0);
// Load the paddle image and identify it with the constant IMG_PADDLE
dbLoadImage("Paddle.bmp", IMG_PADDLE);
// Set the transparent color for the block
dbSetImageColorKey(66, 65, 0);
// Load the block image and identify it with the constant IMG_BLOCK
dbLoadImage("Block.bmp", IMG_BLOCK);
// Set the initial location for the ball
int x = 200;
int y = 150;
// Set the right margin and the bottom margin for the motion of the ball
int xMax = 440;
int yMax = 340;
// set direction at random between 225 and 315 degrees (ie downwards)
int direction = 45 + dbRND(90);
int dx = 5 * dbCos(direction);
int dy = 5 * dbSin(direction);
// Render the ball
dbSprite(SPR_BALL, x, y, IMG_BALL);
// Set the initial location and sprite for the paddle
int xPaddle = 200;
int yPaddle = 300;
// Render the paddle
dbSprite(SPR_PADDLE, xPaddle, yPaddle, IMG_PADDLE);
// Set the initial location and sprite for the block
int xBlock = 200;
int yBlock = 50;
bool isAlive = true;
// Render the block
dbSprite(SPR_BLOCK, xBlock, yBlock, IMG_BLOCK);
// now we come to our main loop, we call LoopGDK so some internal
// work can be carried out by the GDK
// the loop executes with a frame rate of 60 frames per second (fps).
while ( LoopGDK () )
{
// set the background to gray
dbCLS(GRAY);
// Move the ball for the game step
x += dx;
y += dy;
// if we hit the sides, reverse the direction
if ((x <= 0) || (x >= xMax)) {
dx = -dx;
}
// if we hit the top or bottom, reverse the direction
if ((y <= 0) || (y >= yMax)) {
dy = -dy;
}
// Move the paddle
if (dbRightKey())
xPaddle += 4;
if (dbLeftKey())
xPaddle -= 4;
// Detect collision between ball and paddle
if (dbSpriteCollision(SPR_BALL, SPR_PADDLE)) {
// reverse the direction of the ball
dy = -dy;
// move the ball away from the paddle (ie 'undo' the last move of the ball)
y += 2 * dy;
}
// Detect collision between ball and block (but only if it's still visible)
if (isAlive) {
if (dbSpriteCollision(SPR_BALL, SPR_BLOCK)) {
isAlive = false;
// hide the block
dbHideSprite(SPR_BLOCK);
// reverse the direction of the ball
dy = -dy;
}
}
// Render the ball
dbSprite(SPR_BALL, x, y, IMG_BALL);
// Render the paddle
dbSprite(SPR_PADDLE, xPaddle, yPaddle, IMG_PADDLE);
// if the block is visible, render the block
if (isAlive) {
dbSprite(SPR_BLOCK, xBlock, yBlock, IMG_BLOCK);
}
// here we check if the escape key has been pressed, when it has
// we will break out of the loop
if ( dbEscapeKey ( ) )
break;
// here we make a call to update the contents of the screen
dbSync ( );
}
// when the user presses escape the code will break out to this location
// and we can free up any previously allocated resources
// delete all the sprites
dbDeleteSprite(SPR_BALL);
dbDeleteSprite(SPR_PADDLE);
dbDeleteSprite(SPR_BLOCK);
// delete the images
dbDeleteImage(IMG_BALL);
dbDeleteImage(IMG_PADDLE);
dbDeleteImage(IMG_BLOCK);
// and now everything is ready to return back to Windows
return;
}
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 23rd Nov 2009 16:22
First off, please learn to use code blocks. It's hard to read code without the recommended indentation.

Brick Bricks[5]
for (i = 0; i < 5; i++){
Bricks[i].x = i * brickWidth; //How do i place the bricks in a row??
Bricks[i].y = 50;
Bricks[i].id = i + 1
Bricks.isAlive = true;
}

Or you can use

Brick Bricks[5] =
{
{1, 0, 50, true},
{2, 10, 50, true},
{3, 20, 50, true},
{4, 30, 50, true},
{5, 40, 50, true}
};

which assumes that the bricks are 10 pixels wide.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
RobBrown
15
Years of Service
User Offline
Joined: 23rd Nov 2009
Location:
Posted: 23rd Nov 2009 21:36
Thanks lilith.

When i typed this in last night i did use code blocks, i dont know why they didint show up right.
RobBrown
15
Years of Service
User Offline
Joined: 23rd Nov 2009
Location:
Posted: 24th Nov 2009 01:36
When i use the code you showd me it only displays one block, so ia m not sure why it's still not working.

Login to post a reply

Server time is: 2024-11-24 18:03:04
Your offset time is: 2024-11-24 18:03:04