Author Topic: [SOLVED]PingPong an Image?  (Read 1147 times)

0 Members and 1 Guest are viewing this topic.

Offline Omnikam

  • Atari ST
  • ***
  • Posts: 101
  • Karma: 4
    • View Profile
[SOLVED]PingPong an Image?
« on: November 01, 2015 »
Hey there, I'm having a problem with my code
I'm trying to move an image across the screen and then move it back, and repeat the procedure, so it move moves in a PingPong. My solution was to use a timer and have it set conditions using if/end   Unfortunately it doest work as expected, I'm not sure why because my code logic seem logical. Any help would be appreciated
Code: [Select]
InitSprite()
UseJPEGImageDecoder()
UsePNGImageDecoder()
InitKeyboard() 
 Global timer= 1
a= 0
b= -1100
OpenScreen(800,600,32,"PBWINDOW")

wave = LoadSprite (#PB_Any, "wave.png")
Repeat
 
  ExamineKeyboard()   
TransparentSpriteColor(wave,#White)
ClearScreen($0)

DisplayTransparentSprite(wave, a,300)
DisplayTransparentSprite(wave,b,400)
If timer<900
  b=b+2
  timer=timer+1
  EndIf
  If timer>1200
    b=b-2
    timer = timer+1
    EndIf
    If timer=>2000
     timer=0
 EndIf

 
FlipBuffers()

Until KeyboardPushed (#PB_Key_Escape)
End

« Last Edit: November 01, 2015 by Omnikam »

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2723
  • Karma: 485
    • View Profile
    • http://www.rbraz.com/
Re: PingPong an Image?
« Reply #1 on: November 01, 2015 »
I don't have purebasic here, but try to use screen coordinates and a variable to control direction, something like this:

Code: [Select]
InitSprite()
UseJPEGImageDecoder()
UsePNGImageDecoder()
InitKeyboard() 
Global timer= 1

a = 0
b = 200
moveflag = 0

OpenScreen(800,600,32,"PBWINDOW")

wave = LoadSprite (#PB_Any, "wave.png")

Repeat
 
ExamineKeyboard()   
TransparentSpriteColor(wave,#White)
ClearScreen($0)

DisplayTransparentSprite(wave, a,300)
DisplayTransparentSprite(wave,b,400)


If moveflag=0
    b=b+2
 
    if b>500
        moveflag = 1
    EndIf
 
EndIf

If moveflag=1
    b=b-2

    if b<100
        moveflag = 0
    EndIf

EndIf
 
FlipBuffers()

Until KeyboardPushed (#PB_Key_Escape)
End

Challenge Trophies Won:

Offline Omnikam

  • Atari ST
  • ***
  • Posts: 101
  • Karma: 4
    • View Profile
Re: PingPong an Image?
« Reply #2 on: November 01, 2015 »
That was just what i was looking for, Thankyou. It`s easy to understand whats going on, so ill be able to
use it for lots of different applications
have good karma

Offline emook

  • C= 64
  • **
  • Posts: 88
  • Karma: 12
    • View Profile
Re: [SOLVED]PingPong an Image?
« Reply #3 on: November 12, 2015 »
Here are some other ping pong routines that you may find useful.

The first is a ping pong using a procedure, everytime it is called the value is increased or decreased if it has reached its max :

#maxCount = 500

Code: [Select]
Procedure PingPong()
  Static _count = #maxCount - 1
  _count = (_count + 1) % (#maxCount << 1)
  ProcedureReturn Abs(_count - #maxCount)
EndProcedure

Repeat
  Debug PingPong()
  Delay (5)
ForEver

Then we have one which looks much more complex but has the benefit of being autonomous, and you can have multiple pong pongs being managed with the same procedure :

Code: [Select]
; With this one you can set a min, a max, and you can keep track of more then one ping-pong at the time.

Structure T_PINGPONG
 min.i
 max.i
 counter.i
 direction.i
EndStructure

Procedure.i PingPong (*p.T_PINGPONG)
  If *p\direction = 0
    *p\direction = 1
    *p\counter = *p\min
  Else
    *p\counter + *p\direction   
    If *p\counter = *p\min Or *p\counter = *p\max
        *p\direction = -*p\direction
    EndIf   
  EndIf   
  ProcedureReturn *p\counter
EndProcedure



;****************************
; DEMO
;****************************


Define pp1.T_PINGPONG
Define pp2.T_PINGPONG

pp1\min = 1
pp1\max = 10

pp2\min = -5
pp2\max =  5

Repeat
  Debug "pp1 = " + PingPong(@pp1) + ", pp2 = " + PingPong(@pp2) 
  Delay (500)
ForEver

And put into your demo, as you can see the logic loop is much simpler now  :

Code: [Select]
InitSprite()
UseJPEGImageDecoder()
UsePNGImageDecoder()
InitKeyboard() 
Global timer= 1

Structure T_PINGPONG
  min.i
  max.i
  counter.i
  direction.i
EndStructure

Procedure.i PingPong (*p.T_PINGPONG)
  If *p\direction = 0
    *p\direction = 1
    *p\counter = *p\min
  Else
    *p\counter + *p\direction   
    If *p\counter = *p\min Or *p\counter = *p\max
      *p\direction = -*p\direction
    EndIf   
  EndIf   
  ProcedureReturn *p\counter
EndProcedure

Define pp1.T_PINGPONG
Define pp2.T_PINGPONG

pp1\min = 0
pp1\max = 50

pp2\min = 0
pp2\max =  50


OpenScreen(800,600,32,"PBWINDOW")

wave = LoadSprite (#PB_Any, "wave.png")

Repeat
 
  ExamineKeyboard()   
  TransparentSpriteColor(wave,#White)
  ClearScreen($0)
 
  DisplayTransparentSprite(wave, 50-PingPong(@pp1)-100,300)
  DisplayTransparentSprite(wave,PingPong(@pp2)-50,400)
 
 
  FlipBuffers()
 
Until KeyboardPushed (#PB_Key_Escape)
End
----

R Tape loading error, 0:1

Offline Omnikam

  • Atari ST
  • ***
  • Posts: 101
  • Karma: 4
    • View Profile
Re: [SOLVED]PingPong an Image?
« Reply #4 on: November 12, 2015 »
Nice one Emook  :goodpost: Ill definatly have to study each example
Have good karma  ;D
Okay i looked at them, Brilliant  :clap: very very handy
« Last Edit: November 12, 2015 by Omnikam »