JoC #16: Adding a score counter

      Habe Mut, dich deines eigenen Verstandes zu bedienen.
            — Immanuel Kant

Welcome back after a little break in putting out new episodes.

I was at a conference for a week or so (Melbourne, other side of the world!), and then I had to get settled into my time zone again, but I’m back! I still feel a little jet-lagged, so if my voice sounds even more drab than usual in this episode – I blame the time zones.

This episode deals with an incredibly important concept: object interaction. (It could actually also have been titled “Object interaction – a second look”.) We started to look at object interaction a little bit in Episode #15, and now we’re really getting stuck in. We’re throwing around terms like “object references” and “constructors”.

Yes, we’re slowly cranking up to a more professional level here. So sit back, switch your brain on, and watch. And of course — as always — after watching: Fire up Greenfoot, and get coding yourself!

Download video

Oh, and one more thing: I am showing how to implement a score counter here, because several people have asked me how to do that. So if you have a question, or something you want explained: tell me in the comments, and I’ll try to fit it in in one of the future episodes.

Update: This episode shows how to use a ready-made counter object. If you are interested in making such a counter yourself, you can now look at Episode 26.

Concepts discussedobject interaction, constructor, field, local variable, assignment, external method call

Downloads:  trick-the-turtle-v4.zip

22 thoughts on “JoC #16: Adding a score counter

  1. Thanks a lot Mik; it is a really excenlent video. made me understrand More about OOP progreaming.
    I really looking forword the next one…

  2. Pingback: Joy Of Code #16 Scoreboard - compu2Learn.co.uk

  3. Hi. Thank you or this video, it really helped. I’ve been looking for days how to add a score board before I found this.
    Anyway, maybe you could help with another problem I’m having. I’m making a game where you have to pick let’s say 10 objects. They give you 50 points and what I want to do is I want to make exit doors. They have to be closed throughout the game (closed door image) and they have to open (set another image of opened doors) when the score is 50. I’ve tried to do this like this:
    public void animate( ) {
    setImage( image1 );
    if (counter.getValue() == 5)
    setImage( image2 );}
    but it won’t work. I would be very grateful for your help 🙂

  4. Ok! There is a reason I never learnt to code… And this episode reminds me why! The section on creating a counter starting at 6.38 is where my confusion starts and I get that feeling of dread that here is stuff which is not going to stick in my head…and hours of frustration are on the horizon when I try to step outside the sandbox…

    Am I right in thinking this is “creating a counter variable of type counter and assigning it the object counter”? I don’t think I have that right… It would certainly be helpful if variables were called varCounter, types were called typeCounter, and objects were called objectCounter. I am sure that this is tautologous as there is probably some way to identify when the word “counter” is referring to a type, variable or object…but I have no idea how to tell the difference right now… Or how to explain it to my students!

  5. Oh. And at 13.17. Passing the turtle the counter reference in the parameter… Very confused…

    I get that the turtle object needs to know about the counter (though I will forget that in the future I guarantee you)…but why would that happen in the turtle parameter preparation bit of the Turtleworld? Why not just tell it about the counter object in the turtle object code (or do I mean class?) which is where I instinctively would assume we should tell the turtle what it has to do and know.

    In other words, how do we remember that the parameter in the prepare bit (do I mean prepare method?) is where we reference other objects?

  6. 14.08 also very confused. Again 😉

    What does it mean for the constructor method to receive the Counter ? And why Counter and not counter?

    And aaagh! At 15.40 we get another thing called counter, this time it’s a field!

  7. Juta, did you call that method anywhere? And where is that method? To be long-lived, I’m guessing there needs to be an Actor class, eg. “door”. Just like the counter.

    Dan, there’s a lot of negative self-talk. Cut that out; it doesn’t help anything. Anyway, you’re right about “creating a counter variable of type counter and assigning it the object counter”. Now, the difference between counter and Counter is right in the code. The class is named Counter, with the upper case C. This is not law, but it is a universal convention. When you see the small c, as in counter, that’s a variable. Once you assigned an object to a variable, you can think of them as the same thing for now. Finally, where there are parentheses, as in counter(), that’s a method call, and since the method’s name is the same as the class name, it’s a special thing called a constructor. It gets called to create new objects of that class.

  8. I’m unable to locate the source code for Counter.java. The link included in Joy of Code #16 has code that is very terse and nothing like the code that is shown in the video. Am I downloading from an incorrect link? As Michael states object interaction is very basic to learning Java so I’d like to follow along with the video. Any suggestions for where I can locate this code will be deeply appreciated. Michael Riney

  9. @Michael: You can download the Counter class from this page. Look at the end of this post (above the comments) for the “Downloads” line. You need to download Counter.java and counter.png. Then copy the java file into your scenario, and set the image. You can then use it as shown in the video.

  10. Thank for your quick response. Ii was another silly user error. You must get tired of hearing what great work you have done with these tutorial. Having worked with some exceptionally sharp talent over the past 30 years my hat’s off to you. Your work is something that Coursera and others should be very interested. Thank you so much.

  11. Hi, and thanks for your amazing video! I have no idea what I’d do without it.
    However, I have a problem. I added a second counter and did the same thing, but when the crab died and was created again (it had another life) the point counter stopped counting. Any ideas why? Thanks so much!

    • u don’t need to, just copy/paste the text from the new tab into the counter.class

      and yea…u need to delete the 2. line and in the 80th line delete the “Color.” so it compiles and works.

      It will be white but it’l work.

  12. I downloaded the java file and copied it into my counter source code. it returned that the way the Color.BLACK was written, it could not be converted from java to greenfoot.
    GreenfootImage text = new GreenfootImage(“” + value, 22, Color.BLACK, transparent); I looked up the documentation for color in greenfoot and couldn’t find any errors. could it simply be that I have a different version of Greenfoot then when the code was originally written?

  13. Hi Michael,,

    I got an error when i pasted from the txt file.

    I can’t copy and paste error – it appears when i compile – it refers to incompatibility problems.
    I took out the 2nd line of code from the txt file – import java.awt.Color;
    (the error message was underneath the black colour reference).

    It seems to work now. I hope this wont cause problems later.

    Regards

    Paul

  14. Um i got much porblmo when i tryed to dou
    public Turtle(Counter world pointCounter)
    It says invalid method declaration; return type required
    plez help

Leave a Reply

Your email address will not be published. Required fields are marked *