# Unit 3, Lesson 13

**URL:** <https://forum.code.org/t/unit-3-lesson-13/37785>\
**Category:** Coding and Debugging Help\
**Created:** [March 21, 2023, 12:26pm UTC](https://forum.code.org/t/unit-3-lesson-13/37785 "2023-03-21T12:26:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![skschiltz](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/skschiltz/32/7805_2.png) [@skschiltz](https://forum.code.org/u/skschiltz)\
**Post date:** [March 21, 2023, 12:26pm UTC](https://forum.code.org/t/unit-3-lesson-13/37785/1 "2023-03-21T12:26:33Z")

</div>

[Project](https://fortimail.mpsomaha.org/fmlurlsvc/?fewReq=:B:JVo+NTE2PiJyOTYqNCJtYDk0NT40NSJ3bWNqZXBxdmE5MTYwNTc9PWVgZWE9PWE0NmYxNTE8NTI3MzQwPDI3NWViND08MjA1PCJwOTUyMz03Nz03MjQidW1gOTc2T049Tk9ANDQ1NzQyKTc2T049Tk9CNDQ1NzQyInZndHA5d293Z2xtaHB+RGl0d2tpZWxlKmt2YyJnOTE2ImxgaDk0&url=https%3a%2f%2fstudio.code.org%2fprojects%2fgamelab%2fgRv9D4d4qLjbIKzBkAB-Xxytz5mMQPdu5_FHzaWZ7TI)

The student wants a star to appear randomly on the screen every time the program is run.

If she places the ellipse block before the draw function, it is covered by the background. If she places it in the draw function block, the star moves all over the place. If she places it outside the draw function block, it doesn’t draw at all.

In the random number exercises, students learned to use random numbers so an ellipse would show up at a different spot every time you ran the program. This is what she is trying to replicate.

---

<div class="post-metadata">

**Author:** ![mriley](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@mriley](https://forum.code.org/u/mriley)\
**Post date:** [March 21, 2023, 1:21pm UTC](https://forum.code.org/t/unit-3-lesson-13/37785/2 "2023-03-21T13:21:37Z")

</div>

Remember the order of things matters in Game Lab  
If something is drawn before the drawloop, then anything in the draw loop will cover it.  
Think art class, if the student in the earlier class drew a star on paper, but in the next class the student is told to paint a house on that same piece of paper, the star will be covered and forgotten about.  
In the drawloop the order is;  
Background  
Element 1 - circle  
Element 2 - Star.x randomNumber  
Element 2 - Star.yrandomNumber  
Drawsprites

see if this helps

---

<div class="post-metadata">

**Author:** ![skschiltz](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/skschiltz/32/7805_2.png) [@skschiltz](https://forum.code.org/u/skschiltz)\
**Post date:** [March 21, 2023, 1:37pm UTC](https://forum.code.org/t/unit-3-lesson-13/37785/3 "2023-03-21T13:37:11Z")

</div>

The student can’t draw the ellipse inside the draw loop or it animates the star. The ellipse needs to be a stationary object that appears at a random spot on the screen when the program is run.

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [March 21, 2023, 8:48pm UTC](https://forum.code.org/t/unit-3-lesson-13/37785/4 "2023-03-21T20:48:26Z")

</div>

@skschiltz,

What if they were to create two variables outside the draw loop, starX and starY.

This would generate a random position one time.

Then, inside the draw loop, create the ellipse using those variables as the x and y coordinates.

This should solve the problem and put the star in a random position each time the program is run.

Mike

---

<div class="post-metadata">

**Author:** ![varrience](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/varrience/32/8042_2.png) [@varrience](https://forum.code.org/u/varrience)\
**Post date:** [March 22, 2023, 2:01am UTC](https://forum.code.org/t/unit-3-lesson-13/37785/6 "2023-03-22T02:01:45Z")

</div>

Greetings @skschiltz,

if I’m understanding correctly wouldn’t it be better to show her how to generate an object @mwood? I’ll provide the following snippet below

```auto
var star = {
  x: randomNumber(1, 400),
  y: randomNumber(1, 400),
  w: 10,
  h: 10,
  // this function is optional you can do it another way
  display: function() {
    fill(255);
    ellipse(this.x, this.y, this.w, this.h)
  }
};

```

by using it this way you should show how the basis of sprites work as they are just a glorified class of properties used to make basic physics and animation to the game, also this is to be declared with the group of sprites you’ve created outside of the array however, you may need to slightly change how you draw the sprite which there are some ways of going about it I’ll share two that I like the most though

```auto
// Loop starts Here!
function draw() {
 // Example #1
 fill("white");
 ellipse(star.x, star.y, star.w, star.h);
 // Example #2
 star.display();
}

```

hopefully this resolves your issue, though if your just starting out I’d much more reccomend @mwood’s suggestion rather than showing them a completely new concept as they were looking for a solution that they could understand anyways best of luck!

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [March 22, 2023, 4:01am UTC](https://forum.code.org/t/unit-3-lesson-13/37785/7 "2023-03-22T04:01:23Z")

</div>

@varrience ,

your solution is a very good one, but a little outside the scope of the CSD lessons.

Also, it would require the students to leave block mode. That’s great when they are ready, but as you mention, if they are beginners and are trying to create just the one star, the simplest approach may be the best.

I do like seeing your creative approaches to the problems, though. I have learned a thing or two myself from your posts.

Thanks!

Mike

---

<div class="post-metadata">

**Author:** ![skschiltz](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/skschiltz/32/7805_2.png) [@skschiltz](https://forum.code.org/u/skschiltz)\
**Post date:** [March 22, 2023, 1:04pm UTC](https://forum.code.org/t/unit-3-lesson-13/37785/8 "2023-03-22T13:04:01Z")

</div>

Thank you, Mike! The star x and y variable is something my student understands and is able to do.

---

<div class="post-metadata">

**Author:** ![mriley](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@mriley](https://forum.code.org/u/mriley)\
**Post date:** [March 23, 2023, 12:53pm UTC](https://forum.code.org/t/unit-3-lesson-13/37785/9 "2023-03-23T12:53:29Z")

</div>

So happy that helped!
