Array of rectangles

elnerdo
11-20-2004, 08:17 AM
I'm making a simple dodge the other things game, I want 10 rectangles for you to dodge. I have no experience with using arrays at all. How could I make an array of 10 rectangles?

Iceplug
11-20-2004, 08:31 AM
Dim Rects(9) As Rectangle
'Ten rectangles - 0 To 9. All are empty.

Dim Rects() As Rectangle = New Rectangle(9) {}
'Size declaration... the .NET way.

Dim Rects() As Rectangle = New Rectangle() {New Rectangle(...), New Rectangle(...),
New Rectangle(...), ... }
'Content declaration... the .NET way.

To set an element in the array:
Rects(0) = New Rectangle(10, 10, 100, 10)
'Set the 0th element.
Rects(9) = New Rectangle(44, 44, 444, 44)
'Set the last element.
Dim X As Integer = 6
Rects(X) = New Rectangle(33, 33, 333, 33)
'Set element 6.

For Y As Integer = 0 To 9
Rects(Y) = New Rectangle(100, 100, 100, 100)
Next
'Sets all 10 rectangles.

For Z As Integer= 0 To 9
Rects(Z) = New Rectangle(64 * Z, 0, 64, 64)
Next
'Ten rectangles standing all in a row.
:)

elnerdo
11-20-2004, 08:34 AM
Thanks, and a special thanks for your 5 minute response time :)

It was the dim I was having trouble with, I was trying to do things like

Dim them(9) as array

which didn't work.

Thanks again.

Ps. Oh, I didn't know you could do for nexts like that, without dimming first, that helps a lot!

Iceplug
11-20-2004, 08:40 AM
Oh, I didn't know you could do for nexts like that, without dimming first, that helps a lot!
You can only do this in .NET 2003 or 2005, however (which I see in your profile you can do this)

elnerdo
11-20-2004, 09:11 AM
For t As Integer = 0 To 8
TheyMoveHori = Int(Rnd() * 2) - 1
TheyMoveVert = Int(Rnd() * 2) - 1
them(t).X = them(t).X + TheyMoveHori
them(t).Y = them(t).Y + TheyMoveVert
Next
If them(9).X > you.X Then HeMoveHori = HeMoveHori - 1
If them(9).X < you.X Then HeMoveHori = HeMoveHori + 1
If them(9).Y > you.Y Then HeMoveVert = HeMoveVert - 1
If them(9).Y < you.Y Then HeMoveVert = HeMoveVert + 1
them(9).X = them(9).X + HeMoveHori
them(9).Y = them(9).Y + HeMoveVert


This is the code to move the enemies in my game ( Yes I have a me.invalidate() later ) The enemies don't move. I have no idea why.

Also,

For y As Integer = 0 To 9
If you.IntersectsWith(them(y)) Then restart()
Next y


only works for the first 8 of my enemies. What am I doing wrong?

Edit: Restart is the name of a sub, it ends the game

elnerdo
11-20-2004, 09:21 AM
Nevermind, I got it

Iceplug
11-20-2004, 06:44 PM
You should be using the Random class instead of Int() and Rnd()
Dim Ri As Random
Ri = New Random
Ri.Next(2) 'Generates 0 or 1.
Ri.Next(-1, 0) 'I think this generates -1 or 0.

elnerdo
11-20-2004, 06:54 PM
Thanks, I'll try that. I learned Qbasic before I started Vb, so a lot of the code I know comes directly from Qbasic and int(rnd * maxnumber)+ minnumber is how you did it in qbasic. Thanks though.

Though, I completely changed this game, the random numbers aren't needed.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum