Flappy single pipe -Test-
Posted: Sun Jun 14, 2015 9:37 am
Flappy Bird (reduced) in smart Basic
Fast enough even for iPhone 4 users...
Any coding hints are WELCOME.
enjoy...
Dropbox link for code and assets:
https://www.dropbox.com/sh/tsx7xiu7pd6y ... pZVfa?dl=0
Fast enough even for iPhone 4 users...
Any coding hints are WELCOME.
enjoy...
Dropbox link for code and assets:
https://www.dropbox.com/sh/tsx7xiu7pd6y ... pZVfa?dl=0
Code: Select all
REM Flappy single pipe -Test-
REM
REM sBasic 4.9/iPhone 4/iOS 6.1/ by Operator
REM
REM bird sprite out of: http://www.appcycle.me/flappy/img/bird.png
REM pipe sprite out of: http://flappycreator.com/
REM sound fx out of: http://ice-web.cc.gatech.edu/dl/?q=node/978
REM sub link: https://drive.google.com/folderview?id=0B8jxaj85tY94V19udHd4MHBqdmM&usp=sharing
REM
REM feel free to (re)use, expand (more pipes)
REM any hints for better coding are WELCOME
RESET:
GOSUB init_bird
GOSUB init_button
GOSUB init_text_fld()
GOSUB init_sfx
'this vars are here for easier set-up
bird_g = 600 'bird g force
bird_y = 40 'bird initial y position
bird_x = 90 'bird initial x position
bird_vy = 150 'bird initial y velocity
bird_vang = 120 'bird initil angle velocity
pipe_x = scw 'pipe initial x position
pipe_vx = -100 'pipe initial y velocity
pipe_y = schh 'pipe initial y pisition
pipe_scl = 3 'pipe scale
pipe_hit = 0 'pipe has no hits at start
GOSUB init_pipe
score = 0
'loops per second, increase till lower lps
'show up while crossing the pipe
'with iPad mini first gen.:lps_max = 60 (OK)
lps_max = 25
dt_min = 1/lps_max 'minimum time for a loop
'minimalitic intro
START:
IF BUTTON_PRESSED ("butA") THEN
FIELD "info" FONT COLOR 1,1,1
GOTO LOOP
END IF
c = RND(1)
FIELD "info" FONT COLOR c,c,c
FIELD "info" TEXT "Hit FLY Button to fly up"
GOTO START
'------ main loop -------
LOOP:
t0 = TIME()
pipe_dx = pipe_vx*dt
pipe_x = pipe_x + pipe_dx
'reposition pipe when out of screen
IF pipe_x < -40 THEN
pipe_hit = 0 'reset hit flag
pipe_x = scw+40
pipe_y = schh - 170 + RND(340)
END IF
'bird angle calc
bird_dang = bird_vang*dt
bird_ang = bird_ang + bird_dang
IF bird_ang >=90 THEN bird_ang = 90
IF BUTTON_PRESSED ("butA") THEN
IF NOT MUSIC_PLAYING ("flap") THEN
MUSIC "flap" PLAY
END IF
bird_vy = -250 'inc. for sharper pull up
bird_ang = -50 'inc. for pull up angle
END IF
'bird y speed & position calc
bird_vy = bird_vy + bird_g*dt
bird_dy = bird_vy*dt + 0.5*bird_g*dt^2
bird_y = bird_y + bird_dy
'if bird hits the floor then reset it...
IF bird_y > sch-10 THEN
PAUSE 2
GOTO RESET
END IF
IF bird_ang > 0 THEN
bird_delay = 1
ELSE
bird_delay = 0.05
END IF
SPRITE "bird" DELAY bird_delay
SPRITE "bird" AT bird_x,bird_y ANGLE bird_ang
SPRITE "pipe 1" AT pipe_x,pipe_y SCALE pipe_scl
'looks like this col.function takes some time
IF SPRITES_COLLIDE ("bird", "pipe 1") THEN
IF NOT MUSIC_PLAYING ("smack") THEN
MUSIC "smack" PLAY
score = score - 1
END IF
pipe_hit = 1 'hit flag set
END IF
IF bird_x > pipe_x+110 AND pipe_hit = 0 THEN
IF NOT MUSIC_PLAYING ("bading") THEN
MUSIC "bading" PLAY
score = score + 1
pipe_vx = pipe_vx - 2 'make the pipes a bit faster
END IF
pipe_hit = 0
END IF
dt = TIME()-t0
IF dt < dt_min THEN
PAUSE dt_min - dt
dt = TIME() - t0
END IF
lps$ = STR$(INT(1/dt))
FIELD "info" TEXT "lps: "&lps$&" SCORE: "&STR$(score)
GOTO LOOP
'--------- Functions & Subs --------
init_bird:
OPTION SPRITE POS CENTRAL
OPTION ANGLE DEGREES
scw = SCREEN_WIDTH()
sch = SCREEN_HEIGHT()
scwh = scw/2
schh = sch/2
SPRITE "bird" LOAD "bird.png", 3,1
SPRITE "bird" AT 100,40
SPRITE "bird" ORDER 2
SPRITE "bird" SHOW
SPRITE "bird" LOOP
RETURN
init_pipe:
SPRITE "pipe 1" LOAD "pipe topbot.png"
SPRITE "pipe 1" AT pipe_x,pipe_y SCALE pipe_scl
SPRITE "pipe 1" SHOW
RETURN
init_sfx:
MUSIC "flap" LOAD "flap.wav"
MUSIC "smack" LOAD "smack.wav"
MUSIC "bading" LOAD "bading.wav"
RETURN
init_button:
SET BUTTONS CUSTOM
FILL COLOR 0,0,1
DRAW COLOR 1,1,1
FILL ALPHA 0.1
butA_txt$ = "FLY" 'button A text
butA_scl = 1 'button A scale
butA_w = 60*butA_scl 'button A width
butA_h = 60*butA_scl 'button A height
butA_x = scwh-butA_w/2 'button A x pos
butA_y = sch-butA_h 'button A y pos
SET BUTTONS FONT SIZE 20*butA_scl
BUTTON "butA" TEXT butA_txt$ AT butA_x, butA_y SIZE butA_w,butA_h
BUTTON "butA" SHOW
GRAPHICS ! GRAPHICS CLEAR
RETURN
init_text_fld:
FIELD "info" TEXT "" AT 0,0 SIZE 300,20 RO
FIELD "info" BACK ALPHA 0
FIELD "info" FONT COLOR 1,1,1
FIELD "info" SHOW
RETURN
[attachment=0]image.jpg[/attachment]