Professional Documents
Culture Documents
Code 15yte6r3
Code 15yte6r3
Code 15yte6r3
txt
#include <FEHLCD.h>
#include <FEHIO.h>
#include <FEHUtility.h>
#include <math.h>
#include <stdlib.h>
#define PI 3.1415926535897
#define radius (10)
#define rotation_speed 120.0
#define MILLION 2.0
class Asteroid
{
private:
float slope;
float delta_x;
float delta_y;
public:
float spd;
float x;
float y;
float size;
bool alive;
float timer;
Asteroid(bool =false, float =5.0);
void Generate();
void Update();
void CalcSlope();
void Draw(bool);
};
class Shot
{
private:
float slope;
float delta_x;
float delta_y;
float magnitude;
public:
float XF;
float YF;
float XB;
float YB;
bool alive;
Page 1
Asteroids Code.txt
Shot(bool =false);
void Generate(float,float,float,float);
void Update();
void CalcSlope(float,float,float,float);
void Draw(bool);
};
ButtonBoard buttons(FEHIO::Bank3);
class Menu
{
public:
int value;
int y;
void Display(bool);
void MoveChoiceUp();
void MoveChoiceDown();
Menu(int =80, int =1);
};
int main(void)
{
bool special_mode = false;
int score = 0;
float survival_time = 0.0;
LCD.SetBackgroundColor(BLACK);
LCD.Clear();
LCD.Write("Welcome to Asteroids. \n\n Use Left and Right to navigate the menu.
\n\nMiddle button to select.");
Sleep(3.00);
while (difficulty_wait == 0)
{
LCD.Clear();
LCD.WriteAt("Select Difficulty: ", 60,80);
LCD.WriteAt("Left Press for Easy.",60,100);
LCD.WriteAt("Right Press for Normal.",60,120);
if (buttons.LeftPressed())
{
difficulty_wait = 1;
difficulty = 1;
}
if (buttons.RightPressed())
{
difficulty_wait = 1;
difficulty = 2;
}
}
int truth=1;
//Score starts at 0
score = 0;
LCD.SetBackgroundColor(BLACK);
LCD.Clear();
while (truth==1)
{
LCD.Clear();
//Draws Triangle Based on Points RCP1, RCP2, and RCP3 (each with
an X and Y coordinate.)
LCD.SetFontColor(WHITE);
LCD.DrawLine(RCP1X,RCP1Y,RCP2X,RCP2Y);
LCD.DrawLine(RCP2X,RCP2Y,RCP3X,RCP3Y);
LCD.DrawLine(RCP3X,RCP3Y,RCP1X,RCP1Y);
LCD.DrawPixel(backPX,backPY);
if (buttons.LeftPressed())
{
RotationAngle1 = RotationAngle1 + (PI/rotation_speed);
RotationAngle2 = RotationAngle2 + (PI/rotation_speed);
RotationAngle3 = RotationAngle3 + (PI/rotation_speed);
Page 4
Asteroids Code.txt
backangle = backangle + (PI/120.0);
RCP1X = RCP1X+radius*(cos(RotationAngle1)/42);
RCP1Y = RCP1Y+radius*(sin(RotationAngle1)/42);
RCP2X = RCP2X+radius*cos(RotationAngle2)/42;
RCP2Y = RCP2Y+radius*sin(RotationAngle2)/42;
RCP3X = RCP3X+radius*cos(RotationAngle3)/42;
RCP3Y = RCP3Y+radius*sin(RotationAngle3)/42;
backPX = backPX+radius*cos(backangle)/42;
backPY = backPY+radius*sin(backangle)/42;
if (buttons.RightPressed())
{
RotationAngle1 = RotationAngle1 - (PI/rotation_speed);
RotationAngle2 = RotationAngle2 - (PI/rotation_speed);
RotationAngle3 = RotationAngle3 - (PI/rotation_speed);
backangle = backangle - (PI/120.0);
RCP1X = RCP1X-radius*(cos(RotationAngle1)/42);
RCP1Y = RCP1Y-radius*(sin(RotationAngle1)/42);
RCP2X = RCP2X-radius*cos(RotationAngle2)/42;
RCP2Y = RCP2Y-radius*sin(RotationAngle2)/42;
RCP3X = RCP3X-radius*cos(RotationAngle3)/42;
RCP3Y = RCP3Y-radius*sin(RotationAngle3)/42;
backPX = backPX-radius*cos(backangle)/42;
backPY = backPY-radius*sin(backangle)/42;
// ** Asteroid Stuff **
} */
// float cooldown=1000;
// int shot_counter = 1;
if (buttons.MiddlePressed())
{
if ( (TimeNow()-cooldown ) > 0.10 )
{
LCD.DrawCircle(159,119,10);
for (shot_counter=0;shot_counter<10;shot_counter++)
{
if (bullet[0].alive == false)
{
bullet[0].Generate(RCP1X,RCP1Y,backPX,backPY);
// bullet[0].CalcSlope(RCP1X, RCP1Y, backPX,
backPY);
shot_counter=10;
}
bullet[shot_counter].Generate(RCP1X,RCP1Y,backPX,backPY);
//
bullet[shot_counter].CalcSlope(RCP1X,RCP1Y,backPX,backPY);
shot_counter = 10;
}
}
Page 7
Asteroids Code.txt
cooldown = TimeNow();
}
}
int asteroid_counter;
// Amazingly, this loop checks if any of the shots have hit any
of the asteroids and erases both the bullet and the asteroids if that is true.
for (asteroid_counter=0;asteroid_counter<5;asteroid_counter++)
{
for (shot_counter=0;shot_counter<10;shot_counter++)
{
if ( ( pow( (bullet[shot_counter].XB -
ass[asteroid_counter].x) , 2) + pow( (bullet[shot_counter].YB -
ass[asteroid_counter].y) , 2) ) <= pow(ass[asteroid_counter].size, 2) &&
(ass[asteroid_counter].alive == true) )
{
// Asteroid is destroyed
ass[asteroid_counter].alive = false;
ass[asteroid_counter].timer = TimeNow();
LCD.WriteAt("HIT",50,60);
// LCD.WriteAt(ass[asteroid_counter].alive,30,60);
// Instructions Case
case 2 :
menu.Display(special_mode);
if (buttons.LeftPressed())
{
menu.MoveChoiceUp();
menu.value = 1;
}
if (buttons.RightPressed())
{
menu.MoveChoiceDown();
menu.value = 3;
}
if (buttons.MiddlePressed())
{
LCD.Clear();
LCD.Write("Instructions: \nLeft and Right to rotate the
ship.\nMiddle button to fire a shot.\nShoot the asteroids to survive.\nDon't get hit
with the 'roids.");
Sleep(7.0);
}
break;
// Stats Case
case 3 :
menu.Display(special_mode);
if (buttons.LeftPressed())
{
menu.MoveChoiceUp();
menu.value = 2;
}
if (buttons.RightPressed())
{
menu.MoveChoiceDown();
menu.value = 4;
}
if (buttons.MiddlePressed())
{
LCD.Clear();
LCD.WriteAt("Score: ",40,80);
Page 9
Asteroids Code.txt
LCD.WriteAt(score,120,80);
LCD.WriteAt("Time Survived (s): ",40,100);
LCD.WriteAt(survival_time,200,100);
Sleep(5.0);
}
break;
// References Case
case 4 :
menu.Display(special_mode);
if (buttons.LeftPressed())
{
menu.MoveChoiceUp();
menu.value = 3;
}
if (buttons.RightPressed())
{
menu.MoveChoiceDown();
menu.value = 5;
}
if (buttons.MiddlePressed())
{
LCD.Clear();
LCD.Write("Game made entirely by \n Griffin Rudy and Shreya Nalluri.
\nNot copyrighted, go ahead and\n steal it, ya punks.");
Sleep(5.0);
}
break;
// References Case
case 5 :
menu.Display(special_mode);
if (buttons.LeftPressed())
{
menu.MoveChoiceUp();
menu.value = 4;
}
if (buttons.RightPressed())
{
menu.MoveChoiceDown();
menu.value = 6;
}
if (buttons.MiddlePressed())
{
if (special_mode == false)
{
special_mode = true;
}
Page 10
Asteroids Code.txt
else
{
special_mode = false;
}
}
break;
// Quit Case
case 6 :
menu.Display(special_mode);
if (buttons.LeftPressed())
{
menu.MoveChoiceUp();
menu.value = 5;
}
if (buttons.RightPressed())
{
menu.MoveChoiceDown();
menu.value = 1;
}
if (buttons.MiddlePressed())
{
menu.value = 42;
}
break;
}
}
void Asteroid::Generate()
{
int top_or_bottom;
int which_border;
srand(TimeNow());
which_border = rand ()% 2;
srand(TimeNow());
top_or_bottom = rand()% 2;
Page 11
Asteroids Code.txt
if (which_border == 1)
{
x = rand()%319;
if (top_or_bottom == 1)
{
y = 219;
}
else
{
y = 0;
}
}
else
{
y = rand()%239;
if (top_or_bottom == 1)
{
x = 319;
}
else
{
x = 0;
}
}
size = rand()%30 + 6;
alive = true;
}
void Asteroid::CalcSlope()
{
slope = fabsf( (119.0-y)/(159.0-x) );
if (x<159)
{
delta_x = (1.0/spd);
}
else if (x>159)
{
delta_x = -(1.0/spd);
}
else
{
delta_x = 0.0;
}
//
if (y<119)
Page 12
Asteroids Code.txt
{
delta_y = slope/spd;
}
else if (y>119)
{
delta_y = -slope/spd;
}
else
{
delta_y = 0.0;
}
}
void Asteroid::Update()
{
if (alive == true)
{
x = x + delta_x;
y = y + delta_y;
}
}
Shot::Shot(bool bb)
{
alive = bb;
}
alive = true;
//
if (TIPY>119)
{
delta_y = slope/MILLION;
}
else if (TIPY<119)
{
delta_y = -slope/MILLION;
}
else
{
delta_y = 0.0;
}
magnitude = sqrt ( delta_x*delta_x + delta_y*delta_y );
XB = TIPX;
YB = TIPY;
XF = TIPX + ( sqrt(25.0)*(delta_x/magnitude*MILLION) );
YF = TIPY + ( sqrt(25.0)*(delta_y/magnitude*MILLION) );
if (TIPY>119)
{
delta_y = slope/MILLION;
}
else if (TIPY<119)
{
delta_y = -slope/MILLION;
}
else
{
delta_y = 0.0;
}
magnitude = sqrt ( delta_x*delta_x + delta_y*delta_y );
}
void Shot::Update()
{
if (alive == true)
{
XB = XB + delta_x;
YB = YB + delta_y;
XF = XF + delta_x;
YB = YB + delta_y;
}
}
void Menu::MoveChoiceUp()
{
if (y>80)
{
y = y-20;
}
else if (y == 80)
{
y = 180;
}
}
void Menu::MoveChoiceDown()
{
Page 16
Asteroids Code.txt
if (y<180)
{
y = y+20;
}
else if (y == 180)
{
y = 80;
}
}
Page 17