Real beginner needs help over basic code

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kerfew.

How do I go about displaying 1 sprite on a screen several times at once, but all doing different things?

DisplaySprite (0, rabbitx, rabbity)

Say I want one sprite to stay still, while another moves left, and another goes around in a circle. I tried to understand the examples in Pure Basic using structures, but I couldn't get it to work.

I tried copying the sprite and then doing somehting like this

For c=0 to 3
DisplaySprite(c, rabbitx(c), rabbity(c))
Next

But I'm not really sure how to properly go about it.

Everyone else here doesn't seem to be beginners compared to me. :(

Kefrew
confused - dazed - but not dead
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.

You don't need to copy the sprite you can display the same sprite several times at different coordinates(on Windows/Linux version this is true, amiga version might be different though).
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by TheBeck.

Try this:

For c=0 to 3
DisplaySprite(0, rabbitx(c), rabbity(c))
Next
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kerfew.

The reason why I copied the sprite (and only because I know no better) is that if I want one sprite to do something to interact with another sprite, I assumed that a sprite with the same number would then be read by all, and not just the one that is interacting. if that even makes a ounce of sense. :)

I tried that line of code TheBeck, but it says it's not a function. :(

This is my code in full.

; Make sure the thing will work
If InitKeyboard() = 0 Or InitSprite() = 0
MessageRequester("Error", "Can't open DirectX 7", 0)
End
EndIf

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

; Set basic variables
rabbitx=100
rabbity=375

; Draw a box on screen, then turn it into a sprite, then delete drawn box
StartDrawing(ScreenOutput())
tone=RGB(100,0,100)
Box(10, 10, 10, 3, tone)
Box(0, 0, 10, 10, tone)
StopDrawing()
GrabSprite(0, 0, 0, 10, 10, 0)
ClearScreen(0,0,0)

For c=1 To 2
CopySprite(0, c)
Next

; animate and move sprite
Repeat
FlipBuffers()
ClearScreen(0,0,0)
ExamineKeyboard()
For c=0 To 2
DisplaySprite(c, rabbitx+(c), rabbity)
Next

; this is just a simple line to see if I can get one of the boxes to do its own thing
rabbitx2=rabbitx2+1

If (KeyboardPushed(#PB_Key_Right))
rabbitx=rabbitx+5
EndIf

If (KeyboardPushed(#PB_Key_Left))
rabbitx=rabbitx-5
EndIf

If (KeyboardPushed(#PB_Key_Up))
rabbity=rabbity-5
EndIf

If (KeyboardPushed(#PB_Key_Down))
rabbity=rabbity+5
EndIf

Until KeyboardPushed(#PB_Key_Escape)

End

As you can see there is a line that reads rabbitx2=rabbitx2+1. What I'm trying to achieve is just to see if I can get one sprite to move off by itself without being controlled by the arrow keys.

I've done this in AMOS over a decade ago, and more receintly in FLASH, but I don't know the structure of how to do things in Pure Basic, and I'm not following the online help.

I'd probably be alright if I knew the syntax of Pure Basic. For example in structures, I don't really understand how to call it and retrieve data. And I see things like y.x which means nothing to me. :(

Hope someone smart out there can help me.

Kefrew
confused - dazed - but not dead
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> How do I go about displaying 1 sprite on a screen several times at once, but all doing different things?

Store each sprite's X/Y values in an array, and position them according
to the arrays. Here's your code updated to reflect this... when you
run this app, press space a few times and each sprite will move to the
right, independently of each other.

Code: Select all

; Make sure the thing will work
If InitKeyboard() = 0 Or InitSprite() = 0
MessageRequester("Error", "Can't open DirectX 7", 0)
End
EndIf

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

Dim rabbitx(2)
Dim rabbity(2)

rabbitx(0)=100 : rabbity(0)=100
rabbitx(1)=200 : rabbity(1)=200
rabbitx(2)=300 : rabbity(2)=300

; Draw a box on screen, then turn it into a sprite, then delete drawn box
StartDrawing(ScreenOutput())
tone=RGB(100,0,100)
Box(10, 10, 10, 3, tone)
Box(0, 0, 10, 10, tone)
StopDrawing()
GrabSprite(0, 0, 0, 10, 10, 0)
ClearScreen(0,0,0)

For c=1 To 2
CopySprite(0, c) 
Next

; animate and move sprite
Repeat
FlipBuffers()
ClearScreen(0,0,0)
ExamineKeyboard()
For c=0 To 2
DisplaySprite(c,rabbitx(c),rabbity(c))
Next

If (KeyboardPushed(#PB_Key_Space))
rabbitx(0)+1
rabbitx(1)+2
rabbitx(2)+3
EndIf

Until KeyboardPushed(#PB_Key_Escape)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kerfew.

Thanks all, especially PB. That'll help me get off on the right foot to some degree. I'll probably be back later to ask other silly questions.

No actually I'll do it now, What's the differences between Procedure, Structure, and Gosub? And how do they work. A small example with an clear explination would really help me. I'm not following the manual at all. Gawd how dumb am I aye..... :)

Kefrew
confused - dazed - but not dead
Post Reply