Help with array within structures

Advanced game related topics
robert1352
User
User
Posts: 34
Joined: Tue Mar 14, 2006 8:17 pm

Help with array within structures

Post 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 :lol:
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post 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
robert1352
User
User
Posts: 34
Joined: Tue Mar 14, 2006 8:17 pm

Reply

Post 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
dontmailme
Enthusiast
Enthusiast
Posts: 537
Joined: Wed Oct 29, 2003 10:35 am

Post 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

:)
Paid up PB User !
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post 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
--Kale

Image
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post 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.
dontmailme
Enthusiast
Enthusiast
Posts: 537
Joined: Wed Oct 29, 2003 10:35 am

Post 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....

:D
Paid up PB User !
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Priceless! :roll:
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post 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 :twisted:

robert1352, don't feel bad about this... it takes time to learn how to speak purebasic :P
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post 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! :D
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. :wink:
dontmailme
Enthusiast
Enthusiast
Posts: 537
Joined: Wed Oct 29, 2003 10:35 am

Post 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 :twisted:

robert1352, don't feel bad about this... it takes time to learn how to speak purebasic :P
Nobody's going hard on the guy ;)

3 helpful answers is not being hard...

:D
Paid up PB User !
robert1352
User
User
Posts: 34
Joined: Tue Mar 14, 2006 8:17 pm

Thank You all!!!!!!!

Post 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. :lol:
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Re: Thank You all!!!!!!!

Post 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! :D

Is your problem solved or not? If not please gimme a shout!
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post 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)
robert1352
User
User
Posts: 34
Joined: Tue Mar 14, 2006 8:17 pm

Fluid Byte

Post 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 :lol: Thank you all very much :D
Post Reply