#New to Processing

1517 messages ยท Page 2 of 2 (latest)

stiff niche
#

so now

analog garnet
#

Since I already showed you the other half, I didn't include it

stiff niche
#

we gotta make sure it dont move off to the screen

#

I honestly have no idea how to do that

#

Do we have to do another method/function

#

for that ?

#

oh the 4 connors think

#

i remember u said that like last time

analog garnet
# stiff niche we gotta make sure it dont move off to the screen

For that, we can do this: ```pde

class Player {
int id; // player number (1, 2, etc)
int x, y; // position

Player(int number) {
id = number;

resetPosition();

}

void resetPosition() {
if (id == 1) {
x = 10;
y = 10;
}
else {
x = width - robberImage.width - 10;
y = height - robberImage.height - 10;
}
}

void move() {
if (id == 1) {
if (keys[2]) // A
x = x - 5;
else if (keys[3]) // D
x = x + 5;

  if (x > width - policeImage.width)
    x = width - policeImage.width;
}
else {
  if (keys[6])      // LEFT
    x = x - 5;
  else if (keys[7]) // RIGHT
    x = x + 5;
  
  if (x > width - robberImage.width)
    x = width - robberImage.width;
}

if (x < 0)
  x = 0;

}
}

stiff niche
#

This is the entire class?

analog garnet
#

yes

stiff niche
#

hol up lemme fix my set up rq

#

PImage

#

oops

#

Oh god idk what i have done lol

#

something aint workin

analog garnet
#

I think you named your images differently than me

stiff niche
#

what this mean ThisDot_anguished

analog garnet
#

Can you show me the line it highlighted?

stiff niche
#

it doesnt show any line

analog garnet
#

okay, can you show me your full sketch file?

stiff niche
#

k

#

should i put in a zip file instead

#

might be alot

analog garnet
#

its fine

#

By the way, I usually create a separate page for classes. you can do it here

stiff niche
#

oh

#

for some reasn

#

its still not wroking

#

not sure what the reason is

#

ur is working fine?

analog garnet
#

Oh, I know what's wrong

#

So because width is a property of the images, we can't use it until they're fully set up, so we have to create the players inside setup() ```pde
Player playerOne;
Player playerTwo;

void setup() {
size(800,600);
bkg = loadImage("city.jpg");
policeImage = loadImage("police.png");
robberImage = loadImage("robber.png");

playerOne = new Player(1);
playerTwo = new Player(2);
}

#

oops i changed the image names to fit mine

#

