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 / Color Array Problem

Author
Message
mjmaurer
14
Years of Service
User Offline
Joined: 12th Jun 2010
Location:
Posted: 12th Jun 2010 21:15
I'm trying to write a program where there are an array of 4 colors displayed randomly every 1/2 second. The problem is, the program might pick the same color twice and display that color for longer than 1/2 a second. I need the program to not display the same color twice in a row. I've tried using if statements to fix it, but came up empty. Can any one help? This is what I have so far.

// This program demonstrates an array of colors.
#include "DarkGDK.h"

void DarkGDK()
{
// Color constants
const DWORD RED = dbRGB(255, 0, 0);
const DWORD GREEN = dbRGB(0, 255, 0);
const DWORD BLUE = dbRGB(0, 0, 255);
const DWORD MAGENTA = dbRGB(255, 0, 255);

// Constant for time delay (half a second)
const int DELAY = 500;

// Constant for the array size
const int SIZE = 4;

// Array of colors
DWORD colors[SIZE] = { RED, GREEN, BLUE, MAGENTA };

// Variable to use as an array index
int index;

// Seed the random number generator.
dbRandomize( dbTimer() );

// Repeatedly clear the screen with random colors.
while ( LoopGDK() )
{
// Get a random number
index = dbRND( SIZE - 1 );

// Clear the screen to a random color.
dbCLS( colors[index] );

// Wait.
dbWait(DELAY);
}
}
Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 13th Jun 2010 00:15
Try creating two variables lastIndex, newIndex:



Not tested but looks like it should work.

Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 13th Jun 2010 09:15 Edited at: 13th Jun 2010 09:19
I thought I remembered this question and yes, it was asked in April. Same solution as Matty suggested.

http://forum.thegamecreators.com/?m=forum_view&t=169192&b=22

P.S. Forum advice: use the "code" button to enclose your code between two code tags, so that it can be opened/closed with a pushbutton like in Matty's post. It will preserve the indentation as well so the code will be much easier to read.
mjmaurer
14
Years of Service
User Offline
Joined: 12th Jun 2010
Location:
Posted: 13th Jun 2010 17:16
Thanks Mireben, that worked great!

Login to post a reply

Server time is: 2024-11-19 23:30:02
Your offset time is: 2024-11-19 23:30:02