Monkey programming step by step blog (examples)

73 replies to this topic
Posted 2 months ago #51

I am still working on adding examples. Getting close to 240 examples.

31000 pageviews since the blog started.

I wil be getting a book on game 2d collision detection soon and if I understand the code and theory in the book then I will be adding a number of examples on collision and vectors. The book said it was for beginners :)

Posted 2 months ago #52

Pakz thats awesome! if i could make a few suggestions. The file names are really long and have characters that dont allow to run, the other night i was editing names for about 20 mins and then the next day the names reverted somehow...arrrrggg i wanted to pull my hair out lol. can all file names be changed with code to remove or replace characters? the other thing is variable names, can the be more meaningful for the sake of a noob :) or some explanation. just a request so we get the most out of your efforts with less struggle. And thanks for your contributions!

Posted 2 months ago #53

I wil take a look at the filename issue. I never load them myself since i just copy and paste the code into ted myself.

I think i wil make a program that renames every file to a legal filename. You would need to run this file in monkey and it would rename every file in the directory. The structure in github atm is the title of the blog post.

I hope by removing the text monkey-x and replacing the " " ( space) with underscore "_" wil be enough.

I wil start to explain (comment) variable names when i create them.

Posted 2 months ago #54

good news! fyi 1 or more files have () in it.

Posted 2 months ago #56

sorry not sure where to put file directory

Posted 2 months ago #57

If you have downloaded the github zip file (what I think you ment) and have unzipped it then you need to select this directory(location on the disk) to modify the files.

Posted 2 months ago #58

i didnt see a zip, i copied and pasted and saved then ran and i get an error native file system not implemented

Posted 2 months ago #59

Do you have a mac?

The zip download should be here :

Posted 2 months ago #60

no when i click the link i dont get green link(clone/download)

Posted 2 months ago #61

I can not directly find anything about this with google on why the link is not shown. Are you registered with github? Maybe that is a requirement but I doubt it.

I can make a zip myself and give the link and I would try to keep it up to date. Let me know.

edit : I do think you need to be registered at github because cloning is a member feature I am sure. It is free btw.

Posted 2 months ago #62

@Pakz: You have linked directly to the file view in the repository and not the download/clone which is only meant only for the entire repository. The file view has a button to called Raw see the file in un-formatted raw form.

@dubbsta: If you are seeing error native file system not implemented. Then the compiler target for that version of MonkeyX doesn't support a particular module or function set that you are trying to use. Such errors will show the module that is causing the problem and you can work out what targets are valid for that module.

This is one of the reasons why MonkeyX is not the best language for a beginner.

Posted 2 months ago #63

I made a code file for dubbsta with a requestdir command used. That must not be supported on his target then.

