Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rotated and scaled image drawing
#5
(03-03-2024, 10:49 AM)Marcus Wrote: I wrote a quick prototype of drawing images with arbitrary rotation and scaling in n7.
...

TEST DRIVE
Using your transform (scaling) function and polyline, here it is a car racing game.
It is not perfect yet and I don't know how to curve the road

...but it's playable  Big Grin

   
zoom in to enlarge the image


Code:
'=========================================
'            TEST DRIVE
'
'Controls : LEFT, RIGHT
'
'Limitation :
'- I don't know how to make a curved road
'
'Written in :
'N7 24.02.16 release
'
'Sources :
'TitleScreen https://www.imgflip.com
'TitleScreen Background music
'   This World Has Gone Crazy
'   by lemonmusicstudio
'   https://pixabay.com
'Sport Cars https://www.pngfind.com
'=========================================

'----------------
' INITIALIZATION
'----------------
#win32
include "assets/transform.n7"
include "assets/polyline.n7"; visible path= [] ; visible distance =[]; visible move_pixel = []
include "sfx.n7"            ; visible sfx = SFX() 'create an sfx instance
visible font1 = createfont("Arial",20,1,0,0,0)'name,size,bold,italic,underlined,smoothed
randomize clock()
set window "TEST DRIVE", 360, 271,false,2
set redraw off

'color definition
sky   = [51,204,204]
black = [0,0,0]
gray  = [100,100,100]
white = [255,255,255]
green = [51,153,102]

'Create PolyLine for each path
path[0] = PolyLine([[width(primary)/2,height(primary)/2-80],[width(primary)/2    ,height(primary)]])
path[1] = PolyLine([[width(primary)/2,height(primary)/2-80],[width(primary)/2-80,height(primary)]])
path[2] = PolyLine([[width(primary)/2,height(primary)/2-80],[width(primary)/2+80,height(primary)]])
move_pixel[0] = rnd(2,5);move_pixel[1] = rnd(2,5); move_pixel[2] = rnd(2,5);

'Sprite class definition
Sprite =
[
    '--- Properties ---
    img:0,x:0,y:0,dx:20,s:0,a:0,px:0,py:0,distance:1,
   
    '--- Methods ------
    Draw:function(x,y,s,a)
        ' DrawImageTransformed(image, x, y, scale_x, scaleY, angle, pivot_x, pivot_y)
        ' Parameter :
        '   image               = .png format
        '   (x,y)               = location of the image in the screen coordinates
        '   scale_x, scale_y    = scale the image's size
        '   angle               = in radian.
        '   (pivot_x, pivot_y)  = center point of the image. E.g:(0,0) = top left.
        this.x = x; this.y = y ; this.s = s; this.a = a
        this.px = width(this.img)/2; this.py = height(this.img)/2
        DrawImageTransformed(this.img, this.x,this.y, this.s, this.s, this.a, this.px, this.py)
        endfunc,
     XY:function(i)
           this.distance = (this.distance + move_pixel[i])%path[i].GetLength()
           pos = path[i].GetPoint(this.distance,true)     
           this.x = pos[0]
           this.y = pos[1]
         endfunc
]

'Initial Value
Player = copy(Sprite)
Player.img = loadimage("assets/player.png")
Player.x = width(primary)/2
Player.y = height(primary)-15
Player.a = rad(0)'convert degree to radian
Player.s = 1

Car = fill(Sprite,3)
Car[0].img = loadimage("assets/blue.png")
Car[1].img = loadimage("assets/red.png")
Car[2].img = loadimage("assets/yellow.png")

score = 0; lifes = 5; quit =false; collide = false


'---------------
' MAIN LOOP
'---------------
TitleScreen()
PlayTune(1)

