1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
// my stuff
// as the player of this game you have three choices, move the turtle forward and backwards
// choose its direction - Either left or right
// I have to create a subclass
#include <iostream>
#include <math.h>
#include "allocore/io/al_App.hpp"
using namespace al;
using namespace std;
// the turtle class
class Turtle {
public:
float x, y;
Turtle(){
x = 0; // this is the starting point for the x
y = 0; // this is the starting point for the y
mUx = 1;
mUy = 0;
}
void move(float ds){
x += mUx * ds;
y += mUy * ds;
}
void turn(float ang){
float ux = mUx;
float uy = mUy;
mUx = ux * cos(ang) - uy * sin(ang);
mUy = uy * cos(ang) + ux * sin(ang);
}
private:
// we make the unit vector private because we provide
// a sperate interface for changing them
float mUx, mUy;
};
class DrawApp : public App{
public:
Mesh myDraw; // calling the mesh for myDraw
Turtle t;
// constructor to what needs to be drawn
DrawApp(){
nav().pos(0,0,20);
initWindow();
}
virtual void onAnimate(double framespersecond){
// myDraw.reset();
myDraw.primitive(Graphics::LINE_STRIP);
myDraw.color(0,0.5,1); // almost a cyan with RGB color coding
// THIS IS WERE I PUT MY ACTIONS
Turtle t;
char myresponse; // I use here char because i want to write a command and not type an int
std::cout << "your turtle just woke up at " << t.x << "," << t.y << " what should it do?" << std::endl;
do{
cin >> myresponse;
// Here I will use conditionals
if (myresponse == 'l')
{
t.turn(M_PI/2);
myDraw.vertex(t.x, t.y);
std::cout << "the turtle turns left and stays at " << t.x << ", " <<t.y << std::endl;
}
else if (myresponse == 'r')
{
t.turn(M_PI/-2); // I divide with -2 because the calculation t.turn(M_PI/-2); turns
myDraw.vertex(t.x, t.y); // the angle left by default. To turn the angle right I divide with -2
std::cout << "the turtle turns right and stays at " << t.x << ", " <<t.y << std::endl;
}
else if (myresponse == 'f')
{
t.move(1);
myDraw.vertex(t.x, t.y);
std::cout << "the turtle steps forward and stops at " << t.x << ", " <<t.y << std::endl;
}
else if (myresponse == 'b')
{
t.move(-1); //forward means to move along the x-axis, so to move back I need to put -1
myDraw.vertex(t.x, t.y);
std::cout << "the turtle moves backwards and stops at " << t.x << ", " <<t.y << std::endl;
}
else if (myresponse == 's')
{
std::cout << "the turtle stops to draw a square" << std::endl;
for(int i=0;i<4;++i){
std::cout << t.x << ", " <<t.y << std::endl;
t.move(5); // we put 1 here "optional" this is ds
t.turn(M_PI/2); // why 4, because 360 divided with 4 is 90
}
std::cout << "done drawing" << t.x << ", " << t.y << std::endl;
}
}while( myresponse != 'Q' );
}
virtual void onDraw(Graphics& g, const Viewpoint& v){
// drawing the graphics
g.draw(myDraw);
g.lineWidth(1.5);
}
private:
};
int main(){
DrawApp().start();
}
|