Hello, so i am making a game, and there are two main objects, there is you, and the obstacle.
i want to crate a new obstacle, when the previous one gets to a certain space,
Here is the code for the obstacle
PVector locat2 = new PVector(1000, 500);
PVector velocityObst;
PVector gravity2;
class Cactus
{
{
gravity2 = new PVector(0, 0.2);
velocityObst = new PVector(-5, 0);
velocityObst.add(gravity2);
locat2.add(velocityObst);
if (locat2.y > 500) {
locat2.y = 499;
}
stroke(255);
strokeWeight(2);
fill(0);
rect(locat2.x, locat2.y, 39, 56);
}
}
and here is the code for the main character:
PVector location;
PVector velocity;
PVector gravity;
PVector upforce;
void setup() {
size(1100, 600);
location = new PVector(50, 500);
velocity = new PVector(0, 0);
gravity = new PVector(0,0.2);
upforce = new PVector(0, -5.4);
}
void jump() {
location.add(upforce);
}
void draw() {
background(255);
new Cactus();
location.add(velocity);
velocity.add(gravity);
if ((location.x > width) || (location.x < 0)) {
velocity.x = 0;
}
if (location.y > 500) {
velocity.y = 0;
}
stroke(255);
strokeWeight(2);
fill(127);
rect(location.x, location.y, 39, 56);
if (keyPressed) {
if (key == ' ') {
jump();
}
} else {
}
}
1 Like
Hey There!
Rather than add one you can just return the one which just went off screen back to the start or something.
Here’s the code.
PVector locat2 = new PVector(1000, 500);
PVector velocityObst;
PVector gravity2;
class Cactus
{
{
gravity2 = new PVector(0, 0.2);
velocityObst = new PVector(-5, 0);
// velocityObst.add(gravity2);
locat2.add(velocityObst);
if (locat2.x < 0) {
locat2.x = width;
}
stroke(255);
strokeWeight(2);
fill(0);
rect(locat2.x, locat2.y, 39, 56);
}
}
PVector location;
PVector velocity;
PVector gravity;
PVector upforce;
void setup() {
size(1100, 600);
location = new PVector(50, 500);
velocity = new PVector(0, 0);
gravity = new PVector(0, 0.2);
upforce = new PVector(0, -5.4);
}
void jump() {
location.add(upforce);
}
void draw() {
background(255);
new Cactus();
location.add(velocity);
velocity.add(gravity);
if ((location.x > width) || (location.x < 0)) {
velocity.x = 0;
}
if (location.y > 500) {
velocity.y = 0;
}
stroke(255);
strokeWeight(2);
fill(127);
rect(location.x, location.y, 39, 56);
if (keyPressed) {
if (key == ' ') {
jump();
}
} else {
}
}
So where in the cactus you have if it’s position is < than 0 return back to width where it is the start of the screen on the right.
( Also commented out the enemies velocity as this causes it to go off screen but you do whatever you like )
1 Like
sorry i have not responded, i was really caught up with school, visiting relatives, and trying to fix my desktop, which i might have accidentally reformated.
Yes, but here is the problem, i want to be able to have multiple of them on the screen at once
ArrayList<Cactus> cList = new ArrayList();
void setup() {
size(1100, 600);
for (int i=0; i<6; i++) {
Cactus c = new Cactus();
c.locat2 = new PVector(random(width), random(height));
c.velocity = new PVector(0, 0);
c.gravity = new PVector(0, 0.2);
c.upforce = new PVector(0, -5.4);
cList.add(c);
}
}
void draw() {
background(255);
for (Cactus c : cList) {
c.run();
}
if (keyPressed) {
if (key == ' ') {
//jump();
}
} else {
}
}
//===================================================================
class Cactus {
PVector locat2 = new PVector(1000, 500);
PVector velocityObst;
PVector gravity2;
// PVector location;
PVector velocity;
PVector gravity;
PVector upforce;
Cactus() {
gravity2 = new PVector(0, 0.2);
velocityObst = new PVector(-5, 0);
// velocityObst.add(gravity2);
}
void run() {
//location.add(velocity);
//velocity.add(gravity);
if ((locat2.x > width) || (locat2.x < 0)) {
velocity.x = 0;
}
if (locat2.y > 500) {
velocity.y = 0;
}
locat2.add(velocityObst);
if (locat2.x < 0) {
locat2.x = width;
}
stroke(255);
strokeWeight(2);
fill(0);
rect(locat2.x, locat2.y, 39, 56);
}
void jump() {
//c.location.add(c.upforce);
}
}
//===================================================================
2 Likes