This site requires JavaScript, please enable it in your browser!
Greenfoot back
tomw
tomw wrote ...

2013/12/17

Quick code

tomw tomw

2013/12/17

#
Hi can anyone give me a quick code for my actor hits the edge of the world it will turn in a random direction? Help will be very helpfull.
danpost danpost

2013/12/17

#
You do not learn by people writing code for you.
askgriff askgriff

2013/12/17

#
I think most of us learn by people writing code snippets for us. The way I learned is by watching Mik's "Joy of Code" series on YouTube. In this case, this is from "Trick the Turtle":
1
2
3
4
5
6
7
8
9
10
11
12
/**
     * Test if we are close to one of the edges of the world. Return true is we are.
     */
    public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }
Then you call that method with something like this:
1
2
3
4
5
6
7
8
9
10
/**
     * If we reach the edge of the world, turn a little bit.
     */
    public void turnAtEdge()
    {
        if (atWorldEdge())
        {
            turn(7);
        }
    }
bourne bourne

2013/12/17

#
@askgriff, If you learn on code snippets, you will always seek out code snippets (which seems to be the case frequently)? Which is a bit sad when a lot of answers can be found by just exploring what methods a class has to offer from the API. I would argue self motivated experimentation with API as a guide is most useful for learning, but perhaps you are right - most people don't do it that way
bourne bourne

2013/12/17

#
@askgriff, And from your example I don't see what you learned. Seems like you now have a handy method to do something for you. But, perhaps you did in fact had a learning experience by testing yourself of what it should look or behave like as the code was presented to you, I don't know
tomw tomw

2013/12/17

#
Thanks and I'm new to green foot so I don't know much code but hopefully I will become better at this.
askgriff askgriff

2013/12/18

#
Bourne -- whether you remember or not, you learned from code snippets. There is no way you can learn programming without seeing it in action. Imagine I'm going to teach you to speak Swahili and I say, "I'm not going to give you words -- you have to learn those on your own." It's ridiculous. I've been a high school and college computer science teacher for the last 16 years, I have studied education methodology, learning modalities, and a variety of other practices. Examples are useful learning tools for beginners. Now, I gave him Mik's code and a reference to his video tutorials because I thought it was a good starting point and because I learned a lot about setting a method to look at the edge of the world. I don't use that method any more because I typically place my objects with x,y coordinates. I guess what I'm saying is that this is a forum for asking for help -- and simply replying with, "Go learn it yourself" is defeating the purpose of coming here. I know if I was learning the language and people like danpost hadn't jumped in to help me figure out the oddities of Greenfoot, I would have abandoned it for another language. I think the thing that really separates Greenfoot from the "other guys" has been the friendly community where people help each other.
bourne bourne

2013/12/18

#
I learned syntax/grammar from code snippets. Things like declarations and control flows. Not for implementations, which is the creative part of programming I think. Handing out examples on a silver platter doesn't make sense to me when it comes to learning, sorry. Helping someone through a problem is different. I don't remember my High School instructor giving us examples to implement something. It was: here is a list of methods of a library and what they do, figure it out, collaborate with your neighbor, or lets examine the problem. And she is probably a better CS instructor than any I had in college. And I never much liked looking at other people's code, I intentionally avoid it. I was a T/A for a programming class in High School and it bothered me very much trying to go through a student's code. Props to you and your fellow CS instructors.
askgriff askgriff

2013/12/18

#
It's the hardest subject I have ever taught -- mostly because someone will raise their hand and you have to go to them, figure out what the heck they were trying to do without knowing what variables they used, methods they access, or classes they have created. THEN you have to figure out what they are trying to achieve. And while you're trying to figure it out, 2 or 3 other people have their hands up. Sometimes the only way to help them is to point them to examples of the code so they can figure out how someone else did it. Maybe it's because one of my Bachelor's degrees is in History that I want people to learn from what was done in the past. :) And I wanted to thank both you (Bourne) and Danpost because you are probably the most prolific programmer on Greenfoot.org and Danpost has answered more questions than I think all other people combined. :)
lordhershey lordhershey

2013/12/18

#
Sounds like maintaining a legacy system is something you might want to avoid. :)
bourne bourne

2013/12/18

#
@askgriff, And thanks, I'm glad all of my scenarios are appreciated. =) @lordhershey, I'm sure legacy systems are everywhere, when it comes to a job I will deal with it. But for recreation, I will avoid looking at other people's code.
You need to login to post a reply.