Arrays ?
Arrays ?
Arrays ?
Arrays ?
Arrays ?
Arrays ? Arrays ? Arrays ? Arrays ? Arrays ? Arrays ? Arrays ? Arrays ?
Arrays ? Arrays ?
Arrays ?
Go Back  Xtreme Visual Basic Talk > > > Arrays ?


Reply
 
Thread Tools Display Modes
  #1  
Old 01-15-2011, 06:57 PM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default Arrays ?


Hi, i'm thinking of making a 'tron' based game. Recently I made a snake game, however I didn't use arrays, As I have no idea how to use them. So I did individual picture boxes and just made them hide and show when needed.

now a tron game, following this method would take a lifetime, so I'm asking where would I start to start an array? I have some idea how they work. You have different objects and just assign the to Array(1) or Array(2) or Array(3) etc, how do you implement this. Also with arrays how would I do collision detection for the trails.

also how do to add sounds to a project? Do I have to make a sub for it. I only want the sound to activate upon an event.

thanks.

Last edited by 45minutes; 01-16-2011 at 01:23 AM.
Reply With Quote
  #2  
Old 01-16-2011, 09:30 PM
HellfireXP's Avatar
HellfireXP HellfireXP is offline
Freshman
 
Join Date: May 2010
Location: South Carolina
Posts: 41
Default

I wrote a game called Pat Man (basically Pac Man) awhile back. Check out the code, it may help you....

http://www.mediafire.com/file/tyyokd3zydw/Pat-Man.zip
Reply With Quote
  #3  
Old 01-16-2011, 11:33 PM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Yes, I know how to add sounds now. Just the arrays I have to learn. (thats the best way ay for a tron game?).
Reply With Quote
  #4  
Old 01-17-2011, 10:49 AM
AtmaWeapon's Avatar
AtmaWeaponArrays ? AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Sometimes the answer to this kind of question is "you don't". This is sort of one of those cases. Arrays are the answer, but not in the way you expect.

I assume by a "Tron" game you mean the light cycle game; it's similar enough to your snake example I think it's OK to make this assumption. You could certainly implement this by making an array of picturebox controls, but it would be wasteful at best and likely very awkward. It'd be slightly better to keep an array of Integer values, where some number represents "blue" and another number represents "gold" and some number represents "blank". Then you could manually draw the form and put the right colored square in the location.

If you're not too familiar with Windows Forms, it can be kind of daunting, but it's not very complicated. First, you need to understand how to create arrays. You declare an array with n elements by putting parenthesis and the desired size next to the variable name:
Code:
Dim farms(n-1) As Integer
Why n-1? Arrays are numbered starting at 0. If there are 10 elements, that means the indexes will be 0 through 9. The number you pass to the array represents not the size but the desired upper bound. So farms(10) is an 11-element array with indices 0 through 10. People make lots of off-by-one errors here; so be careful.

You can also create arrays without a size, but we won't worry about that because it's not relevant to the example. There's also some concerns with arrays of reference types (like controls) but again I'll ignore them.

The next level is creating a 2D array. If a 1D array is like many variables lined up in a row, a 2D array is more like a grid of variables in rows and columns. To create a 2D array, you provide two sizes separated by a comma. remember the "size" is actually the desired upper bound. So if you want a 10x10 array:
Code:
Dim gameGrid(9, 9) As Integer
Now for a crash course in drawing. Windows Forms is an immediate mode graphics system, which means it draws to the screen immediately when you ask it too *and* it stores no information about what is drawn. Sometimes an event happens that causes part of your drawing to be lost; when this happens your form is invalidated to give you a chance to redraw what was lost. In Windows Forms, the Paint event is raised when a control is invalidated, and this is your chance to redraw the information. If you would like to force a redraw, you can call the Invalidate() method to invalidate your form manually.

From all of this information, you can deduce the skeleton of the form should look like this:
Code:
Public Class LightCycles

    Private _gameGrid(??, ??)

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        ' Draw the grid
    End Sub

End Class
The specifics of drawing the grid are fairly trivial; I'll skip the explanation.

I've attached a project that demonstrates the concept. It displays a 20x20 game grid with 10 pixel squares. When you click the button, a game is simulated and the results are displayed. The complexities of simulating the game are hidden away in other classes, but well-commented if you're curious. The simulation's a decent start towards implementing the full game, but that's not really the demo. You didn't indicate a VS version, so you get VS 2010.
Attached Files
File Type: zip LightCycles.zip (13.8 KB, 7 views)
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #5  
Old 01-18-2011, 03:47 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

am I meant to be able to run ur app? I extracted it, opened it, and a bunch of errors arise. :/ I can't run the program

I will give arrays a go, see what happens.



thanks

Last edited by 45minutes; 01-18-2011 at 05:57 AM.
Reply With Quote
  #6  
Old 01-18-2011, 08:53 AM
AtmaWeapon's Avatar
AtmaWeaponArrays ? AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Yes, you should be able to run the application. I just downloaded, built, and executed the application with no troubles. What were the errors?
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #7  
Old 01-18-2011, 02:24 PM
Cerian Knight's Avatar
Cerian KnightArrays ? Cerian Knight is offline
Polymath (in disciplina)

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,199
Default

Only works in 2010 (as stated above... I used Express). SLN extension will not open in old Version Selector after upgrade (must manually associate SLNs with 2010).
__________________
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').
Reply With Quote
  #8  
Old 01-18-2011, 06:29 PM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

oh, yeah I'm using VB 2008 express edition. dam.

maybe make an .exe so I can just see what it does, without having to run it through vb.

there's errors that relate to the designer coding (which is not done by you, but the program itself)
Reply With Quote
  #9  
Old 01-18-2011, 08:37 PM
passel's Avatar
passelArrays ? passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,026
Default

You're not allowed to post .exes on the forum.
But I just copied the code from the 2010 vb files into a new 2008 project, and it worked without a problem. Attached is the 2008 version.
Attached Files
File Type: zip LightCycles.zip (16.3 KB, 8 views)
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #10  
Old 01-18-2011, 08:50 PM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

ah yes I see it. You have to keep clicking button though and happens so fast. I would assume there is a way to make it playable.
Reply With Quote
  #11  
Old 01-19-2011, 08:35 AM
AtmaWeapon's Avatar
AtmaWeaponArrays ? AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Quote:
Originally Posted by 45minutes View Post
ah yes I see it. You have to keep clicking button though and happens so fast. I would assume there is a way to make it playable.
Yes, as evidenced here:
Quote:
The simulation's a decent start towards implementing the full game, but that's not really the demo.
I wasn't writing the game for you, I was writing a demonstration of using a 2D array to represent the grid that *happened* to be similar to the game. The best way to make a game out of it would be to start over using my code as a guide; it is almost always more difficult to hack features into code than to write them in from the start. There's quite a few bugs in the demo that I didn't waste time fixing because the demo worked well enough with them.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #12  
Old 01-19-2011, 05:40 PM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Quote:
Originally Posted by AtmaWeapon View Post
Yes, as evidenced here:

I wasn't writing the game for you, I was writing a demonstration of using a 2D array to represent the grid that *happened* to be similar to the game. The best way to make a game out of it would be to start over using my code as a guide; it is almost always more difficult to hack features into code than to write them in from the start. There's quite a few bugs in the demo that I didn't waste time fixing because the demo worked well enough with them.
Yeah, I didn't expect anything like this anyways, so thanks so much. I'll try and make my own now.

Can timers be applied to the array?

Last edited by 45minutes; 01-19-2011 at 06:11 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Arrays ?
Arrays ?
Arrays ? Arrays ?
Arrays ?
Arrays ?
Arrays ? Arrays ? Arrays ? Arrays ? Arrays ? Arrays ? Arrays ?
Arrays ?
Arrays ?
 
Arrays ?
Arrays ?
 
-->