Skip to content

Commit

Permalink
Lunar lander rocket added
Browse files Browse the repository at this point in the history
  • Loading branch information
s-litvin committed Sep 28, 2019
1 parent 2cda906 commit 9088ba3
Show file tree
Hide file tree
Showing 9 changed files with 800 additions and 1,188 deletions.
11 changes: 2 additions & 9 deletions main/LibertyBell.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class LibertyBell : public Gameplayer
while (digitalRead(BUTTON) && direction == 0) {
direction = map(analogRead(A1), 0, 1024, -1, 2);
}
rotations = random(10, 17);
rotations = 10;
}
}

Expand Down Expand Up @@ -78,20 +78,13 @@ class LibertyBell : public Gameplayer
break;
}
}




}

unsigned int getDelayBetweenFrames()
{
return 5;
}

static unsigned char * getPreviewImg()
{
return bell84x48;
}

};

Expand Down
2 changes: 1 addition & 1 deletion main/Particle.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Particle
Vector * acceleration;
Vector * orientation;

float mass;
uint8_t mass;

Particle(float _x, float _y)
{
Expand Down
40 changes: 20 additions & 20 deletions main/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,26 @@ class Renderer
delay(50);
}

int availableMemory()
{
int size = 1024; // Use 2048 with ATmega328
byte *buf;

while ((buf = (byte *) malloc(--size)) == NULL)
;

free(buf);

return size;
}

void printMemory()
{
char text[5];
sprintf(text, "%d", availableMemory());
cursorToXY(0, 0);
LcdWriteString(text);
}
// int availableMemory()
// {
// int size = 1024; // Use 2048 with ATmega328
// byte *buf;

// while ((buf = (byte *) malloc(--size)) == NULL)
// ;

// free(buf);

// return size;
// }

// void printMemory()
// {
// char text[5];
// sprintf(text, "%d", availableMemory());
// cursorToXY(0, 0);
// LcdWriteString(text);
// }


void drawImage(const uint8_t *image, int imageSize, byte posX, byte posY, byte sizeX, byte sizeY, byte shiftBits = 0, bool showImmediately = false)
Expand Down
17 changes: 6 additions & 11 deletions main/Snake.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ class Snake : public Gameplayer
return 250;
}

static unsigned char * getPreviewImg()
{
return snake84x48;
}

