xxxxxxxxxx
257
let spritesheet;
let gameState =0;
let prevTime = 0;
let horses = [];
let rval = 0;
let step = 0;
let bg = [];
let right = false;
let left = false;
let serial;
let xPos;
let prevVal = 0;
let direction;
let prevDirection = 1;
let baseReading = 120;
let w;
function preload() {
myFont = loadFont("Comfortaa-VariableFont_wght.ttf");
spritesheet = loadImage("horse_final_run.png");
soundBG = loadSound("tugan_zher.mp3");
menuBG = loadImage("menu1.png");
instructions=loadImage("instructions.png");
goBack = loadImage("back.png");
inst = loadImage("inst.jpg");
for (let i = 1; i < 21; i++) {
bg.push(loadImage(str(i) + "bg.jpg"));
}
}
function setup() {
createCanvas(800, 580);
soundBG.loop();
soundBG.setVolume(0.5);
w = spritesheet.width;
let h = spritesheet.height / 7;
for (let y = 0; y < 7; y++) {
for (let x = 0; x < 1; x++) {
horses.push(spritesheet.get((x * w) / 7, y * h, w, h));
}
}
xPos = -w/2;
setUpSerial();
setInterval(() => {step = (step + 1) % 7}, 75);
}
function keyPressed() {
if (key == " ") {
setUpSerial(SELECT_PORT);
}
}
function draw() {
background(menuBG);
textFont(myFont);
textSize(40);
text("Across Kazakhstan", width/2-170, height/2+23);
textSize(15);
text("click here to start", width/2-50, height/2+45);
fill(255, 255, 204);
image(instructions, 670, 480, 80, 80);
// background(bg[int(map(rval - 1, 0, 1023, 0, 20))]);
if (gameState ==1){
background(bg[int(map(rval - 1, 0, 1023, 0, 20))]);
image(goBack, 690, 500, 60,60);
let currentTime = millis() % 75;
if (currentTime < prevTime) {
step = (step + 1) % 7;
}
prevTime = currentTime;
push();
imageMode(CENTER);
translate(xPos, 420);
if (left) scale(-1, 1);
image(horses[step], 0, 0);
pop();
if (direction == null) {
// print("waiting for sensor...");
} else {
if (direction != prevDirection){
if (direction == 0) { // moving left
left = true;
right = false;
xPos = width + w/2;
} else if (direction == 1) { // moving right
left = false;
right = true;
xPos = -w/2;
}
}
prevDirection = direction;
if (right && xPos < width+ w/2) {
xPos += 10 ;
}
if (left && xPos > -w/2) {
xPos -= 10;
}
}
}
if (gameState == 2){
background((inst), width, height);
textSize(30);
fill(0);
text("wave your hand", width/2-350, height/2-130);
text ("over the grass",width/2-350, height/2-100);
image(goBack, 690, 500, 60,60);
}
}
function mouseClicked(){
if (gameState == 0) {
if (mouseX > 150 && mouseX < 650 && mouseY > 150 && mouseY < 480) {
// start();
gameState = 1;
}
if (mouseX > 670 && mouseX < 750 && mouseY > 400 && mouseY < 600) {
gameState = 2;
}
}
if (gameState == 1 || gameState==2){
if (mouseX > 670 && mouseX < 750 && mouseY > 450 && mouseY < 530){
gameState=0;
}
}
}
function readSerial(data) {
////////////////////////////////////
//READ FROM ARDUINO HERE
////////////////////////////////////
if (data != null) {
// make sure there is actually a message
// split the message
let fromArduino = split(trim(data), ",");
// if the right length, then proceed
if (fromArduino.length == 2) {
direction = fromArduino[0];
rval = fromArduino[1];
}
//////////////////////////////////
//SEND TO ARDUINO HERE (handshake)
//////////////////////////////////
let sendToArduino = "\n";
writeSerial(sendToArduino);
}
}
/*
#include <Wire.h>
#include <ZX_Sensor.h>
#include <FastLED.h>
#define LED_PIN 5
#define NUM_LEDS 35
#define BRIGHTNESS 64
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
int currentVal = 0;
const int ZX_ADDR = 0x10; // ZX Sensor I2C address
// Global Variables
ZX_Sensor zx_sensor = ZX_Sensor(ZX_ADDR);
uint8_t x_pos;
uint8_t z_pos;
CRGBPalette16 currentPalette;
GestureType gesture;
void setup() {
delay( 3000 ); // power-up safety delay
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness( BRIGHTNESS );
// Initialize Serial port
Serial.begin(9600);
zx_sensor.init();
while (Serial.available() <= 0) {
Serial.println("0,0");
delay(300);
}
}
void loop()
{
FastLED.show();
if ( zx_sensor.gestureAvailable() ) {
gesture = zx_sensor.readGesture();
switch ( gesture ) {
case RIGHT_SWIPE:
currentPalette[0] = CRGB(255, 255, 0);
x_pos = 0;
break;
case LEFT_SWIPE:
x_pos = 1;
currentPalette[0] = CRGB(0, 255, 0);
break;
default:
break;
}
}
for (int i = 0; i < NUM_LEDS ; i++) {
// let's set an led value
leds[i] = currentPalette[0];//CHSV(hue++, 255, 255);
}
while (Serial.available()) {
byte inComing = Serial.read();
if (inComing == '\n') {
int sensor = analogRead(A0);
delay(1);
Serial.print(x_pos);
Serial.print(',');
Serial.println(sensor);
}
}
}
*/