Use left/right mouse buttons to change health amount.
setVirtualResolution(640,480)
health = createSprite(healthBar(300, 50))
t# = 1.0
do
if getRawMouseRightState()
t# = t# - 0.05
if t# < 0 then t# = 0
endif
if getRawMouseLeftState()
t# = t# + 0.05
if t# > 1 then t# = 1
endif
setHealthBar(health, getpointerx(), getpointery()-50, t#)
sync()
loop
function setHealthBar(spr, x, y, p#)
setSpritePosition(spr, x, y)
w = getSpriteWidth(spr)*p#
if w < 1 then w = 1
h = getSpriteHeight(spr)
setSpriteScissor(spr, x, y, x+w, y+h)
endfunction
function healthBar(width, height)
mem = 1
memSize = width*height*4 + 12
createMemblock(mem, memSize)
setMemblockInt(mem, 0, width)
setMemblockInt(mem, 4, height)
setMemblockInt(mem, 8, 32)
seg# = width / 3
red = argb(255, 255, 0, 0)
orange = argb(255, 255, 128, 0)
yellow = argb(255, 255, 255, 0)
green = argb(255, 0, 255, 0)
white = argb(255,255,255,255)
h2 = height / 2.0
x1 = 0
x2 = seg#-1
for x = x1 to x2
p# = (x-x1) / seg#
base = getColor(red, orange, p#)
h# = height-1
for y = 0 to height-1
k# = (y+0.0) / h2
k# = 0.8 - 0.695*k#
c = getColor(base, white, k#)
if y > h2 then c = base
pos = (y*width + x)*4 + 12
setMemblockInt(mem, pos, c)
next y
next x
x1 = seg#
x2 = seg#*2 - 1
for x = x1 to x2
p# = (x-x1) / seg#
base = getColor(orange, yellow, p#)
for y = 0 to height-1
k# = (y+0.0) / h2
k# = 0.8 - 0.695*k#
c = getColor(base, white, k#)
if y > h2 then c = base
pos = (y*width + x)*4 + 12
setMemblockInt(mem, pos, c)
next y
next x
x1 = seg#*2
x2 = width-1
for x = x1 to x2
p# = (x-x1) / seg#
base = getColor(yellow, green, p#)
for y = 0 to height-1
k# = (y+0.0) / h2
k# = 0.8 - 0.695*k#
c = getColor(base, white, k#)
if y > h2 then c = base
pos = (y*width + x)*4 + 12
setMemblockInt(mem, pos, c)
next y
next x
img = createImageFromMemblock(mem)
deleteMemblock(mem)
endfunction img
function getColor(base, target, p#)
br = base && 0x000000FF
bg = (base && 0x0000FF00) >> 8
bb = (base && 0x00FF0000) >> 16
ba = (base && 0xFF000000) >> 24
tr = target && 0x000000FF
tg = (target && 0x0000FF00) >> 8
tb = (target && 0x00FF0000) >> 16
ta = (target && 0xFF000000) >> 24
r = br + (tr-br)*p#
g = bg + (tg-bg)*p#
b = bb + (tb-bb)*p#
a = ba + (ta-ba)*p#
c = argb(255, r, g, b)
endfunction c
function argb(alpha, red, green, blue)
base = (alpha*16777216) + (blue*65536) + (green*256) + red
endfunction base
Here's a new version with a little animation to show chunks of the health bar cut away. Not my most modular code ever, but shows a nice example I think. MOUSE buttons to grow/shrink health bar, press SPACE to cut away a chunk.
V.2
setVirtualResolution(640,480)
hWidth = 300
health = createSprite(healthBar(hWidth,50))
t# = 1.0
thing = 0
do
if getRawMouseRightState()
t# = t# - 0.05
if t# < 0 then t# = 0
endif
if getRawMouseLeftState()
t# = t# + 0.05
if t# > 1 then t# = 1
endif
setHealthBar(health, getpointerx(), getpointery()-50, t#)
if getrawkeypressed(32) = 1 and thing = 0
x = getpointerx()
y = getpointery()-50
e = hWidth*t#
s = e - 30
chunk# = 30.0 / hWidth
if t# > chunk#
t# = t# - chunk#
base = getSpriteImageId(health)
thing = createSprite(copyImage(base, s, 0, 30, 50))
setSpriteOffset(thing, 0, 50)
setSpritePositionByOffset(thing, x+s, y+50)
angle# = 0
thingY# = y+50
acc# = 0
endif
endif
if thing > 0
angle# = angle# + 6
acc# = acc# + 1
thingY# = thingY# + acc#
alpha = 255 - 255*(angle# / 140)
setSpriteAngle(thing, angle#)
setSpriteColorAlpha(thing, alpha)
setSpritePositionByOffset(thing, getSpriteX(thing), thingY#)
if angle# >= 140
deleteSprite(thing)
thing = 0
endif
endif
print(getImageWidth(base))
print(getImageHeight(base))
sync()
loop
function setHealthBar(spr, x, y, p#)
setSpritePosition(spr, x, y)
w = getSpriteWidth(spr)*p#
if w < 1 then w = 1
h = getSpriteHeight(spr)
setSpriteScissor(spr, x, y, x+w, y+h)
endfunction
function healthBar(width, height)
mem = 1
memSize = width*height*4 + 12
createMemblock(mem, memSize)
setMemblockInt(mem, 0, width)
setMemblockInt(mem, 4, height)
setMemblockInt(mem, 8, 32)
seg# = width / 3
red = argb(255, 255, 0, 0)
orange = argb(255, 255, 128, 0)
yellow = argb(255, 255, 255, 0)
green = argb(255, 0, 255, 0)
white = argb(255,255,255,255)
h2 = height / 2.0
x1 = 0
x2 = seg#-1
for x = x1 to x2
p# = (x-x1) / seg#
base = getColor(red, orange, p#)
h# = height-1
for y = 0 to height-1
k# = (y+0.0) / h2
k# = 0.8 - 0.695*k#
c = getColor(base, white, k#)
if y > h2 then c = base
pos = (y*width + x)*4 + 12
setMemblockInt(mem, pos, c)
next y
next x
x1 = seg#
x2 = seg#*2 - 1
for x = x1 to x2
p# = (x-x1) / seg#
base = getColor(orange, yellow, p#)
for y = 0 to height-1
k# = (y+0.0) / h2
k# = 0.8 - 0.695*k#
c = getColor(base, white, k#)
if y > h2 then c = base
pos = (y*width + x)*4 + 12
setMemblockInt(mem, pos, c)
next y
next x
x1 = seg#*2
x2 = width-1
for x = x1 to x2
p# = (x-x1) / seg#
base = getColor(yellow, green, p#)
for y = 0 to height-1
k# = (y+0.0) / h2
k# = 0.8 - 0.695*k#
c = getColor(base, white, k#)
if y > h2 then c = base
pos = (y*width + x)*4 + 12
setMemblockInt(mem, pos, c)
next y
next x
img = createImageFromMemblock(mem)
deleteMemblock(mem)
endfunction img
function getColor(base, target, p#)
br = base && 0x000000FF
bg = (base && 0x0000FF00) >> 8
bb = (base && 0x00FF0000) >> 16
ba = (base && 0xFF000000) >> 24
tr = target && 0x000000FF
tg = (target && 0x0000FF00) >> 8
tb = (target && 0x00FF0000) >> 16
ta = (target && 0xFF000000) >> 24
r = br + (tr-br)*p#
g = bg + (tg-bg)*p#
b = bb + (tb-bb)*p#
a = ba + (ta-ba)*p#
c = argb(255, r, g, b)
endfunction c
function argb(alpha, red, green, blue)
base = (alpha*16777216) + (blue*65536) + (green*256) + red
endfunction base
"You're all wrong. You're all idiots." ~Fluffy Rabbit