Page 1 of 2
Help with array within structures
Posted: Fri Aug 11, 2006 12:09 am
by robert1352
Hi all im working an a game in which im using a structure for creating my enemy bad guys. The problem im facing is how do I take one structure and use it to display diffrent sprites on screen instead of just the same one over and over again? I think the answer might be to use an array within my structure ,but Im not sure how to pull it off. Any suggestions would be helpful. Thank you all very much

Posted: Fri Aug 11, 2006 3:41 am
by Fluid Byte
Honestly throw me a bone here! How do you display a sprite using a structure?
The problem im facing is how do I take one structure and use it to display diffrent sprites on screen instead of just the same one over and over again?
A good start would be to post the piece of code that can show us how you currently
use a structure to display a sprite. I can only guess that you mean a specific kind of property field within the structure that defines what kind of sprite to display. If that's the case, you looking for something like that?
Code: Select all
InitSprite() : InitKeyboard()
OpenScreen(640,480,16,"untitled")
CreateSprite(0,64,64)
CreateSprite(1,64,64)
CreateSprite(2,64,64)
StartDrawing(SpriteOutput(0))
Box(0,0,64,64,$FF0000)
StopDrawing()
StartDrawing(SpriteOutput(1))
Ellipse(32,32,32,32,$00FF00)
StopDrawing()
StartDrawing(SpriteOutput(2))
FrontColor($0000FF)
LineXY(0,64,32,0) : LineXY(32,0,64,64) : LineXY(0,63,63,63)
FillArea(32,32,$0000FF)
StopDrawing()
Structure MYSTRUCT
X.w
Y.w
Type.b
EndStructure
NewList mys.MYSTRUCT()
AddElement(mys())
mys()\X = 200 : mys()\Y = 200 : mys()\Type = 0
AddElement(mys())
mys()\X = 300 : mys()\Y = 200 : mys()\Type = 1
AddElement(mys())
mys()\X = 400 : mys()\Y = 200 : mys()\Type = 2
Repeat
ClearScreen(RGB(20,50,100))
ExamineKeyboard()
ForEach mys()
Select mys()\Type
Case 0 : SprNum = 0
Case 1 : SprNum = 1
Case 2 : SprNum = 2
EndSelect
DisplaySprite(SprNum,mys()\X,mys()\Y)
Next
FlipBuffers()
Until KeyboardPushed(1)
I think the answer might be to use an array within my structure ,but Im not sure how to pull it off.
You could use static arrays like :
Code: Select all
Structure Window
X.w
Y.w
Array.l[10] ; 10 elements available (from 0 to 9)
EndStructure
Reply
Posted: Fri Aug 11, 2006 3:53 pm
by robert1352
Im sorry maybe I didnt phrase
my topic correctly.Below is some of my code. What Im trying to do is use this single structure with different sprites (I hope thats a little clearer),but when I use a for next loop to change the sprite file number ,the last sprite is the only one displayed .Im not sure if this is the best way to go about this, Im stumped.
Structure ghost
ghostX.w
ghostY.w
ghost_track_X.w
ghost_track_Y.w
ghost_direction.w
ghost_speedX.w
ghost_speedY.w
ghost_move.w
ghost_sprite.w
EndStructure
AddElement(ghosts())
ghosts()\ghostX=Random(10)*32
ghosts()\ghostY=Random(10)*32
ghosts()\ghost_track_X
ghosts()\ghost_track_Y
ghosts()\ghost_direction
ghosts()\ghost_speedX
ghosts()\ghost_speedY
ghosts()\ghost_move
ghosts()\ghost_sprite = 31
Posted: Fri Aug 11, 2006 4:17 pm
by dontmailme
Look at the Manual.... under linkedlist!
you want something like this...
resetlist ghost()
While NextElement(ghost())
displaysprite(ghost()\sprite, ghost()\x, ghost()\y)
Wend

