#include <FEHLCD.h>
#include <FEHIO.h>
#include <FEHUtility.h>
#include <FEHBuzzer.h>
#include <FEHAccel.h>
#include <cstdlib>
#include <stdlib.h>
#include <math.h>
/*
* Authors: Nicholas Denson and Ashwin Chidambaram
* Click a Color Color Clicker game
* Dr. Parke 3:00 ENGR 1281.01H
* */
//main class user
class User { //CLASS/OBJECT CODING REQUIREMENT
public:
float score, x_pos, y_pos, avg_react_time;
int word_col, scr_col;
User() {//constructor
float score, x_pos, y_pos, avg_react_time;
int word_col, scr_col;
}
//color changing function
void Color_Change() {
int scr_col,word_col;
scr_col=rand()%6; //generates random number for screen color and changes screen color
word_col=rand()%6; //generates random number for word color and changes word
LCD.Clear();
switch (scr_col) { //SWITCH CASE CODING REQUIREMENT
case 0:
LCD.SetFontColor(SCARLET);
LCD.FillRectangle(0,0,319,239);
break;
case 1:
LCD.SetFontColor(GRAY);
LCD.FillRectangle(0,0,319,239);
break;
case 2:
LCD.SetFontColor(BLUE);
LCD.FillRectangle(0,0,319,239);
break;
case 3:
LCD.SetFontColor(GREEN);
LCD.FillRectangle(0,0,319,239);
break;
case 4:
LCD.SetFontColor(WHITE);
LCD.FillRectangle(0,0,319,239);
break;
}
LCD.SetBackgroundColor(WHITE);
LCD.SetFontColor(BLACK);
Sleep(0.1);
switch (word_col) {
case 0:
LCD.WriteAt("SCARLET",94,111);
break;
case 1:
LCD.WriteAt("GRAY",94,111);
break;
case 2:
LCD.WriteAt("BLUE",94,111);
break;
case 3:
LCD.WriteAt("GREEN",94,111);
break;
case 4:
LCD.WriteAt("WHITE",94,111);
break;
}
}
float compare() {
int i;
float start_time,end_time,react_time,x,y;
double xforce;
start_time=TimeNow()+5;
i=65;
while (TimeNow()<=start_time && i==65)//runs each frame for 5 seconds (WHILE LOOP CODING REQUIREMENT)
{
if (scr_col==word_col)//executes this sequence if the colors match (IF/ELSE CODING REQUIREMENT)
{
if(xforce=fabs(Accel.X())>=0.15) //RELATIONAL OPERATOR CODING REQUIREMENT
{
react_time=5;
i=64;
}
else if (LCD.Touch(&x,&y)==1)
{
end_time=TimeNow();
react_time=start_time-end_time;
i=64;
}
else {//allows loop to run until a decision is made by the user or 5 seconds is up
i=65;
}
}
else if (scr_col!=word_col)//executes this sequence if the colors don't match
{
if(LCD.Touch(&x,&y)==1)
{
react_time=5;
i=64;
}
else if (xforce=fabs(Accel.X())>=0.15)
{
end_time=TimeNow();
react_time=start_time-end_time;
i=64;
}
else {//allows loop to run until a decision is made by the user or 5 seconds is up
i=65;
}
}
else {
i=65;
}
}
return react_time; //return variable is added to the score
};
float score_adder (float s) {//adds score to the respective member of class User and returns the score
float score;
score=s;
return score;
};
void thanks(float w) { //creates a end scene for each game played (USER-DEFINED FUNCTION CODING REQUIREMENT)
float wscore=w;
LCD.SetBackgroundColor(BLACK);
LCD.SetFontColor(SCARLET);
LCD.WriteAt("Thanks for playing",11,150);
LCD.WriteAt("Time: ",11,167);
LCD.WriteAt(wscore,83,167);
Sleep(5.0);
};
};
int Menu_Screen() {
LCD.Clear();
LCD.SetBackgroundColor(BLACK);
LCD.SetFontColor(SCARLET);
LCD.FillRectangle(5,20,100,100); //user 1 button
LCD.FillRectangle(110,20,100,100); //user 2 button
LCD.FillRectangle(215,20,100,100); //user 3 button
LCD.FillRectangle(5,130,100,100); //high score button
LCD.FillRectangle(110,130,140,100); //instructions/credits button
LCD.FillRectangle(255,130,60,100); //quit button
LCD.SetFontColor(GRAY);
LCD.WriteAt("USER NUMBER",94,2);
LCD.WriteAt("1",46,62);
LCD.WriteAt("2",157,62);
LCD.WriteAt("3",262,62);
LCD.WriteAt("High",31,170); //high text
LCD.WriteAt("Scores",31,187); //scores text
LCD.WriteAt("Instruct./",112,170); //instructions text
LCD.WriteAt("Credits",112,187); //credits text
LCD.WriteAt("Quit",263,170); //quit text
int x;
x=65;
float x_pos,y_pos;
while (LCD.Touch(&x_pos,&y_pos)==0) {
if(LCD.Touch(&x_pos, &y_pos)) {
if (x_pos<105 && x_pos>5) { //checks to see if button 1 was touched
if (y_pos<120 && y_pos>20) {
x=1;
}
}
if (x_pos<220 && x_pos>110) { //checks to see if button 2 was touched
if (y_pos<120 && y_pos>20) {
x=2;
}
}
if (x_pos<315 && x_pos>215) { //checks to see if button 3 was touched
if (y_pos<120 && y_pos>20) {
x=3;
}
}
if (x_pos<105 && x_pos>5) { //high score button
if (y_pos<230 && y_pos>130) {
x=4;
}
}
if (x_pos<250 && x_pos>110) { //instructions/credits button
if (y_pos<230 && y_pos>130) {
x=6;
}
}
if (x_pos<315 && x_pos>255) { //quit button
if (y_pos<230 && y_pos>130) {
x=5;
}
}
}
}
return x;
};
void highscore (float o, float tw, float th) {
LCD.Clear();
float one,two,three,p,t;
one=o;
two=tw;
three=th;
LCD.SetFontColor(SCARLET);
LCD.WriteLine("Most Recent Highscores");
LCD.WriteLine("Tap screen to return to main menu");
if (one>two && two>three) //1,2,3
{
LCD.Write("1: User 1 ");
LCD.WriteLine(one);
LCD.Write("2: User 2 ");
LCD.WriteLine(two);
LCD.Write("3: User 3 ");
LCD.WriteLine(three);
}
else if (two>one && one>three)//2,1,3
{
LCD.Write("1: User 2 ");
LCD.WriteLine(two);
LCD.Write("2: User 1 ");
LCD.WriteLine(one);
LCD.Write("3: User 3 ");
LCD.WriteLine(three);
}
else if (two>three && three>one)//2,3,1
{
LCD.Write("1: User 2 ");
LCD.WriteLine(two);
LCD.Write("2: User 3 ");
LCD.WriteLine(three);
LCD.Write("3: User 1 ");
LCD.WriteLine(one);
}
else if (three>two && two>one)//3,2,1
{
LCD.Write("1: User 3 ");
LCD.WriteLine(three);
LCD.Write("2: User 2 ");
LCD.WriteLine(two);
LCD.Write("3: User 1 ");
LCD.WriteLine(one);
}
else if (three>one && one>two)//3,1,2
{
LCD.Write("1: User 3 ");
LCD.WriteLine(three);
LCD.Write("2: User 1 ");
LCD.WriteLine(one);
LCD.Write("3: User 2 ");
LCD.WriteLine(two);
}
else if (one>three && three>two)//1,3,2
{
LCD.Write("1: User 1 ");
LCD.WriteLine(one);
LCD.Write("2: User 3 ");
LCD.WriteLine(three);
LCD.Write("3: User 2 ");
LCD.WriteLine(two);
}
else if (one==two && two==three)
{
LCD.WriteLine("No games played yet");
}
else if (one>two && two==three)
{
LCD.Write("1: User 1 ");
LCD.WriteLine(one);
LCD.Write("2: Users 2 and 3: 0");
}
else
{
LCD.WriteLine("ERROR");
}
while (!LCD.Touch(&p,&t));
while (LCD.Touch(&p,&t));
LCD.Clear();
};
void instructions() {
LCD.Clear();
LCD.SetFontColor(SCARLET);
LCD.SetBackgroundColor(BLACK);
LCD.WriteLine("INSTRUCTIONS/CREDITS");
LCD.WriteLine("When the screen and written color are the same tap the screen. If they are different, tilt the Proteus to the left");
LCD.WriteLine("Source Code Authors: Nicholas Denson and Ashwin Chidambaram");
LCD.WriteLine("Help from Alex Egyed, Andrew Derge, Dr. Mike Parke, and the goat Max Kross");
LCD.WriteLine("Tap screen to return to main menu");
Sleep(1.0);
//waits for user to tap screen
float n,m;
while (!LCD.Touch(&n,&m));
while (LCD.Touch(&n,&m));
}
int main(void)
{
int g,x,i;
float react,score,one_score,two_score,three_score;
score=0;
one_score=0;
two_score=0;
three_score=0;
g=0;
do { //DO WHILE LOOP CODING REQUIREMENT
x=Menu_Screen();
srand(TimeNow()*1000);
//checks what user pressed on main screen
if(x==1)//LOGICAL OPERATOR CODING REQUIREMENT
{
User one;
score=0;
for (i=1;i<=15;i++) //FOR LOOP CODING REQUIREMENT
{
one.Color_Change();
react=one.compare();
score=score+react;
LCD.Clear();
}
one_score=one.score_adder(score);
LCD.Clear();
one.thanks(one_score);
}
if(x==2)
{
User two;
score=0;
for (i=1;i<=15;i++)
{
two.Color_Change();
react=two.compare();
score=score+react;
LCD.Clear();
}
two_score=two.score_adder(score);
LCD.Clear();
two.thanks(two_score);
}
if(x==3)
{
User three;
score=0;
for (i=1;i<=15;i++)
{
three.Color_Change();
react=three.compare();
score=score+react;
LCD.Clear();
}
three_score=three.score_adder(score);
LCD.Clear();
three.thanks(three_score);
}
if(x==4)
{
highscore(one_score,two_score,three_score);
}
if(x==5)
{
g=1;
LCD.Clear();
}
if(x==6)
{
instructions();
}
} while (g==0);
}