2D KeyDown PictureBox movement and Grids
2D KeyDown PictureBox movement and Grids
2D KeyDown PictureBox movement and Grids
2D KeyDown PictureBox movement and Grids
2D KeyDown PictureBox movement and Grids
2D KeyDown PictureBox movement and Grids 2D KeyDown PictureBox movement and Grids 2D KeyDown PictureBox movement and Grids 2D KeyDown PictureBox movement and Grids 2D KeyDown PictureBox movement and Grids 2D KeyDown PictureBox movement and Grids 2D KeyDown PictureBox movement and Grids 2D KeyDown PictureBox movement and Grids
2D KeyDown PictureBox movement and Grids 2D KeyDown PictureBox movement and Grids
2D KeyDown PictureBox movement and Grids
Go Back  Xtreme Visual Basic Talk > > > 2D KeyDown PictureBox movement and Grids


Reply
 
Thread Tools Display Modes
  #1  
Old 12-12-2005, 07:21 AM
JackofDiamonds JackofDiamonds is offline
Newcomer
 
Join Date: Dec 2005
Posts: 5
Default 2D KeyDown PictureBox movement and Grids


Well first let me say, I'm completely and totally new to VB.Net, and really VB as a whole, though I messed around with VB some a few years back. I know everyone says that, but I know how to do next to nothing - seriously. I hope to eventually do a 2D game (of course, like everyone haha) but right now I'm just trying to get the code right for moving an image based on KeyDown...
I know how to get KeyDown to work, I can do a ShowMessage based on KeyDowns, but now I need to find out how to make the image move "+1" or whatever to the right, if i press one, or left. I figured it was something along the lines of
PictureBox1.Location() = PictureBox1.Location(+1,+0) or something like that, it seems like I remember VB using something like that (I did this in normal VB once) and then that's not even accounting for collision detection and/or impassable terrain, but I'll cross that bridge when I get to it --- unless it's something I should know about now because it involves the same code or something?
I considered the (theoretical) benefits of creating a "grid" class which goes over the screen and just has integer values for each square of a matrix, determining what type of terrain is there, and a true/false for passable or impassable. I guess at some point I should make the player's sprite a true class and not just a picturebox too, but I have NO clue how to do either of these things, only know that they can (and probably for most efficient and "proper" coding should) be done. I'm not a TOTAL newb to programming itself, haha but the bulk of my experience was with Hypercard and QBasic x.x

If someone could please help me with this project - even though I'm sure it's been asked on here before (I looked before asking but couldn't find it) - I would REALLY appreciate it.
Reply With Quote
  #2  
Old 12-12-2005, 08:35 AM
JackofDiamonds JackofDiamonds is offline
Newcomer
 
Join Date: Dec 2005
Posts: 5
Default

Okay, scratch the first part of that, that was easy to find out I just wasn't listing the changes right.

I still have a question or two though - how do I create an abstract variable with no real "place" in a textbox or anything? I tried using like, for generic RPG's sake, "Dim hp As Integer" but I don't think that was the right thing to do. I just want something that will keep track of the HP itself as an integer and only display it in a textbox, that way it's more "real" in the platonic sense, I guess haha.

The other question I still have is, do you guys think that would be worthwhile, to make a class that is a grid on the screen and basically matrix a bunch of tile numbers and passable/impassable to it? I'm not sure how I'd go about doing it, but I'd like to know if it would be inefficient or a waste of my time to go through the trouble of finding out.

Thanks again, sorry for the changes to the post.
Reply With Quote
  #3  
Old 12-12-2005, 04:13 PM
blastoboy1000's Avatar
blastoboy1000 blastoboy1000 is offline
Junior Contributor
 
Join Date: Sep 2004
Posts: 220
Default

Hi. Welcome to the forum.
To answer your first question, about moving shapes (or any object on a form for that matter), you'll want to modify not the Location property (which as far as I know doesn't exist), but the Top and Left properties. There are also Width and Height, but those won't help with moving an object. An example is:
Code:
Object1.Top += 15

*a side note: The "+=" just replaces having to retype out the statement again. So += is the same as "X = X + 1"


