I think this week’s assignment adequately embodies the Of Mice and Men quote: “best laid plans o’mice and men” often go awry. I had a much better idea but it really fell apart. So now I’m saving that one for a rainy day presenting a lamer substitute for this week’s assignment.
Basically, the player of the game is a (supposed to be) constantly-moving, never-tiring person who runs around the screen. The potentiometer input is supposed to dictate the angle of movement and the goal of the game is to just run around the screen, avoiding the 5 rectangle obstacles and the edges.
The potentiometer input manages to be processed by my Processing program, but there are some other problems that I wasn’t able to fix in time.
Here’s my main code:
import processing.serial.*; Serial myPort; boolean main_game; int reading; float angle; Person self; PFont wound; int wound_count; PShape starry; Obstacles [] rectangle = new Obstacles[5]; ArrayList<Star>counting_star; void setup() { size(800, 750, P2D); self = new Person(400, 700, 1); wound = loadFont("AmericanTypewriter-CondensedBold-32.vlw"); wound_count = 0; counting_star = new ArrayList<Star>(); main_game = true; //Arduino --> Processing: println(Serial.list()); String portName = Serial.list()[0]; myPort = new Serial(this, "/dev/tty.usbmodem1411", 9600); myPort.readStringUntil('\n'); //For creating a star: PShape starry = createShape(); starry.beginShape(); // You can set fill and stroke starry.fill(255, 255, 0); starry.stroke(255, 255, 0); // Here, we are hardcoding a series of vertices starry.vertex(0, -50); starry.vertex(14, -20); starry.vertex(47, -15); starry.vertex(23, 7); starry.vertex(29, 40); starry.vertex(0, 25); starry.vertex(-29, 40); starry.vertex(-23, 7); starry.vertex(-47, -15); starry.vertex(-14, -20); starry.endShape(CLOSE); for (int i=0;i<rectangle.length;i++) { rectangle[i] = new Obstacles((int)random(0, width-100), (int)random(0, height-100)); } for (int i = 0; i < 0; i++) { counting_star.add(new Star(starry)); } } // End of SETUP void draw() { if (main_game) { background (0, 145, 145); //Obstacles: for (int i=0;i<rectangle.length;i++) { rectangle[i].drawRect(); } //Star: for (int i = 0; i < counting_star.size(); i++) { counting_star.get(i).drawStar(); } //Person: self.changeDirectionAngle(); self.set_theta(angle); self.movePerson(); //self.rotate_person(); if (self.checkBoundaries()) { self.changeColor(); //self.set_random_move(); self.bounce(); textFont(wound, 48); text("Ouch!", 350, 50); wound_count++; counting_star.add(new Star(starry)); } // for (int i=0;i<rectangle.length; i++) { if (self.check_Collision(rectangle[i])) { wound_count++; counting_star.add(new Star(starry)); } // } } if (wound_count==6) { main game = false; background(255, 0, 0); textFont(wound, 48); text("You've died!", 350, 50); } } // End of DRAW void serialEvent(Serial _port) { //this is a callback function if (myPort == null) return; //this is a hack to cover a bug where the port does not get set up in time. //this says if the port is not set up yet, bail (for now.) String input = myPort.readStringUntil('\n'); if (input != null) { //if a '\n' character has in fact now arrived input = input.trim(); //Take off the '\n' character reading = int(input); //Turn it into number System.out.println("Here is regular number"+reading); angle = map(reading, 0, 1000, 0, 360); println("Here is angle"+angle); } }
Obstacles class:
class Obstacles { int x; int y; int r = (int)random(0,255); int b =(int)random(0,255); int g = (int)random(0,255); int obstacle_width = (int)(random(50,150)); int obstacle_height = (int)(random(50,150)); //Constructor Obstacles(int _x, int _y){ x = _x; y = _y; } void drawRect(){ stroke(r, b, g); fill(r, b, g); rect(x, y, obstacle_width, obstacle_height); } int get_X_Position(){ return x; } int get_Y_Position(){ return Y; } int get_Width(){ return obstacle_width; } int get_Height(){ return obstacle_height; } //void check_Collision(){ }
Person class:
class Person{ int pos_X; int pos_Y; int person_width = 0; int person_height =0; float size; //Coloring head: int r = 255; int b = 255; int g = 255; //MOVEMENT: float velocity_X =3; float velocity_Y =3; float theta; //potentiometer input //Constructor Person(int x, int y, float _size){ pos_X = x; pos_Y = y; size = _size; } void drawPerson() { line( pos_X+20*size, pos_Y, pos_X+20*size, pos_Y+70*size) ; // Body line( pos_X+20*size, pos_Y+70*size, pos_X, pos_Y+130*size) ; //Left leg line( pos_X+20*size, pos_Y+70*size, pos_X+40*size, pos_Y+130*size) ; // right leg line( pos_X+20*size, pos_Y+30*size, pos_X, pos_Y+70*size) ; // left arm line( pos_X+20*size, pos_Y+30*size, pos_X+40*size, pos_Y+70*size) ; // right arm fill(r,b,g) ; // grey for head fill ellipse(pos_X+20*size, pos_Y, 40*size, 35*size) ; // head person_width = (int)(40*size); person_height = (int)((65*size) + (65*size) + (35*size)); } void movePerson(){ pos_X += velocity_X; pos_Y += velocity_Y; this.drawPerson(); } void changeColor(){ r = (int)random(0, 255); b = (int)random(0, 255); g = (int)random (0, 255); } boolean checkBoundaries() { boolean out_of_bounds = false; //Out of screen (horizontal) if (pos_X+(40*size) > width || pos_X < 0) { out_of_bounds = true; } //Out of screen (vertical) if (pos_Y + person_height >height ||pos_Y <0){ out_of_bounds = true; } return out_of_bounds; } void bounce(){ if (pos_X + person_width >width || pos_X < 0) { velocity_X *= -1; } //Out of screen (vertical) if (pos_Y + person_height > height ||pos_Y <0 ) { velocity_Y *= -1; } } void set_random_move(){ velocity_X = (int)random(1,5); velocity_Y = (int)random(1,5); } void set_theta(float _theta){ theta = radians(_theta); System.out.println("here is theta"+theta); } void changeDirectionAngle() { //"Rotate" the face's velocities with given angle: velocity_X = cos(theta); velocity_Y = sin(theta); } //COLLISION boolean check_Collision(Obstacles temporary) { boolean signal = false; int temp_x = temporary.get_X_Position(); int temp_y = temporary.get_Y_Position(); int temp_height = temporary.get_Height(); int temp_width = temporary.get_Width(); if (pos_X < (temp_x + temp_width) && (x+person_width) > (temp_x )) { if (pos_Y < (temp_y + temp_height) && (y+person_height) > (temp_y )) { signal = true; } else { signal = false; } } return signal; } /* void rotate_person(){ pushMatrix(); rotate(theta); this.movePerson(); popMatrix(); } */ }//end of person class
Star class:
class Star{ PShape star; int x= (int)random(0,width); int y = (int)random(0, height); Star( PShape create_star){ star = create_star; } void drawStar(){ shape(star, x,y); } }
So the problems I’m having:
– The bounce function is ignored somehow
– The potentiometer angle isn’t processed completely as I hoped and the movement is jerky. Sometimes the person doesn’t move at all (which may be due to a mathematical computation but I’m very bewildered by this).