Platformer / Jump & run

Discuss any general programming issues here
Post Reply
phipslk
Posts: 34
Joined: Sun Oct 29, 2023 7:21 pm

Platformer / Jump & run

Post by phipslk »

I am trying to code a platformer with hollywood-mal and I have some troubles with it. Just trying now the player movement. Left/Right works well :-)

I ported the code from pico8-lua, but it doesn't work as expected. If I press "up" to jump, it reacts delayed.
Any hints for a beginner?

Code: Select all

@DISPLAY{WIDTH = 800, HEIGHT = 600, MODE = "WINDOWED"} ;Create the window

CreateBrush(1, 16, 16, #GREEN)  ; CREATE BLOCK-MAN
CreateBrush(2, 32, 32, #GRAY)   ; CREATE OBSTICLE
CreateBrush(3,800,  5, #WHITE)  ; create floor

; ----------------- init

x = 380			; x position player
y = 555			; y position player
dx = 0			; x movement player
dy = 0			; y movement player
onground = True		; true if player is "on ground" else false
jump = 0		; jump height
jumpmax = 5		; maximum jump height
speed = 3		; movement speed player
jumping = False		; seems to be needed for Hollywood


Function p_UpdateScreen()

	dx = 0
	dy = dy + 1
		
	If IsKeyDown("LEFT") = True Then dx = dx - speed
	If IsKeyDown("RIGHT") = True Then dx = dx + speed
	If IsKeyDown("UP") = True And (jumping = False And onground = True) Then jumping = True

	If jumping = True
		onground=False
		jump=jump + 1
		dy = dy - 2
	EndIf
	
	If jump >jumpmax
		jump = 0
		jumping = False
		onground = True
	EndIf

;	If jump > jumpmax 
;		dy = dy + 5
;	ElseIf jump <= 0
;		onground = True
;;		dy = 0
;	EndIf

	If dy > 0 Then onground = True
	If onground 
		jump = 0
;		dy = 0
	EndIf
	
	x = x + dx
	y = y + dy

    ;Check if player leaves the screen
	If x < 0 Then x = 0
	If x > 784 Then x = 784
	If y < 0 Then y = 0
	If y > 555 Then y = 555	


    	DisplayBrush(1, x, y)
	DisplayBrush(3, 0,555+16)
	
EndFunction

BeginDoubleBuffer()
EscapeQuit(True)
Repeat
	Cls()
	p_UpdateScreen()
	Flip()
Forever
EndDoubleBuffer()
plouf
Posts: 585
Joined: Sun Feb 04, 2018 11:51 pm
Location: Athens,Greece

Re: Platformer / Jump & run

Post by plouf »

hello

you iskeyup algorithm is a bit strange

i have remoevd dy=dy+1 ,why is there ? its cosntantly rising and confusing rest of checks
and changed isKeyDown("UP")

Code: Select all

@DISPLAY{WIDTH = 800, HEIGHT = 600, MODE = "WINDOWED"} ;Create the window

CreateBrush(1, 16, 16, #GREEN)  ; CREATE BLOCK-MAN
CreateBrush(2, 32, 32, #GRAY)   ; CREATE OBSTICLE
CreateBrush(3,800,  5, #WHITE)  ; create floor

; ----------------- init

x = 380			; x position player
y = 555			; y position player
dx = 0			; x movement player
dy = 0			; y movement player
onground = True		; true if player is "on ground" else false
jump = 0		; jump height
jumpmax = 5		; maximum jump height
speed = 3		; movement speed player
jumping = False		; seems to be needed for Hollywood


Function p_UpdateScreen()

	dx = 0
	;dy = dy + 1   ; WHY ?
	
	
	If IsKeyDown("LEFT") = True Then dx = dx - speed
	If IsKeyDown("RIGHT") = True Then dx = dx + speed
	If IsKeyDown("UP") = True 
	  jumping = True
	  dy=dy-2
  Else 
	  jumping = False
	 If Not onground
	  dy=dy+1
	 EndIf
	EndIf	    


	If jumping = True
		onground=False
		jump=jump + 1
		;dy = dy - 2      ;  BETTER IN isKEyUP
	EndIf
	
	If jump >jumpmax
		jump = 0
		jumping = False
		onground = True
	EndIf

;	If jump > jumpmax 
;		dy = dy + 5
;	ElseIf jump <= 0
;		onground = True
;;		dy = 0
;	EndIf

	If dy > 0 Then onground = True
	If onground 
		jump = 0
;		dy = 0
	EndIf
	
	x = x + dx
	y = y + dy

    ;Check if player leaves the screen
	If x < 0 Then x = 0
	If x > 784 Then x = 784
	If y < 0 Then y = 0
	If y > 555 Then y = 555	


    	DisplayBrush(1, x, y)
	DisplayBrush(3, 0,555+16)
	
EndFunction

BeginDoubleBuffer()
EscapeQuit(True)
Repeat
	Cls()
	p_UpdateScreen()
	Flip()
Forever
EndDoubleBuffer()

Christos
jalih
Posts: 278
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: Platformer / Jump & run

Post by jalih »

plouf wrote: Thu Sep 19, 2024 7:06 pm i have remoevd dy=dy+1 ,why is there ? its cosntantly rising and confusing rest of checks
Probably should be simulating gravity and it needs to constantly rise on a platformer game or the player can't fall! Ground tile collision detection code when colliding should push the player back up until it's bottom collision point is no more colliding with the ground tile, then set 'dy=0' and 'jumping=false'.
jalih
Posts: 278
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: Platformer / Jump & run

Post by jalih »

Here is how it should behave.
phipslk
Posts: 34
Joined: Sun Oct 29, 2023 7:21 pm

Re: Platformer / Jump & run

Post by phipslk »

jalih wrote: Thu Sep 19, 2024 9:58 pm Here is how it should behave.
that's how it should work. Did you code that in hollywood?

plouf, it's as jalih said. it's for gravity, so the player falls always down, otherwise he would stay "in the air". I didn't do the collision routines, just the player movement, so it may interfere now with other calculations of player movement
plouf
Posts: 585
Joined: Sun Feb 04, 2018 11:51 pm
Location: Athens,Greece

Re: Platformer / Jump & run

Post by plouf »

yes i understand that is gravity
but its there rising at any case, so when you press up it FIRST decreased town to 0 and THEN jumps .
thats the delay he has notice

replace

Code: Select all

dy = dy + 1
with

Code: Select all

	If dy<1
		dy = dy + 1
	EndIf
to get what (i think) you are planing to
Christos
Post Reply