For collision detection, VB.NET (2003, 2005) has greatly improved over Legacy VB (4, 5, 6) in that, in a Legacy VB program, one line of collision detection would involve checking all the sides of an object against the other object. If you want more info on that, do a forum search for IcePlug's Collision Detection Tutorial (it's amazing, seriously).

In VB.NET, collision detection is as simple as this:
Code:
If Object1.Bounds.IntersectsWith(Object2.Bounds) Then MessageBox.Show("Object1 has collided with Object2.") End If



Your next point is a more complicated one, that of making a grid system and assigning variables to each of the grid. Although it's somewhat confusing at first, it's actually quite easy and once you get it the first time, it's much easier.
To create an Array (a.k.a Matrix, except an array can have a huge amount of dimensions) you would create a new variable, but just assign parameters to the end of it.
Code:
Dim Array(1 To 15, 1 To 15)
This statement creates a two dimensional array, 15 squares across and 15 squares high. At this point though, it's all just in the memory, no physical objects have been assigned to the various squares.

*another side note: If you want all the boxes (for lack of a better word) to be Strings or if you want them all to be Integers or whatever, just add "As String", or "As Integer" or whatever to the end of the Dim statement (after the parentheses).


What you are trying to do, create a grid system that the player interacts with, is somewhat easy yet requires more code. To do so, you will need to assign variables to the Player(s) that will directly correspond to the Grid Array. For example, the Player's X and Y coordinates and then in the Grid Array, whether the square is accessible or not.
You will need to check, before the player is moved, whether the square they wish to move to is accessible or not.

*yet another side note: You may wish to store the variables in a User Defined Structure (which I can get into if you wish, or anyone else can for that matter).



Quote:
Originally Posted by JackofDiamonds
I still have a question or two though - how do I create an abstract variable with no real "place" in a textbox or anything? I tried using like, for generic RPG's sake, "Dim hp As Integer" but I don't think that was the right thing to do. I just want something that will keep track of the HP itself as an integer and only display it in a textbox, that way it's more "real" in the platonic sense, I guess haha.

The other question I still have is, do you guys think that would be worthwhile, to make a class that is a grid on the screen and basically matrix a bunch of tile numbers and passable/impassable to it? I'm not sure how I'd go about doing it, but I'd like to know if it would be inefficient or a waste of my time to go through the trouble of finding out.
...I'm not quite sure what you mean by an abstract variable, but if I'm correctly understanding your question, then the answer is that all variables are "abstract" in the sense that they don't directly correspond to anything at the time of creation. They need to be assigned to follow another value.

For the matrix question: sure, I think it would be worthwhile to make an array (as described above) and assign an "Is this space accessible?" Boolean variable to each square for use with controlling the movement of the Player. To create a visual aspect of an array, you would probably want to have a set of pictures (either in the form of a sprite sheet or as separate pictures) that can be called and replicated on the form under the direction of the array, much like DNA and cell replication.


I hope this was all coherent and understandable, but if it wasn't, feel free to ask for clarification.
Reply With Quote
  #4  
Old 12-12-2005, 06:49 PM
JackofDiamonds JackofDiamonds is offline
Newcomer
 
Join Date: Dec 2005
Posts: 5
Default

Wow! Thanks for the extensive reply!
I actually found out how to do the keydown but it wasn't quite what you said. i think that self = self (change property) is more the old VB, I didn't know you could just do += 45 or whatever number.

Code:
if e.keycode = keys.numpad4 then
PictureBox1.Left = PictureBox1.Left - 45
End If
I even got the collision detection down, at least to the intersecting squares part, and got it to show a messagebox. my problem now is actually doing something about, or somehow making a conditional where if something is "solid" you can't move, rather than alerting you once you've collided (and therefore already slipped x pixels under or over or into the image itself). I tried first just creating a function which did collision detection as a boolean, but because i haven't used functions yet it was a bit difficult for me to sort through which ByVals I needed or whatever (and I'm still not ENTIRELY certain what those are). I'm wondering mostly how to construct a universally useful collision detection function (in this game i mean) - anytime any two things intersect "plugging in" the picture boxes (whatever they are) into the operaters of the function and figuring it out that way. maybe that's excessive, or just stupid, i'm unsure haha.

And about that matrix/array, well, would it be better to make it its own class, like a "map" class or "grid" class, because it's the kind of thing that would be applied to every non-combat "screen" in the game, just like old school Final Fantasies, I guess, only it'll change when it's told to go to a new screen [i'm not even going to bother with scrolling just yet lol]. This is only my second day actually doing anything with VB.Net and I have to say I'm impressed that I got this far, I usually learn new languages pretty slowly, but it helps that this one is made to be so convenient and this forum has helped immensely. I also got the abstract variables thing, it was just what i thought it was, dim x as integer or what have you, I just didn't realize it was correct at the time. I probably should've browsed more extensively for this kind of stuff, but I ended up finding alot of what I needed under headings that weren't initially about those things *shrug*. At any rate, my main problem now isn't collision detection, but knowing what to DO about it, or more specifically how to "refute" a moving character let's say from going onto something solid or impassable, which probably goes hand in hand with the grid thing, so i'm not sure. Any ideas how I should go about doing this? Actually just knowing a bit of how Functions work would help alot, I've looked for sites but had a hard time coming up with any solid examples, which unfortunately is what i learn best by, so :-/. Anyhoo, let me know what you think, haha I don't want to waste alot of your time asking for loads of code or anything - plus that would take some of the fun out of it, I'm just a bit bewildered as to all the different commands i can use in functions and how they work and also using parameters for new classes and functions, so any help with general stuff on that would, well, help haha. Thanks again.
Reply With Quote
  #5  
Old 12-12-2005, 07:27 PM
blastoboy1000's Avatar
blastoboy1000 blastoboy1000 is offline
Junior Contributor
 
Join Date: Sep 2004
Posts: 220
Default

I'm glad you're getting the hang of it, VB.NET is actually a lot more coherent than Legacy (having started in VB 6).

For the preventing the player from moving into a square, I won't give you actual code (at your request) but will give you a general idea as to what you'd want to do:

Not using the grid system:
Code:
If the key that is pressed is the Up key Then
    If the Player is not touching / intersecting with another object Then
        Allow the Player to move up X amount
    Else
        Do not allow the Player to move up X amount
    End If
End If
^^ the above code is unoptimized (meaning that it can be boiled down more)
You'll have to do it for each direction, and for all the objects that you have onscreen at any given time. Another possibility is to make a For....Next Loop (which I can give you the basis of, but I don't have very good luck with them myself) and just loop through all the objects and check for all of them at once.


Using a Grid system:
Code:
If the key that is pressed is the Up key Then
    If there is not an object occupying the space directly above the Player Then
        Move the Player up one square
    Else
        Do not move the Player up one square
    End If
End If
Using the grid system will require more setting up but will be more efficient and easier to work with than the non-grid system.

Functions:

Functions are basically the same as Subs (programs) except they return a value and are specialized.

Again, you can think of a cell (either plant or animal). The Functions are like the Mitochondria, Endoplasmic Reticulum, and other organelles. The Subs are like the Cell Wall / Membrane, or the Nucleus or other more general areas of the Cell.

Hope this helps, feel free to ask other questions.
Reply With Quote
  #6  
Old 12-13-2005, 05:50 AM
Iceplug's Avatar
Iceplug2D KeyDown PictureBox movement and Grids Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

I don't know if you've seen my thread on this yet, but:
Quick example of a Tile Map
Might help you out logic-wise perhaps... for instructional purposes only.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
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
2D KeyDown PictureBox movement and Grids
2D KeyDown PictureBox movement and Grids
2D KeyDown PictureBox movement and Grids 2D KeyDown PictureBox movement and Grids
2D KeyDown PictureBox movement and Grids
2D KeyDown PictureBox movement and Grids
2D KeyDown PictureBox movement and Grids 2D KeyDown PictureBox movement and Grids 2D KeyDown PictureBox movement and Grids 2D KeyDown PictureBox movement and Grids 2D KeyDown PictureBox movement and Grids 2D KeyDown PictureBox movement and Grids 2D KeyDown PictureBox movement and Grids
2D KeyDown PictureBox movement and Grids
2D KeyDown PictureBox movement and Grids
 
2D KeyDown PictureBox movement and Grids
2D KeyDown PictureBox movement and Grids
 
-->