Magic Lantern Forum

Developing Magic Lantern => Scripting Corner => Scripting Q&A => Topic started by: a1ex on February 24, 2013, 10:47:55 PM

Title: First game in PicoC - Sokoban
Post by: a1ex on February 24, 2013, 10:47:55 PM
Since PicoC has now functions for simple graphics and can detect button presses, why not try to write a small game?

Source - sokoban.c (https://bitbucket.org/hudson/magic-lantern/src/tip/scripts/sokoban.c)

Don't expect fancy graphics; it's just a basic demo from which - I hope - you can learn some nice scripting tricks.

(http://a1ex.magiclantern.fm/bleeding-edge/sokoban.png)

Feel free to add some new levels.
Title: Re: First game in PicoC - Sokoban
Post by: Marsu42 on February 25, 2013, 12:03:23 AM
Quote from: a1ex on February 24, 2013, 10:47:55 PM
Since PicoC has now functions for simple graphics and can detect button presses, why not try to write a small game?

Could we please have Pong, complete mit "beep" sound and computer opponent :-) ? It's *the* classic and would really make a great ml demo, I saw you already have the more graphic routines added but not implemented. https://en.wikipedia.org/wiki/Pong

(http://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Pong.png/286px-Pong.png)
Title: Re: First game in PicoC - Sokoban
Post by: SDX on February 25, 2013, 08:45:37 AM
Properly the main reason for adding the drawing routines is, that some who had making a minigame in mind requested it - or am I wrong on that one? (I'm at least one of those).

Original pong isn't such a big deal once one has figured how to get smooth timing ;)

Title: Re: First game in PicoC - Sokoban
Post by: a1ex on February 25, 2013, 09:42:33 AM
Or, instead of smooth timing, one may assume variable timing for simulation, and sync with the DIGIC clock (which is very accurate).

For Pong, the API lacks a method to detect keys in a non-blocking way (currently get_key is blocking until you press something). Other than that, shouldn't be very hard.
Title: Re: First game in PicoC - Sokoban
Post by: wolf on February 25, 2013, 09:24:09 PM
Wow
...And I was always afraid of the day that a EOS offers the possibility to play a game. ;-)
The new picoc functions and picoc in generally are really great and I'm sure that some good and helpful scripts will follow.

Didn't heard of sokoban yet, but it's really nice...


Title: Re: First game in PicoC - Sokoban
Post by: a1ex on February 25, 2013, 11:15:51 PM
Timing for simple animations is not a problem at all :)


// Animation demo

void main()
{
    console_hide();
    set_gui_mode(1); // Play
   
    fill_rect(0, 0, 720, 480, COLOR_EMPTY);
   
    int x = 320;
    int y = 240;
    int r = 10;
   
    int dx = 0;
    int dy = 0;
   
    while(1)
    {
        int key = last_key();
       
        switch(key)
        {
            case LEFT:
                dx--;
                break;
            case RIGHT:
                dx++;
                break;
            case UP:
                dy--;
                break;
            case DOWN:
                dy++;
                break;
        }

        fill_circle(x, y, r, COLOR_EMPTY);
       
        x += dx;
        y += dy;
       
        // collisions are not exact
        if (x < r) { x = r; dx = -dx; }
        else if (x > 720-r) { x = 720-r; dx = -dx; }
        if (y < r) { y = r; dy = -dy; }
        else if (y > 480-r) { y = 480-r; dy = -dy; }
       
        fill_circle(x, y, r, COLOR_WHITE);
       
        sleep(0.01);
    }
}

main();

Title: Re: First game in PicoC - Sokoban
Post by: Alia5 on March 06, 2013, 05:42:32 PM
Wonder how long it will take untill we can play tetris! :D
Great stuff here!
Title: Re: First game in PicoC - Sokoban
Post by: 1% on March 06, 2013, 06:27:51 PM
Tetris seems like the game to have. Perfect for the arrow keys and wasting time.
Title: Re: First game in PicoC - Sokoban
Post by: Alia5 on March 07, 2013, 02:37:16 PM
Only problem when thinking about Tetris is that get_key is currently blocking :(
Tetris makes no fun if you have to move Blocks down manually...
Title: Re: First game in PicoC - Sokoban
Post by: a1ex on March 07, 2013, 02:40:37 PM
F5, see the sticky post ;)

last_key() is not blocking
Title: Re: First game in PicoC - Sokoban
Post by: Alia5 on March 07, 2013, 02:42:19 PM
Just wanted to edit my post! ;)
Title: Re: First game in PicoC - Sokoban
Post by: obiyan19 on June 25, 2013, 11:13:54 AM
sorry for old reply, but i discovered the sokoban game in the 600d audio built, and i dont have solution for the last (6) level !!!