Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tiny Golf
#1
TINY GOLF

           
click the image to zoom in
Edit : added a hole-in-one

 Description
 Help Mario to make a hole-in-one
 and achieve The World Record
 of The Tiny Golf

 Made with  
 N7_240310

 Controls :
 1. LEFT click and drag from O to aim
 2. ENTER to swing the golf club
 3. ESC to continue / quit

 Acknowledgements :
 - polygon, polyline.n7, sfx.n7 by Marcus
 - some sound effects are copied from the "Pool" game by Kevin
   hit_ball_sound, oh_no_sound
 - draggable point on a polyline by Kevin
   https://naalaa.com/forum/thread-90-post-493.html#pid493
 - sprite https://www.spriters-resource.com/game_b...eet/12294/

Code:
'-----------------------------------
' TINY GOLF
' =========
' Help Mario to make a hole-in-one
' and achieve The World Record
' of The Tiny Golf
'
' Made with N7_240310
'
' Controls :
' 1. LEFT click and drag from O to aim
' 2. ENTER to swing the golf club
' 3. ESC to continue / quit
'
' Acknowledgements :
' - polygon, polyline.n7, sfx.n7 by Marcus
' - some sound effects are copied from the "Pool" game by Kevin
'   hit_ball_sound
'   oh_no_sound
' - draggable point on a polyline by Kevin
'   https://naalaa.com/forum/thread-90-post-493.html#pid493
' - sprite https://www.spriters-resource.com/game_boy_gbc/mariogolf/sheet/12294/
'----------------------------------

'-----------------
' INITIALIZATION
'-----------------
#win32
include "assets/sound_effect.n7"
include "polyline.n7"
set window "TINY GOLF",250,180,false,3
set redraw off

'color definition
visible black_alpha = [0,0,0,24]
visible black       = [0,0,0]
visible white       = [255,255,255]
visible gray        = [150,150,150]
visible green_      = [0,80,0]
visible green       = [51,153,10,24]
visible yellow      = [200,200,0]

'cartesian coordinates
visible ox = 50,oy = 165    'origin point
axy = 360-45                'angle between x and y-axis
length = 150                'length of axis 

'Polygon
grass =
[
    ox,oy,
    length+ox,oy,
    ox+length*cos(rad(axy))+length-80 ,oy+length*sin(rad(axy)),
    ox+length*cos(rad(axy))          ,oy+length*sin(rad(axy))
]

'PolyLine Path
visible points = []
ResetPoints()
path = PolyLine(points,0)'0 = opened path
distance = 0
speed = 5

'Player
visible Player = []
Player.standby = 0
Player.face = 0
Player.swing = 0
Player.x = ox-25
Player.y = oy-30

'Sound effect
hit_ball_sound = CreateNoiseSfx(0.1, 1.05, 0, 14200)'(duration, pitch, fadeOut, sampleRate)
oh_no_sound = CreateSineSfx(1.7,600,310,0.215,11600)

'Other initial values
hit = false
drawline = false
stay = false
visible title = true
visible timer = []
timer.Start = clock()
visible i=0,j=0 'counter
visible p10,p11,p20 = ox; p21=oy
score = 50
holex = (144+224)/2
holey = 70

'------------
' MAIN LOOP
'------------
while not keydown(KEY_ESCAPE,true)
    '----------------------
    '     title screen
    '----------------------
    if title=true then Action(4,0,0)

    '----------------------
    '   screen layout
    '----------------------
    '1. golf course
    set color green; draw poly grass,1
    set color white; draw poly grass
    set caret ox,oy;wln "O"
    set color black; draw ellipse holex,holey,3,2,1
    set color white; draw ellipse holex,holey,3,2
       
    'supporting lines
    'set color yellow; draw line 0,70,width(primary),70
    'set color yellow; draw line 144,0,144,height(primary)
    'set color yellow; draw line 224,0,224,height(primary)
    'set color yellow; draw line ox,oy,holex,holey
    'set color yellow; draw line 117,0,117,height(primary)

    '3. Score
    set color white; set caret 10,40; wln "Timer"; wln round(score)
    if round(score) >= 1 then
        score = score - 0.05
    else
        free distance
        hit = false
        Say("TIME OUT..ESC")
        redraw
        play sound oh_no_sound       
        do;wait 1;until keydown(KEY_ESCAPE,true)
        ResetPoints()
        score = 50
    endif        
       
    '----------
    ' Control
    '----------
    'Left click mouse
    if mousebutton(0) then
        points[1][0] = mousex()
        points[1][1] = mousey()
       
        'use random number, wind blowing perhaps :-)
        p10 = points[1][0]+rnd(5,10)
        p11 = points[1][1]+rnd(5,10)
        drawline = true
    else
        drawline = false
    endif  

    'set how high your golf ball
    set color black_alpha; cls 'clear screen
    if drawline = true or stay then
        set color yellow
        draw line points[0][0],points[0][1],points[1][0],points[1][1]
        draw rect points[1][0] - 8, points[1][1] - 8,16,16 
        Say("ENTER to shoot")
        stay = true
    else
        Say("Drag point from O")       
    endif

    ' 2. ENTER to move ball along the PolyLine path
    if keydown(KEY_RETURN,true) then
        Action(3,0,2)
        play sound hit_ball_sound
        hit = not hit 'toggle on/off
        stay = false
    endif   
    if hit then
        points[0]=[ox,oy]
        points[1]=[p10,p11]
        points[2]=[p20,p21]
       
        'points[2] coordinate
        p20 =  p10*2
        'p21 is on a line between (ox,oy) and (holex,holey)
        'the equation of the line is y = -0.71x + 200.45
        p21 = -0.71*p20 + 200.45       
       
        path = PolyLine(points,0)'0 = opened_path
        distance = (distance + speed)%path.GetLength()
        pos = path.GetPoint(distance, 1) '1=curved
       
        draw ellipse pos[0], pos[1], 2, 2, 1 '1 = filled ellipse
       
        set caret width(primary)/2, height(primary) - 25
        set color white;draw image Player.swing,Player.x,Player.y,2 'stay at the position during the shoot

        'stop the ball and clear the remaining path's distance
        if round(distance) > 0.95*path.GetLength()then
            free distance
            hit = false
            Say("Drag square from O")
        endif
                       
        'collision detection between the ball and the hole
        '1.distance between the center of the ball and the center of the hole
        cbh = sqr(pow(holex-pos[0],2)+pow(holey-pos[1],2))
        if cbh<(2+3) then
            Say("Hole-in-one")
            PlayTune(1)
            free distance
            redraw
            hit = false
            do;wait 1;until keydown(KEY_RETURN,true) 'pause
        endif

    endif

    '-------------------------------
    ' Standy by
    '-------------------------------
    if hit=false then Action(0,0,1)
       
    redraw
    fwait 30
