Skip to content

Instantly share code, notes, and snippets.

@iw4rr10r iw4rr10r/timeline.ino Secret
Last active Mar 31, 2019

Embed
What would you like to do?
Ça s'est passé quand ?
/*
* ---------------------------------------------------------
* Croquis de démonstration
* ---------------------------------------------------------
* Demande d'assistance de gregvsmj
* https://gamebuino.com/fr/creations/timeline
* Date : 31 mars 2019
* ---------------------------------------------------------
*/
#include <Gamebuino-Meta.h>
// ---------------------------------------------------------
// Définition des constantes globales
// ---------------------------------------------------------
const uint8_t SCREEN_WIDTH = 80;
const uint8_t SCREEN_HEIGHT = 64;
const uint8_t TIMELINE_WIDTH = SCREEN_WIDTH - 4;
const uint8_t NB_EVENTS = 15;
const uint8_t GAME_MODE_START = 0;
const uint8_t GAME_MODE_PLAY = 1;
const uint8_t GAME_MODE_WON = 2;
const uint8_t GAME_MODE_LOST = 3;
const Color color[4] = {
BLACK, // GAME_MODE_START
LIGHTBLUE, // GAME_MODE_PLAY
LIGHTGREEN, // GAME_MODE_WON
RED // GAME_MODE_LOST
};
// ---------------------------------------------------------
// Définition des structures de donnée
// ---------------------------------------------------------
struct Event
{
uint16_t year;
String name;
bool status;
};
struct Game
{
uint8_t eventIndex;
uint8_t cursor;
uint8_t score;
uint8_t mode;
};
// ---------------------------------------------------------
// Initialisation des structures de donnée
// ---------------------------------------------------------
Event timeline[NB_EVENTS] = {
{1912, "Naufrage du Titanic", false},
{1927, "Travers. Atlantique", false},
{1929, "Le jeudi noir", false},
{1933, "Le film King kong", false},
{1940, "Appel du 18 juin", false},
{1944, "Debarque. Normandie", false},
{1945, "Hiroshima", false},
{1963, "Assassinat Kennedy", false},
{1969, "Premier pas Lune", false},
{1981, "Abol. peine de mort", false},
{1989, "Chute mur de Berlin", false},
{1990, "Liberation Mandela", false},
{1991, "Guerre du Golfe", false},
{1997, "Kasparov / Deep Blue", false},
{1999, "Naissance de l'euro", false}
};
Game game = {
0, // eventIndex
0, // cursor
0, // score
GAME_MODE_START // mode
};
// ---------------------------------------------------------
// Initialisation de la META
// ---------------------------------------------------------
void setup() {
gb.begin();
}
// ---------------------------------------------------------
// Boucle d'exécution
// ---------------------------------------------------------
void loop() {
gb.waitForUpdate();
switch (game.mode) {
case GAME_MODE_START:
initGame();
break;
case GAME_MODE_PLAY:
checkButtons();
break;
}
draw();
}
// ---------------------------------------------------------
// Réinitialisation du jeu
// ---------------------------------------------------------
void initGame() {
game.eventIndex = random(NB_EVENTS);
game.cursor = .5 * NB_EVENTS;
game.mode = GAME_MODE_PLAY;
initTimeline();
}
void initTimeline() {
uint8_t i;
for (i=0; i<NB_EVENTS; i++) {
timeline[i].status = false;
}
}
// ---------------------------------------------------------
// Gestion des boutons
// ---------------------------------------------------------
void checkButtons() {
if (gb.buttons.pressed(BUTTON_LEFT)) {
left();
} else if (gb.buttons.pressed(BUTTON_RIGHT)) {
right();
}
}
// ---------------------------------------------------------
// Gestion des commandes du joueur
// ---------------------------------------------------------
void left() {
if (game.cursor > 0) {
game.cursor--;
}
}
void right() {
if (game.cursor < NB_EVENTS - 1) {
game.cursor++;
}
}
// ---------------------------------------------------------
// Gestion de l'affichage
// ---------------------------------------------------------
void draw() {
gb.display.clear();
drawTimeline();
drawYear();
drawName();
drawLED();
}
void drawTimeline() {
uint8_t nb = NB_EVENTS - 1;
uint8_t step = TIMELINE_WIDTH / nb;
uint8_t w = step * nb;
uint8_t xo = .5 * (SCREEN_WIDTH - w);
uint8_t y = .5 * SCREEN_HEIGHT;
uint8_t x,i;
gb.display.setColor(GRAY);
gb.display.drawFastHLine(xo, y, w);
for (i=0; i<NB_EVENTS; i++) {
x = xo + i * step;
gb.display.drawFastVLine(x, y-1, 3);
}
gb.display.setColor(BEIGE);
gb.display.drawRect(xo + game.cursor*step - 1, y-2, 3, 5);
}
void drawYear() {
gb.display.setColor(color[game.mode]);
gb.display.setFontSize(2);
gb.display.print(timeline[game.cursor].year);
}
void drawName() {
String name = timeline[game.eventIndex].name;
uint8_t n = name.length();
gb.display.setColor(color[game.mode]);
gb.display.setFontSize(1);
gb.display.setCursor(.5*(SCREEN_WIDTH - 4*n), .5*SCREEN_HEIGHT + 10);
gb.display.print(name);
}
void drawLED() {
gb.lights.fill(color[game.mode]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.