Last active
October 28, 2019 22:19
-
-
Save martinschierle/074156c0e285bfe1d2eaf533de040e8f to your computer and use it in GitHub Desktop.
Small game - inspired by the esp8622 'SpyGame' (humans vs. zombies), but newly written for M5Stick, using its display
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFi.h> | |
#include <M5StickC.h> | |
int team = 0; | |
String teamnames[2] = { "Human", "Zombie"}; | |
int zombieTeam = 1; | |
int humanTeam = 0; | |
int colors[2] = { GREEN, RED}; | |
int hideTime = 30; // in seconds | |
int zombieExtrDelay = 10; | |
int playTime = 300; // in seconds | |
long startTime; | |
uint8_t brightness = 80; | |
// from https://support.randomsolutions.nl/827069-Best-dBm-Values-for-Wifi but adapted after tests | |
int MAX_RSSI = -65; | |
int MIN_RSSI = -100; | |
void setup() { | |
Serial.begin(115200); | |
Serial.print("Initing game!"); | |
startTime = millis(); | |
// initialize the M5StickC object | |
M5.begin(); | |
// low display brightness to save power | |
//M5.Lcd.setBrightness(brightness); | |
// if there's no zombie around, we'll be the zombie (first device actviated is zombie, other humans) | |
Serial.print("Searching for existing zombies..."); | |
int numberOfNetworks = WiFi.scanNetworks(); | |
bool zombieFound = false; | |
for(int i = 0; i < numberOfNetworks; i++) { | |
Serial.println(WiFi.SSID(i)); | |
if(WiFi.SSID(i) == teamnames[zombieTeam]) { | |
zombieFound = true; | |
break; | |
} | |
} | |
if(zombieFound) { | |
team = humanTeam; | |
} | |
else { | |
team = zombieTeam; | |
hideTime += zombieExtrDelay; | |
} | |
Serial.print("You are now team:"); | |
Serial.print(teamnames[team]); | |
WiFi.softAP((const char*)teamnames[team].c_str()); | |
IPAddress IP = WiFi.softAPIP(); | |
Serial.println("Wifi established!"); | |
} | |
void loop(){ | |
int activeSeconds = (millis() - startTime)/1000; | |
// hide phase - then print countdown to game start | |
if(activeSeconds < hideTime) { | |
int restTime = hideTime - activeSeconds; | |
Serial.println("We're in 'hide mode'"); | |
M5.Lcd.fillScreen(BLACK); | |
M5.Lcd.setTextColor(colors[team]); | |
if(restTime >= 10) { | |
M5.Lcd.setCursor(20, 50); | |
M5.Lcd.setTextSize(3); | |
} | |
else { | |
M5.Lcd.setCursor(20, 50); | |
M5.Lcd.setTextSize(20); | |
} | |
M5.Lcd.printf("%i", restTime); | |
delay(300); | |
return; | |
} | |
if(activeSeconds < (hideTime + playTime)) { | |
Serial.println("We're in 'play mode'"); | |
// scan for networks | |
int numberOfNetworks = WiFi.scanNetworks(); | |
// find enemy networks and max signal strength for them | |
int maxSignal = 0; | |
String enemyTeam = team == 0 ? teamnames[1] : teamnames[0]; | |
for(int i = 0; i < numberOfNetworks; i++) { | |
Serial.println(WiFi.SSID(i)); | |
if(WiFi.SSID(i) == enemyTeam) { | |
Serial.println("found target network"); | |
int signal = WiFi.RSSI(i); | |
// enforce the limits | |
signal = max(signal, MIN_RSSI); | |
signal = min(signal, MAX_RSSI); | |
// standardize to 0-100 | |
signal = ((signal - MIN_RSSI)*1.0/(MAX_RSSI - MIN_RSSI)) * 100; | |
// make sure it's never really zero, given we found something | |
signal = max(signal, 5); | |
maxSignal = max(signal, maxSignal); | |
} | |
} | |
// if there's nothing close print a ? | |
if(maxSignal == 0) { | |
Serial.println("No enemy close"); | |
M5.Lcd.fillScreen(BLACK); | |
M5.Lcd.setTextColor(colors[team]); | |
M5.Lcd.setCursor(20, 50); | |
M5.Lcd.setTextSize(20); | |
M5.Lcd.printf("?"); | |
} | |
else { | |
// draw a bar for strength of enemies | |
Serial.println("Signal strength: "); | |
Serial.println(maxSignal); | |
M5.Lcd.fillScreen(BLACK); | |
int barheight = 160 * maxSignal/100.0; | |
M5.Lcd.fillRect(0, 160-barheight, 80, barheight, colors[team]); | |
// draw some black lines in between to get the bar pattern | |
for(int i = 1; i < 9; i++) { | |
M5.Lcd.fillRect(0, i*20, 80, 3, BLACK); | |
} | |
} | |
// if a human gets too close to a zombie, he becaomes a zombie too | |
if(team == humanTeam && maxSignal >= 98) { | |
team = zombieTeam; | |
} | |
delay(300); | |
return; | |
} | |
// playtime has ended - print a winner screen for the remaining humans! | |
Serial.println("Game Ended!"); | |
M5.Lcd.fillScreen(BLACK); | |
M5.Lcd.setCursor(4, 40); | |
M5.Lcd.setTextColor(colors[team]); | |
M5.Lcd.setTextSize(2); | |
if(team == zombieTeam) { | |
M5.Lcd.printf("Spiel beendet!"); | |
} | |
else { | |
M5.Lcd.printf("Du hast gewonnen!"); | |
} | |
delay(300); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment