Page 1 of 1

Mini Mario

Posted: Thu Aug 14, 2025 2:35 pm
by threedslider
Hi,

Some nice game as mini mario so you can expand more in your purpose game :D

Here the code :

Code: Select all

InitSprite()
InitKeyboard()
InitMouse()

#SCREEN_WIDTH = 800
#SCREEN_HEIGHT = 600
#PLAYER_WIDTH = 32
#PLAYER_HEIGHT = 32
#GROUND_Y = 550

OpenWindow(0, 0, 0, #SCREEN_WIDTH/DesktopResolutionX(), #SCREEN_HEIGHT/DesktopResolutionY(), "Mini Mario", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)

Structure Player
  x.f
  y.f
  vx.f
  vy.f
  onGround.b
EndStructure

Global mario.Player
mario\x = 100
mario\y = #GROUND_Y - #PLAYER_HEIGHT
mario\vx = 0
mario\vy = 0
mario\onGround = #True

Global gravity.f = 0.5
Global jumpForce.f = -10
Global moveSpeed.f = 4

Repeat
  ClearScreen(RGB(135, 206, 235)) ; blue sky
  
  ExamineKeyboard()
  
  ; move to left/right
  If KeyboardPushed(#PB_Key_Left)
    mario\vx = -moveSpeed
  ElseIf KeyboardPushed(#PB_Key_Right)
    mario\vx = moveSpeed
  Else
    mario\vx = 0
  EndIf
  
  ; jump
  If KeyboardPushed(#PB_Key_Space) And mario\onGround
    mario\vy = jumpForce
    mario\onGround = #False
  EndIf
  
  ; gravity
  mario\vy + gravity
  
  ; position update
  mario\x + mario\vx
  mario\y + mario\vy
  
  ; Collision with the ground
  If mario\y >= #GROUND_Y - #PLAYER_HEIGHT
    mario\y = #GROUND_Y - #PLAYER_HEIGHT
    mario\vy = 0
    mario\onGround = #True
  EndIf
  
  StartDrawing(ScreenOutput())
  
  ; draw of ground
  Box(0, #GROUND_Y, #SCREEN_WIDTH, #SCREEN_HEIGHT - #GROUND_Y, RGB(34, 139, 34)) ; green ground
  
  ; draw of player
  Box(mario\x, mario\y, #PLAYER_WIDTH, #PLAYER_HEIGHT, RGB(255, 0, 0)) ; red player
  
  StopDrawing()
  
  FlipBuffers()
  
  Delay(16) ; ~60 FPS
Until KeyboardPushed(#PB_Key_Escape)
left/right with arrow key and jump with space key

Enjoy and happy coding !

Re: Mini Mario

Posted: Thu Aug 14, 2025 3:18 pm
by moulder61
@threedslider

It didn't work for me in Linux until I added an event handler routine. Don't know about other OS's?

It could be useful to me at some point, so thanks. :wink:

The PB help says this about drawing operations to the screen:
On Linux and OS X, ScreenOutput() copies the whole screen buffer back to main memory to do 2D drawing operations (OpenGL doesn't allow direct buffer access). Therefore drawing on a screen is very slow and should be avoided. ScreenOutput() has to be called in the same thread where OpenScreen() was called.
Moulder.

Re: Mini Mario

Posted: Thu Aug 14, 2025 3:29 pm
by Axolotl
Hi threedslider,
thanks for sharing.
I'm not interested in Screen...stuff, but the mouse pointer looks like a doughnut and the window can't be closed using the close button.
Acc. to the help I added this between Repeat and ClearScreen.

Code: Select all

Repeat 
; begin 
  ; It's very important to process all the events remaining in the queue at each frame
  ;
  Repeat
    Select WindowEvent()
      Case #PB_Event_None 
        Break 
      Case #PB_Event_CloseWindow
        Break 2 ; instead of End  ? 
    EndSelect
  ForEver 
; end 
  ClearScreen(RGB(135, 206, 235)) ; blue sky

Re: Mini Mario

Posted: Thu Aug 14, 2025 3:38 pm
by miso
Let's expand then. No need for delay with flipbuffers presented. Theres setframerate(), or alternatively vsync.

Code: Select all

InitSprite()
InitKeyboard()
InitMouse()

#SCREEN_WIDTH = 800
#SCREEN_HEIGHT = 600
#PLAYER_WIDTH = 32
#PLAYER_HEIGHT = 32
#GROUND_Y = 550

OpenWindow(0, 0, 0, #SCREEN_WIDTH/DesktopResolutionX(), #SCREEN_HEIGHT/DesktopResolutionY(), "Mini Mario", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600) : SetFrameRate(60)
;OpenWindowedScreen(WindowID(0), 0, 0, 800, 600, 0, 0, 0, #PB_Screen_WaitSynchronization)

Structure Player
  x.f
  y.f
  vx.f
  vy.f
  onGround.b
EndStructure

Global mario.Player
mario\x = 100
mario\y = #GROUND_Y - #PLAYER_HEIGHT
mario\vx = 0
mario\vy = 0
mario\onGround = #True

Global gravity.f = 0.5
Global jumpForce.f = -10
Global moveSpeed.f = 4

Repeat
  ; It's very important to process all the events remaining in the queue at each frame
  ;
  Repeat
    Select WindowEvent()
      Case #PB_Event_None 
        Break 
      Case #PB_Event_CloseWindow
        Break 2 ; instead of End  ? 
    EndSelect
  ForEver 

  ClearScreen(RGB(135, 206, 235)) ; blue sky

  
  ClearScreen(RGB(135, 206, 235)) ; blue sky
  
  ExamineKeyboard()
  
  ; move to left/right
  If KeyboardPushed(#PB_Key_Left)
    mario\vx = -moveSpeed
  ElseIf KeyboardPushed(#PB_Key_Right)
    mario\vx = moveSpeed
  Else
    mario\vx = 0
  EndIf
  
  ; jump
  If KeyboardPushed(#PB_Key_Space) And mario\onGround
    mario\vy = jumpForce
    mario\onGround = #False
  EndIf
  
  ; gravity
  mario\vy + gravity
  
  ; position update
  mario\x + mario\vx
  mario\y + mario\vy
  
  ; Collision with the ground
  If mario\y >= #GROUND_Y - #PLAYER_HEIGHT
    mario\y = #GROUND_Y - #PLAYER_HEIGHT
    mario\vy = 0
    mario\onGround = #True
  EndIf
  
  StartDrawing(ScreenOutput())
  
  ; draw of ground
  Box(0, #GROUND_Y, #SCREEN_WIDTH, #SCREEN_HEIGHT - #GROUND_Y, RGB(34, 139, 34)) ; green ground
  
  ; draw of player
  Box(mario\x, mario\y, #PLAYER_WIDTH, #PLAYER_HEIGHT, RGB(255, 0, 0)) ; red player
  
  StopDrawing()
  
  FlipBuffers()
  
;  Delay(16) ; ~60 FPS
Until KeyboardPushed(#PB_Key_Escape)

Re: Mini Mario

Posted: Thu Aug 14, 2025 4:11 pm
by moulder61
@miso

That looks a lot smoother. :D

Moulder.

Re: Mini Mario

Posted: Thu Aug 14, 2025 4:59 pm
by threedslider
Thanks to all for correction :mrgreen:

Re: Mini Mario

Posted: Thu Aug 14, 2025 11:29 pm
by minimy
Hey guys, very nice example. Thanks for share!. Where is Luigi? :lol: