Klausj
Published © GPL3+

Constructing a Game using the Plug-and-Make Kit

A world of electronic games can be realized using Arduinos. This time I tried to construct a simple game using the new Plug-and-Make-Kit.

AdvancedFull instructions provided1 hour102
Constructing a Game using the Plug-and-Make Kit

Things used in this project

Hardware components

Plug-and-Make kit
×1

Story

Read more

Code

7-leds_no-keys_WS2812_R4.ino

Arduino
7-buttons-7-LEDs-game
/*
  Version with simulated player
  R4 Plug and Make Kit
  place jumper in pins-6-7 to disable delays
*/

#include <Modulino.h>
ModulinoPixels strip;

const byte NUM = 7;
const byte mask = (1 << NUM) - 1;
byte state = 0;
int count;
int df; // disable delays

void setup() {
  Serial.begin(9600);
  Serial.println(__FILE__);
  Modulino.begin();
  strip.begin();
  // Parameter: Index der LED, Farbe, Helligkeit
  // strip.set(0, colorBlue, HELLIGKEIT);
  pinMode(6, OUTPUT);
  pinMode(7, INPUT_PULLUP);
  df = digitalRead(7);
  newGame();
}

void loop() {
  df = digitalRead(7);
  do {
    count++;
    byte k = getKey();
    blink(k); // this key has been pressed
    bitToggle3(k); // perform one by one
    delay(1000 * df);
  }
  while (state != mask);
  newGame();
}

byte getKey() {
  static byte lastKey;
  byte k;
  // don't repeat yourself:
  do k = random(NUM);
  while (k == lastKey);
  lastKey = k;
  if (df == 1) {
    Serial.print("key = ");
    Serial.println(k);
  }
  return k;
}

void newGame() {
  // success
  if (df == 1) {
      rainbow();
      delay(100);
      Serial.print("count = ");
    }
  Serial.println(count);
  delay(1000 * df);
  state = random(mask);
  count = 0;
  update(-1); // -1 = no fading
}

void blink(byte j) {
  byte b = bitRead(state, j);
  byte red = 255 * j;
  for (byte i = 0; i < 10; i++) {
    // last i must be odd
    byte green;
    if (i & 1) green = 0; else green = 255;
    strip.set(j, ModulinoColor(red, green, 1));
    strip.show();
    delay(100 * df);
  }
}

void bitToggle3(byte b) {
  byte a = b > 0 ? b - 1 : NUM - 1;
  byte c = b < NUM - 1 ? b + 1 : 0;
  update(b);
  update(a);
  update(c);
}

void update(byte k) {
  // fade changing led up or down
  int b = bitRead(state, k);
  float fade = 1 + b * 254; // start value
  float factor = 1.25 - b * 0.5; // speed
  do {
    for (byte i = 0; i < NUM; i++) {
      byte red = 0; // red or off
      if (i == k) red = fade;
      else if (bitRead(state, i)) red = 255;
      strip.set(i, ModulinoColor(red, 0, 1));
    }
    strip.show();
    delay(50 * df);
    fade = min(fade * factor, 255);
  }
  while ((fade > 0.5) && (fade < 255));
  delay(300 * df);
  bitToggle(state, k);
}

void rainbow() {
  const int A = 2 * 128;
  const int B = 3 * 128;
  const int C = 4 * 128;
  const long D = 6 * 128; // long verhindert Ueberlauf
  for (int i = 0; i < 10; i++) {
    for (int pixno = 0; pixno < NUM; pixno++) {
      int x = ((pixno + i) * D / NUM) % D;
      // constrain beschraenkt auf Bereich zwischen 0 und 255
      int r = constrain(abs(x - B) - 128, 0, 255);
      int g = constrain(256 - abs(x - A), 0, 255);
      int b = constrain(256 - abs(x - C), 0, 255);
      strip.set(pixno, ModulinoColor(r, g, b));
      strip.show();
    }
    delay(200);
  }
}

Credits

Klausj
83 projects • 7 followers
Contact

Comments

Please log in or sign up to comment.