do
    'Player is sent to out of screen if collide = true
    if Player.y = -100 then
        while keydown(KEY_RETURN,true)
           Player.y = height(primary)-15 
        wend
    endif

    'Prepare screen : green grass, blue sky, gray road, white lines, score and lifes
    set color green;cls
    set color sky
      draw rect 0,0,width(primary),height(primary)/2-80,1
    set color gray
      road = [width(primary)/2-8,height(primary)/2-80,width(primary)/2+8,height(primary)/2-80
             ,width(primary)/2+120,height(primary),width(primary)/2-120,height(primary)]
      draw poly road,1
    set color white
      draw line 0                 ,height(primary)/2-80 ,width(primary)         ,height(primary)/2-80
      draw line width(primary)/2  ,height(primary)/2-80 ,width(primary)/2       ,height(primary)
      draw line width(primary)/2-8,height(primary)/2-80 ,width(primary)/2-120   ,height(primary)
      draw line width(primary)/2+8,height(primary)/2-80 ,width(primary)/2+120   ,height(primary)
    set font font1
      set caret 10,5;wln "Score : "+score
      if Player.y = -100 then; set caret 120,100;wln "ENTER TO PLAY";endif
      set caret width(primary)-75,5;wln "Lifes : "+lifes 
      score = score + 1   
   
    'Check left and right Player's boundary
    if Player.x > 120 and Player.x < 240 then
       boundary = true
    elseif Player.x<= 120 then
       boundary = false
       Player.x = Player.x + 5
       PlayTune(2)
    else
       boundrary = false
       Player.x = Player.x - 5
       PlayTune(2)
    endif
   
    'Player's control
    if keydown(KEY_LEFT,true) and boundary then Player.x = Player.x - Player.dx
    if keydown(KEY_RIGHT,true) and boundary then Player.x = Player.x + Player.dx
    if keydown(KEY_ESCAPE,true) quit = true

    'Draw objects
    Player.Draw(Player.x,Player.y,Player.s,Player.a)
    Car[0].XY(0);Car[1].XY(1);Car[2].XY(2)
    foreach i in Car
        if i.y > height(primary)+20 then i.y = 0
        i.s = i.y/(height(primary))
        i.Draw(i.x,i.y,i.s,i.a)
       
        'Collision Detection
        if (Player.x>=i.x and Player.x<=i.x+51) and
           (Player.y+20>=i.y and Player.y+20<=i.y+34) then collide=true
        if (Player.x+45>=i.x and Player.x+45<=i.x+51) and
           (Player.y+20>=i.y and Player.y+20<=i.y+34) then collide=true
        if collide then
            lifes = lifes-1
            collide = false
            Player.y = -100
            set caret width(primary)-75,5;wln "Lifes : "+lifes
            PlayTune(1)
            if lifes <= 0 then Ending(white)
        endif       
    next   
   
    PlayTune(3)'car's sound effect       
   
    redraw
    fwait 10
until quit=true

'-----------
' FUNCTIONS
'-----------
function TitleScreen()
    car = loadimage("assets/titlescreen.png",5,3)
    track = loadmusic("assets/rock.wav")
    set music volume track,0.5
    play music track,1 '1 = looping

    i= 0
    while not keydown(KEY_RETURN,true)
        if i<=11 then; i = i+1; else; i=0; endif; draw image car,0,0,i
        set caret width(primary)/2,20
        set font font1; center "TEST DRIVE"
        set font 0 'default font
        center "Face the challenge"
        center "Conquer the street of..."
        center "The Fast and The Furious"
        center "Go Now...ENTER"
        redraw
        fwait 60
    wend
   
    'remove from memory
    free music track
    free image car 
endfunc

function Sound(b,x)
    mytune = sfx.SquareWave(b,x,0.5)'(duration,frequency, volume)
    play sound mytune; wait 50
    free sound mytune 'release sound from memory
endfunc

function PlayTune(i)
    select i
        case 1 'starting the game's scene
          Sound(0.5,261.63);Sound(0.5,293.66);Sound(0.5,329.63)
        case 2 'road's boundary
          Sound(0.5,329.63*3);Sound(0.5,261.63*3)
        case 3 'car's sound effect
          Sound(0.1,261.63/2) 
    endsel
endfunc

function Ending(c)
    set color c
    set font font1
    set caret width(primary)/2,5;center "THE END"
    redraw
    while not keydown(KEY_ESCAPE,true)
        wait 1
    wend
    TitleScreen()
    end
endfunc


Attached Files
.zip   Test Drive.zip (Size: 4.44 MB / Downloads: 6)
Reply


Messages In This Thread
Rotated and scaled image drawing - by Marcus - 03-03-2024, 10:49 AM
RE: Rotated and scaled image drawing - by Marcus - 03-03-2024, 08:25 PM
RE: Rotated and scaled image drawing - by 1micha.elok - 03-06-2024, 03:38 PM
RE: Rotated and scaled image drawing - by Marcus - 03-06-2024, 04:08 PM
RE: Rotated and scaled image drawing - by Marcus - 03-06-2024, 05:01 PM
RE: Rotated and scaled image drawing - by Marcus - 03-06-2024, 06:20 PM
RE: Rotated and scaled image drawing - by Marcus - 03-10-2024, 01:24 PM
RE: Rotated and scaled image drawing - by Marcus - 03-14-2024, 04:38 PM
RE: Rotated and scaled image drawing - by Marcus - 03-14-2024, 07:00 PM
RE: Rotated and scaled image drawing - by Marcus - 03-15-2024, 09:38 PM
RE: Rotated and scaled image drawing - by kevin - 03-16-2024, 01:19 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)