Week 3: Bluetooth(Network Everything):

Our first step to completing this assignment was getting our bluetooth connected. We had a lot of problems doing so. Our first problem was the actual bluetooth modem, which we later switched out.

Here is a video of the arduino side of things working with the values printing out on the serial monitor.

Even though we knew the values were sending through bluetooth, we were kind of confused because suddenly the processing game stopped working and even the circuits started acting strangely.

Eventually, with the help of Scott, we realized that part of our processing code was incorrect. We didn’t need a whole chunk of it, because it turned out to be redundant.

After we deleted the code we needed to, we added several things to the game. First, we changed the background. Then, we added a need ship. Lastly, we added a start screen. The user clicks the ‘a’ key to start the game. Adding these elements was kind of simple, but we did have some structural problems with our code that gave us some trouble. Having to practice this was good for us though, because we had to play with our code for a while. The bluetooth took the longest amount of time to figure and we realized the physical bluetooth connection to arduino was much more stable, but overall, this assignment was a good(and frustrating) challenge.

Here is a video of the final game:

17015384_10211464002536482_1541813188_o

Here is our final code:

import processing.serial.*;
String serial;
String values;
int speed, left, right, up, down;
int sensors[];
//int mysensors[];
Ship myShip; // create a spaceship
Serial port;
int xChange, yChange; // variables for the ship
int increment = 1; // variable to change the rate of movement
int speed2;
Star[] stars; // array of stars
int starNum = 20; // number of stars
PImage img1;
PImage img2;
boolean initialScreen = true;
void setup() {
img1 =loadImage(“Earth_and_Moon.jpg”);
img2= loadImage(“new_rocket.png”);
size(800, 600);
printArray(Serial.list());
port= new Serial(this, Serial.list()[1], 9600);
// port.clear();

port.bufferUntil(‘\n’);

myShip = new Ship(); // instantiate the ship
stars = new Star[starNum]; // new array of stars

for (int x=0; x<starNum; x++) {
stars[x] = new Star(); // instatntiate the stars
}
}
void draw() {
if (initialScreen == true) {
initialScreen();
} else {
the_game();
}

//image(img1, 0, 0, width, height); // clear the background

//if (initialScreen == false) {

// the_game();
//}
//println(speed2,”/”, serial);
}

void the_game() {
// directions
image(img1, 0, 0, width, height);

String words = “use the buttons to move, move potientiometer for ship speed”;
fill(255);
text(words, 10, 30);

// loop through all the stars
for (int x=0; x< starNum; x++) {
stars[x].update(); // update their position
stars[x].collisionCheck(myShip.xPos, myShip.yPos); // check if colliding with the ship
stars[x].render(); // draw the stars
}

myShip.update(xChange, yChange); // update the ship’s position & shield size
myShip.render(); // render the ship

// reset vars if you want
//yChange = 0;
//xChange = 0;

// ************
// change the speed of the ship
// this will be replaced by serial code
// ************
if (keyPressed == true) {
int pressed = key;
if (pressed >= 49 && pressed <=57) {
increment = pressed – 48;
}
}
}

void serialEvent(Serial port) {
if (port.available()>4) {
serial = port.readStringUntil(‘\n’);
print(serial);
if (serial!= null) {
serial= trim(serial);
int mysensors[]=int(split(serial, ‘\t’));
printArray(mysensors);
//int count=mysensors.length;
//for ( int i=0; i<count; i++) {
// sensors[i]= mysensors[i];
// //print(i+”:”+mysensors[i]+”\t”);
//}

speed=mysensors[0];
left=mysensors[1];
right=mysensors[2];
up=mysensors[3];
down=mysensors[4];
//println(speed, left, right);

if (up == 1) {
yChange = increment * -1 *speed/100;
}
if (down == 1) {
yChange = increment *speed/100;
}
if (left == 1) {
xChange = increment * -1 *speed/100;
}
if (right == 1) {
xChange = increment *speed/100;
}
}
}
}

// move the ship
// this will be replaced by serial code
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
yChange = increment * -1;
}
if (keyCode == DOWN) {
yChange = increment;
}
if (keyCode == LEFT) {
xChange = increment * -1;
}
if (keyCode == RIGHT) {
xChange = increment;
}
}
}

void keyReleased() {
if (key ==’a’) {
initialScreen = false;
}
}

//************** Star class
class Star {
float xPos, yPos, starSize, speed; // variables
boolean collision; // check for collision

// star constructor
Star() { // initial state
//fill(random(255), random(255), random(255), random(255));
speed = random(1, 10);
starSize = random(10, 100);
xPos = random(0, width);
yPos = random(100, width) * -1;
collision = false;
}

void update() { // update star position

yPos += speed;

if (yPos > height+starSize/2) {
yPos = random(100, width) * -1;
speed = random(1, 10);
starSize = random(10, 50);
xPos = random(0, width);
}
}

void collisionCheck(int _x, int _y) { // check for a collision

int shipX = _x;
int shipY = _y;

float dx = shipX – xPos;
float dy = shipY – yPos;
float d = sqrt(sq(dx)+sq(dy)); // distance between star and ship

if (d < starSize/2 + 10) { // if distance is less than the radius of the star & ship
collision = !collision; // there’s a crash
}
}

void render() {
// if there’s no collision
if (!collision) {
noStroke();
fill(220, 160, 0);
ellipse(xPos, yPos, starSize, starSize);
} else { // if there is a colliison, supernova
strokeWeight(5);
stroke(255);
fill(220, 100, 0);
ellipse(xPos, yPos, starSize*1.5, starSize*1.5);
collision = !collision; // reset the collison state for the next iteration
}
}
}

//************** Ship class
class Ship {
int xPos;
int yPos;
int shieldSize;

Ship() { // ship constructor
xPos = width/2;
yPos = height-100;
shieldSize = 0;
}

void update(int _xDelta, int _yDelta) {
xPos += _xDelta;
yPos += _yDelta;
}

void render() {
if (yPos > height-10) {
yPos = height-10;
}
if (yPos < height-200) {
yPos = height-200;
}

if (xPos > width-10) {
xPos = width-10;
}
if (xPos < 10) {
xPos = 10;
}

image(img2, xPos, yPos);
}
}

void initialScreen() {
image(img1, 0, 0, width, height);
fill(0);
text(“click A to start”, 400, 400);
}

Leave a Reply