Concept:
I tried to create a first person shooter game with falling blocks that would result in a point when shot .
What went well :
I learned how to use OOP more effectively for both the falling blocks and bullets. I successfully attached a hardware component
What didn’t:
Games are not my strong suit as I couldn’t find a way to make collision detection work in this case.
Took a lot of code to write but couldn’t finish the collision detection.
How to improve:
Either choose a game that doesn’t involve collision detection or work on collision detection first.
import processing.serial.*;
Serial myPort;
int xPos=0;
//code for player and bullet borrowed and modified from
//https://forum.processing.org/two/discussion/26683/trying-to-get-bullets-to-shoot-with-void-mousepressed-but-nothing-happens
final float SPEED = 2;
final float BULLET_SPEED = 4;
final int ellipse_WIDTH = 50;
final int ellipse_HEIGHT = 50;
int centerX, centerY; //width/2; //height/2;
float posX, posY;
float ellipsePos1X, ellipsePos2X, ellipsePos3X;
float ellipsePos3Y;
float bulletDiam = 10;
float bulletX, bulletY;
float x, y;
void setup() {
size(500, 500);
printArray(Serial.list());
String portname=Serial.list()[3];
println(portname);
myPort = new Serial(this, portname, 9600);
centerX = width/2;
centerY = height/2;
posX = centerX - ellipse_WIDTH/2;
posY = height - ellipse_HEIGHT;
rainFactory(numberOfRainDrops);
}
void draw() {
background(200);
drawGun();
moveShot();
drawShot();
drawRain();
}
void drawGun() {
ellipsePos1X = posX;
ellipsePos2X = posX + ellipse_WIDTH;
ellipsePos3X = posX + ellipse_WIDTH/2;
ellipsePos3Y = posY - ellipse_WIDTH;
fill(255);
//ellipse(posX+ellipse_WIDTH/2, posY +ellipse_HEIGHT/2, ellipse_WIDTH, ellipse_HEIGHT);
ellipse(xPos, posY +ellipse_HEIGHT/2, ellipse_WIDTH, ellipse_HEIGHT);
}
void mousePressed() {
bulletY = posY;
bulletX = ellipsePos3X;
}
void drawShot() {
fill(255, 0, 0);
rect (xPos, bulletY, bulletDiam, bulletDiam);
}
void moveShot() {
bulletY = bulletY - BULLET_SPEED;
}
void serialEvent(Serial myPort) {
xPos=myPort.read();
}
//___ new tab
int numberOfRainDrops = 10;
Rain[] rainDrops;
void drawRain() {
for (int i=0; i<rainDrops.length; i++) {
rainDrops[i].draw();
rainDrops[i].update();
};
};
void rainFactory(int quantity) {
rainDrops = new Rain[quantity];
for (int i = 0; i < quantity; i++) {
float x = random(0, width);
float y = random(0, height);
float w = 20;
float h = 40;
float ySpd= random(2);
Rain newRain = new Rain(x, y, w, h, ySpd);
rainDrops[i] = newRain;
};
return;
}
class Rain {
float width_, height_;
float xPos, yPos, ySpeed;
//color myColor = color(0);
Rain(float x, float y, float w, float h, float ySpd) {
width_ = w;
height_ = h;
xPos = x;
yPos = y;
ySpeed = ySpd;
}
void draw() {
fill(255);
rect(xPos, yPos, width_, height_);
}
void update() {
yPos += ySpeed;
if (yPos > height) {
fill(0);
yPos= 0;
}
}
}
/*Arduino code
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int sensor = analogRead(A0);
delay(1);
sensor/=4;
Serial.write(sensor);
}
*/
video of work in progress:
Hi Alia, please share a link or video that is viewable. This one requires permissions.
Sorry, I had trouble uploading it to youtube. I will try that again.
Hi Aaron, uploaded the video on YouTube, the link is there now. 🙂