[2D Programming] Bouncing Block Script not Work, Help Need!

Advanced game related topics
User avatar
JamieVanCadsand
User
User
Posts: 34
Joined: Sun Jun 24, 2018 12:28 pm
Location: Holland

[2D Programming] Bouncing Block Script not Work, Help Need!

Post by JamieVanCadsand »

Hey Game Programmers...,

I try to create an simple block, they must be bounce from under to above, but it doesn't work perfect...


So This is my script, written in PB5.6.2, on Windows XP:

Code: Select all

Procedure Initialize2D()
  If InitSprite() = 0
    MessageRequester("Fatal Error", "Failed to Initialize Sprites", #PB_MessageRequester_Error)
    End
  EndIf
  
  If InitKeyboard() = 0
    MessageRequester("Fatal Error", "Failed to Initialize Keyboard", #PB_MessageRequester_Error)
    End
  EndIf
  
  If OpenScreen(640, 480, 32, "") = 0
    MessageRequester("Fatal Error", "Failed to Open Screen", #PB_MessageRequester_Error)
    End
  EndIf 
EndProcedure

Initialize2D()




Global X.i = 64
Global Y.i = 480


Procedure Draw2D()
  Repeat
    
    Select Direction
      Case 1
        Y - 2
      Case 2
        Y + 2
    EndSelect
    
    StartDrawing(ScreenOutput())
    Box(X, Y, 32, 32, RGB(255, 32, 100))
    StopDrawing()
    
    FlipBuffers()
    ClearScreen(0)
    
    ExamineKeyboard()
    
    Direction = 1
    If Y < 0
      Direction = 2
    EndIf
    
    If Y > 480
      Direction = 1
    EndIf
    
    
    
    If KeyboardPushed(#PB_Key_Escape)
      End
    EndIf
    
  ForEver
EndProcedure

Draw2D()

Just run this script and you should see thad this isn't work perfect...
Here my problem:
- The Box starts from under (position Y > 480) to above...
- But if he bounced from position 'Y < 0', than he don't goes to under... He is and remains on the 'Y < 0' Position..


If you can see what the problem is, please correct my code so simple as possible, just i can learn about it...
Pleace, only correct my code so simple as possible, without dificult and complex example.... i am an noob with
programming, just pleace, try to correct my code so simple as possible...

Thanks for help, Jamie.
Saboteur
Enthusiast
Enthusiast
Posts: 271
Joined: Fri Apr 25, 2003 7:09 pm
Location: (Madrid) Spain
Contact:

Re: [2D Programming] Bouncing Block Script not Work, Help Ne

Post by Saboteur »

Put Global Direction.l=1 with other global declarations.
Remove first alone "Direction = 1" from loop.
[:: PB Registered ::]

Win10 Intel core i5-3330 8GB RAM Nvidia GTX 1050Ti
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: [2D Programming] Bouncing Block Script not Work, Help Ne

Post by Trond »

There is no need to make it global, simply move it to right before repeat:

Code: Select all

Procedure Draw2D()
Direction = 1
  Repeat
User avatar
JamieVanCadsand
User
User
Posts: 34
Joined: Sun Jun 24, 2018 12:28 pm
Location: Holland

Re: [2D Programming] Bouncing Block Script not Work, Help Ne

Post by JamieVanCadsand »

Trond wrote:There is no need to make it global, simply move it to right before repeat:

Code: Select all

Procedure Draw2D()
Direction = 1
  Repeat


It works perfectly...., thanks for help.
Jamie.




Edit:

BFernhout get helped my with this follow code, just to exmiriment with gravity and friction yet:

Code: Select all

;Bal gravety

Global friction.f = 0.90
Global Gravity.f = 1.0
Global Jump.b = 0
Global Moving.b = 0
Global Bouncing.b = 0

Structure Ball_s
  x.i
  y.i
  speed.f
  radius.i
  col.l
EndStructure

Global NewList ball.ball_s()

If InitSprite() = 0 Or InitKeyboard() = 0 Or InitSound() = 0 Or InitSprite() = 0
  MessageRequester("Error", "Unable to init system utilities (Possible Direct X7 or higher not installed!)", 0)
  End
EndIf

OpenWindow(0, 0, 0, 640, 480, "A screen in a window...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
If OpenWindowedScreen(WindowID(0), 0, 0, 640, 480) = 0
  MessageRequester("Error", "Can't open windowed screen!", 0)
  End
EndIf


AddElement(ball())
With ball()
  \x = ScreenWidth()/2
  \y = ScreenHeight()-31
  \speed = 0
  \radius = 30
  \col = RGB(Random(255,0),Random(255,0),Random(255,0))
EndWith

Repeat
  
  If ball()\y + ball()\radius > ScreenHeight()
    ball()\speed = 0
    Jump = 0
    Bouncing = 0
  Else
    ball()\speed + Gravity
  EndIf
  
  
  ExamineKeyboard()
  If Bouncing = 0
    If KeyboardPushed(#PB_Key_Space) And Jump = 0
      ball()\speed = -20
      Jump = 1
    EndIf
    
    If KeyboardPushed(#PB_Key_Left)
      Moving = -1
    ElseIf KeyboardPushed(#PB_Key_Right)
      Moving = 1
    Else
      Moving = 0
    EndIf
  EndIf
  
  If ball()\x - 30 < 0
    Moving = -Moving
    Bouncing = 1
  EndIf
  
  If ball()\x + 30 > ScreenWidth()
    Moving = -Moving
    Bouncing = 1
  EndIf
  
  ball()\x + (2 * Moving)
  ball()\y + ball()\speed
  
  StartDrawing(ScreenOutput())
  Circle(ball()\x,ball()\y,ball()\radius,ball()\col)
  StopDrawing()
  
  FlipBuffers()
  ClearScreen(0)
  
  
  
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Escape)
    End
  EndIf
  event = WindowEvent()
  If event=#PB_Event_CloseWindow
    End
  EndIf
ForEver
dman
User
User
Posts: 48
Joined: Thu Oct 01, 2009 2:10 pm

Re: [2D Programming] Bouncing Block Script not Work, Help Ne

Post by dman »

It's not a great example of gravity with bounce effect but it works for what I needed it for, hope it serves a purpose for you.

Code: Select all

; ------------
; Gravity Test
; ------------

UsePNGImageDecoder()

InitSprite()
InitKeyboard()

If OpenScreen(640, 480, 32, "Gravity")
  posX.f
  posY.f
  
  direction.i
  
  ground.f = 385.0
  gravity.f = 20.0
  bounceFactor.f = 8.0
  velocity.f = 0.0
  
  start.i = 0
  
  LoadSprite(0, "ball.png",#PB_Sprite_AlphaBlending)
  
  posX = ScreenWidth()/2
  posY = ground
  
  SetFrameRate(30)
  
  Repeat
    ClearScreen(RGB(0, 0, 0))
    
    ExamineKeyboard()
    
    If KeyboardPushed(#PB_Key_Space)
      start = 1
      gravity = 20.0
      bounceFactor = 8.0   
    EndIf
    
    If (KeyboardPushed(#PB_Key_Up))
      direction = 0
    EndIf
    
    If (KeyboardPushed(#PB_Key_Left))
      direction = -1
    EndIf
    
    If (KeyboardPushed(#PB_Key_Right))
      direction = 1
    EndIf
    
    If start = 1
      velocity = velocity + 1.0
      
      posY = posY + velocity - gravity
      posX = posX + (direction*3)
      
      ;If sprite has hit the ground
      If posY >= ground
        ;Apply bounce
        gravity = bounceFactor
        bounceFactor = bounceFactor - 1.0
        
        ;Reset bounceFactor
        If bounceFactor <= 0.0
          bounceFactor = 8.0
          gravity = 20.0
          start = 0
        EndIf
        
        velocity = 0.0
      EndIf
    EndIf
    
    DisplayTransparentSprite(0, posX, posY)
    
    FlipBuffers()
   
  Until KeyboardPushed(#PB_Key_Escape)
EndIf
PureBasic
User avatar
IceSoft
Addict
Addict
Posts: 1616
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: [2D Programming] Bouncing Block Script not Work, Help Ne

Post by IceSoft »

Maybe you have to look add
[Chipmunk2D4PB] 2D Physics Engine
viewtopic.php?f=14&t=44068
There are some Chipmunk2D4PB examples which maybe be useful for you too.
Belive!
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
zefiro_flashparty
User
User
Posts: 74
Joined: Fri Mar 04, 2005 7:46 pm
Location: argentina

Re: [2D Programming] Bouncing Block Script not Work, Help Ne

Post by zefiro_flashparty »

need fix the position of the box i think. look

Code: Select all

Procedure Initialize2D()
  If InitSprite() = 0
    MessageRequester("Fatal Error", "Failed to Initialize Sprites")
    End
  EndIf
 
  If InitKeyboard() = 0
    MessageRequester("Fatal Error", "Failed to Initialize Keyboard")
    End
  EndIf
 
  If OpenScreen(640, 480, 32, "") = 0
    MessageRequester("Fatal Error", "Failed to Open Screen")
    End
  EndIf
EndProcedure

Initialize2D()




Global X.i = 64
Global Y.i = 480-32;(box side)
                   ;
                   ;
                   ;480 is the last pixel, if create here u create box (down)
                   ;
                   ;480---------*--------------*---------------------------
                   ;            l              l
                   ;            l              l   NOT VISIBLE SCREEN
                   ;      32px  l     box      l
                   ;            l              l
                   ;            l              l
                   ;            l--------------l
;
                   ;
                   ;
                   ;



Procedure Draw2D()
   Direction = 1
  Repeat
   
    Select Direction
      Case 1
        Y - 2
      Case 2
        Y + 2
    EndSelect
   
    StartDrawing(ScreenOutput())
    Box(X, Y, 32, 32, RGB(255, 32, 100))
    StopDrawing()
   
    FlipBuffers()
    ClearScreen(0)
   
    ExamineKeyboard()
   
   
    If Y < 0
      Direction = 2
    EndIf
   
    If Y > 480 -32;(box side)
      Direction = 1
    EndIf
   
   
   
    If KeyboardPushed(#PB_Key_Escape)
      End
    EndIf
   
  ForEver
EndProcedure

Draw2D()
Amd Vishera fx8350 ,16Gbram, Gtx650 ti, 2gb,Win 10pro. 13tbs. 8)
Post Reply