This is what my main sketch file looks like ```pde
PImage bgImage;
PImage arrowImage;
PImage policeImage;
PImage robberImage;
PImage rockImage;

Player playerOne;
Player playerTwo;

// W, S, A, D, UP, DOWN, LEFT, RIGHT
boolean[] keys = new boolean[8];

void setup() {
size(800, 600);

bgImage = loadImage("city.jpg");
arrowImage = loadImage("arrow.png");
policeImage = loadImage("police.png");
robberImage = loadImage("robber.png");
rockImage = loadImage("rock.png");

rockImage.resize(50, 45);

playerOne = new Player(1);
playerTwo = new Player(2);
}

void draw() {
background(bgImage);

image(policeImage, playerOne.x, playerOne.y);
image(robberImage, playerTwo.x, playerTwo.y);

playerOne.move();
playerTwo.move();
}

/** Updates the keys array with the current key */
void keyPressed() {
int index = getIndex();

if (index != -1)
keys[index] = true;
}

/** Updates the keys array with the current key */
void keyReleased() {
int index = getIndex();

if (index != -1)
keys[index] = false;
}

/** Get the index of the current pressed/released key

  • Since keys that are not letters are stored as ints
  • inside keyCode, iterate twice to find the key. */
    int getIndex() {
    char[] chars = {'w', 's', 'a', 'd'};

// Compare the first 4 possible keys
for (int i = 0; i < 4; i++)
if (key == chars[i])
return i;

int[] codes = {UP, DOWN, LEFT, RIGHT};

// Compare the last 4 possible key(codes)
for (int i = 0; i < 4; i++)
if (keyCode == codes[i])
return 4 + i;

return -1;
}

#

and I already sent my player class

stiff niche
#

wait wait

#

OHH

#

THE WIDTH

analog garnet
#

yep

stiff niche
#

oh right

#

well now i forgot

#

we got the weapons still

analog garnet
#

sorry I gtg eat dinner now

stiff niche
#

so lemme just adjust the images

#

alright

#

we at least did some more progress tho Cat! : )

analog garnet
#

yeah

stiff niche
#

i'ev learned a few or two

#

more understanding about classes

#

but still havent gotten to the tricky part yet

#

the weapons lol ๐Ÿ˜‚

analog garnet
#

Haha yeah

stiff niche
#

well anwayys

#

u should go eat dinner now]

#

I need go sleep rn so tmr could we finish up the game? @analog garnet How does that sound

#

I remove for the weapons u done another class for Rock

#

And an arraylist or sumthing

#

anyways i gtg

stiff niche
#

@analog garnet Hey wsp Cat gm btw but today i'm basically on the entire day lol ThisDot_sweatsmile

analog garnet
stiff niche
#

wbu

#

?

analog garnet
#

I have school and work today

#

I'm gonna be busy until 5pm my time, which is 6pm for you

stiff niche
#

thats fine!

#

well then i'll see u than?

#

cause I really wanna finish the game today or at least get to there

stiff niche
#

@analog garnet ct_semicolon are u free rn?

analog garnet
#

Yeah, I have time

stiff niche
#

Alright!

#

U wanna finish up the game ?

analog garnet
#

Sure

stiff niche
#

lemme get my ressources rq

#

Add in the straight โ€˜rockโ€™ weapons. Make sure they only move in ONE direction.

#

Now were on this steps

#

so I assumed we did what u did last time where ushowed me a simple example of a weapon

#

^ this?

analog garnet
#

Yeah, though I'm pretty sure the rocks are actually going to be moving up/down instead of left/right

stiff niche
#

How would I do that?

analog garnet
#

Well, do you understand the code in that example?

stiff niche
#

Not the most probably a quarter of it since comments I would understand more of it

#

Rather than doing 'Class Rock { and class arrow {

#

why not class weapon {

#

as like a catagory

analog garnet
#

Well basically, vel = new PVector(3, 0); means that the velocity of the weapon is 3 pixels per frame in the X direction and 0 pixels in the Y direction

stiff niche
#

ohh

analog garnet
# stiff niche why not class weapon {

You could, but the way that arrows and rocks move are different, so you would have to either keep track of the weapon type, like the player number in the Player class

stiff niche
#

ur right nvm

#

serpertely would be much easier

#

so we could use this code but instead of rect we use our image, and then use key array instead of mouse

analog garnet
#

Yeah

stiff niche
#

well the image part thats pretty easy but the key array part is the tricky one

#

from my understanding we use another method for the key array again?

analog garnet
#

We should be able to add it to the player class pretty easily

#

It's very similar to the movement detection

stiff niche
#

wait add this to player class??

#

I thought we do a another serperate class

analog garnet
stiff niche
#

right..

analog garnet
stiff niche
#

how do you use another class on another class

analog garnet
#

The same as using it anywhere else. Rock rock = new Rock(x, y);

stiff niche
#

Gotcha

#
class Weapon {
  PVector pos, vel;
  
  Rock(PVector start) {
    pos = start.copy();
    vel = new PVector(3, 0);
  }
  
  void display() {
    fill(200, 150, 125);
    rect(pos.x, pos.y, 50, 50);
  }
  
  void update() {
    pos.add(vel);
  }
}```
#

something like that?

analog garnet
#

I thought you said creating separate classes for each weapon was easier

stiff niche
#

wdym I thought this what u mean

analog garnet
#

Also, that code wouldn't run. You can't make the constructor ( Rock() {} ) be different from the class name (Weapon)

analog garnet
stiff niche
#

Rainbow_sob ok

analog garnet
#

You named this class Weapon, so it's hard to know which weapon were talking about

stiff niche
#

well let's start wtih class Rock { then

analog garnet
#

Okay

stiff niche
#
class Rock {
  PVector pos, vel;
  
  Rock(PVector start) {
    pos = start.copy();
    vel = new PVector(3, 0);
  }
  
  void display() {
    image(rockImage,0,0);
  }
  
  void update() {
    pos.add(vel);
  }
}```
#

IT lookin good now Cat?

analog garnet
#

Well, that code would make each rock moving to the right, and it would only draw rocks at the top left corner of the screen

stiff niche
#

oh.

analog garnet
#

Since you're not familiar with vectors, we can use integers for the position and velocity like the player

stiff niche
#

yeah

#

hm

#

I honestly dont know how to Asterisk_sweatsmile

analog garnet
#

It's basically the same code as the players, at least for setting up variables. The only difference is that now id is something like type and now we have a velocity

#

I mean we could still call it id

#

But that typically refers to an individual

stiff niche
#

what im confused is like

#

what do i do after defining the variable

#

s

analog garnet
#

Use them

#

So each rock has a type, a position, and a velocity

stiff niche
#
class Rock {
  int type;
  int pos;
  int vel;
  
  Rock(type, pos, vel) {
    pos = start.copy();
    vel = new PVector(3, 0);
  }
  
  void display() {
    image(rockImage,0,0);
  }
  
  void update() {
    pos.add(vel);
  }
}```
#

this where im at

#

what am i missing here

analog garnet
#

Remember, each position has 2 components: x and y. Also, don't forget to include the data types on that constructor

stiff niche
#

datat types?

analog garnet
#

Data type is the category of variable, like integer, float, String, etc

stiff niche
#
class Rock {
  int type;
  int x;
  int y;
  int vel;
  
  Rock(int type, vel) {
    pos = start.copy();
    vel = new PVector(3, 0);
  }
  
  void display() {
    image(rockImage,0,0);
  }
  
  void update() {
    pos.add(vel);
  }
}```
#

am i there?

#

like this?

analog garnet
#

Well, pos isn't a variable anymore

#

Oh wait! I'm dumb lol I forgot rocks don't have a type

#

That must have been so confusing lol

stiff niche
#

huh?

#

im also confused too Cat Rainbow_sweatsmile

analog garnet
#

For some reason, I had started making the Rock class as if it was a Weapon class. So I made the type variable to determine if it was a rock or an arrow. But I just remembered that we had decided against that strategy because having separate classes is easier. Unless you want to do it with only 1 class

#

It's not actually too hard to do it with a single Weapon class

stiff niche
#

well I wanna know how u done it with like comments that ik what is going on or something (we could do single weapon class if its more efficient)

analog garnet
#

Alright. I wouldn't worry about efficiency right now btw, we're just trying to make it work

stiff niche
#

mb

analog garnet
#

Here's something ```pde
/** Basic weapon class for Rocks and Arrows */
class Weapon {
int type; // Rock (1) or Arrow (2)

// Use float numbers because arrows curve slightly
float x, y; // Position
float vx, vy; // Velocity

// Used to create a new Weapon()
Weapon(int wtype, int startX, int startY, float velX, float velY) {
type = wtype;

x = startX;
y = startY;

vx = velX;
vy = velY;

}

// draw the weapon to the screen
void display() {
if (type == 1)
image(rockImage, x, y);
else
image(arrowImage, x, y);
}

// Change position of weapon
void update() {
// Add velocity (speed) to position
x = x + vx;
y = y + vy;

// Arrows curve
if (type == 2) {
  // If an arrow is moving up, go more up
  // Otherwise, go more down
  if (vy > 0)
    vy = vy + 0.1;
  else
    vy = vy - 0.1;
}

}
}

stiff niche
#

definitely the comments make a big impact

#

so everytime we reupdate the position of

#

wait this for arrow and rocks?

#

So from my understanding this is the set up stage. we still need a method for the key array?

analog garnet
#

Yeah, we still need to add weapons to the game screen

#

we can do that in the player class

stiff niche
#

in the player class?

analog garnet
#

Let's rename our move() method to action()
We're going to add weapon firing functionality to it

stiff niche
#

: O

#

so void move() becomes void action () instead

#

this is all inside the 'player' class

analog garnet
#

yep!

stiff niche
#

I have some notes about weapon firing

#

You can think of a weapon as having x and y coordinates that are updated as long as the weapon is ON the playing area. If it is NOT on the playing area, it has NOT been fired and should NOT be displayed. (If this kinda helps ๐Ÿคทโ€โ™‚๏ธ )

analog garnet
#

But first, we need to add a few more variables to each player: ```pde
class Player {
int id; // player number (1, 2, etc)
int x, y; // position

boolean facingLeft;
ArrayList<Weapon> weapons;

Player(int number) {
id = number;

facingLeft = false;
weapons = new ArrayList<>();

resetPosition();

}

void resetPosition() {
if (id == 1) {
x = 10;
y = 10;

  facingLeft = false;
}
else {
  x = width - robberImage.width - 10;
  y = height - robberImage.height - 10;
  
  facingLeft = true;
}

}

analog garnet
stiff niche
analog garnet
#

yeah

stiff niche
#

alright ๐Ÿ‘Œ

#

The comments most definitely do alot I totally forgot their so useful ๐Ÿคฆโ€โ™‚๏ธ

analog garnet
#

I made the new action method, but I have to send it in 2 parts

stiff niche
#

Is there comments on it so I could understand? Asterisk_smile

analog garnet
#
// Perform an action related to user input
// For example, moving around or firing weapons
void action() {
  /* ----- PLAYER 1 ----- */
  if (id == 1) {
    // P1 move left (A)
    if (keys[2]) {
      x = x - 5;
      facingLeft = true;
    }
    // P1 move right (D)
    else if (keys[3]) {
      x = x + 5;
      facingLeft = false;
    }
    
    // Don't go out of bounds
    if (x > width - policeImage.width)
      x = width - policeImage.width;
    
    // P1 fire Rock (W)
    if (keys[0]) {
      int sx = x + policeImage.width/2 - rockImage.width/2;
      int sy = y + policeImage.height;
      
      // Add a Rock at the bottom of the player
      Weapon weapon = new Weapon(1, sx, sy, 0, 3);
      weapons.add(weapon);
    }
    // P1 fire Arrow
    else if (keys[1]) {
      int sx = x + policeImage.width;
      if (facingLeft)
        sx = x - arrowImage.width;
      
      int sy = y + policeImage.height/2;
      
      float vx = 3;
      if (facingLeft)
        vx = -3;
      
      // Fire an arrow at the left/right of P1
      Weapon weapon = new Weapon(2, sx, sy, vx, 0.1);
      weapons.add(weapon);
    }
  }
#

  /* ----- PLAYER 2 ----- */
  else {
    // P2 move left (LEFT)
    if (keys[6]) {
      x = x - 5;
      facingLeft = true;
    }
    // P2 move right (RIGHT)
    else if (keys[7]) {
      x = x + 5;
      facingLeft = false;
    }
    
    // Don't go out of bounds
    if (x > width - robberImage.width)
      x = width - robberImage.width;
    
    // P2 fire Rock (UP)
    if (keys[4]) {
      int sx = x + robberImage.width/2 - rockImage.width/2;
      int sy = y - rockImage.height;
      
      // Add a Rock at the bottom of the player
      Weapon weapon = new Weapon(1, sx, sy, 0, -3);
      weapons.add(weapon);
    }
    // P2 fire Arrow
    else if (keys[5]) {
      int sx = x + robberImage.width;
      if (facingLeft)
        sx = x - arrowImage.width;
      
      int sy = y + robberImage.height/2;
      
      float vx = 3;
      if (facingLeft)
        vx = -3;
      
      // Fire an arrow at the left/right of P2
      Weapon weapon = new Weapon(2, sx, sy, vx, -0.1);
      weapons.add(weapon);
    }
  }
  
  /* ----- BOTH PLAYERS ----- */
  if (x < 0)
    x = 0;

  // Display and update all weapons
  // Remove weapons that are out of bounds
  for (int i = weapons.size() - 1; i >= 0; i--) {
    Weapon weapon = weapons.get(i);
    
    weapon.display();
    weapon.update();
    
    if (weapon.x > width || weapon.y > height || weapon.x < -100 || weapon.y < -100)
      weapons.remove(i);
  }
}
stiff niche
#

Ok Iโ€™ll read this rn

#

So this entire code

analog garnet
#

yeah

stiff niche
#

I've read it now i know whats going on in this code

analog garnet
#

One thing you'll notice tho

#

This code does not stop the user from firing weapons 100 times per second

stiff niche
#

wait is this like an entire rework with the two player key movement

analog garnet
stiff niche
analog garnet
#

๐Ÿคฃ

stiff niche
#

THE LAG XD ๐Ÿ˜‚

analog garnet
stiff niche
#

but i thought this replaces move() or is move still up there?

analog garnet
#

I combined player movement with player firing a weapon because they're both related to key presses

#

Looking back, this makes the code very hard to read ๐Ÿ˜‚

stiff niche
#

the rock thing is funny lol

analog garnet
#

it is

stiff niche
#

could you send me ur player class

#

not sure why mine always doesn't work

analog garnet
stiff niche
analog garnet
stiff niche
analog garnet
#

interesting

stiff niche
#

and now the main code isn't working ๐Ÿ˜‚

analog garnet
#

did you replace move() with action()?

stiff niche
#

right

#

I Totally forgot

#

u changed move()

#

oh so this what u mean

#

OHHOH

#

OMG

analog garnet
#

its an easy fix, but a funny bug lol

stiff niche
#

i forgot the class weawpon part

stiff niche
stiff niche
#

I've gotten it to work

stiff niche
analog garnet
#

cool

#

lol

stiff niche
#

everytime u fire

#

3 comes out instead of 1

#

but ima honest now there is a lot of lines its hard to find the bug or resolution unless u have encountered it

analog garnet
#

its firing every frame, so roughly 60 times per second. Unless you can let go after 1/60th of a second, it's gonna fire at least 5 lol

stiff niche
#

add some delay?"

analog garnet
#

yup!

stiff niche
#

๐Ÿ˜ฎ

#

I actually got it for once

#

๐Ÿ˜ญ

#

where does this delay() go? ๐Ÿ‘€

analog garnet
#

I didn't create a method for a delay

#

I mean uhh I wouldn't create a method, yeah

stiff niche
#

XD

#

thought delay is just a command

analog garnet
#

well it is, but it's not the kind of delay we want here

#

Processing's delay() will cause the program to pause. So once i's done pausing the game would just keep firing weapons still

stiff niche
#

Oh

#

I didnโ€™t think outside that box ๐Ÿค”

#

Then how will this bug be fixed

analog garnet
#

I fixed it by setting time constraints on the rate the player's fire arrows. Let's introduce some new global variables: ```pde
int ROCK_INTERVAL = 1000 / 2; // can fire 2 rocks per second
int ARROW_INTERVAL = 1000 / 1; // can fire 1 arrow per second

