Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Game Library Sneak Peek
#5
I have some proof of concept code working. It's coming along very nicely. This little bit of code below sets up a sprite for user movement around the screen.

Keys:
    RIGHT/LEFT ARROW KEYS - move Mario back and forth across the screen
    UP/DOWN ARROW KEYS - zoom Mario in and out
    ESC KEY - exit demo and return to operating system

The ZIP file included below contains the demo code, Sprite_Test_1.BAS, and a folder named LIB that contains all of the external library files needed. Simply unzip the .ZIP file into your QB64pe root directory.

This library is still very much a work in progress but feel free to play around with it and the other libraries and individual library demo code included in the LIB folder.

Code: (Select All)
OPTION _EXPLICIT

'$INCLUDE:'.\lib\sprite\lib_sprite.bi'  library variable declarations

' -----------------
'| Begin test code |
' -----------------

DIM SHT_mario AS INTEGER '      sprite sheet
DIM IMG_standing AS INTEGER '  sprite standing still image
DIM SPR_mario AS INTEGER '      sprite
DIM CLP_walking AS INTEGER '    sprite walking animation clip
DIM AS INTEGER x1, y1, x2, y2 ' sprite rectangular area

' ----------------------
'| Create sprite sheets |
' ----------------------

SHT_mario = SHEET_Load(".\lib\sprite\mario32x32.png", 32, 32) ' load the mario sprite sheet with assigned 32x32 grid

' ---------------------
'| Create still images |
' ---------------------

IMG_standing = IMAGE_Make(SHT_mario, 1, 1) ' get standing image from sprite sheet at column 1 row 1

' ------------------------
'| Create animation clips |
' ------------------------

'Created from sprite sheet at column 2, row 1, 3 animation cells, forward animation, looping animation, 10 FPS

CLP_walking = CLIP_Make(SHT_mario, 2, 1, 3, LIB_FORWARD, LIB_LOOP, 10) ' create walking animation clip from sprite sheet

' ----------------
'| Define sprites |
' ----------------

SPR_mario = SPRITE_Make% '                  create the sprite
SPRITE_ImageApply SPR_mario, IMG_standing ' apply a still image to the sprite
SPRITE_ClipApply SPR_mario, CLP_walking '  apply an animation clip to the sprite
SPRITE_Put SPR_mario, 319, 239 '            set the x,y location of the sprite
SPRITE_Justify SPR_mario, LIB_CENTER '      center the sprite on the sprite's x,y location

' --------------
'| Define world |
' --------------

SCREEN_Make 640, 480 ' create layered graphics screen
SPRITE_GlobalFPS 60 '  screen updates will happen 60 times per second

' -----------------
'| Begin game loop |
' -----------------

DO '                                                                                            begin game loop
    SPRITE_LimitFPS '                                                                            limit loop to global FPS
    IF _KEYDOWN(19712) THEN '                                                                    RIGHT ARROW key pressed?
        SPRITE_Flip SPR_mario, LIB_NONE '                                                        yes, don't flip the sprite
        SPRITE_Mode SPR_mario, LIB_ANIMATE '                                                    animate the sprite
        SPRITE_PutX SPR_mario, SPRITE_X(SPR_mario) + SPRITE_Zoom(SPR_mario) * 2 '                update sprite's x position
    ELSEIF _KEYDOWN(19200) THEN '                                                                no, LEFT ARROW pressed?
        SPRITE_Flip SPR_mario, LIB_HORIZONTAL '                                                  yes, flip the sprite horizontally
        SPRITE_Mode SPR_mario, LIB_ANIMATE '                                                    animate the sprite
        SPRITE_PutX SPR_mario, SPRITE_X(SPR_mario) - SPRITE_Zoom(SPR_mario) * 2 '                update sprite's x position
    ELSE '                                                                                      neither RIGHT or LEFT ARROW keys pressed
        SPRITE_Mode SPR_mario, LIB_STILL '                                                      sprite is now a still image
    END IF
    IF _KEYDOWN(18432) THEN '                                                                    UP ARROW key pressed?
        IF SPRITE_Zoom(SPR_mario) < 8 THEN SPRITE_Zoom SPR_mario, SPRITE_Zoom(SPR_mario) + .1 '  yes, zoom the sprite larger
    ELSEIF _KEYDOWN(20480) THEN '                                                                no, DOWN ARROW key pressed?
        IF SPRITE_Zoom(SPR_mario) > .5 THEN SPRITE_Zoom SPR_mario, SPRITE_Zoom(SPR_mario) - .1 ' yes, zoom the sprite smaller
    END IF
    SPRITE_Area SPR_mario, x1, y1, x2, y2 '                                                      get rectangular area of sprite
    IF x1 < 0 THEN '                                                                            sprite beyond left side of screen?
        SPRITE_PutX SPR_mario, SPRITE_X(SPR_mario) + ABS(x1) '                                  yes, put it back on screen
    ELSEIF x2 > 639 THEN '                                                                      no, sprite beyond right side of screen?
        SPRITE_PutX SPR_mario, SPRITE_X(SPR_mario) - (x2 - 639) '                                yes, put it back on screen
    END IF
    SPRITE_Display '                                                                            update screen
LOOP UNTIL _KEYDOWN(27) '                                                                        leave when ESC key pressed
SYSTEM '                                                                                        return to operating system

' ---------------
'| End test code |
' ---------------

'$INCLUDE:'.\lib\sprite\lib_sprite.bm'    library subroutines and functions


Attached Files
.zip   GAMELIB.zip (Size: 207.3 KB / Downloads: 14)
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply


Messages In This Thread
Game Library Sneak Peek - by TerryRitchie - 08-10-2024, 06:42 PM
RE: Game Library Sneak Peek - by Pete - 08-10-2024, 06:56 PM
RE: Game Library Sneak Peek - by TerryRitchie - 08-10-2024, 07:01 PM
RE: Game Library Sneak Peek - by Pete - 08-10-2024, 07:25 PM
RE: Game Library Sneak Peek - by TerryRitchie - 08-19-2024, 04:07 PM
RE: Game Library Sneak Peek - by SpriggsySpriggs - 08-19-2024, 05:34 PM
RE: Game Library Sneak Peek - by TerryRitchie - 08-19-2024, 06:49 PM
RE: Game Library Sneak Peek - by NakedApe - 08-19-2024, 07:35 PM



Users browsing this thread: 7 Guest(s)