Game Pad out of sprites - Test -
Posted: Tue Jun 09, 2015 11:22 pm
Game-Pad test, hopefully re-usable.
enjoy
Sprite-Sheet
https://www.dropbox.com/s/fs7ybtq1o59ux ... 2.png?dl=0
enjoy
Sprite-Sheet
https://www.dropbox.com/s/fs7ybtq1o59ux ... 2.png?dl=0
Code: Select all
REM Game-Pad out of Sprites - Test
REM sB 4.9 / iPhone4 / iOS 6.1 / by Operator
REM used sprite sheet: by (arikel)
REM http://opengameart.org/content/2d-rpg-character-walk-spritesheet
REM
REM save spritesheet to same location as code
REM
REM issues:
REM 1) walker speed only adjusted by pause
REM command, adjust at the end of the main loop
REM 2) won't resize, reset for other devices
'---- Start of pad code - reusable
'Pad position and size
'please change position and size if req.
OPTION ANGLE DEGREES
pad_cx = 85 'pad center x-position
pad_cy = 350 'pad center y-position
but_sc = 1.6 'pad buttons scale
but_ox = 35*but_sc 'buttons x offset
but_oy = 35*but_sc 'buttons y offset
butA_ox = 150 'button-A x offset
butA_oy = 0 'button-A y offset
butB_ox = 200 'button-B x offset
butB_oy = 40 'button-B y offset
'create the pad_buttons sprites
GRAPHICS
GRAPHICS CLEAR
FILL ALPHA 0.4
FILL COLOR 96/255,130/255,162/255
REFRESH OFF 'hide sprite creation
FILL RECT 15,15 TO 47,47
FILL COLOR 1,1,1
FILL TRI 31,18, 42,42, 20,42
SPRITE "up" SCAN 14,14, 34,34
GRAPHICS CLEAR
SPRITE "up" AT 15,15 ANGLE 180
SPRITE "up" STAMP
SPRITE "down" SCAN 14,14, 34,34
GRAPHICS CLEAR
SPRITE "up" AT 15,15 ANGLE -90
SPRITE "up" STAMP
SPRITE "left" SCAN 14,14, 34,34
GRAPHICS CLEAR
SPRITE "up" AT 15,15 ANGLE 90
SPRITE "up" STAMP
SPRITE "right" SCAN 14,14, 34,34
GRAPHICS CLEAR
DRAW COLOR 1,1,1
FILL COLOR 96/255,130/255,162/255
FILL CIRCLE 100,31 SIZE 16
DRAW TEXT "A" AT 94,20
SPRITE "but_A" SCAN 83,14, 34,34
GRAPHICS CLEAR
FILL CIRCLE 100,31 SIZE 16
DRAW TEXT "B" AT 94,20
SPRITE "but_B" SCAN 83,14, 34,34
GRAPHICS CLEAR 0,0,0
'draw the pad
OPTION SPRITE POS CENTRAL
SPRITE "up" AT pad_cx, pad_cy - but_oy SCALE but_sc
SPRITE "down" AT pad_cx, pad_cy + but_oy SCALE but_sc
SPRITE "left" AT pad_cx - but_ox, pad_cy SCALE but_sc
SPRITE "right" AT pad_cx + but_ox, pad_cy SCALE but_sc
SPRITE "but_A" AT pad_cx + butA_ox, pad_cy + butA_oy SCALE but_sc
SPRITE "but_B" AT pad_cx + butB_ox, pad_cy - butB_oy SCALE but_sc
SPRITE "up" SHOW
SPRITE "down" SHOW
SPRITE "left" SHOW
SPRITE "right" SHOW
SPRITE "but_A" SHOW
SPRITE "but_B" SHOW
DEF pad_touch(where$)
'where$: up,down,left,right
'get 2 posible touches, since button A or B
'can be touched at the same time
GET TOUCH 0 AS tx,ty
GET TOUCH 1 AS txx,tyy
pad_touch = OR(SPRITE_HIT (where$,tx,ty),SPRITE_HIT (where$, txx,tyy))
END DEF
DEF butA_touch()
'get 2 posible touches, since pad can
'be touched at the same time
GET TOUCH 0 AS tx,ty
GET TOUCH 1 AS txx,tyy
butA_touch = OR(SPRITE_HIT ("but_A", tx,ty),SPRITE_HIT ("but_A", txx,tyy))
END DEF
DEF butB_touch()
'get 2 posible touches, since pad can
'be touched at the same time
GET TOUCH 0 AS tx,ty
GET TOUCH 1 AS txx,tyy
butB_touch = OR(SPRITE_HIT ("but_B", tx,ty),SPRITE_HIT ("but_B", txx,tyy))
END DEF
'---- End of Pad ----
'Test the Pad
'move a "walker" around and show if But A/B
'was pressed
NOTES INSTRUMENTS "Examples/10. Music & Sound/files/sfx.sf2"
NOTES MIDI 0,12,0
SPRITE "walker" LOAD "Walker top view 2.png", 8,4
y = 220 'starting x position of walker
x = 100 'starting y position of walker
walker_scale = 2
GET SPRITE "walker" SIZE walker_w,walker_h
walker_wh = walker_w/2
walker_hh = walker_h/2
SPRITE "walker" AT x,y SCALE walker_scale
SPRITE "walker" SHOW
speed = 3
DY = speed 'walker speed in y direction
DX = speed 'walker speed in x direction
scr_h = SCREEN_HEIGHT()
scr_w = SCREEN_WIDTH()
FIELD "FPS" TEXT "" AT 5,5 SIZE 320,25 RO
FIELD "FPS" FONT COLOR 0.5,0.5,0.5
FIELD "FPS" FONT SIZE 15
FIELD "FPS" BACK ALPHA 0
REFRESH ON
GRAPHICS CLEAR 0,0,0
LOOP: 'main
TIME RESET
i = i+1
bA$=""
bB$=""
IF pad_touch("up") AND inbounds() THEN
'walk up
start_frame = 8
i_sp_up = (i_sp_up + 1)%8
SPRITE "walker" FRAME start_frame+i_sp_up
y = y-DY
drw_sprite()
END IF
IF pad_touch("down") AND inbounds() THEN
'walk down
start_frame = 0
i_sp_down = (i_sp_down + 1)%8
SPRITE "walker" FRAME start_frame+i_sp_down
y = y+DY
drw_sprite()
END IF
IF pad_touch("left") AND inbounds() THEN
'walk left
start_frame = 16
i_sp_left = (i_sp_left + 1)%8
SPRITE "walker" FRAME start_frame+i_sp_left
x = x-DX
drw_sprite()
END IF
IF pad_touch("right") AND inbounds THEN
'walk right
start_frame = 24
i_sp_right = (i_sp_right + 1)%8
SPRITE "walker" FRAME start_frame+i_sp_right
x = x+DX
drw_sprite()
END IF
IF butA_touch() THEN
NOTES MIDI 0,9,60,127
bA$=" Button >A< pressed"
END IF
IF butB_touch() THEN
NOTES MIDI 0,9,62,127
bB$=" Button >B< pressed"
END IF
dT = dT+(TIME())
FIELD "FPS" TEXT "loops/sec: "&STR$(FLOOR(i/dT))&" "&bA$&bB$
IF i> 500 THEN
i=0
dT=0
END IF
PAUSE 0.05 'adjust if req.
GOTO LOOP
DEF drw_sprite()
SPRITE "walker" AT .x,.y SCALE 2
END DEF
DEF inbounds()
'tests if walker is inside screen(bounds)
'and if not puts it in bounds back again
inbounds = 1
IF .x<.walker_wh THEN
inbounds = 0
.x = .x+.walker_wh*1.01
END IF
IF .x>.scr_w-.walker_wh THEN
inbounds = 0
.x = .x-.walker_wh*1.01
END IF
IF .y>.scr_h-.walker_hh THEN
inbounds = 0
.y = .y-.walker_hh*1.01
END IF
IF .y<.walker_hh THEN
inbounds = 0
.y = .y+.walker_hh*1.01
END IF
END DEF