JoC #16: Adding a score counter | Programming Education Blog Skip to primary content Programming Education Blog Michael Kölling on BlueJ, Greenfoot, Stride, and other Programming Education Topics Search Main menu Home New Joy of Code Stride Joy of Code Docs I Object Who is Michael? Post navigation ← Previous Next → JoC #16: Adding a score counter Posted on February 12, 2012 by Michael Kolling 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 discussed: object interaction, constructor, field, local variable, assignment, external method call Downloads: trick-the-turtle-v4.zip, Counter.java, counter.png This entry was posted in Greenfoot, Joy Of Code and tagged assignment, constructor, field, local variable, method call, object interaction by Michael Kolling. Bookmark the permalink. 22 thoughts on “JoC #16: Adding a score counter” Kevj on February 13, 2012 at 14:01 said: Hi Michael, thanks for another great video, but unfortunately the link to trick-the-turtle zip is broken. Reply ↓ mik on February 13, 2012 at 14:04 said: Sorry about that – fixed now. Reply ↓ Bonemama on February 15, 2012 at 17:00 said: Thanks a lot Mik; it is a really excenlent video. made me understrand More about OOP progreaming. I really looking forword the next one… Reply ↓ Pingback: Joy Of Code #16 Scoreboard - compu2Learn.co.uk Diana on May 5, 2012 at 05:36 said: Great Video but my bug disappears when I run the game. Any ideas to fix it? Reply ↓ Juta on May 13, 2012 at 11:19 said: 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 🙂 Reply ↓ Dan Leighton on July 15, 2012 at 10:46 said: 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! Reply ↓ Dan Leighton on July 15, 2012 at 11:01 said: 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? Reply ↓ Dan Leighton on July 15, 2012 at 11:12 said: 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! Reply ↓ ImagineNoHell on August 2, 2012 at 13:32 said: 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. Reply ↓ Marshall Java on September 26, 2012 at 14:00 said: Great video. My class now wants to keep track of “high score”. Is there a video that speaks to this method? Reply ↓ Michael Riney on October 17, 2012 at 15:26 said: 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 Reply ↓ mik on October 18, 2012 at 09:28 said: @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. Reply ↓ Michael Riney on October 18, 2012 at 14:43 said: 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. Reply ↓ mik on October 18, 2012 at 14:52 said: I never get tired of hearing praise… 🙂 Thanks! Reply ↓ Lucky1235 on November 14, 2012 at 16:37 said: Hi Mik, i was wondering how do you copy the java file into the scenario? Reply ↓ Miki on December 19, 2012 at 14:46 said: 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! Reply ↓ sraddha on April 30, 2017 at 16:44 said: how to download counter.java Reply ↓ Nenad on July 12, 2017 at 11:46 said: 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. Reply ↓ Joe Hoffman on July 18, 2017 at 16:33 said: 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? Reply ↓ paul on July 21, 2017 at 06:02 said: 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 Reply ↓ Dalillama on October 24, 2017 at 18:04 said: 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 Reply ↓ Leave a Reply Cancel reply Your email address will not be published. Required fields are marked * Comment Name * Email * Website × 9 = 9 Proudly powered by WordPress