wend


'-----------
' FUNCTIONS
'-----------
function Action(type,cell1,cell2)
    set color white
    select type
    case 0 'stand by position
        Player.standby = loadimage("assets/standby.png",2,1)
        timer.Duration = (clock() - timer.Start)/1000
        if round(timer.Duration)%2 = 0 then
            i=cell2
        else
            i=cell1
        endif
        draw image Player.standby, Player.x, Player.y, i
    case 1 'Mario's face animation
        Player.face = loadimage("assets/say.png",3,1)
        draw image Player.face, 20,5,j
        timer.Duration = (clock() - timer.Start)/1000
        if round(timer.Duration)%2 = 0 then
            if j <= 1 then
                j = j+1
            endif
        else
            j = 0
        endif
    case 3 'swing the golf club
        for k = cell1 to cell2
            set color black
               draw rect Player.x,Player.y,width(Player.standby),height(Player.standby),1
            set color white
            Player.swing = loadimage("assets/swing.png",3,1)
            draw image Player.swing, Player.x, Player.y,k
            redraw
            wait 250
        next
        set color black; draw rect Player.x,Player.y,width(Player.standby),height(Player.standby),1
        redraw
    case 4'title screen
        title = false
        set color white
        set caret width(primary)/2,0
        center "---------"
        center "Tiny Golf"
        center "---------"
        center
        center "Help Mario"
        center "to make a hole-in-one"
        center "and achieve"
        center "The World Record"
        center "of The Tiny Golf"
        center
        center "Press ESC to continue"
        redraw
        PlayTune(1)
        do; wait 1; until keydown(KEY_ESCAPE,true)
    endsel
endfunc

'Reset draggable path points
function ResetPoints()
    points[0] = [ox,oy]
    points[1] = [ox,oy]
    points[2] = [ox,oy]
endfunc

function Say(message)
    set color green_; draw rect 0,0,width(primary),35,1 '1 = filled rectangle
    set color white; set caret width(primary)/2,15; center message

    'Mario's face
    Action(1,0,2)
    draw line 0,35,width(primary),35
endfunc


Attached Files
.zip   golf.zip (Size: 6.22 KB / Downloads: 5)
Reply


Messages In This Thread
Tiny Golf - by 1micha.elok - 03-21-2024, 01:31 PM
RE: Tiny Golf - by johnno56 - 03-21-2024, 06:02 PM
RE: Tiny Golf - by Marcus - 03-21-2024, 07:20 PM
RE: Tiny Golf - by johnno56 - 03-21-2024, 09:55 PM
RE: Tiny Golf - by 1micha.elok - 03-22-2024, 01:43 AM
RE: Tiny Golf - by johnno56 - 03-22-2024, 04:33 AM
RE: Tiny Golf - by 1micha.elok - 03-25-2024, 10:42 AM
RE: Tiny Golf - by Marcus - 03-25-2024, 07:09 PM
RE: Tiny Golf - by 1micha.elok - 03-26-2024, 02:42 AM
RE: Tiny Golf - by 1micha.elok - 03-28-2024, 02:17 AM
RE: Tiny Golf - by kevin - 03-28-2024, 10:21 AM
RE: Tiny Golf - by 1micha.elok - 03-28-2024, 10:28 AM
RE: Tiny Golf - by kevin - 03-28-2024, 02:12 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)