It is currently Thu Jul 02, 2020 7:54 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: [2D Programming] Bouncing Block Script not Work, Help Need!
PostPosted: Fri Jul 27, 2018 12:35 pm 
Offline
User
User
User avatar

Joined: Sun Jun 24, 2018 12:28 pm
Posts: 34
Location: Holland
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:
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: [2D Programming] Bouncing Block Script not Work, Help Ne
PostPosted: Fri Jul 27, 2018 1:21 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Apr 25, 2003 7:09 pm
Posts: 250
Location: (Madrid) Spain
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


Top
 Profile  
Reply with quote  
 Post subject: Re: [2D Programming] Bouncing Block Script not Work, Help Ne
PostPosted: Fri Jul 27, 2018 5:23 pm 
Offline
Always Here
Always Here

Joined: Mon Sep 22, 2003 6:45 pm
Posts: 7446
Location: Norway
There is no need to make it global, simply move it to right before repeat:
Code:
Procedure Draw2D()
Direction = 1
  Repeat


Top
 Profile  
Reply with quote  
 Post subject: Re: [2D Programming] Bouncing Block Script not Work, Help Ne
PostPosted: Sun Jul 29, 2018 10:45 am 
Offline
User
User
User avatar

Joined: Sun Jun 24, 2018 12:28 pm
Posts: 34
Location: Holland
Trond wrote:
There is no need to make it global, simply move it to right before repeat:
Code:
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:
;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


Top
 Profile  
Reply with quote  
 Post subject: Re: [2D Programming] Bouncing Block Script not Work, Help Ne
PostPosted: Fri Aug 10, 2018 3:06 pm 
Offline
User
User

Joined: Thu Oct 01, 2009 2:10 pm
Posts: 48
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:
; ------------
; 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


Top
 Profile  
Reply with quote  
 Post subject: Re: [2D Programming] Bouncing Block Script not Work, Help Ne
PostPosted: Fri Aug 10, 2018 6:37 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 24, 2004 8:51 am
Posts: 1482
Location: Germany
Maybe you have to look add
[Chipmunk2D4PB] 2D Physics Engine
https://www.purebasic.fr/english/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,...


Top
 Profile  
Reply with quote  
 Post subject: Re: [2D Programming] Bouncing Block Script not Work, Help Ne
PostPosted: Fri Sep 07, 2018 8:37 pm 
Offline
User
User
User avatar

Joined: Fri Mar 04, 2005 7:46 pm
Posts: 73
Location: argentina
need fix the position of the box i think. look

Code:
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)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye