Cannon Ball Game 1.1

Post Reply
DrChip
Posts: 167
Joined: Wed Oct 22, 2014 3:26 pm
My devices: iPhone 4 to 6+,iPad mini to iPad air 2

Cannon Ball Game 1.1

Post by DrChip »

Code: Select all

REM Cannon Ball ( remake ) V1.1
REM Basic 5.1 / iPhone 6 Plus / iOS 9.3.1 beta 1
REM 
REM Touch and slide to modify cannon
REM angle ( 0-90 degrees ) press fire 
REM button to fire...[ single shoot ]
REM v0.6 
REM added object to hit and yards to
REM object (over, short and hit)
REM Still need to reset after hit.
REM 
REM v0.7
REM reset, victory sound and 
REM red hit object added.
REM
REM v0.8
REM bug fixes, ground, color changes...
REM
REM v0.9
REM bug fixes, color changes.
REM
REM v1.1
REM count the misses.
REM voice instead of sound efx
REM ported my misoft code to smart basic 5.1
REM this was a simple quick game to play with my son. 
REM enjoy! if you want to fix some bugs or improve on it, great!

GRAPHICS 
RESET:
GRAPHICS CLEAR 0,0,0

sh = SCREEN_HEIGHT()
sw = SCREEN_WIDTH()
csw = sw/2
csh = sh/2

' object size
objsize=7

' move angle step of cannon
astep = 1

' ball power 
bpower = 1.03

' ball speed = time increment 
bspeed = .02

' gravity constant (effects y-ball-speed)
ayb = 0.004

' cannon base coordinates and lenght
cx0 = 15
cy0 = sh/2+RND(90)
cl0 = 25
alfa0 = 0
alfa = (alfa0 + 90) * 0.017453 ' rad -> deg
cx1 = cx0 - cl0*COS(alfa)
cy1 = cy0 - cl0*SIN(alfa)
misses = 1

' object to destroy
ox0 = (sw-RND(150))-objsize
oy0 = cy0 'sh/2+RND(50) 'cy0

GRAPHICS CLEAR
DRAW TEXT  " Touch and slide to adjust cannon angle" AT 0,0

LOOP:

' cannon angle out of touch coordinates
IF TOUCH_X(0) <> -1 OR TOUCH_Y(0) <> -1 THEN
    GRAPHICS CLEAR
    xT = TOUCH_X(0)
    yT = TOUCH_Y(0)
    'CLEAR tty
    DX = xT - cx0
    DY = yT - cy0
    alfa = 90 + ATN(DY/DX)* 57.29577
    IF alfa < 0  THEN
        alfa = 0
    END IF
    IF alfa > 90 THEN
        alfa = 90
    END IF
    DRAW COLOR 1,1,1
    DRAW TEXT "ANGLE [deg]: "&INT(90-alfa) AT 0,0
END IF

' dtermine end position of cannon
cx1 = cx0 - cl0*COS((90 + alfa)*.017453)
cy1 = cy0 - cl0*SIN((90 + alfa)*.017453)

'cannon
FILL COLOR 111/255,111/255,111/255
FILL CIRCLE cx0,cy0 SIZE objsize
DRAW COLOR 111/255,111/255,50/255
DRAW SIZE objsize-1
DRAW LINE cx0,cy0 TO cx1,cy1

'object
FILL COLOR 0,1,1
FILL CIRCLE ox0,oy0 SIZE objsize

' horizon line
DRAW COLOR 111/255,111/255,2/255
DRAW SIZE 2
DRAW LINE 0,cy0+objsize TO sw,cy0+objsize

' ground
FILL COLOR 0,1,0
FILL RECT 0,cy0+objsize+1 TO sw,sh



' fire button
BUTTON "1" title "FIRE" AT sw-60, sh-50

'DRAW COLOR 0,0,0
'DRAW TEXT " Fire" AT sw-60, sh-50 

' initial ball conditions: pos. & speed
xb0 = cx1
yb0 = cy1
vxb = bpower * COS((90+alfa)*.017453)
vyb = bpower * SIN((90+alfa)*.017453)

' fire button touched
IF BUTTON_PRESSED("1") THEN
    SAY TEXT "fire!"
    
    ' ball trace
    FOR t = 0 TO 600 STEP bspeed
        xb = xb0 - vxb*t
        yb = yb0 - vyb*t + 0.5*ayb*t^2
        
        IF yb >= (cy0+4) THEN
            DRAW COLOR 1,1,1
            DRAW TEXT "Range [pix]: "&INT(xb-cx0) AT 0,64
            hit = INT(xb-ox0)
            t = 600
        END IF
       
        'ball
        FILL COLOR 0,0,0
        FILL CIRCLE xb,yb SIZE 4.5
        FILL COLOR 1,1,0
        FILL CIRCLE xb,yb SIZE 4
       
    NEXT t
    
    'collision/hit report
    DRAW COLOR 1,1,1
    'too short
    IF hit < -6 THEN 
        SAY TEXT "missed,  short!"
        DRAW TEXT "Short by "&hit&" yards." AT 0,24
        DRAW TEXT "Missed: "&INT(misses)&" times." AT 0,50
        misses = misses + 1
    END IF 
    'too far
    IF hit > 6 THEN 
        SAY TEXT "missed, over!"
        DRAW TEXT "Over by "&hit&" yards." AT 0,24
        DRAW TEXT "Missed: "&INT(misses)&" times" AT 0,50
        misses = misses + 1
    END IF 
    'hit
    IF hit > -6 AND hit < 6 THEN
        SAY TEXT "hit!"
        DRAW TEXT  "Hit!" AT 0,50
        DRAW TEXT "Only took "&misses&" try(s)!" AT 0,40
        FILL COLOR 1,0,0
        FOR i = 1 TO 15
            FILL CIRCLE ox0,oy0 SIZE i
        NEXT i
        
        'victory sound
        SAY TEXT "winner!"
        PAUSE 4
        SAY STOP
        GRAPHICS CLEAR
        GOTO RESET 
    END IF 
    'REFRESH ON
END IF

GOTO LOOP
Attachments
image.png
image.png (337.6 KiB) Viewed 7064 times
image.png
image.png (258.47 KiB) Viewed 7064 times
image.png
image.png (249.19 KiB) Viewed 7064 times

Post Reply