class Player {
int id; // player number (1, 2, etc)
int x, y; // position

boolean facingLeft;
ArrayList<Weapon> weapons;

int lastRock;
int lastArrow;

Player(int number) {
id = number;

facingLeft = false;
weapons = new ArrayList<>();

lastRock = -1;
lastArrow = -1;

resetPosition();

}

#

There's more code later

stiff niche
#

Iโ€™ve never seen this..

#

I

#

O

analog garnet
#

The variables lastRock and lastArrow represent the last rock/arrow fire time

stiff niche
#

Yep

analog garnet
#

(in milliseconds, 1000 ms = 1 second)

stiff niche
#

I get that part

#

Wait

#

I do remember doing this

#

The millisecond thingy

analog garnet
#

cool

#

Can you guess how to go from here?

stiff niche
#

hm

#

someone with void draw

#

iirc

analog garnet
#

not this time

stiff niche
#

i meant

#

wait

#

ur right ur doing a different way

#

well then this would be implement in the class weapon?

analog garnet
#

No, I did it in the player class

#

lol the vast majority of this project is in the player class

stiff niche
#

alr lemme guess

#

the next part of code

#

is gonna be in player classs!

stiff niche
#

Did I get it..? ๐Ÿ‘€

stiff niche
#

inside the scope?

analog garnet
#

which one?

stiff niche
#

the players scope?

analog garnet
#

right, but that's basically just the player class. I meant which method inside that class

stiff niche
#

void resetPosition... possibly..?

analog garnet
#

hint: it's the part that has to do with weapons

stiff niche
#

void update??

analog garnet
#

I was thinking more inside action()

stiff niche
#

OHH

analog garnet
#

Before we fire the weapon, check if we should

stiff niche
#

๐Ÿคฆโ€โ™‚๏ธ around.. void action() scope?

analog garnet
#

inside* but yes

stiff niche
#

so u would have to do the same thing two times

#

because the two weapons

analog garnet
#

yeah

stiff niche
#

LETS GO : )

analog garnet
#

Or 4 if you count both players

stiff niche
analog garnet
stiff niche
#

Gotcha!

#

what happen to the class weapon()

analog garnet
#

Mine's in a separate file

stiff niche
#

ahh

#

well if we run it

#

we definitely wont see the 100 rocks ๐Ÿ˜‚

analog garnet
#

lol

stiff niche
#

Wrosk eprfectly

#

: D

#

I am definitely very thankful Cat that u came in help me like this

#

I've gotten to understand more on what processing is truly .

#

if anything comments are prob the best there is in processing Rainbow_joy

stiff niche
analog garnet
stiff niche
#

I love this emoji lol

analog garnet
#

ct_processing this one's a little outdated

stiff niche
#

good old days.

#

processing 3

analog garnet
#

did you even use it?

#

lol

stiff niche
#

Yes

#

I used to

analog garnet
#

oh, cool

stiff niche
#

On those dell laptops ๐Ÿ’€

#

well anyways

analog garnet
#

haha

stiff niche
#

well now this thing is firing and : )

#

we need detect hit or miss thingy

#

so it means gotta come back to the falling game

#

pretty sure its similar

analog garnet
#

dang this comparison

stiff niche
#

WOW

#

NAH

analog garnet
#

haha

stiff niche
#

Thats insane

#

Alr cat let's do the detecting hit or miss now

#

: )

#

we could use Pvector but i probably still wouldn;t understand p vector tbh

analog garnet
# stiff niche pretty sure its similar

The thing is, the robber image is at a much wider aspect ratio than the cop. So if we just did a basic bounding-box collision on that, things would hit the robber much easier.

stiff niche
#

wait ur images are diff

#

compare to mines

#

robber smaller

analog garnet
#

I resized mine, but I don't think I made it larger

stiff niche
#

ah

#

I'm kinda over shadowing the robber rn

analog garnet
#

Making it smaller might actually be a good way to balance that

#

I think the real issue isn't about the dimensions of the image, it's that it's easier for a weapon to collide with the border of the robber image

stiff niche
#

mhm

#

ok

#

I'll resize the robbers image ig

analog garnet
stiff niche
#

hmm

#

i know what u mean ur right

#

so maybe resize the robbers width

#

i mean height is perfectly fine

#

wait sorry im getting confused

#

mb mb Dan_sweatsmile

analog garnet
#

Yeah, I can resize the robber image so that the width is the same as the cop. It will make it smaller, but hopefully not too much

stiff niche
#

๐Ÿ‘

#

now it looks pretty fair

#

now it looks perfect!

#

So once they collide its a pretty fair collision

analog garnet
#

lol his heads the size of a rock

stiff niche
#

HAHA

#

thats totally fine! : D

analog garnet
#

btw what do we want to happen when a weapon hits a player?

#

increase score?

stiff niche
#

oh yh

#

hold up lemme see

#

kinda like this yh

analog garnet
#

btw, can both players fire both weapons?

stiff niche
#

yeah

analog garnet
#

because that's how I've been making it

#

okay

stiff niche
#

like fire both weapons at sametime??

analog garnet
#

no, P1 and P2 can fire either a rock or an arrow

stiff niche
#

oh yeah

#

definitely!

analog garnet
#

from the picture, it looked like the weapons were player specific

stiff niche
#

it was just an example Dan_sweatsmile

analog garnet
#

haha alrighty

#

Something like this? (I haven't added the code for scores yet, just the text)

stiff niche
#

yeah something like that

#

so everyimte miss well ofc that doesnt count

#

but everytime it hits

#

it would up the score

analog garnet
#

And when does the game end?

stiff niche
#

when u reach 5 hit the game ends and maybe a message on the screen (I've never tried this yet)

analog garnet
#

cool

analog garnet
stiff niche
#

the pattern

analog garnet
#

back and forth

stiff niche
#

mhmm

#

You done the collison?

analog garnet
#

I guess

#

It's a pretty basic collision test

#

Basically, for every weapon, I checked if the middle pixel is inside the opponent's box

stiff niche
#

oohh

analog garnet
#

its not perfect, but its good enough

stiff niche
#

ct_rainbow nothing needs to be perfect : )

analog garnet
#

Most of the changes are at the bottom of the player class ```pde
/* ----- BOTH PLAYERS ----- */
if (x < 0)
x = 0;

Player opponent;
PImage opImg;

if (id == 1) {
  opponent = playerTwo;
  opImg = robberImage;
}
else {
  opponent = playerOne;
  opImg = policeImage;
}

float opx = opponent.x;
float opy = opponent.y;

// Display and update all weapons
// Remove weapons that are out of bounds
for (int i = weapons.size() - 1; i >= 0; i--) {
  Weapon weapon = weapons.get(i);
  
  weapon.display();
  weapon.update();
  
  PImage weaponImg = rockImage;
  
  if (weapon.vx != 0) // Only arrows can move left/right
    weaponImg = arrowImage;
  
  // The middle pixel of the current weapon
  float mx = weapon.x + weaponImg.width/2;
  float my = weapon.y + weaponImg.height/2;
  
  // If the center collides with the boundary of the opponent image, increase score
  if (mx > opx && mx < opx + opImg.width && my > opy && my < opy + opImg.height) {
      score++;
      weapons.remove(i);
  }
  
  if (weapon.x > width || weapon.y > height || weapon.x < -100 || weapon.y < -100)
    weapons.remove(i);
}

}
}

Look where it says BOTH PLAYERS and replace the remaining code there.
Also, I added a `score` variable to the player class.
stiff niche
#

could you send me the file it prob be easier if i read from there

analog garnet
stiff niche
#

Cat u also have to eat dinner soon so I don't wanna take ur precious time Dan_cry

analog garnet
#

We're almost done

stiff niche
#

true

analog garnet
#

But anyway, we also have to draw the score ```pde
void draw() {
background(bgImage);

image(policeImage, playerOne.x, playerOne.y);
image(robberImage, playerTwo.x, playerTwo.y);

/* DEBUG - outlines of players
noFill();
strokeWeight(2);

stroke(255, 0, 0);
rect(playerOne.x, playerOne.y, policeImage.width, policeImage.height);

stroke(255, 255, 0);
rect(playerTwo.x, playerTwo.y, robberImage.width, robberImage.height);
*/

fill(128);
textAlign(CENTER);
text("COP: " + playerOne.score, width/2, 40);
text("ROBBER: " + playerTwo.score, width/2, 80);

playerOne.action();
playerTwo.action();
}

stiff niche
#

would this be in the main code?

analog garnet
#

yes

stiff niche
#

wait ur main set might look a lil diff hol up

analog garnet
#

what's different?

stiff niche
#

nvm

#

we good

#

it works now!

#

how do I end the game like once a player reaches 5 hits and then show a message on the screen?

analog garnet
#

You could use noLoop() and then draw an end screen

stiff niche
stiff niche
analog garnet
#

yeah ```pde
void draw() {
background(bgImage);

image(policeImage, playerOne.x, playerOne.y);
image(robberImage, playerTwo.x, playerTwo.y);

/* DEBUG - player outlines
noFill();
strokeWeight(2);

stroke(255, 0, 0);
rect(playerOne.x, playerOne.y, policeImage.width, policeImage.height);

stroke(255, 255, 0);
rect(playerTwo.x, playerTwo.y, robberImage.width, robberImage.height);
*/

fill(128);
textAlign(CENTER);
text("COP: " + playerOne.score, width/2, 40);
text("ROBBER: " + playerTwo.score, width/2, 80);

playerOne.action();
playerTwo.action();

if (playerOne.score >= 5) {
noLoop();

textSize(100);
text("COP WINS", width/2, height/2);

}
else if (playerTwo.score >= 5) {
noLoop();

textSize(100);
text("ROBBER WINS", width/2, height/2);

}
}

stiff niche
#

O

#

IT WORKS

#

I think its basically done

analog garnet
#

neat

#

right on time too

#

I gotta go eat dinner

stiff niche
#

yeah defeinitely

#

Have a blessday day Cat! : D

analog garnet
#

you too Dan_smile

stiff niche
analog garnet
#

Did you make any changes?

stiff niche
#

Nope

#

Before it was workings

analog garnet
#

Try restarting processing

stiff niche
#

restarting?

#

It isnโ€™t about anything else?

analog garnet
#

just close the ide and re open it

#

Sometimes that fixes it for me

stiff niche
#

Hmm ok

#

So it isnโ€™t like a coding problem?

analog garnet
#

Well you might have accidentally added a letter somewhere that broke the code, then if you removed it there's a chance the error checker didn't notice yet

stiff niche
#

Gotcha Iโ€™m gonna try

analog garnet
#

๐Ÿ‘

stiff niche
#

Itโ€™s still not working

analog garnet
#

Are you sure you didn't change anything?

#

Send me your code

stiff niche
#

Ok

#

Alr mb

#

This internet ๐Ÿ˜ญ

#

Do you still have the code from before we did cat?

#

The internet for my computer aboustely not loading

#

Google isnโ€™t even loading

analog garnet
#

Yeah I do have the code, one sec

stiff niche
#

Sorry I kinda had to reboot my internet

stiff niche
#

Alright Iโ€™ll check this after school because I donโ€™t know why itโ€™s not working for me

stiff niche
#

@analog garnet Idk why it isn't working

#

It works for me now but when I want to load into another my laptop

#

it says something

stiff niche
#

@analog garnet I think its something about the serperate tabs

#

the reasn the code doesn't work in zip

#

but only works when ex tracted

analog garnet
stiff niche
#

so I'll need to extract it

analog garnet
#

Yeah, extract it to your sketchbook folder

stiff niche
#

wdym sketchbook folder?

analog garnet
# stiff niche wdym sketchbook folder?

With Processing, it's common to have a single folder to manage your projects, which are called "sketches". It's typically called the Sketchbook because it contains your sketches

#

If you open the folder containing one of your sketches, it's likely to be in the sketchbook folder if you didn't change it

stiff niche
#

Only 1 tab shows out for example SlingsArrows

#

but not the player and weapon

analog garnet
#

What do you mean by open? Extract the zip file, and then when you open Processing, open the SlingsAndArrows.pde file inside the SlingsAndArrows folder that you extracted from it

stiff niche
#

Like I'm inside the zip file and I try to open the dode

#

code

#

but it doesn't work

#

It only works when I extract the zip file

analog garnet
#

Dont open the zip file. That's just a preview of the files it contains

#

Right click on the zip file and select "extract" or "extract to..."

stiff niche
#

ohh

#

I totally forgot about extracting it

#

It works now

#

Thanks Cat @analog garnet

#

So the entire time

#

It wasn't about the code or anything

#

just extracting?

stiff niche
analog garnet
#

I'm not sure what the issue was. I'm not fully convinced that you didn't change the code before it stopped working either

#

If it was working in the past, I dont see a reason for it not to work unless something important changed

stiff niche
#

nope nothing at all

#

im pretty sure i didn't extract is the issue

#

theres no other way

analog garnet
#

The extraction was only for running my code, I'm talking about why yours stopped working

stiff niche
#

stopped wqorking?

analog garnet
#

well you said you didn't change the code, but it gave that error

stiff niche
#

yeah

#

i think at that time i was in zip file mode

#

im sure i've never extracted anything before running it

analog garnet
#

For the record, it is technically unsafe to download, extract and run code you found on the internet. You're lucky that all I sent was my Processing sketch, but in the future, someone could easily send you maliciious code and ask you to run it. Just make sure that you trust the source of your software, and ideally read the code for yourself and try to understand it.

stiff niche
#

Ur right

#

I kinda just trusted you, I thought of you as my friend Rainbow_sweatsmile

#

Thank you for the advice tho ๐Ÿ™

analog garnet
#

That's fine, just be careful in the future

#

You can't trust everybody

stiff niche
analog garnet
#

Alright, see you ๐Ÿ‘‹

stiff niche
#

@analog garnet I found the problem

#

I had to make to make your arrayList declarations and instantiation work. I had to do the instantiation at the class level rather than inside your constructor.

#

arrayList declaration after instantiation was not with legal syntax.