Posted: Fri Aug 11, 2006 4:36 pm
by Kale
dontmailme wrote:Look at the Manual.... under linkedlist!
you want something like this...
resetlist ghost()
While NextElement(ghost())
displaysprite(ghost()\sprite, ghost()\x, ghost()\y)
Wend

It's faster to use 'ForEach'
Code: Select all
ForEach ghost()
DisplaySprite(ghost()\sprite, ghost()\x, ghost()\y)
Next
Posted: Fri Aug 11, 2006 4:51 pm
by Fluid Byte
What Im trying to do is use this single structure with different sprites
That's exactly what my code and the examples in the other two posts do.
Posted: Fri Aug 11, 2006 5:03 pm
by dontmailme
Fluid Byte wrote:What Im trying to do is use this single structure with different sprites
That's exactly what my code and the examples in the other two posts do.
Yes, but he's not got it yet !
He has to read the manual on linked list and he'll get it....

Posted: Fri Aug 11, 2006 8:21 pm
by Fluid Byte
Priceless! :roll:
Posted: Fri Aug 11, 2006 9:52 pm
by Num3
Don't go hard on the guy, we are all noob's at something...
Structures and linked lists puzzled me too for quite some time.
Not many basic dialects have those.
Just for fun, try to do this same thing in BlitzMax...
That's the reason i dumped it, takes too much time to work with linked lists
robert1352, don't feel bad about this... it takes time to learn how to speak purebasic

Posted: Fri Aug 11, 2006 10:32 pm
by Fluid Byte
Don't go hard on the guy, we are all noob's at something...
True!
Structures and linked lists puzzled me too for quite some time.
No one said they are easy to learn but I think it's more a semantic error of his.
Not many basic dialects have those.
Proves that PureBasic is tha' greatest once again!
robert1352, don't feel bad about this... it takes time to learn how to speak purebasic
Indeed! Don't hesitate to ask, we are willing to help.

Posted: Sat Aug 12, 2006 12:15 am
by dontmailme
Num3 wrote:Don't go hard on the guy, we are all noob's at something...
Structures and linked lists puzzled me too for quite some time.
Not many basic dialects have those.
Just for fun, try to do this same thing in BlitzMax...
That's the reason i dumped it, takes too much time to work with linked lists
robert1352, don't feel bad about this... it takes time to learn how to speak purebasic

Nobody's going hard on the guy
3 helpful answers is not being hard...

Thank You all!!!!!!!
Posted: Sat Aug 12, 2006 1:19 am
by robert1352
Thank you all for your insights. Its going to take me a while to figure this stuff out but as soon as i do i will let you know how it worked out. Once again thank you for your help and patience.

