How To Move A Shape

This article will show you how to move a shape in SwinGame.

Level

Expert

Details


  1. Make sure your code is opening SwinGame's graphic window
  2. Clear the screen
  3. Draw a shape that you want to move on the screen
  4. Move the shape by calling Keydown with appropriate parameter like vk_UP, vk_DOWN etc depend on which direction you want it to move to
  5. Refresh Screen
  6. Make sure you release all resources at the end of your program to prevent memory leak
  7. Run the program

On the right is a short video clip showing the program created from the code below.

Source Code Widget

  • program HowToMoveAShape;
    uses SwinGame,sgTypes;
    
    procedure Main();
    var
        r : Rectangle;
    begin
        r := RectangleFrom(140 ,110 ,40 ,20 );
        OpenGraphicsWindow('How To Move A Shape' ,320 ,240 );
        LoadDefaultColors();
        repeat
            ProcessEvents();
            if  KeyDown(vk_UP )    then
            begin
                r.y -= 1;
                if  r.y < -20   then
                    r.y := 240;
            end;
            if  KeyDown(vk_DOWN )    then
            begin
                r.y += 1;
                if  r.y > 240   then
                    r.y := -20;
            end;
            if  KeyDown(vk_LEFT )    then
            begin
                r.x -= 1;
                if  r.x < -40   then
                    r.x := 320;
            end;
            if  KeyDown(vk_RIGHT )    then
            begin
                r.x += 1;
                if  r.x > 320   then
                    r.x := -40;
            end;
            ClearScreen(ColorWhite );
            FillRectangle(ColorGreen ,r );
            RefreshScreen(60 );
        
        until WindowCloseRequested();
        ReleaseAllResources();
    end;
    begin
        Main();
    end.
    Download Now"
  • #include 
    #include "SwinGame.h"
    int main() 
    {
        rectangle  r;
        r = rectangle_from(140 ,110 ,40 ,20 ) ;
        open_graphics_window("How To Move A Shape" ,320 ,240 );
        load_default_colors();
        do 
        {
            process_events();
            if ( key_down(VK_UP )  )
            {
                (r).y -= 1 ;
                if ( (r).y < -20  )
                    (r).y = 240 ;
            }
            if ( key_down(VK_DOWN )  )
            {
                (r).y += 1 ;
                if ( (r).y > 240  )
                    (r).y = -20 ;
            }
            if ( key_down(VK_LEFT )  )
            {
                (r).x -= 1 ;
                if ( (r).x < -40  )
                    (r).x = 320 ;
            }
            if ( key_down(VK_RIGHT )  )
            {
                (r).x += 1 ;
                if ( (r).x > 320  )
                    (r).x = -40 ;
            }
            clear_screen(color_white );
            fill_rectangle(color_green ,r );
            refresh_screen(60 );
        }
        while ( ! (window_close_requested() ) );
        release_all_resources();
        return 0;
    }
    
    Download Now"
  • Source Code Coming Soon

  • Source Code Coming Soon

  • Source Code Coming Soon


Possibilities

  • Move game object, rotate object
  • Make object collide with each other