- Joined
- Jan 2, 2020
- Messages
- 1,280
Here's how to draw a sprite inside the game itself.
I bumped into a problem. The coordinates for the sprite seems out of place. You need to add 1 to the y coordinate (not x only y).
Is this a rounding error, some kind of bug? I include screenshots so you can see the invidiual pixels. One with Y = 0 (where you cannot see the top pixel line), and one with Y=1 where you can just see the perfect sprite.
I bumped into a problem. The coordinates for the sprite seems out of place. You need to add 1 to the y coordinate (not x only y).
Is this a rounding error, some kind of bug? I include screenshots so you can see the invidiual pixels. One with Y = 0 (where you cannot see the top pixel line), and one with Y=1 where you can just see the perfect sprite.
Code:
Import mojo2
Function Main()
New MyApp
End
Class MyApp Extends App
' Define sprite
Field sprite:Image ' sprite is what we use to refer to when we want to draw the sprite *TO* the main canvas.
Field spritecanvas:Canvas ' spritecanvas is what we use to refer to when we want to draw *TO* the sprite.
Field def:String
Field canvas : Canvas
Method OnCreate()
canvas = New Canvas
SetUpdateRate 0
' Create Sprite
sprite=New Image(16,16,0,0,0) ' Align to upper left corner and no AA.
spritecanvas=New Canvas(sprite)
createSprite(spritecanvas)
End
Method OnRender()
canvas.Clear
canvas.DrawImage sprite,0,0 ' HERE IS THE PROBLEM! Why is 1 needed for y to see the whole sprite and not 0?
canvas.Flush
End
' CREATE A SPRITE! HAVE FUN!
Method createSprite(cp:Canvas)
cp.Clear 0,0,0,0
' CYAN
cp.SetColor 0,1,1
def = " * **** * " ' Put something distinct to see the top part of the sprite.
def = def + " ******** "
def = def + " ********** "
def = def + " ************ "
def = def + " ************ "
def = def + " ************ "
def = def + "************** "
def = def + "************** "
def = def + "************** "
def = def + "************** "
def = def + "************** "
def = def + "************** "
def = def + "************** "
def = def + "************** "
def = def + "** *** *** ** "
def = def + "* ** ** * "
Local tempx:Int = 0, tempy:Int = 0
For Local temp:Int = 1 To Len(def)
If Mid(def,temp,1) = "*" Then cp.DrawRect tempx,tempy,1,1
tempx = tempx + 1 ; If tempx >= 16 Then tempx=tempx-16 ; tempy = tempy + 1
Next
' BLACK
cp.SetColor 0,0,0
cp.DrawOval 1,4,4,5
cp.DrawOval 8,4,4,5
cp.DrawLine 4,12,9,12
' WHITE
cp.SetColor 1,1,1
cp.DrawOval 1,4,2,2
cp.DrawOval 8,4,2,2
cp.Flush
End
Function Len:Int(s:String)
Return s.Length
End
Function Mid:String(s:String,p:Int,n:Int)
p=p-1 ; Return s[(p)..(p+n)]
End
End
Attachments
Last edited: