How To Center Camera On A Sprite

This article will show you how to center the game camera to a certain position on the screen.

Level

Advanced

Details

In many 2d games the player will be a sprite, so it can be useful to center the screen on the player’s position. Camera’s CenterCameraOn can be used to center a sprite on the screen.

This how to exhibited the use of SpriteSetY, SpriteSetX, SpriteX, SpriteY to move the sprite around the virtual world and have a user centering on a sprite when the key Space is pressed.

The video display on the right showing the program created from the code below. It gives example to how to center a camera on a sprite.



Source Code Widget

  • program HowToCenterCameraOnASprite;
    uses SwinGame,sgTypes;
    
    procedure Main();
    var
        sSprite : Sprite;
    begin
        OpenGraphicsWindow('Center Camera On A Sprite' ,800 ,600 );
        LoadDefaultColors();
        LoadBitmapNamed('ufo' ,'ufo.png' );
        sSprite := CreateSprite(BitmapNamed('ufo' )  );
        SpriteSetX(sSprite ,60 );
        SpriteSetY(sSprite ,60 );
        repeat
            ProcessEvents();
            ClearScreen(ColorWhite );
            if  KeyDown(vk_UP )    then
                SpriteSetY(sSprite ,SpriteY(sSprite )  -1 );
            if  KeyDown(vk_DOWN )    then
                SpriteSetY(sSprite ,SpriteY(sSprite )  +1 );
            if  KeyDown(vk_LEFT )    then
                SpriteSetX(sSprite ,SpriteX(sSprite )  -1 );
            if  KeyDown(vk_RIGHT )    then
                SpriteSetX(sSprite ,SpriteX(sSprite )  +1 );
            FillRectangle(RGBColor(205 ,201 ,201 )  ,-150 ,250 ,1150 ,20 );
            FillRectangle(RGBColor(205 ,201 ,201 )  ,-150 ,330 ,1150 ,20 );
            FillRectangle(RGBColor(255 ,255 ,0 )  ,-150 ,290 ,50 ,20 );
            FillRectangle(RGBColor(124 ,252 ,0 )  ,-50 ,290 ,50 ,20 );
            FillRectangle(RGBColor(184 ,134 ,11 )  ,50 ,290 ,50 ,20 );
            FillRectangle(RGBColor(0 ,255 ,255 )  ,150 ,290 ,50 ,20 );
            FillRectangle(RGBColor(255 ,165 ,0 )  ,250 ,290 ,50 ,20 );
            FillRectangle(RGBColor(255 ,192 ,203 )  ,350 ,290 ,50 ,20 );
            FillRectangle(RGBColor(160 ,32 ,240 )  ,450 ,290 ,50 ,20 );
            FillRectangle(RGBColor(165 ,42 ,42 )  ,550 ,290 ,50 ,20 );
            FillRectangle(RGBColor(240 ,230 ,140 )  ,650 ,290 ,50 ,20 );
            FillRectangle(RGBColor(0 ,0 ,128 )  ,750 ,290 ,50 ,20 );
            FillRectangle(RGBColor(245 ,255 ,250 )  ,850 ,290 ,50 ,20 );
            FillRectangle(RGBColor(255 ,228 ,225 )  ,950 ,290 ,50 ,20 );
            FillRectangle(RGBColor(205 ,201 ,201 )  ,320 ,-275 ,20 ,1150 );
            FillRectangle(RGBColor(205 ,201 ,201 )  ,510 ,-275 ,20 ,1150 );
            if  KeyTyped(vk_SPACE )    then
                CenterCameraOn(sSprite ,0 ,0 );
            DrawTextOnScreen(PointToString(CameraPos()  )  ,ColorBlack ,0 ,0 );
            DrawSprite(sSprite );
            RefreshScreen();
        
        until WindowCloseRequested();
        FreeSprite(sSprite );
        ReleaseAllResources();
    end;
    begin
        Main();
    end.
    Download Now"
  • #include 
    #include "SwinGame.h"
    int main() 
    {
        sprite  s_sprite;
        open_graphics_window("Center Camera On A Sprite" ,800 ,600 );
        load_default_colors();
        load_bitmap_named("ufo" ,"ufo.png" );
        s_sprite = create_sprite(bitmap_named("ufo" ) ) ;
        sprite_set_x(s_sprite ,60 );
        sprite_set_y(s_sprite ,60 );
        do 
        {
            process_events();
            clear_screen(color_white );
            if ( key_down(VK_UP )  )
                sprite_set_y(s_sprite ,sprite_y(s_sprite ) -1 );
            if ( key_down(VK_DOWN )  )
                sprite_set_y(s_sprite ,sprite_y(s_sprite ) +1 );
            if ( key_down(VK_LEFT )  )
                sprite_set_x(s_sprite ,sprite_x(s_sprite ) -1 );
            if ( key_down(VK_RIGHT )  )
                sprite_set_x(s_sprite ,sprite_x(s_sprite ) +1 );
            fill_rectangle(rgbcolor(205 ,201 ,201 ) ,-150 ,250 ,1150 ,20 );
            fill_rectangle(rgbcolor(205 ,201 ,201 ) ,-150 ,330 ,1150 ,20 );
            fill_rectangle(rgbcolor(255 ,255 ,0 ) ,-150 ,290 ,50 ,20 );
            fill_rectangle(rgbcolor(124 ,252 ,0 ) ,-50 ,290 ,50 ,20 );
            fill_rectangle(rgbcolor(184 ,134 ,11 ) ,50 ,290 ,50 ,20 );
            fill_rectangle(rgbcolor(0 ,255 ,255 ) ,150 ,290 ,50 ,20 );
            fill_rectangle(rgbcolor(255 ,165 ,0 ) ,250 ,290 ,50 ,20 );
            fill_rectangle(rgbcolor(255 ,192 ,203 ) ,350 ,290 ,50 ,20 );
            fill_rectangle(rgbcolor(160 ,32 ,240 ) ,450 ,290 ,50 ,20 );
            fill_rectangle(rgbcolor(165 ,42 ,42 ) ,550 ,290 ,50 ,20 );
            fill_rectangle(rgbcolor(240 ,230 ,140 ) ,650 ,290 ,50 ,20 );
            fill_rectangle(rgbcolor(0 ,0 ,128 ) ,750 ,290 ,50 ,20 );
            fill_rectangle(rgbcolor(245 ,255 ,250 ) ,850 ,290 ,50 ,20 );
            fill_rectangle(rgbcolor(255 ,228 ,225 ) ,950 ,290 ,50 ,20 );
            fill_rectangle(rgbcolor(205 ,201 ,201 ) ,320 ,-275 ,20 ,1150 );
            fill_rectangle(rgbcolor(205 ,201 ,201 ) ,510 ,-275 ,20 ,1150 );
            if ( key_typed(VK_SPACE )  )
                center_camera_on(s_sprite ,0 ,0 );
            draw_text_on_screen(point_to_string(camera_pos() ) ,color_black ,0 ,0 );
            draw_sprite(s_sprite );
            refresh_screen();
        }
        while ( ! (window_close_requested() ) );
        free_sprite(s_sprite );
        release_all_resources();
        return 0;
    }
    
  • Source Code Coming Soon

  • Source Code Coming Soon

  • Source Code Coming Soon

Possibilities

  • Abilities to focus on certain position of the game world by centering the camera.