Need help with my basic breakout clone please(XNA)

Started by
3 comments, last by acddklr07 16 years, 5 months ago
Here's the code: http://rafb.net/p/oQsz9d62.html http://rafb.net/p/ec0PXO17.html I use that partial class just to make things a little cleaner and not have everything in the main file. It's probably a bad way to go about it, but I am a beginner! lol. Anyways, I'm trying to make it to where each block is set to a random color, but I can't figure out how to do it. The way I have it now makes it to where the colors change every frame, and I know it's doing that because the random is set every time draw is called, but I can't figure out another way to do it :( Any help would be really appreciated...it's probably really simple and I'm gonna kick myself for not being able to figure it out...
Advertisement
Can anyone help? I've tried everything and I can't get it to work :(
You need to set the colors out side the draw function and then have them passed to the function. Setup or use an existing class to store the colors in.

Now if C# allows much the same practice as C++ this may be of some help.
1. create a class that will hold what color has been assigned to which block you may want to add this to the class that holds the information about what blocks are left and where they are positioned.
2. make sure you create the class in main so it doesn't go out of scope.
3. add a set color function to randomly assign the colors. you'll need to pass the class that will hold these by reference.
4. now you should have a variable holding all the colors of each block now all you need to do is access this class when in the draw function all the colors should remain constant until you call set color function again.
wherever you are storing your block locations (i assume it's your block class after looking at your code for only about 2 seconds) when you initialize all your data set a colour value for each block then. each time you go to draw the array of blocks you just set the colour to whatever the block has stored for it's particular colour.

does that help?
Hello,

I would actually try to design it in a different way. I would actually make a class called blocks that would contain all the update logic and stuff in there. Then I would actually make a enum of colors, and then in the constructor of the class blocks I would make it seed a random number that will fit the length of the enum of colors. For example, if there was 7 colors in the enum I would seed a random number between the choices of 1-7. Then in the constructor it will set the color based on the random number choice. Then in your main part you could just create an array of the blocks object. I hope this helps. If you need me to go more into detail about this then just pm me and I will write up a more detailed description.

~Carl J. Loucius

P.S. Object Orientated Programming can be your best friend if you start thinking of things as objects.

This topic is closed to new replies.

Advertisement