Skip to content

Instantly share code, notes, and snippets.

@jessefreeman
Created May 28, 2016 18:16
Show Gist options
  • Select an option

  • Save jessefreeman/481fb7f8288cd93a4a8239b0f647fc2b to your computer and use it in GitHub Desktop.

Select an option

Save jessefreeman/481fb7f8288cd93a4a8239b0f647fc2b to your computer and use it in GitHub Desktop.
Sample code showing off how PixelVision8 (http://pixelvision8.com) works to display and animate a sprite and capture player imput.
local apiBridge = LuaBridge()
-- Gameboy Game
-- Resolution 160 x 144
-- Max sprites: 40
-- variables for game
animDelay = 0
animTime = .5
currentAnim = 0
totalFrames = 2
dir = 0
flipH = false
x = 50
y = 50
function init()
-- change the bg color
apiBridge:ChangeBackgroundColor(2)
end
function update()
-- capture imput
if(apiBridge:ButtonDown(0, 0)) then
-- show back of player
dir = 1
flipH = false
elseif (apiBridge:ButtonDown(1, 0)) then
-- show front of player
dir = 0
flipH = false
elseif(apiBridge:ButtonDown(2, 0)) then
-- show side of player and toggle flip
dir = 2
flipH = true
elseif (apiBridge:ButtonDown(3, 0)) then
-- show side of player and toggle flip
dir = 2
flipH = false
end
-- update animation timer
animDelay = animDelay + apiBridge.delta
-- see if we should change animation
if(animDelay > animTime) then
-- reset the delay
animDelay = 0
-- update the current frame
currentAnim = currentAnim + 1
-- make sure we reset the frame counter when out of frames
if(currentAnim >= totalFrames) then
currentAnim = 0
end
end
end
function draw()
-- clear the display
apiBridge:Clear()
-- calcualte the correct sprite for the player
spriteID = currentAnim + (dir * totalFrames)
-- draw sprite to display
apiBridge:DrawSprite(spriteID , x, y, flipH, false, true)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment