Weekly Qbasic and Qb64 Lesson Topics
March 29, 2024, 03:29:09 am
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Want to see a specific Tutorial? ASK!
 
  Home Help Search Arcade Links Staff List Login Register  

Using Sprite Sheets

Pages: [1]
  Print  
Author Topic: Using Sprite Sheets  (Read 771 times)
Unseen Machine
Post Demos
*
Posts: 46


« on: April 27, 2011, 06:00:11 pm »

Medium/Hard level of tutorial. Not beginner stuff.

Required file : http://dl.dropbox.com/u/8822351/earth.PNG

Part 1 :
Code:
'// Sprite sheet tutorial.
'// This demo requires QB64 and Earth.png - It is medium/advanced and not aimed at the begineer.
'//
'// Sprite sheets are used for simple animation of sprites. Learning how to use sprite sheets is an essential
'// part of programming games.
'// This demo covers how to use both equally and non equally spaced sprites sheet using a rectangle method.
'//
'// Create a graphics screen.
SCREEN _NEWIMAGE(600, 400, 32)
'//
'// First create a type array to hold all the rectangle's variables.
'//
TYPE Rectangle
  X AS INTEGER
  Y AS INTEGER
  Width AS INTEGER
  Height AS INTEGER
END TYPE

'//
'// Load the sprite sheet image into memory
'//
Sprite& = _LOADIMAGE("Earth.png")
'//
'// The sprite we are using has 12 frames, it has 4 frames on the X axis and 3 on the Y axis.
'//
'// So the easiest way to loop through a set of rectangles is to create an array of them.
DIM SpriteImageRect(12) AS Rectangle
'//
'// As we are dealing with an equally spaced sprite sheet we can load all the x/y points in a set of for loops.
'//
FOR j% = 0 TO _HEIGHT(Sprite&) - (_HEIGHT(Sprite&) / 3) STEP (_HEIGHT(Sprite&) / 3)
  FOR i% = 0 TO _WIDTH(Sprite&) - (_WIDTH(Sprite&) / 4) STEP (_WIDTH(Sprite&) / 4)
    SpriteImageRect(rectcnt%).X = i%
    SpriteImageRect(rectcnt%).Y = j%
    SpriteImageRect(rectcnt%).Width = _WIDTH(Sprite&) / 4
    SpriteImageRect(rectcnt%).Height = _HEIGHT(Sprite&) / 3
    rectcnt% = rectcnt% + 1
  NEXT
NEXT
'//
'// Draw the images in turn.
FOR i% = 0 TO 11
  _PUTIMAGE (0, 0), Sprite&, , (SpriteImageRect(i%).X, SpriteImageRect(i%).Y)-(SpriteImageRect(i%).X + SpriteImageRect(i%).Width, SpriteImageRect(i%).Y + SpriteImageRect(i%).Height)
  _DISPLAY
  _DELAY 1
NEXT

'// It's not pretty code at the moment, but we will clean that up in part two and make it usabel for more tha jsut this one sprite.



Report Spam   Logged

Share on Facebook Share on Twitter

GarrisonRicketson
Admin
Administrator
Hero Member
*****
Posts: 583



WWW
« Reply #1 on: September 06, 2011, 08:01:11 pm »

I am just looking thru things, trying to decide what to keep,  some of the stuff is pretty old,...
This is a good demo so it will stay,
from Garry
Report Spam   Logged


Pages: [1]
  Print  
 
Jump to:  

Powered by EzPortal
Bookmark this site! | Upgrade This Forum
SMF For Free - Create your own Forum


Powered by SMF | SMF © 2016, Simple Machines
Privacy Policy