Hey, I'm making a game in which you control a character with the right and left arrow keys, you reach the location of a door, click it and proceed to the next level.
The way I'm doing it is: the character is a button with an image, that moves when the right and left arrow keys are pressed. when the character reaches the location of a door(also a button) and clicks it, he proceeds to the next level. The character's location is reset to the initial, the image of the picturebox that is the background switches, and some buttons with images appear, and others disappear.
I know I'm "cheating" in every possible way, but I just wanna make somthing simple for a school project.
The problem is, since I'm doing everything in the same form, it's getting completely clogged up with buttons and I've only made 4 levels so far, and I can only see the initial image, so I need to predict the location of some items so that they will fit in the background of a certain level when I tell them to become visible.
So, I need you to tell me if there is a better approach for this. I've tried new forms for every level, but I think it's worse because I have to transport variables, and the timers that make the character move don't work in other forms.
You might want to consider using the buttons for more than one thing, or in other words reusing the same buttons from 1 level to the next that alone could help a lot.
Anothe roption would be to use more picture boxes, perhaps one per level and have only one active at a time. You can spread them out on the form at design time and set the visible property to false then add code to the form load to position them all to 0,0 or whatever then make them visible as needed.
A way of thinking at this could be:
you sould have a class to hold character and a class to hold door that inherit from Button. Then you could have a List (of door), but you are not obliged to add all to the forms.controls: you could add only the visible ones.
Each object should have a property to know its position on a form,
and method to move (up, down, left, right) that simply change the value above property,plus method to perform actions required (opening the door...what do you said it does? Or start a dialogue with another character -if applicable) and a Visible property to know if it should be drawed (but you get both kind of property free as you inherit from Button and a button has Left and Top properties and a visible property -but - even if you can think you're really lucky as changing visible property will really make the door or character appear or disappear - you should add or remove those class from Form.controls when visible property changes)
Each object should rise events like "PositionPorpertyChanged" and "VisiblePropertyChanged" or "SomethingChanged", so that the consumer (your form) can know when a change happens and take appropriate actions (like enabling character to "click" a door). But this is a plus: you can always go on coding in keydown or keyup or keypress event of your form to decide what to show.
Levels should be the ground through which your character move and on where your doors are. They should be classes (probably inherited from PictureBox?) which should be able to tell in which coordinates doors might appear and where a character cannot go.
You could have a List (of level) - or you could create one on the fly just before use it - and use one as background of your form.
Not only to avoid to get a too cluttered keypress event routine, you should delegate operations there to an "underlying engine", that is a class (we usually call it "business object") that will receive the key pressed, the character, the list of doors and the actual level, and will perform actions on character (eg: changing its position) on doors (eg.displaying one, hiding another, opening one) and even on level (changing it with the previous or the next).
Does this make sense for you?
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.