Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Scaling and Rotating
#1
Just a simple scaling and rotating  Cool

Possible use in games, such as
- Space Invaders, Gargoyle Attack
- Tetris

Code:
'==========================================
'       SIMPLE SCALING AND ROTATING
'
'Possible use in games, such as
'- Space Invaders, Gargoyle Attack
'- Tetris
'
'Limitation
'Rotate : only multiplication of 90 degree
'==========================================


'----------------
' INITIALIZATION
'----------------
set window "Pixel Art", 150, 150, false,4
set redraw off

'Color definition
visible cBlack  = [0,0,0]
visible cGreen  = [0,255,0]

'Sprite class definition
Sprite =
[
    '--- Properties ---
    data: [
    [0,0,0,0,0,0,0,0],
    [0,0,1,0,0,1,0,0],
    [0,1,1,1,1,1,1,0],
    [0,1,1,0,0,1,1,0],
    [0,0,1,1,1,1,0,0],
    [0,1,1,1,1,1,1,0],
    [0,1,0,0,0,0,1,0],
    [0,0,0,0,0,0,0,0]
    ],
   
    img: 0, x:0, y:0,    
   
    '--- Methods ---
    Transform_Scale : function(s)
                        this.img = Scale(this.data,s)
                        this.x   = (width(primary)-8*s)/2
                        this.y   = (height(primary)-8*s)/2
                       endfunc, 
    Transform_Rotate : function(s,r)
                        this.img = Rotate(this.data,s,r)
                        this.x   = (width(primary)-8*s)/2
                        this.y   = (height(primary)-8*s)/2
                       endfunc
]

'--------------
' MAIN PROGRAM
'--------------
TitleScreen()
scale   = 1
rotate  = 0
do
    'Prepare screen
    set color cBlack; cls
    set color cGreen

    'Draw Sprite
    'Sprite.Transform_Scale(scale)
    Sprite.Transform_Rotate(scale,rotate)
    draw image Sprite.img,Sprite.x,Sprite.y

    'Pause and wait until SPACE BAR is pressed, ESCAPE to quit
    set caret width(primary)/2, height(primary)-20
    center "SPACE BAR"; redraw
    do;wait 1;if keydown(KEY_ESCAPE,true) end;until keydown(KEY_SPACE,true)
    if scale < 10 then
        scale = scale + 1
    else
        scale = 1
    endif
    if rotate < 3 then
        rotate = rotate + 1
    else
        rotate = 0
    endif  

    free image Sprite.img 'free image from memory
loop

'-----------
' FUNCTIONS
'-----------
function Scale(data,s)
    img = createimage(8*s,8*s); set image img
    for y = 0 to 7
        for x = 0 to 7
            if data[y][x] then
                set color cGreen
            else 
                set color cBlack
            endif
            draw rect x*s,y*s,s,s,1
        next
    next
    set image primary; return img
endfunc

function Rotate(data,s,r)
    img = createimage(8*s,8*s); set image img
    for y = 0 to 6
        for x = 0 to 6
            if data[y][x] then
                set color cGreen
            else 
                set color cBlack
            endif
                       
            '(a,b)  = center point of rotation
            '(x,y)  is rotated into (m,n)
            'd      = angle of rotation in radian
            'm      = cos(d)*(x-a)-sin(d)*(y-b)+a
            'n      = sin(d)*(x-a)+cos(d)*(y-b)+b
            a = 3
            b = 3
            d = r*90*(22/7)/180
            m = (round((cos(d)*(x-a))-(sin(d)*(y-b))+a))
            n = (round((sin(d)*(x-a))+(cos(d)*(y-b))+b))
                     
            draw rect m*s,n*s,s,s,1
        next
    next
    set image primary; return img
endfunc

function TitleScreen()
    set caret width(primary)/2,5
    center "Scaling and"
    center "Rotating"
    set caret width(primary)/2, height(primary)-20
    center "Press ENTER"
    redraw
    do;wait 1;until keydown(KEY_RETURN,true)
endfunc
Reply


Messages In This Thread
Simple Scaling and Rotating - by 1micha.elok - 02-28-2024, 11:57 AM
RE: Simple Scaling and Rotating - by kevin - 02-28-2024, 04:36 PM
RE: Simple Scaling and Rotating - by Marcus - 02-28-2024, 04:51 PM
RE: Simple Scaling and Rotating - by johnno56 - 02-28-2024, 06:08 PM
RE: Simple Scaling and Rotating - by aliensoldier - 02-28-2024, 09:58 PM
RE: Simple Scaling and Rotating - by 1micha.elok - 02-29-2024, 11:07 AM
RE: Simple Scaling and Rotating - by johnno56 - 03-01-2024, 04:33 AM
RE: Simple Scaling and Rotating - by 1micha.elok - 03-04-2024, 01:11 AM

Forum Jump:


Users browsing this thread: 4 Guest(s)