# Basic computer course project University

**URL:** <https://discourse.processing.org/t/basic-computer-course-project-university/5526>\
**Category:** Coding Questions\
**Created:** [November 14, 2018, 12:59pm UTC](https://discourse.processing.org/t/basic-computer-course-project-university/5526 "2018-11-14T12:59:23Z")\
**Posts on this page:** 11\
**Page:** 2

<div class="post-metadata">

**Author:** ![SeanTYH](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@SeanTYH](https://discourse.processing.org/u/SeanTYH)\
**Post date:** [November 18, 2018, 6:22am UTC](https://discourse.processing.org/t/basic-computer-course-project-university/5526/25 "2018-11-18T06:22:59Z")

</div>

i have some questions.  
1)Why do you not need to declare the width and height of the character is it because it is constant through out? 2)why is there a rect() in the player class?  
3)what does (dist(x,y,width/2,height/2)\<100 mean ?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 18, 2018, 6:31am UTC](https://discourse.processing.org/t/basic-computer-course-project-university/5526/26 "2018-11-18T06:31:21Z")

</div>

> [@SeanTYH](#):
>
> 3)what does (dist(x,y,width/2,height/2)\<100 mean ?

it checks if the object is less than 100 pix from the middle

but you must see the whole context:

```auto
    x = random(width);
    y = random(height);
    while (dist(x, y, width/2, height/2) < 100) {
      x = random(width);
      y = random(height);
    }  

```

first you make the objects x,y in the whole window / canvas  
then check and try again until it is not in the middle!

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [November 18, 2018, 6:59am UTC](https://discourse.processing.org/t/basic-computer-course-project-university/5526/27 "2018-11-18T06:59:01Z")

</div>

1. Pretty much, yes. I could put the character’s size in the player class, and then use it when I draw the player. This might be useful if, for example, you planned on having your player’s size change during the game. I didn’t. But I did think that it might be useful to have in in the Foe class, because enemies are probably a lot more likely to come in different sizes.

2. That rect() is what draws the player! That’s what the blue square is!

3. Yes, as @kll said, that is to make sure there is a “hole” in the middle where there are no enemies. That way the player doesn’t die immediately (assuming you add player-dies-if-touching-enemy logic later).

---

<div class="post-metadata">

**Author:** ![SeanTYH](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@SeanTYH](https://discourse.processing.org/u/SeanTYH)\
**Post date:** [November 18, 2018, 7:21am UTC](https://discourse.processing.org/t/basic-computer-course-project-university/5526/28 "2018-11-18T07:21:30Z")

</div>

> [@TfGuy44](#):
>
> boolean draw() { move(); if (alive) { fill(255); ellipse(x, y, 5, 5);

What is this boolean draw? why do you not need to draw the enemy rectangle in this? or in my case instead of rect i will be using image()

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [November 18, 2018, 8:26am UTC](https://discourse.processing.org/t/basic-computer-course-project-university/5526/29 "2018-11-18T08:26:28Z")

</div>

That is the draw method for the Bullet class. It draws one Bullet. You are aware that the Blue square can shoot bullets, right? Click in the sketch. A bullet is created at the blue player square that is moving towards the mouse’s position.

The Bullet’s draw() function returns a boolean value so that the loop in the main draw() function can remove Bullets that are not alive from the ArrayList of bullets. Basically, the Bullet reports if it is still alive via the return value of its draw() function. If it is not alive any more, it is removed from the ArrayList.

---

<div class="post-metadata">

**Author:** ![SeanTYH](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@SeanTYH](https://discourse.processing.org/u/SeanTYH)\
**Post date:** [November 18, 2018, 8:53am UTC](https://discourse.processing.org/t/basic-computer-course-project-university/5526/30 "2018-11-18T08:53:42Z")

</div>

does the Player player replace the Player players[] = new Player[1] ? since there is technically only one player so no need for an array?

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [November 18, 2018, 8:57am UTC](https://discourse.processing.org/t/basic-computer-course-project-university/5526/31 "2018-11-18T08:57:32Z")

</div>

Yes. There is only one Player. It is the Player called `player`. There is no need for an array of Player objects if there is only one of them!

---

<div class="post-metadata">

**Author:** ![SeanTYH](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@SeanTYH](https://discourse.processing.org/u/SeanTYH)\
**Post date:** [November 18, 2018, 9:03am UTC](https://discourse.processing.org/t/basic-computer-course-project-university/5526/32 "2018-11-18T09:03:48Z")

</div>

my original one is using

Player players = new Player[1];  
PImage chara = new PImage[1];

when i try to introduce the bullet class as above, its says  
players.charaStartX cannot be resolved or is not a field.

this is around the part where  
Bullet()  
{  
alive = true;  
x = player.x; ( i use float charaStartX in Player class so i wrote x = players.charaStartX)  
}

> [@TfGuy44](#):
>
> class Bullet { boolean alive; float x, y, a; Bullet() { alive = true; x = player.x; y = player.y; a = atan2(mouseY - player.y, mouseX - player.x);

i am also trying to use Player players but since i was using array to load images earlier i m not to sure how to convert these:

for (int i=0; i\<players.length; i++)  
{  
players[i] = new Player();  
}  
// from void setup()  
and  
// from void Homepage()  
for (int i=0; i\<players.length; i++)  
{  
players[i].draw(300,500,300,300,i);  
}

and basically i know how to draw a normal character using image. but now i m using classes i m not too sure do i just call the class to draw the image or i can just use “image(…)”

this is somewhere where i am at now  
[https://drive.google.com/file/d/1QMmwwGcCObS7C72scbZr6o2bWcekZOR2/view?usp=sharing](https://drive.google.com/file/d/1QMmwwGcCObS7C72scbZr6o2bWcekZOR2/view?usp=sharing)

i m trying not to copy everything since i feel that it isnt right. i m retaining some parts of what i did on my own before but i copied the classes thing (not much choice there)

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [November 18, 2018, 9:32am UTC](https://discourse.processing.org/t/basic-computer-course-project-university/5526/33 "2018-11-18T09:32:46Z")

</div>

Let’s be clear: You no longer want to have an array of Players. You only need one Player, so it’s not an array called players, it’s now one Player called `player`.

As such, you should first remove any loops that are happening over the players array. Because there is no players array any more.

To make the new Player to store in your Player object called `player`, just do:

`Player player = new Player();`

That is, there is a Player object. It is called `player`. And the value assigned to it is a new Player object.

Similarly, there is no need for a loop over the players array in `draw()`, once again, because there is only one Player object. Just draw it directly:

`player.draw();`

* * *

Notice that the player class doesn’t have a `charaStartX` variable! The name is simpler now. It’s just `x`.

You still want to load the images for the ghosts and the art of chara in the same way:

```auto
PImage chara; // There is one image called chara.
PImage[3] ghosts; // There is also an array of three images for the ghosts.
// Load the images the same way as before. One directly, the other three in a loop.

```

In the player class, you can just call `image()` to draw the image of chara instead of drawing a blue square with `rect()`. Notice that the PImage called `chara` is a global variable, which means that you can access it inside any class or function.

In short:

`image( chara, x, y );`

---

<div class="post-metadata">

**Author:** ![SeanTYH](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@SeanTYH](https://discourse.processing.org/u/SeanTYH)\
**Post date:** [November 18, 2018, 1:22pm UTC](https://discourse.processing.org/t/basic-computer-course-project-university/5526/34 "2018-11-18T13:22:15Z")

</div>

if i get rid of charaStartX in player class what happens if i need to refer to it outside of the class? i use players.x? when i need to players.draw(charaStartX, charaStartY, charaWid, charaHght) it says i cant use players.x inside of it. and when i put x it says component not visible.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 18, 2018, 1:34pm UTC](https://discourse.processing.org/t/basic-computer-course-project-university/5526/35 "2018-11-18T13:34:14Z")

</div>

[Docs.Oracle.com/javase/tutorial/java/concepts/index.html](http://Docs.Oracle.com/javase/tutorial/java/concepts/index.html)

[Previous page](https://discourse.processing.org/t/basic-computer-course-project-university/5526.md?page=1)
