Skip to content

Instantly share code, notes, and snippets.

@PhilipWitte
Created June 9, 2015 02:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PhilipWitte/a38afecf9dddad4cdd16 to your computer and use it in GitHub Desktop.
Save PhilipWitte/a38afecf9dddad4cdd16 to your computer and use it in GitHub Desktop.
type
Sprite {.pure inheritable.} = object
x, y: float
image: string
Fighter = object of Sprite
health: float
strength: float
proc move(s:var Sprite, x, y:float) =
s.x += x
s.y += y
proc touch(f:var Fighter, other:Fighter) =
f.health -= other.strength
if f.health < 0:
echo "Ship ", f.image, " dead!"
proc print(f:Fighter) =
echo f.x, ", ", f.y, ", ", f.image, ", ", f.health, ", ", f.strength
proc newFighter(x, y:float, image:string, health, strength:float): Fighter =
when Fighter is ref:
result.new()
result.x = x
result.y = y
result.image = image
result.health = health
result.strength = strength
var
tie = newFighter(1, 2, "Tie Fighter", 1, 0.1)
xwing = newFighter(3, 4, "Rebel XWing", 1, 0.5)
echo sizeof Sprite
echo sizeof Fighter
tie.print()
xwing.print()
tie.move(10, 20)
xwing.move(5, 3)
tie.touch(xwing)
xwing.touch(tie)
tie.print()
xwing.print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment