|
-
Sep 4th, 2008, 06:45 AM
#1
Thread Starter
New Member
[RESOLVED] Help with Images please
Hi, i've only started using vb so bear with me. I have to make a snakes and ladders type game for one of my classess. The best way I saw fit to do this was use around 100 different image boxs and use a counter. A dice would generate a random number and I wanted this to corollate with the number of squares an image moved. However, i'm having trouble with this bit. Is it possible to get something like this?
Code:
Square("Board").Picture = LoadPicture(...)
Where the board variable is tells the program which square to colour in? I know the above code is wrong, but is there anything like it that would work?
If you have an alternate methods, or idea's they'd be welcome
Cheers, James
-
Sep 4th, 2008, 07:24 AM
#2
Re: Help with Images please
Moved to Games programming (VB6)
-
Sep 4th, 2008, 07:44 AM
#3
Re: Help with Images please
Welcome to VBForums 
What you are looking for is a Control Array... for info on how to set it up, see this tutorial (the first paragraph, and post #3).
Once it has been set up, you can use a variable for it, eg:
Code:
Board = 7
Square(Board).Picture = LoadPicture(...)
(notice the quotes have been removed from around Board - they made it the text Board rather than the contents of the variable called Board)
-
Sep 8th, 2008, 08:11 PM
#4
Thread Starter
New Member
Re: Help with Images please
Thanks for your reply, sorry mine is a bit late; Our internet died...
Anyway, I took your advice but i can't seem to get it working.. Could you perhaps point out any error's? I just need to get the basic outline of the snakes and ladders working, i've got all the other forms for the project, just having some issues with this.
Code:
Private Sub Form_Load()
Dim i As Integer, Board as Integer
Board = 0
For i = 1 To 4
Load imgSquare(i)
With imgSquare(i)
.Left = 6600 + (615 * i) ' This only works from 1 to 4 (help?)
.Top = 6600
.Width = 615
.Height = 615
End With
Next
For i = 5 To 9
Load imgSquare(i)
With imgSquare(i)
.Left = 6600
.Top = 6000
.Width = 615
.Height = 615
End With
Next
For i = 10 To 14
Load imgSquare(i)
With imgSquare(i)
.Left = 6600 + i
.Top = 5400
.Width = 615
.Height = 615
End With
Next
For i = 15 To 19
Load imgSquare(i)
With imgSquare(i)
.Left = 6600 + i
.Top = 4800
.Width = 615
.Height = 615
End With
Next
End Sub
Private Sub rolldice_Click()
Dim Dice As Integer, Board As Integer
Randomize
Dice = Int(Rnd * 6)
'Roll Dice
Select Case Dice
'selelcts the right die image
Case 1
imgDice.Picture = LoadPicture("C:\Users\James\Documents\IPT Assignment\D1.jpg")
Case 2
imgDice.Picture = LoadPicture("C:\Users\James\Documents\IPT Assignment\D2.jpg")
Case 3
imgDice.Picture = LoadPicture("C:\Users\James\Documents\IPT Assignment\D3.jpg")
Case 4
imgDice.Picture = LoadPicture("C:\Users\James\Documents\IPT Assignment\D4.jpg")
Case 5
imgDice.Picture = LoadPicture("C:\Users\James\Documents\IPT Assignment\D5.jpg")
Case 6
imgDice.Picture = LoadPicture("C:\Users\James\Documents\IPT Assignment\D.jpg")
End Select
Board = Board + Dice
imgSquare(Board).Picture = LoadPicture("C:\Users\James\Documents\IPT Assignment\Character Select\Mario.jpg")
'I couldn't find an unload command for the picture.
End Sub
I know the code is incomplete, but all help is appreciated
Last edited by Jimmeh; Sep 8th, 2008 at 09:57 PM.
-
Sep 9th, 2008, 03:50 AM
#5
Re: Help with Images please
Based on your loop counters you appear to be using imgSquare(0) in the same way as the others, which I would not recommend - it makes things easier if you just keep it as a template. Set the .Width and .Height as you want (the others you create will copy them), and set .Visible to False so it can't be seen.
An obvious issue is that you have only got + (615 * i) in the first loop, and presumably you want it in all of them... however, with a couple of minor changes (to calculate .Top, and an alteration for .Left) you only need to use one loop.
For .Top you can simply use this:
Code:
.Top = 6600 - 600 * ((i-1) \ 5)
\ is for integer division, so the result will have no decimal places - when i is between 1 and 5 it will return 0 (so 600*0), from 6 to 10 it will return 1 (so 600*1), etc.
For .Left you can use a similar operator, called Mod, which returns just the remainder of the division, eg:
Code:
.Left = 6600 + 615 * ((i-1) mod 5)
When i is 1/6/11/... this will return 0, and for 2/7/12/.. it will return 1, etc.
So, all of your loops could be replaced by this one:
Code:
For i = 1 To 20
Load imgSquare(i)
With imgSquare(i)
.Left = 6600 + (615 * (((i-1) mod 5) + 1))
.Top = 6600 - 600 * ((i-1) \ 5)
.Visible = True
End With
Next i
Now to your random numbers... first of all, you should only call Randomize once in your program (any more actually makes the numbers less random), so move it to Form_Load.
Next up, as the Rnd function returns a number from 0 to under 1, the way to get a number from 1 to 6 is to use this:
Code:
Dice = Int(Rnd * 6) + 1
'I couldn't find an unload command for the picture.
It's an odd one - you actually load no picture, ie:
Code:
Set imgSquare(Board).Picture = LoadPicture("")
-
Sep 9th, 2008, 04:32 AM
#6
Thread Starter
New Member
Re: Help with Images please
Thank you very much si, this is greatly appreciated. Just two more things and hopefully it will all work 
Logically this doesn't look right, I want the dice to keep adding to the board count, but in the program resets itself to 0 every time. So only squares from 1 to 6 seem to get a picture
Code:
board = board + dice
Code:
Board = 0
imgSquare(Board).Picture = LoadPicture("C:\Users\James\Documents\IPT Assignment\Character Select\Mario.jpg")
With this bit here, When it's before the array, all the images have this picture (get that), but this allows the piece to move around. However, when after the array, nothing works. Could you explain?
-
Sep 9th, 2008, 04:46 AM
#7
Re: Help with Images please
board is getting reset because it doesn't exist where you are trying to use it - and VB is being "nice" to you by creating another variable.
I think it would help you to read a couple of articles from our Classic VB FAQs (in the FAQ forum, which is shown near the top of our home page):
With this bit here, When it's before the array, all the images have this picture (get that), but this allows the piece to move around. However, when after the array, nothing works. Could you explain?
"nothing works" is a very misleading term - it is best to explain to us in detail what is/isn't happening, as otherwise we can only make guesses (admittedly often educated ones, but it does make our role harder).
In this case I would actually recommend changing that part of your program anyway - it would be better to use a completely separate picturebox for the player(s), as that way you don't need to worry about re-drawing the squares that it was previously on, and if you want you can move it smoothly, etc.
-
Sep 9th, 2008, 04:59 AM
#8
Thread Starter
New Member
Re: Help with Images please
Ok, thank you si once again. I think the FAQ's are a better resource than my big book. I'll try explain a little more detail then.
I would like the picture to start in the 1st imagebox (works well), then each time the die is cast, it moves to the corrosponding square (As it moves the previous square loses its picture.
You mentioned giving the player a picturebox that would move around 'smoother'. Could you give me a hint on how this would work or a link to something like it? Sorry for being so needy, but right now this forum is my best resource
-
Sep 9th, 2008, 05:22 AM
#9
Re: Help with Images please
Add a new picturebox to your form, and set the picture using the properties window.
To show it "on" the correct square, set the .Top and .Left properties as apt (hint: think about how the squares were placed).
When it needs to go to a new square, change those properties... and to move it smoothly, don't set it to the new value immediately (hint: a loop could be useful).
-
Sep 9th, 2008, 06:39 AM
#10
Thread Starter
New Member
Re: Help with Images please
Ok then. So using the code that loaded the images, I just splice it in with the picbox?
Instead of using 'i', I use 'board'. So The picbox knows which square to move to, right.
Ok I get that big, but how would encorperating a loop make it seem 'smoother'. One other thing before i go to bed, the picbox method, it eliminates the use of the image array doesn't it. That makes things better, thank you for that
Cheers.
-
Sep 9th, 2008, 06:56 AM
#11
Re: Help with Images please
 Originally Posted by Jimmeh
Ok then. So using the code that loaded the images, I just splice it in with the picbox?
Instead of using 'i', I use 'board'. So The picbox knows which square to move to, right.
That's right. 
Ok I get that big, but how would encorperating a loop make it seem 'smoother'.
Rather than having just one big move from A to B, it allows you break that down into several smaller moves, so that you don't arrive at B instantly.
One other thing before i go to bed, the picbox method, it eliminates the use of the image array doesn't it.
It depends.. do you want the squares to be shown, and possibly have pictures in them too?
-
Sep 9th, 2008, 04:09 PM
#12
Thread Starter
New Member
Re: Help with Images please
Ok thanks for all your help si. You've helped me heaps - I have a set gameboard that pieces move across so I don't need the Image Array. The picbox method stops the need for the images, Thanks anyway 
Have a good one.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|