G+_Dean T Posted March 14, 2014 Share Posted March 14, 2014 Hey my Coding 101 compadres! I have some more code for you and fitting in with this weeks lesson I have included some classes! This is a simple game (you can barely call it that to be honest) that lets you chose a character class and adopt its attributes / stats as the player and lets you match up against 2 enemy classes. It doesnt really play as a game but works very much like real game mechanics and I have used very simple classes and heavily commented this to try to help you grasp classes. Due to people having issues running graphical apps because of the extra files needed I have kept this one purely console and nothing else needs included so its ready to paste and play. http://pastebin.com/kHBLF5CU http://pastebin.com/kHBLF5CU Link to comment Share on other sites More sharing options...
G+_Darryl Medley Posted March 15, 2014 Share Posted March 15, 2014 Looks like a cool little demo program. Since all your classes are the same except for the field values, you really only need 1 class and then just create an object for each game character. You can assign each character's Points when you create the object using a Constructor method. (Just a method with the same name as the class.) This would be the class: public class GameChar { public int healthPoints = 0; public int powerPoints = 0; // Constructor method public GameChar(int hp, int pp) { healthPoints = hp; powerPopints = pp; } } Then to create your different characters just call the constructor with the appropriate values: // create a Human player: GameChar player = new GameChar(1, 1); // create an EvilNinja: GameChar evilninja = new GameChar(900, 250); Link to comment Share on other sites More sharing options...
G+_Dean T Posted March 15, 2014 Author Share Posted March 15, 2014 That would save so much coding in a large game! Thank you. The reason I did it this way was because this was the very first time I have ever used a class and it helped me understand their use. I will keep this one as it in hopes it also helps others but in the future I now know about constructors. Link to comment Share on other sites More sharing options...
G+_Dean T Posted March 15, 2014 Author Share Posted March 15, 2014 Just tried this now. This is a much easier way of doing things. I am currently making a shopping cart type example program (will post when complete) and since all the stock has the same attributes its easier setting them all up this way! So thank you!! Im also then adding them to an object array to help sort them for catagories. Link to comment Share on other sites More sharing options...
G+_Dean T Posted March 15, 2014 Author Share Posted March 15, 2014 I used your way in my new code and it made so much more sense. http://pastebin.com/JAUbAM5k Link to comment Share on other sites More sharing options...
G+_Darryl Medley Posted March 17, 2014 Share Posted March 17, 2014 Glad I could help. I was going to post my own little program to show how to create multiple objects for a class but then I saw your program and it saved me the trouble. It looks like you took it to the next level with CODEMART101. Another tip: for your "if choice" section of code you can use switch..case statements instead. See http://msdn.microsoft.com/en-us/library/06tc147t.aspx Link to comment Share on other sites More sharing options...
G+_Dean T Posted March 17, 2014 Author Share Posted March 17, 2014 Brilliant thank you ill read up on this also. Yeah I learn so much more by actually programming and if i get stuck trying to find the solution. That way I seem to remember it easier. So because of that i keep thinking of different things I can make to test out what i have learnt such as codemart to test objects. Link to comment Share on other sites More sharing options...
Recommended Posts