The game we have is currently you would try to get the white dot in between the red dots to clear the level. You would move the board in the direction you wanted the dot to go and you would need to try to control it to perfectly go into the green dot. If you don’t get the dot into the green dot in time the whole board will flash red at you and if you do get it there in time and click the button you will get the whole board to light up green. The goal of the game is to get the white dot into the green dot and when you clear that stage you can decrease the time it takes for you to fail the game in a certain amount of time.The win condition is to try to click the button while your dot is in the green circle and clear the last difficult
Code:
#include <Adafruit_CircuitPlayground.h>
#include <Adafruit_Circuit_Playground.h>
int goalIndex;
int blockLeft;
int blockRight;
unsigned long timer;
unsigned long startTime;
//analog inputs
//potentiometer (A3)
int value;
//button (A2)
int value2;
int speed;
bool lastButton = false;
void setup() {
Serial.begin(9600);
delay(1000);
CircuitPlayground.begin();
randomSeed(analogRead(0));
newGoal();
}
void loop()
{
//inputs
//potentiometer
value = analogRead(A3);
//button
value2 = analogRead(A2);
//speed using potentiometer
if (value <= 200) speed = 1;
if (value > 200 && value <= 300) speed = 2;
if (value > 300 && value <= 400) speed = 3;
if (value > 400 && value <= 500) speed = 4;
if (value > 500 && value <= 600) speed = 5;
if (value > 600 && value <= 700) speed = 6;
if (value > 700 && value <= 800) speed = 7;
if (value > 800 && value <= 900) speed = 8;
if (value > 900) speed = 9;
//timer
timer = speed * 800;
//tilting
float x = CircuitPlayground.motionX();
float y = CircuitPlayground.motionY();
float rad = atan2(y, x);
//led things
int playerLED = mapfloat(rad, -PI, PI, 0, 10);
playerLED = playerLED - 2;
if (playerLED < 0) playerLED += 10;
if (playerLED > 9) playerLED = 9;
//Leds
CircuitPlayground.clearPixels();
CircuitPlayground.setPixelColor(blockLeft, 255, 0, 0);
CircuitPlayground.setPixelColor(blockRight, 255, 0, 0);
CircuitPlayground.setPixelColor(goalIndex, 0, 255, 0);
CircuitPlayground.setPixelColor(playerLED, 80, 80, 80);
//buttons
float buttonScaled = mapfloat(value2, 0, 1023, 0.0, 1.0);
bool buttonNow = (buttonScaled > 0.1);
if (buttonNow && !lastButton)
{
if (playerLED == goalIndex)
{
winFlash();
newGoal();
}
else if (playerLED == blockLeft || playerLED == blockRight)
{
loseFlash();
newGoal();
}
}
lastButton = buttonNow;
//timer
if (millis() - startTime >= timer)
{
loseFlash();
newGoal();
}
delay(20);
}
//sets goals
void newGoal()
{
goalIndex = random(1, 9);
blockLeft = (goalIndex + 9) % 10;
blockRight = (goalIndex + 1) % 10;
startTime = millis();
}
//flash led winning
void winFlash() {
for (int i = 0; i < 3; i++)
{
CircuitPlayground.clearPixels();
for (int j = 0; j < 10; j++)
{
CircuitPlayground.setPixelColor(j, 0, 255, 0);
}
delay(200);
CircuitPlayground.clearPixels();
delay(200);
}
}
//flash led losing
void loseFlash() {
for (int i = 0; i < 3; i++)
{
CircuitPlayground.clearPixels();
for (int j = 0; j < 10; j++)
{
CircuitPlayground.setPixelColor(j, 255, 0, 0);
}
delay(200);
CircuitPlayground.clearPixels();
delay(200);
}
}
//math from teachers vid
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
float result = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
return result;
}
Video:
https://youtube.com/shorts/3bcjK8LFgwA?feature=share




No comments:
Post a Comment
Note: Only a member of this blog may post a comment.