Re: Thank You all!!!!!!!
Posted: Sat Aug 12, 2006 1:34 am
by Fluid Byte
robert1352 wrote:Thank you all for your insights. Its going to take me a while to figure this stuff out but as soon as i do i will let you know how it worked out. Once again thank you for your help and patience. :lol
Lol no! I don't let you get away so easily!
Is your problem solved or not? If not please gimme a shout!
Posted: Sat Aug 12, 2006 11:04 am
by Num3
Here's an example (not my code, but i changed it a bit)
Code: Select all
InitSprite()
InitKeyboard()
OpenScreen(800,600,16,"")
ClearScreen(0)
TransparentSpriteColor(-1,RGB(255,0,255))
StartDrawing(ScreenOutput())
Box(0,0,32,32,RGB(255,0,255))
Circle(16,16,3,RGB(255,255,0))
StopDrawing()
GrabSprite(0,0,0,32,32)
ClearScreen(0)
StartDrawing(ScreenOutput())
For Z = 0 To 32
For ZZ = 0 To 32
Plot(Z,ZZ,RGB(0,Random(150)+100,0))
Next ZZ
Next Z
StopDrawing()
GrabSprite(1,0,0,32,32)
Global PlayerX
Global PlayerY
PlayerX = 64
PlayerY = 284
Structure Tile
X.l
Y.l
Fade.l ;Transparency
Mode.l ;1=normal 2=Fading
EndStructure
Global NewList Tile.Tile()
Procedure AddTile(X,Y); Call this whenever you want to add a tile
AddElement(Tile())
Tile()\X = X
Tile()\Y = Y
Tile()\Fade = 255
Tile()\Mode = 1
EndProcedure
Procedure UpdateTiles() ; Procedure for updating the tiles
If CountList(Tile()) > 0 ;Make sure there is at least 1 item in list
ForEach Tile() ;Loops through the tile list
If SpriteCollision(0,PlayerX,PlayerY,1,Tile()\X,Tile()\Y)
Tile()\Mode = 2
EndIf
If Tile()\Mode = 1
DisplaySprite(1,Tile()\X,Tile()\Y)
EndIf
If Tile()\Mode = 2
If Tile()\Fade<=0
DeleteElement(Tile())
Else
DisplayTranslucentSprite(1,Tile()\X,Tile()\Y,Tile()\Fade)
Tile()\Fade-2
EndIf
EndIf
Next
EndIf
EndProcedure
Procedure UpdatePlayer()
DisplayTransparentSprite(0,PlayerX,PlayerY)
EndProcedure
AddTile(64,284)
AddTile(96,284)
AddTile(128,284)
AddTile(160,284)
AddTile(160,252)
AddTile(160,220)
AddTile(160,316)
AddTile(160,348)
AddTile(192,220)
AddTile(224,220)
AddTile(256,220)
AddTile(192,348)
AddTile(224,348)
AddTile(256,348)
AddTile(288,252)
AddTile(288,220)
AddTile(288,316)
AddTile(288,348)
AddTile(288,284)
Repeat
ClearScreen(0)
;Start3D()
UpdateTiles()
UpdatePlayer()
;Stop3D()
ExamineKeyboard()
If KeyboardReleased(#PB_Key_Up)
PlayerY - 32
EndIf
If KeyboardReleased(#PB_Key_Down)
PlayerY + 32
EndIf
If KeyboardReleased(#PB_Key_Left)
PlayerX - 32
EndIf
If KeyboardReleased(#PB_Key_Right)
PlayerX + 32
EndIf
FlipBuffers()
Until KeyboardReleased(#PB_Key_Escape)
Fluid Byte
Posted: Sat Aug 12, 2006 6:18 pm
by robert1352
Ok evrybody I worked on it till my eyes started burning but I think I finally had some succes. Here is how I altered your suggestions to work with what im doing.
InitSprite() : InitKeyboard()
UsePNGImageDecoder()
OpenScreen(640,480,16,"untitled")
LoadSprite(31,"myPacMan\small_ghost.png")
LoadSprite(32,"myPacMan\small_ghost_red.png")
LoadSprite(33,"myPacMan\small_ghost_yellow.png")
LoadSprite(34,"myPacMan\small_ghost_green.png")
LoadSprite(35,"myPacMan\small_ghost_pink.png")
Structure MYSTRUCT
X.w
Y.w
Type.b
EndStructure
NewList mys.MYSTRUCT()
AddElement(mys())
mys()\X = 200 : mys()\Y = 200 : mys()\Type = 31
AddElement(mys())
mys()\X = 300 : mys()\Y = 200 : mys()\Type = 32
AddElement(mys())
mys()\X = 400 : mys()\Y = 200 : mys()\Type = 33
AddElement(mys())
mys()\X = 40 : mys()\Y = 20 : mys()\Type = 35
Repeat
ClearScreen(0,0,0)
ExamineKeyboard()
ForEach mys()
Select mys()\Type
Case 31 : SprNum = 31
Case 32 : SprNum = 32
Case 33 : SprNum = 33
Case 35 : SprNum = 35
EndSelect
DisplayTransparentSprite(SprNum,mys()\X,mys()\Y)
Next
FlipBuffers()
Until KeyboardPushed(1)
This is how I thought it shoud work but just wasnt able to put my finger on the solution. Thank you all so much !!!!! In case anyone was wondering,the game im working on a pac-man clone. When Im done I will post it here so everyone can learn from it

Thank you all very much
