Thursday, March 05, 2009

Project #62 - An Improved Color Perfect Pitch Tester



(Previously) I took some time today to apply a color picking strategy so that choosing the right one would be a lot more challenging that in the previous version. During testing there were quite a few cases where one would need bionic eyes to get the answer correct on the first try. I haven't yet decided whether this will be a bug or a feature.

Still, it's pretty easy to cheat with this game, so I might include a timer and set a time limit or create a scheme to award more points to quicker guesses. It would also be nice to do some long term score tracking so that we can have a better idea of what is actually a good result vs. a mediocre one. How will I determine high scores? I could track strings of correct guesses on the first try...or something. I'll have to think about it.

My code is a little less elegant this time around. I hesitated posting it a little, but maybe someone will find it educational. Learn from my mistakes, kids!

stop();
import flash.events.MouseEvent;
var cardArray:Array = new Array(card_1, card_2, card_3, card_4, card_5, card_6, card_7, card_8);
var correct = Math.floor(Math.random()*8);
trace(correct);
var total_guesses = 0;
var right_guesses = 0;
var wrong_guesses = 0;
var total_tests = 0;
var wrong_tests = 0;
var wrong_once = false;
var red; var green; var blue; var big_box_color; var choose;
instructions.enabled = false;

reset();
setBoxes();
change_color(color_button.color_bar, big_box_color);

color_button.addEventListener(MouseEvent.CLICK, get_color);

function get_color(e:MouseEvent):void {
color_button.gotoAndStop(2);
change_color(color_button, 0x000000);
instructions.gotoAndStop(2);

var count = 0;
for each (var card:MovieClip in cardArray) {

if (count == correct) {
change_color(card, big_box_color);
card.addEventListener(MouseEvent.CLICK, set_click_right);
}
else {
var near_color = generate_near_color();
change_color(card, near_color);
card.addEventListener(MouseEvent.CLICK, set_click_wrong);
}
count++;
}
}

function setBoxes():void {
var percentage:Number = right_guesses / total_guesses;
if (total_guesses == 0) { percentage = 0;}
box1.text = "Correct guesses: " + percentage.toFixed(2) + "%";
box2.text = "Correct on first try: " + (total_tests - wrong_tests);
box4.text = "TESTS: " + total_tests;

}

function reset():void {
correct = Math.floor(Math.random()*8);
choose = Math.floor(Math.random()*3);
red = generate_hex_value();
green = generate_hex_value();
blue = generate_hex_value();
big_box_color = "0X" + red + green + blue;
}

function set_click_right(e:MouseEvent):void {
color_button.gotoAndStop(1);
reset();
trace(color_button);
right_guesses++;
total_guesses++;

if (wrong_once == true) { wrong_tests++; }
wrong_once = false;
total_tests++;

setBoxes();
change_color(color_button, big_box_color);
instructions.gotoAndStop(1);
var count = 0;
for each (var card:MovieClip in cardArray) {
card.removeEventListener(MouseEvent.CLICK, set_click_right, false);
card.removeEventListener(MouseEvent.CLICK, set_click_wrong, false);
change_color(card, 0xCCCCCC);
count++;
}
}

function set_click_wrong(e:MouseEvent):void {
wrong_guesses++;
total_guesses++; wrong_once = true;
setBoxes();
}

function change_color(card:MovieClip, next_color):void {
var colorTransform:ColorTransform = card.transform.colorTransform;
colorTransform.color = next_color;
card.transform.colorTransform = colorTransform;
}

function generate_hex_value():String {
var hex_array:Array = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"];
var first_num = hex_array[Math.floor(Math.random()*16)];
var second_num = hex_array[Math.floor(Math.random()*16)];
return (first_num + second_num);
}

function generate_near_color():String {
var temp = generate_hex_value();
var new_red = red; var new_green = green; var new_blue = blue;
if (choose == 0) if (temp != red) new_red = temp; else generate_near_color();
else if (choose == 1) if (temp != green) new_green = temp; else generate_near_color();
else if (choose == 2) if (temp != blue) new_blue = temp; else generate_near_color();

return ("0X" + new_red + new_green + new_blue);
}

Labels: ,

posted by Alison 3/05/2009 11:27:00 PM : (0) comments : splink



prime-number.com

site feed

Archives:

Powered by Blogger



www.flickr.com
This is a Flickr badge showing items in a set called DIY. Make your own badge here.


My Rules for the year:
• Make something new every day.
• Don't buy anything you can make yourself, especially clothes.

Elsewhere:
Bio
Graphics Homework
Pictures
Email Me

Very Old Flash Homework:
Old Flash Page
Aquarium
KiteBuilder
Shapes

I belong to:
WeddingBee
Metafilter

Weblogs Written By Nice People:
» Munsoned in the Middle of Nowhere
» Ben Bonetti
» romej.com
» The Rector's Page
» A Dress A Day
» The Point of Babette

Old Versions:
Fall Leaves
Early to Rise
Chopstick
Equine


Old Splash Pages:
Old Splash Pages:
»Original Recipe
»Sketch of Clark
»Big Blob
»Another Big Blob
»Digi Charat Bear
»Linoleum Samples




Google



Search WWW
Search www.prime-number.com



Current Weather in Pittsburgh, PA: The WeatherPixie