Author Topic: JumpFrog  (Read 3109 times)

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10757
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
JumpFrog
« on: 2005-Jul-02 »
Hmm... nachdem sich keiner traut, leg ich mal einen vor. Ein 2 Spieler "JumpFrog". Die Grafik ist etwas.. ähh.. ;)

Spieler 1: Space oder linke Maustaste
Spieler 2: Linke CTRL oder rechte Maustaste

Compiliert man unter 320x240 oder 640x480 - nicht höher :P

Es wundere sich keiner, dass er nix kapiert, so geht's mir nach einer Woche auch :D. Musste halt mal wieder schnell gehen.

Code: (glbasic) [Select]
// --------------------------------- //
// Project: JumpFrog
// Start: Tuesday, May 10, 2005
// IDE Version: 2.50405

LIMITFPS -1

DIM frog_jump[2]
DIM frog_x[2] // offset to current platform - if jumping, real position
DIM frog_y[2] // on what platform
DIM frog_col[2]
frog_col[0] = RGB(0,255,0)
frog_col[1] = RGB(255,0,0)
GETSCREENSIZE screenx, screeny
MAX_PLATFORM = 50
JUMP_HEIGHT = screeny/8

frog_x[0] = 0.1
frog_x[1] = -0.1

WHILE TRUE
dtime = GETTIMERALL()*3
IF ( MOUSEAXIS(3) OR KEY(57) ) AND frog_jump[0] = 0 THEN JumpFrog(0, dtime)
IF ( MOUSEAXIS(4) OR KEY(29) ) AND frog_jump[1] = 0 THEN JumpFrog(1, dtime)
HandleFrog(0, dtime)
HandleFrog(1, dtime)
ShowAll(dtime)
WEND

// ------------------------------------------------------------- //
// -=#  SHOWALL  #=-
// ------------------------------------------------------------- //
FUNCTION ShowAll: dtime
LOCAL i, pos[], fx, nicecam
DIM pos[5]
GetPlatformPos(frog_y[0], dtime, pos[])

scale = 0.9 // dynamic camera speed
nicecam = -pos[1]*JUMP_HEIGHT + screeny*2/3
yoffset = yoffset*scale + (1-scale)*nicecam
FILLRECT 0,0, screenx, screeny, RGB(96,128,255)

// FROGGERS
FOR i=0 TO BOUNDS(frog_x[], 0)-1
GetPlatformPos(frog_y[i], dtime, pos[])
IF frog_jump[i]
fx = frog_x[i]
ELSE
fx = pos[0] + frog_x[i]
ENDIF

IF frog_jump[i] >= 0
dy = frog_jump[i]
ELSE
dy = SIN(90*frog_jump[i])
ENDIF

FILLRECT fx*screenx-16, (pos[1]+dy)*JUMP_HEIGHT+yoffset-32, _
fx*screenx   , (pos[1]+dy)*JUMP_HEIGHT+yoffset, frog_col[i]
DRAWLINE fx*screenx-14, (pos[1]+dy)*JUMP_HEIGHT+yoffset-30, _
fx*screenx -2, (pos[1]+dy)*JUMP_HEIGHT+yoffset-30, RGB(255,255,255)
NEXT

// PLATFORMS
FOR i = 0 TO MAX_PLATFORM
GetPlatformPos(i, dtime, pos[])
IF pos[4]=FALSE // not hidden
ALPHAMODE pos[3]
width = pos[2]*screenx/2
FILLRECT pos[0]*screenx-width, pos[1]*JUMP_HEIGHT+yoffset, _
pos[0]*screenx+width, pos[1]*JUMP_HEIGHT+yoffset + 0.2*JUMP_HEIGHT, _
RGB(200,96,0)
FILLRECT pos[0]*screenx-width, pos[1]*JUMP_HEIGHT+yoffset, _
pos[0]*screenx+width, pos[1]*JUMP_HEIGHT+yoffset + 2, _
RGB(0,128,0)
ALPHAMODE 0
PRINT INTEGER(MAX_PLATFORM-i+1), 0, pos[1]*JUMP_HEIGHT+yoffset
ENDIF // hidden
NEXT

IF bestlevel PRINT "Position: " + frog_y[0] + " Best: " + bestlevel, 0, 0
PRINT "GLBasic.com", GLOBAL screenx-220, GLOBAL screeny-30

dtime=GETTIMERALL()
IF dtime>lasttime+1000
fps = numframes/(dtime-lasttime)*1000
lasttime=dtime
numframes=0
ENDIF
numframes=numframes+1
PRINT "FPS:" +FORMAT$(0, 1, fps), 20, 25 // + dtime, 0,0

SHOWSCREEN
ENDSUB // SHOWALL


// ------------------------------------------------------------- //
// -=#  HANDLEFROG  #=-
// ------------------------------------------------------------- //
FUNCTION HandleFrog: num, dtime
// Diese Variablen sind als LOCAL definiert:
// num, dtime
LOCAL pos[]
DIM pos[5]
SELECT frog_jump[num]
CASE <0 // jumping
frog_jump[num] = frog_jump[num] - GETTIMER()/500
SetOnPlatform(num, dtime)
CASE >0 // falling
frog_jump[num] = frog_jump[num] + GETTIMER()/700
SetOnPlatform(num, dtime)
CASE 0 // standing
GetPlatformPos(frog_y[num], dtime, pos[])
IF pos[4] // toggling platform - is gone!
FallFrog(num, dtime)
ENDIF
ENDSELECT
ENDFUNCTION // HANDLEFROG

