In this weeks recitation, we are split into different workshops to work on what we think is more helpful to our final projects. Since I am planning on making a game, I decided to join the object oriented programming workshop. We were then tasked of writing our own object using the skills we have learned from the workshop.
I decided to create a target class for my shooter game, and started of by setting the fields to x and y position. I then added display and move function for that class, one for updating the position, and the other for drawing the object on to the screen. I also downloaded a sprite image for the target to make it look like a game character.
I also created an array list to be able to create a lot of these targets, but rather than using a for loop to create all of them at once, I decided to add a new target object to the list once every few seconds. After that I made the constructor function of a new target be random, y position and speed and direction. They will also appear on the other side of the screen if they walk out of the screen. The last thing I did was to make them disappear when they are clicked, so I checked mouse position and whether they are within the bounds of the objects size, and delete them from the array list if they are clicked. I also took a crosshair image to make it look more like a shooter game.
I learned a lot this recitation and I really like how it is structured so that we can brush up on the specific subject that we need to work on, and it is also directly related to our final project, which gave me a great starting code to work off of for my project as I got to figure out a lot of OOP syntax problems with the help of fellows.
Code below:
class target{
int x;
int y;
int type;
int speed;
int size;
PImage img;
target(int _type){
type = _type;
y = int(random(550)); //whatever height and width is
img = loadImage(“pacman.png”);
speed = int(random(-4,3));
size = int(random(40,100));
if (speed != -1){
speed+=1;
}
if (speed > 0){
x = -20;
}
else{
x = width+20;
}
}
target(int _x,int _y,int _type){
x = _x;
y = _y;
type = _type;
img = loadImage(“bow1.png”);
}
void display(){
image(img,x,y,size,size);
}
void move(){
x += speed;
if (x-(size/2)>= width && speed >0){
x=-20;
}
else if(x+(size)<=0 && speed <0){
x=width+20;
}
}
}