This github stuff is complex :(

Posted 2 months ago #65

@therevills: Pakz had directly linked to the file view and not to the repository home. The 'download/clone' that I linked is the the repository home page. Maybe I should have made that much more clearer in that post.

If anyone needs to get to the repository home page from the file view all they need to do is to click the blue links on the top left. The one near the branch drop down box is for navigating the current repository. The one at the top is for currently selected repository and to access the users other repositories.

Posted 2 months ago #66

so what can i do to fix it? i still dont really get it. or would it be easier for pakz to run the code and reupload the files?

Posted 2 months ago #67

No because the problem lies with the target and what support for it is in the modules.
To fix it you would have to know the targets API and know how to write native code. Not something a novice or someone looking for write once compile any where tool chain would expect to have to deal with.
For example the requester module only supports Windows and OS X, not Linux. Implementing this on Linux requires a lot more knowledge due to the number of desktop environments, GUI framework quirks and framework incompatibilities; GLFW does not work well with GTK. If someone wanted requesters on Linux with MonkeyX; then they would find that the best way would be to write a GTK target, but even that would throw up a few problems

Posted 1 month ago #68

I am not that experienced with github. I upload using the website there. One at a time. Up until recently I did not know monkey did not handle the filenames I used well. I tried using the github tool but could not understand how to use it/set it up. I also have already linked to the sourcode from elsewhere and that would break the links If I could figure out how to re-upload using the git tool. If I learn one day to use the github tools then I will see.

Here is the current contents with the modified filenames :

I finished with the book with 2d game collision code. I added a whole bunch of example code to the list here (middle section bottom) I was not able to recreate every thing from the book. I spend hours trying to fix 5 examples. They are not on the list here. They are on the github but the first line tells that it does not work correctly (mostly the oriented rectangle code is broken)

Posted 1 month ago #69

I tried using the github tool but could not understand how to use it/set it up.

Are you using the web based tool or the GitHub Desktop Application? I find that this git tool is better. It's also cross platform and has a few tutorial on how to use it.

Youtube tutorials

Edit: There is also GitKraken

Posted 1 month ago #70

cool beans Pakz

edit: hate to burst your bubble, just a fyi alot of the file names have (-) wich is also giving monkey a problem. but when ever your'e up to it..

Posted 1 month ago #71

I updated the zip above : The - chars are removed.

Posted 3 weeks ago #72

I bought a new programming book. It is called "Mazes for programmers" I received it half a day ago and have started reading it.

The book is somewhat understandable for me, not to difficult. I have already added 3 maze generation examples on the blog and github. (Not linked in the list yet)

If I can understand all 12 maze generation methods from the book than they will start to appear on the blog. Maybe there is more in the book that can be turned into examples.

It wil take a while for me to finish the book since it is a nice 270+ pages thick.

Posted 2 weeks ago #73

Thanks for these demos
I tried changing the origin of line in the line vs rectangle demo,
it doesnt seem to work anymore?

' From the book '2d game collision detection'
' rectangle vs Line collision

Import mojo

' This is the collision class.
Class collision
    ' Collision function
    ' The rectangle vs Line Collision
    Function line_rectangle_collide:Bool(l:line,r:rectangle)
        Local n:vector2d = rotate_vector_90(l.direction)
        Local dp1:Float,dp2:Float
        Local dp3:Float,dp4:Float
        Local c1:vector2d = r.origin
        Local c2:vector2d = add_vector(c1,r.size)
        Local c3:vector2d = New vector2d(c2.x,c1.y)
        Local c4:vector2d = New vector2d(c1.x,c2.y)
        c1 = subtract_vector(c1,l.base)
        c2 = subtract_vector(c2,l.base)
        c3 = subtract_vector(c3,l.base)
        c4 = subtract_vector(c4,l.base)                        
        dp1 = dot_product(n,c1)
        dp2 = dot_product(n,c2)
        dp3 = dot_product(n,c3)
        dp4 = dot_product(n,c4)
        Return     (dp1 * dp2 <= 0) Or
                (dp2 * dp3 <= 0) Or
                (dp3 * dp4 <= 0)
    End Function

    ' helper functions
    Function rotate_vector_90:vector2d(v:vector2d)
        Local r:vector2d = New vector2d()
        r.x = -v.y
        r.y = v.x
        Return r
    End Function

    Function add_vector:vector2d(a:vector2d,b:vector2d)
        Local r:vector2d = New vector2d(0,0)
        r.x = a.x + b.x
        r.y = a.y + b.y
        Return r
    End Function  

    Function subtract_vector:vector2d(a:vector2d,b:vector2d)
        Local r:vector2d = New vector2d()
        r.x = a.x - b.x
        r.y = a.y - b.y
        Return r
    End Function

    Function dot_product:Float(a:vector2d,b:vector2d)
        Return a.x * b.x + a.y * b.y 
    End Function    
End Class

Class vector2d
    ' x and y hold the position of the vector
    Field x:Int,y:Int
    Method New(x:Int,y:Int)
        Self.x = x
        Self.y = y
    End Method
End Class

Class line
    Field base:vector2d
    Field direction:vector2d
    Method New(base:vector2d,direction:vector2d)
        Self.base = base
        Self.direction = direction
    End Method
    Method draw()
        DrawLine base.x,base.y,direction.x,direction.y
    End Method
End Class

Class rectangle
    ' origin is a vector that holds the x and y coordinates
    ' of the rectangle
    Field origin:vector2d
    ' size is a vector that holds the x(width) and 
    ' y(height) of the rectangle
    Field size:vector2d
    Method New(origin:vector2d,size:vector2d)
        Self.origin = origin
        Self.size = size
    End Method
    Method draw()
        ' x1,y1,w,h are used to hold the rectangle
        ' location and size.
        Local x1:Int=origin.x
        Local y1:Int=origin.y
        Local w:Int=size.x
        Local h:Int=size.y
        DrawRect x1,y1,w,h
    End Method
End Class

' col is the caller of the class collision. 
' we use it to use the collision methods.
Global col:collision = New collision

Class MyGame Extends App
    Field angle:Float
    Method OnCreate()
    End Method
    Method OnUpdate()
        If angle > 360 Then angle = 0  
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        ' this is a line setup
        Local base:vector2d = New vector2d(100,0)
        Local direction:vector2d = New vector2d(MouseX,MouseY)
        Local line1:line = New line(base,direction)
        ' This is the rectangle
        Local origin:vector2d = New vector2d(320,200)
        Local size:vector2d = New vector2d(100,100)
        Local rect1:rectangle = New rectangle(origin,size)

        ' Here we test for collision
        If col.line_rectangle_collide(line1,rect1)
            DrawText "Line vs Rectangle Collision",0,0
            DrawText "Line vs Rectangle NO Collision",0,0
        End If
        ' Here we draw the line and rectangle
        DrawText "Move the Mouse to test for Collision",DeviceWidth/2,0
        DrawText "Info : Lines are infinite in length.",DeviceWidth/2,20
    End Method
End Class

Function Main()
    New MyGame()
End Function
Posted 2 weeks ago #74

What the code is supposed to be doing is to check if any of the 4 corners of the rectangle are on either side of the line. If they are then there is no collision.

I can not find why the problem is happening. It might take me a while to figure out the problem.

In the meanwhile if you wish you could check the code in the zip below. Maybe you can find a typo. It it the original code from the book.