// ------------------------------------------------------------- //
// -=#  JUMPFROG  #=-
// Make frog jumping
// ------------------------------------------------------------- //
FUNCTION JumpFrog: num, dtime
LOCAL pos[]
DIM pos[5]
IF frog_jump[num] <> 0 THEN RETURN
GetPlatformPos(frog_y[num], dtime, pos[])
frog_jump[num] = -0.01
frog_x[num] = pos[0] + frog_x[num]
PLAYSOUND 0, (2*frog_x[num])-1, 1
ENDFUNCTION

// ------------------------------------------------------------- //
// -=#  FALLFROG  #=-
// Make frog falling
// ------------------------------------------------------------- //
FUNCTION FallFrog: num, dtime
LOCAL pos[]
DIM pos[5]
IF frog_jump[num] <> 0 THEN RETURN
GetPlatformPos(frog_y[num], dtime, pos[])
frog_jump[num] = 0.01
frog_x[num] = pos[0] + frog_x[num]
ENDFUNCTION

// ------------------------------------------------------------- //
// -=#  SETONPLATFORM  #=-
// Try to attach a frog to the next closest platform
// ------------------------------------------------------------- //
FUNCTION SetOnPlatform: num, dtime
LOCAL frogx
LOCAL pos[]
DIM pos[5]
IF ABS(frog_jump[num])>1
IF frog_jump[num] > 1 THEN frog_y[num]=frog_y[num]-1
IF frog_jump[num] <-1 THEN frog_y[num]=frog_y[num]+1
frog_jump[num] = 0
// check if you're on a platform now
GetPlatformPos(frog_y[num], dtime, pos[])
frogx = frog_x[num]
frog_x[num] = frog_x[num]-pos[0]
IF pos[4] OR (ABS(pos[0] - frogx) > pos[2]/2)
FallFrog(num, dtime)
ENDIF
ENDIF
ENDFUNCTION


FUNCTION PseudoRand: num
LOCAL ps_rnd, i
ps_rnd = 1247
FOR i=0 TO ABS(num+1)
// ps_rnd = qCOS(360*(ps_rnd+i))+qSIN(ps_rnd*i)*i
SELECT MOD(i, 4)
CASE 0; ps_rnd=ps_rnd+1027*i
CASE 1; ps_rnd=ps_rnd*21+MOD(i, ps_rnd)
CASE 2; ps_rnd=ps_rnd*i+13
CASE 3; ps_rnd=MOD(ps_rnd, i)
ENDSELECT
NEXT
RETURN ABS(INTEGER(ps_rnd))
ENDFUNCTION

// ------------------------------------------------------------- //
// -=#  GetPlatformPos  #=-
// pos[0] = x [0; 1] or -1000 if invisible
// pos[1] = y [0; n] 1 unit = 1 storey heigth
// pos[2] = width [0; 1]
// pos[3] = alphamode (for vanishing blocks)
// pos[4] = is hidden?
// ------------------------------------------------------------- //
FUNCTION GetPlatformPos: num, dtime, pos[]
// Diese Variablen sind als LOCAL definiert:
// num, dtime
LOCAL x
IF BOUNDS(pos[], 0) < 5 THEN DIM pos[5]

dtime = dtime * (5+num/MAX_PLATFORM)/40
pos[2] = 0.15 + (MAX_PLATFORM-num)/MAX_PLATFORM*0.30
pos[3] = 0
pos[4] = FALSE

SELECT MOD(PseudoRand(num),5) // MOD(num-1, 4)
CASE 0 // linear motion
x = MOD(dtime*(num+10)/MAX_PLATFORM, 1000)
IF x>500
x = (1000-x)/500
ELSE
x = x/500
ENDIF
CASE 1 // disapear in the center
x = MOD(dtime*num/MAX_PLATFORM, 1000)
IF x>700
pos[4]=TRUE
ENDIF
SELECT x
CASE <100
pos[3] = -x/100 + 0.001
CASE >600
pos[3] = -(700-x)/100 + 0.001
ENDSELECT
x = 0.5
CASE 2 // sin
x = (1+SIN(dtime*num/MAX_PLATFORM))/2
CASE 3 // whacky
x = ABS(COS(dtime*num/13/MAX_PLATFORM) + SIN(dtime*num/MAX_PLATFORM))/2
CASE 4 // sin + hide
x = MOD(dtime*num/MAX_PLATFORM, 1000)
IF x>700
pos[4] = TRUE
ENDIF
SELECT x
CASE <100
pos[3] = -x/100 + 0.001
CASE >600
pos[3] = -(700-x)/100 + 0.001
ENDSELECT
x = (1+SIN(0.5 * dtime*num/MAX_PLATFORM))/2
ENDSELECT

pos[0] = (1-pos[2]) * x + pos[2]/2
pos[1] = -num //*JUMP_HEIGHT
IF num = 0; pos[2]=1; pos[0]=0.5; pos[3]=0; pos[4]=FALSE; ENDIF
ENDFUNCTION // GETPLATFROMPOS