Code 15yte6r3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Asteroids Code.

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);

class Menu menu;


while (menu.value != 42)
{
LCD.Clear();
switch(menu.value) {

// Play Game Case


case 1 :
menu.Display(special_mode);
if (buttons.LeftPressed())
{
menu.MoveChoiceUp();
menu.value = 6;
}
if (buttons.RightPressed())
{
Page 2
Asteroids Code.txt
menu.MoveChoiceDown();
menu.value = 2;
}
if (buttons.MiddlePressed())
{
int difficulty_wait = 0;
int difficulty = 0;

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;

// Deals with the Triangle's Rotation


float
RotationAngle1=(PI),RotationAngle2=(323.13*PI/180),RotationAngle3=(75.74*PI/360.0),R
CP1X=159.0,RCP1Y=129.0,RCP2X=153.0,RCP2Y=111.0,RCP3X=165.0,RCP3Y=111.0;

// Deals with the Shot's Orientation


float backPX = 159.0, backPY = 109.0, backangle = (0.0);

// Deals with asteroid count.


float initial = TimeNow();

// Deals with drawing the asteroids 'n stuff


int counter;

// Deals with shot regulation


float cooldown= (-0.10);
int shot_counter = 1;
Page 3
Asteroids Code.txt

// declares a button board in bank3


// ButtonBoard buttons(FEHIO::Bank3);

LCD.SetBackgroundColor(BLACK);
LCD.Clear();

// Initialize an array or Asteroids and an Array of bullets


class Asteroid ass[5];
class Shot bullet[10];

// Sets asteroid speed based on difficulty


for (counter=0;counter<5;counter++)
{
if (difficulty == 1)
{
ass[counter].spd=4.0;
}
if (difficulty == 2)
{
ass[counter].spd=2.0;
}
}

while (truth==1)
{

LCD.Clear();

// Score is written to screen


LCD.Write("Score: ");
LCD.Write(score);

//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 **

// Asteroid No. 1 Generator


if ( (initial>0) && ( (TimeNow() - ass[0].timer) > 3.0) )
{
if (ass[0].alive == false)
{
Page 5
Asteroids Code.txt
ass[0].Generate();
ass[0].CalcSlope();
}

// Asteroid No. 2 Generator


if ( ( (TimeNow() - initial) > (2.0) ) && ( (TimeNow() -
ass[1].timer) > 3.0) )
{
if (ass[1].alive == false)
{
ass[1].Generate();
ass[1].CalcSlope();
}

// Asteroid No. 3 Generator


if (difficulty == 2)
{
if ( ( (TimeNow() - initial) > (4.0) ) && ( (TimeNow() -
ass[2].timer) > 3.0) )
{
if (ass[2].alive == false)
{
ass[2].Generate();
ass[2].CalcSlope();
}
}

/* // Asteroid No. 4 Generator


if ( ( (TimeNow() - initial) > (6.0) ) && ( (TimeNow() -
ass[3].timer) > 3.0) )
{
if (ass[3].alive == false)
{
ass[3].Generate();
ass[3].CalcSlope();
}

// Asteroid No. 5 Generator


if ( ( (TimeNow() - initial) > (8.0) ) && ( (TimeNow() -
ass[4].timer) > 3.0) )
Page 6
Asteroids Code.txt
{
if (ass[4].alive == false)
{
ass[4].Generate();
ass[4].CalcSlope();
}

} */

// Draws all the Asteroids and Updates their coordinates


for (counter=0;counter<5;counter++)
{
if (ass[counter].alive == true)
{
ass[counter].Draw(special_mode);
ass[counter].Update();
}
}

// ** Time for some of the Shooting 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;
}

else if (bullet[shot_counter].alive == false )


{

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();
}
}

// Draws the bullets at their positions and updates those


positions
for (counter=0;counter<10;counter++)
{
bullet[counter].Draw(special_mode);
bullet[counter].Update();
}

// Here comes the collision mechanics

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);

// Bullet is destroyed too


bullet[shot_counter].alive = false;
score++;
}
}

// This checks to see if any of the asteroids have hit the


"ship"
if ( (ass[asteroid_counter].alive == true) && ( ( pow(
(ass[asteroid_counter].x - 159), 2) + pow( (ass[asteroid_counter].y - 119), 2) ) <=
pow( (2.0*radius) ,2 ) ) )
{
truth = 0;
survival_time = (TimeNow() - initial);
LCD.Clear();
Page 8
Asteroids Code.txt
LCD.WriteAt("YOU LOST...",125,115);
Sleep(3.0);
}
}
}
}
break;

// 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;

}
}

LCD.Write("Thanks for playing.");


}
Asteroid::Asteroid(bool bb, float timmy)
{
alive = bb;
timer = timmy;
}

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;
}
}

void Asteroid::Draw(bool spc_mode)


{
if (alive == true)
{
LCD.DrawCircle(x,y,size);
if (spc_mode == true)
{
LCD.SetFontColor(YELLOW);
LCD.WriteAt("M",(x-5),(y-5));
LCD.SetFontColor(WHITE);
}
}
}

Shot::Shot(bool bb)
{
alive = bb;
}

void Shot::Generate(float TIPX, float TIPY, float BACKX, float BACKY)


{

alive = true;

slope = fabsf( (TIPY - BACKY) / (TIPX - BACKX) );


if (TIPX>159)
Page 13
Asteroids Code.txt
{
delta_x = (1.0/MILLION);
}
else if (TIPX<159)
{
delta_x = -(1.0/MILLION);
}
else
{
delta_x = 0.0;
}

//

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) );

void Shot::CalcSlope(float TIPX, float TIPY, float BACKX, float BACKY)


{
slope = fabsf( (TIPY - BACKY) / (TIPX - BACKX) );
if (TIPX>159)
{
delta_x = (1.0/MILLION);
}
else if (TIPX<159)
{
delta_x = -(1.0/MILLION);
}
Page 14
Asteroids Code.txt
else
{
delta_x = 0.0;
}

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 Shot::Draw(bool spc_mode)


{
if (alive == true)
{
// LCD.DrawPixel(XF,YF);
LCD.DrawPixel(XB,YB);
if (spc_mode == true)
{
LCD.SetFontColor(SCARLET);
LCD.WriteAt("O",XB,YB);
LCD.SetFontColor(WHITE);
}
}
}
Page 15
Asteroids Code.txt

Menu::Menu(int why, int v)


{
y = why;
value = 1;
}

void Menu::Display(bool spc_mode)


{
LCD.Clear();
LCD.WriteAt("Play Game", 140, 80);
LCD.WriteAt("Instructions",140,100);
LCD.WriteAt("Stats.",140,120);
LCD.WriteAt("References.",140,140);
LCD.WriteAt("Special Mode: ",120, 160);
if (spc_mode == true)
{
LCD.WriteAt("ON.",290,160);
}
else
{
LCD.WriteAt("OFF.",280,160);
}
LCD.WriteAt("Quit.",140,180);
if (y == 160)
{
LCD.DrawRectangle(119,(y-2),200,20);
}
else
{
LCD.DrawRectangle(139,(y-2),150,20);
}
}

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

You might also like