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

2012/11/25

An interesting question!

jabirfatah91 jabirfatah91

2012/11/25

#
Can anyone tell me please how a computer understand the programing language? I mean if I write "turn(45)" inside the "turn" method then how does the computer know that it's time to turn the perticular picture/object?
danpost danpost

2012/11/25

#
I do not think anybody would want to try and tackle this one (at least as far as trying to break down exactly what happens between the command and the actual processing of it. To begin with, a command is broke down into simple steps, by use of method calls (have you seen the stack traces when an error occurs). From there, they are broke down even more, usually into a lower level language, whose commands are broke down even more into the individual processes of moving bytes in and out of and manipulating bytes and flags within the computer's central processing unit. I do not know how exact I was in trying to explain it. But, that is pretty much it. For 'turn(45)' it probably is just a matter of changing the 'rotation' value. Internally, Greenfoot has a reference to the active world, and the data for that world has a list of all active objects within it. In addition, the world has a reference to the background image that is set. Greenfoot continuously refreshes the screen depending on all this data. Just to change that one value, though, involves locating where in memory that data is located, moving the value into the central processing unit, and adding the amount (45 or 002Dh or 0000 0000 0010 1101 in binary) to the original value. It then checks to see if the result is less than 360 (or 0168h or 0000 0001 0110 0100 in binary) and subtracts 360 until it is; it will also check to see if the result is greater than or equal to zero, and adds 360 until it is. Finally, it will move the resulting value back to the location it got the original value from. SO: 'turn(45);' is broke down to
public void turn(int turnAmt)
{
    int rot = this.getRotation();
    rot = rot + turnAmt;
    this.setRotation(rot);
}
Where getRotation and setRotation deal with where in working memory to find or store the data bytes; which are themselves broke down into individual steps to actually location the data in memory. Some commands may be broken down into many thounsands of steps to accomplish. You, fortunately, chose an easier one.
Upupzealot Upupzealot

2012/11/25

#
good explanation!
danpost danpost

2012/11/25

#
As a sidenote: I did not show where the checking and correcting of the resulting value was located (adding and subtracting of 360 to bring the result within the range of 0 and 359 inclusive. That would be located in the 'setRotation' code which would start out something like the following:
public void setRotation(int newRot)
{
    while (newRot < 0) newRot = newRot + 360;
    while (newRot >=360) newRot = newRot - 360;
followed by the command(s) to store the resulting value. There are other ways to bring the value back into its range, so the code given may not be the actual code used. I only included it to show the idea of breaking down the initial command (and since I mentioned it earlier, thought it should have been included). I will not go into the internal workings (address fetching, data fetching, etc.) as that would be another whole can of beans.
jabirfatah91 jabirfatah91

2012/12/8

#
Danpost! Please have a look at my discussion I created recently. I always expect great help from you. I want to use the existing code I copied+pasted there, but I want to avoid using the Greenfoot library function getNeighbours(…) in the Actor class. Then how to edit the exixting code? i will be very grateful to you with instant response! My discussion.
You need to login to post a reply.