Mini Mario

Advanced game related topics
threedslider
Enthusiast
Enthusiast
Posts: 393
Joined: Sat Feb 12, 2022 7:15 pm

Mini Mario

Post 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 !
User avatar
moulder61
Enthusiast
Enthusiast
Posts: 192
Joined: Sun Sep 19, 2021 6:16 pm
Location: U.K.

Re: Mini Mario

Post 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.
"If it ain't broke, fix it until it is!

This message is brought to you thanks to SenselessComments.com

My PB stuff for Linux: "https://u.pcloud.link/publink/show?code ... z3MR0T3jyV
Axolotl
Addict
Addict
Posts: 819
Joined: Wed Dec 31, 2008 3:36 pm

Re: Mini Mario

Post 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
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
miso
Enthusiast
Enthusiast
Posts: 459
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Mini Mario

Post 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)
User avatar
moulder61
Enthusiast
Enthusiast
Posts: 192
Joined: Sun Sep 19, 2021 6:16 pm
Location: U.K.

Re: Mini Mario

Post by moulder61 »

@miso

That looks a lot smoother. :D

Moulder.
"If it ain't broke, fix it until it is!

This message is brought to you thanks to SenselessComments.com

My PB stuff for Linux: "https://u.pcloud.link/publink/show?code ... z3MR0T3jyV
threedslider
Enthusiast
Enthusiast
Posts: 393
Joined: Sat Feb 12, 2022 7:15 pm

Re: Mini Mario

Post by threedslider »

Thanks to all for correction :mrgreen:
User avatar
minimy
Enthusiast
Enthusiast
Posts: 596
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: Mini Mario

Post by minimy »

Hey guys, very nice example. Thanks for share!. Where is Luigi? :lol:
If translation=Error: reply="Sorry, Im Spanish": Endif
Post Reply