int getXPositionFromCellNumber(byte cell_number)
{
return (cell_number % 21) * 4;
Expand Down Expand Up @@ -157,13 +152,13 @@ class Snake : public Gameplayer
void showScore()
{
initNewSnake();
_lcd->cursorToXY(8, 0);
_lcd->LcdWriteString("Your score:");
// _lcd->cursorToXY(8, 0);
// _lcd->LcdWriteString("Your score:");

char text[5];
sprintf(text, "%d", snakeSize);
_lcd->cursorToXY(33, 2);
_lcd->LcdWriteString(text);
// char text[5];
// sprintf(text, "%d", snakeSize);
// _lcd->cursorToXY(33, 2);
// _lcd->LcdWriteString(text);

_lcd->cursorToXY(4, 4);
_lcd->LcdWriteString("press button");
Expand Down
154 changes: 92 additions & 62 deletions main/SpaceInvaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,16 @@ class SpaceInvaders : public Gameplayer
{
Renderer * _lcd;

Particle * objects[2];
// Vector * wind;
Vector * gravity;
Vector * air;
Vector * throast;
Particle * rocket;
Vector * vars;

public:
SpaceInvaders(Renderer *lcd)
{
_lcd = lcd;

for (byte i = 0; i < 2; i++) {
objects[i] = new Particle((i + 1) * 10, 10);
objects[i]->mass = (i + 1) * 10;

}

// wind = new Vector(0, 0);
gravity = new Vector(0, 0.01);
throast = new Vector(0, 0);
rocket = new Particle(20, 10);
rocket->mass = 150;
vars = new Vector(0, 0);
};

void update_inputs()
Expand All @@ -42,56 +32,84 @@ class SpaceInvaders : public Gameplayer

void update_objects()
{

// Vector center = Vector(42, 24);
// rocket->setAccelerationTo(&center, 0.08);

//// gravity ///
vars->x = 0;
vars->y = 0.04;
vars->mult(rocket->mass);
rocket->applyForce(vars); // gravity
/////////////

//// air friction with drag ///
if (rocket->velocity->getMag() != 0) {
vars->x = rocket->velocity->x;
vars->y = rocket->velocity->y;
vars->normalize();
vars->mult(-0.01 * rocket->velocity->getMag() * rocket->velocity->getMag());
rocket->applyForce(vars);
}
//////////////////

for (byte i = 0; i < 2; i++) {
// objects[i]->setAccelerationTo(&center, 0.08);

//// gravity ///
Vector tmpVector = Vector(gravity->x, gravity->y);
tmpVector.y *= objects[i]->mass;
objects[i]->applyForce(&tmpVector); // gravity
objects[i]->applyForce(gravity);
/////////////

//// air friction ///
if (objects[i]->velocity->getMag() != 0) {
tmpVector = Vector(objects[i]->velocity->x, objects[i]->velocity->y);
tmpVector.normalize();
tmpVector.mult(-0.01);
objects[i]->applyForce(&tmpVector);
}
//////////////////
////// throast ///
int degree = map(analogRead(A0), 0, 1024, 2, -2);
if (degree != 0) {
rocket->orientation->rotate(2 * degree);
}

////// throast ///
int degree = -1 * map(analogRead(A0), 0, 1024, -1, 2);
if (degree > 0) {
objects[i]->orientation->rotate(degree);
}
if (digitalRead(BUTTONA) == 0) {
vars->x = rocket->orientation->x;
vars->y = rocket->orientation->y;
vars->normalize();
vars->mult(9);
rocket->applyForce(vars);
}
//////////////////

if (digitalRead(BUTTONA) == 0) {
tmpVector = Vector(objects[i]->orientation->x, objects[i]->orientation->y);
tmpVector.normalize();
tmpVector.mult(0.3);
objects[i]->applyForce(&tmpVector);
}
//////////////////
//// mass
rocket->acceleration->div(rocket->mass);
rocket->velocity->add(rocket->acceleration);

objects[i]->acceleration->div(objects[i]->mass);

objects[i]->velocity->add(objects[i]->acceleration);
//////// COLLISIONS ///////////
if ((rocket->location->x + rocket->velocity->x) > (80)) {
rocket->location->x = 0;
} else if ((rocket->location->x + rocket->velocity->x) < 0) {
rocket->location->x = 80;
}

objects[i]->velocity->limit(1);
if (
(rocket->location->y + rocket->velocity->y) > 40 ||
(rocket->location->y + rocket->velocity->y) <= 1
) {

if (rocket->velocity->y > 0) { // if directed down
rocket->velocity->y *= -0.49; // collisions grab energy
} else {
rocket->velocity->y *= -1;
}

if (sqrt(rocket->velocity->y * rocket->velocity->y) < 0.08 && vars->y == 0) { // treshhold
rocket->velocity->y = 0;
}
rocket->velocity->x -= rocket->velocity->x * 0.04; // ground friction
}

objects[i]->location->add(objects[i]->velocity);
if (sqrt(rocket->velocity->x * rocket->velocity->x) < 0.01) { // treshhold
rocket->velocity->x = 0;
}

objects[i]->acceleration->mult(0);

////////////////////////////

objects[i]->checkBorders();
rocket->velocity->limit(1);
rocket->location->add(rocket->velocity);
rocket->acceleration->mult(0);

// _lcd->printMemory();
}
// rocket->checkBorders();
// _lcd->printMemory();

}


Expand All @@ -101,17 +119,29 @@ class SpaceInvaders : public Gameplayer

void rendering()
{
_lcd->fillDisplayBuffer();

_lcd->fillDisplayBuffer();
for (byte i = 0; i < 2; i++) {
_lcd->drawImage(
rocket4x8, sizeof(rocket4x8),
rocket->location->x,
rocket->location->y,
4, 8, 0);


_lcd->drawImage(
foods4x4, sizeof(foods4x4),
objects[i]->location->x,
objects[i]->location->y,
4, 4, 0);
for (int i = 0; i < 10; i++) {
_lcd->putToBuffer(1, 45 + i, 46, false);
}

vars->x = rocket->orientation->x;
vars->y = rocket->orientation->y;
vars->mult(0.2);
_lcd->putToBuffer(1, 42, 24, false);
vars->mult(20);
_lcd->putToBuffer(1, vars->x + 42, vars->y + 24, false);
vars->mult(2);
_lcd->putToBuffer(1, vars->x + 42, vars->y + 24, false);
vars->mult(1.3);
_lcd->putToBuffer(1, vars->x + 42, vars->y + 24, false);

_lcd->showDisplayBuffer();
}

Expand Down
4 changes: 1 addition & 3 deletions main/gameplayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class Gameplayer
{
Renderer * _lcd;

public:
Gameplayer() {};

Expand All @@ -18,8 +18,6 @@ class Gameplayer
virtual void rendering() = 0;

virtual unsigned int getDelayBetweenFrames() = 0;

static unsigned char * getPreviewImg(){};

};

Expand Down
Loading

0 comments on commit 9088ba3

Please sign in to comment.