Advertisement Jump to content
  • Advertisement

C# Players Guide : Classes (Balls Exercise)

A4L

1071 views

Hello... This is my version of the Chapter 18 CLASS exercise about throwing and popping a bunch of balls.

: NOTES :  Random()

For the most part as I worked through the exercise things became clear but I am still confused about "scope" or w/e it is called that determines when a variable is available to be read by other sections of the code. I wanted to use Random(); to build random numbers on the fly, but it seemed that I was either calling the random very quickly in succession, so all the results were identical, or I wasn't able to access the random object I created when I wanted to.

I got around this problem by creating a custom random class called RAN. This initiated Random() during the creation of the class in the main program.. so that only happened once. Then it had functions to produce the random numbers I wanted, in the type I wanted.

Also I found the code to be really ugly and hard to read if I just used Random(). I was using Bytes for my 0-255 colour codes.. and as far as I know Random() is returning only floats.. so every time I had to use Random() object I had to use Convert()... this lead to it being a mess. So I build into the RAN class in a way to choose the types for me to clean this up a little.

This is only using Random() to for loop 5-50 times and make a list of balls with a size of 1-25

            Random rand = new Random();
            List<Ball> balls = new List<Ball>();
            for (int i = 0; i < Convert.ToInt32(rand.Next(5, 50)); i++)
            {
                balls.Add(new Ball(Convert.ToInt32(rand.Next(1, 25))));
            }

The Colour class that was using bytes was even worse.


            balls[1].colour.red = Convert.ToByte(rand.Next(1, 255));
            balls[1].colour.green = Convert.ToByte(rand.Next(1, 255));
            balls[1].colour.blue = Convert.ToByte(rand.Next(1, 255));

And the BALL class was every worse worse!!

Ball ball = new Ball(Convert.ToByte(rand.Next(1, 255)), Convert.ToByte(rand.Next(1, 255)), Convert.ToByte(rand.Next(1, 255)), Convert.ToInt32(rand.Next(1, 25));

I found it really strange that using Random() to get specific types would cause such ugly and bloated looking code. So I ended up making my own Random Class called Ran(). This would change the above code to....

            Ran Ran = new Ran();
            List<Ball> balls = new List<Ball>();
            for (int i = 0; i < Ran.Int(5, 50); i++)
            {
                balls.Add(new Ball(Ran.Int(1, 25)));
            }

            balls[1].colour.red = Ran.Byte();
            balls[1].colour.green = Ran.Byte();
            balls[1].colour.blue = Ran.Byte();

            Ball ball = new Ball(Ran.Byte(), Ran.Byte(), Ran.Byte(), Ran.Int(1, 25));

Which seems to me to be a zillion times easier to read.

I has occurred to me now, while writing these notes on the lesson that I probably should have put the conversions into the BALL and COLOUR class themselves.. so it always takes a float in the constructor but stores it as a Byte.. or something like that.. but even doing that would change all the stuff like so....

Ran.Byte() -> random.Next(0,255)
Ran.Int(1,25) -> ranomd.Next(1,25)

I dunno.. even this looks worse than my custom thing I guess... it seems that there is something I may be missing here... as it seems ugly and hard to write. Maybe there is a better way to handle conversions instead of Convert.ToInt32 or w/e

: NOTES :  Code Comments

I really need to start looking into making proper notes into my code. There should be a way using the ///sumemry type stuff to add ways to have VS use intense to show me what the members are expecting as I go. I will make a effort to comment more in my next one, but thought I would level this entire project with no comments.. just to see how little I understand it when I next look at it.

C# Players Guide dose cover comments but after reading it a few times it is still confusing.. I need to source google page on this before next time. - THING TO DO.

: NOTES :  Possible Error

I think there is a error in the main program, not a fatal one.. but I believe now I am going though it for my notes that I should have used <= instead of < for the for loops. As I am counting from 1 and not from 0. I will not bother fixing this.. but it is something to keep in mind. The 0 start in the Arrays and Lists is a little hard to remember. I think it is not an error though as I am using +1 in the readouts... but I am done for now and will not be checking or changing anything lol. It is time to move on.

: CODE :

: EXAMPLE OUTPUT :

Quote

 

Lets create some balls!
We have just created : 12 : balls!
Lets throw those balls around, up to a dozen times each.
      Ball 1 has been thrown 3 times.
      Ball 2 has been thrown 2 times.
      Ball 3 has been thrown 6 times.
      Ball 4 has been thrown 2 times.
      Ball 5 has been thrown 6 times.
      Ball 6 has been thrown 3 times.
      Ball 7 has been thrown 2 times.
      Ball 8 has been thrown 4 times.
      Ball 9 has been thrown 2 times.
      Ball 10 has been thrown 3 times.
      Ball 11 has been thrown 3 times.
      Ball 12 has been thrown 4 times.
Now lets Pop at least 5 balls but no more then 10!
      Ball 1 has been POPPED!!
      Ball 2 has been POPPED!!
      Ball 7 has been POPPED!!
      Ball 8 has been POPPED!!
      Ball 10 has been POPPED!!
      Ball 12 has been POPPED!!
Now Lets throw the remaining balls around at most a three dozen more times
      Now Ball 3 has been thrown 11 times.
      Now Ball 4 has been thrown 7 times.
      Now Ball 5 has been thrown 16 times.
      Now Ball 6 has been thrown 5 times.
      Now Ball 9 has been thrown 9 times.
      Now Ball 11 has been thrown 9 times.

*******************
   Final Results
*******************

We had 12 balls to play with.
The 6 Popped balls were Thrown a total of : 18 times
The 6 unPopped balls were Thrown a total of : 57 times
In total all the balls were Thrown a total of : 75 times

: Thrown Values for each POPPED ball :
      Ball 1 has been thrown 3 times.
      Ball 2 has been thrown 2 times.
      Ball 7 has been thrown 2 times.
      Ball 8 has been thrown 4 times.
      Ball 10 has been thrown 3 times.
      Ball 12 has been thrown 4 times.

: Thrown Values for each UNPOPPED ball :
      Ball 3 has been thrown 11 times.
      Ball 4 has been thrown 7 times.
      Ball 5 has been thrown 16 times.
      Ball 6 has been thrown 5 times.
      Ball 9 has been thrown 9 times.
      Ball 11 has been thrown 9 times.

Press Any Key to Exit

 

Well, see ya next time!



0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
  • Advertisement
  • What is your GameDev Story?

    In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

    (You must login to your GameDev.net account.)

  • Blog Entries

  • Similar Content

    • By adamhuman
      well Hello internet , greeting to all of you smart beautiful Developers people  
      its something i dream about to make my own simple 2D game engine from scratch
      and to make it Easy for my self i thought ill be able to use C# for the game coding and Basic manipulation and Animation ,
      and C++ for the Graphic Engine and the heavy stuff of Importing and exporting and rendering 
       
      but the problem is i only have Experience in C# and Graphic and Illustration Design ( that's = no worry about the Animation and Assets )
      so i don't Have any Experience in SDL/OpenGL or C++ i only know some Basic , 
      and since SDL/OpenGL tutorials are available in youtube i didnt find an advanced one talking about combining between C# and C++ like Unity3D did in their engine 
      any help in this will be Awesome  
    • By rh_galaxy
      Hi, I'm seeking people interested in help making "Galaxy Forces VR" a great game.
      I'm currently alone and like someone that can create simple 3D-models, and a programmer to join the team.
      The project is being developed using Unity/C#. It's a 2D game, but viewed in 3D.
      The game is based on my project "Galaxy Forces V2", but this will be single player only, and support the game modes race and mission. There will be global hi-scores on the website with replays of the record scores and ranking of top players, as it adds a lot to the game. And also keeps players interested for longer time.
      Everything is not written in stone, there are possibilities for someone creative to add their own ideas. In fact I encourage any team member to test and think about different options of how to make the game better.
      A change to the original is that this will be easier to play for beginners to make it more attractive but not easier to master fully.
      This is the original complete version: http://www.galaxy-forces.com/
      I'm new to Unity, and only have a little knowledge of how to create 3D models. I know C++ fully, but C# is mostly the same. I might get the coding done myself, but it would feel better to have one more person coding and testing their ideas.
      I share a picture from the development, and a demo version in current state, so you can decide if you want to join. The plan is to release it on the Oculus Store in half a year. I have a todo list, and I promise to do what I can and spend much time on this to get it done to completion. I like to share the profits with those who want to join and have actually done something that pushes the project forward. The demo runs on Windows without VR, but Oculus Rift is recommended to be able to choose level in the menu (otherwise press Return to play the default level)...

      Galaxy Forces VR v0.1
      https://drive.google.com/open?id=1GpcfMzAsgsBPkht_RV3lTJcRR0zI3AKo
      The presentation right now may not be the final result, especially the menu needs a new look with more contents.
      It is true that it is a hobby project, but I think it has great chances to get accepted by Oculus. There is a large contents of 50 levels for one thing, and the levels don't need much work to reuse for this project. Also since VR is not main stream, and there aren't so many games released, it will not drown in the noise as easily. And I think VR people buy more games than most, at least I do.

      This is the full TODO list, that covers most of the needed work from now to release. I think the time plan is realistic...
      Map elements
      - Landing zone, hangar building - low poly model (only visual)
      - Landing zone, antenna - low poly model (only visual)
      - Map decorations, trees - low poly model (visual, and collision in map)
      - Map decorations, barrels - low poly model (visual, and collision in map)
      - Map decorations, red/green house in 3 parts (left, center, right) - low poly model (visual, and collision in map)
      - Z-objects for decoration. Objects that can be placed in a map that are larger and sticks out towards the player to make it more visually pleasing to play VR, for example brick walls in different shapes (only visual, placed inside walls not accessible to the ship) The levels
      - Now there are 23 race levels and 23 mission levels. There also exist levels for dogfight and mission_coop, take these levels and convert them to race and mission to get 50+ levels
      - Need to fix the editor to make it possible to place the new Z-objects in the maps and go through each map and add them Door element
      - A low poly model for the end points of the door (only one needs to be created, can be rotated in 4 different angles to make all parts)
      - Implement the door element in code Enemies
      - Create them in 3D [enemy 0..6]
      - Implement them in code Game Status
      - Race: show Time, Current checkpoint, Current lap/Total laps, Health bar
      - Mission: show Lives, Health bar, Fuel bar, Cargo bar, Score Sound
      - Add existing music to the menu and game
      - Add existing sound fx in the game Menu
      - More contents (could be Game Name text/logo, animated objects, clouds, anything really)
      - Better gfx (different textures for the level select elements)
      - Show your score vs the world best on each level, also show your rank (gold, silver or bronze)
      - Make part 2 of the menu - after a level is selected in part 1, shows 3 options - play, play your best replay, play the world record replay
      - Settings to turn the music on/off (+a minimum of other settings). The VR room around the player
      - More contents (could be clouds and a skybox, or a room, or blackness in space, anything really) Replay
      - Implement replays in code
      - Online hiscores - that is, be able to send/load the replays to the website (either HTTP or HTTPS if possible, maybe easy to do HTTPS with C# ?)
      - The hiscore implementation on the website. (mostly done already) Website
      - www.galaxy-forces-vr.com exists.
      - Better/more contents + the hi-scores Release
      - Images in different sizes for release on the Oculus Store
      - Game play promotion video
      - Test/fix it working on minimum req hardware The demo + the todo list should help get a picture of what this game will be and help you decide if you want to join and if you have the skills needed.
      Hope to hear from you.

    • By Geonamic
      In my turn-based, RPG game, Forsaken Alchemy, I plan to have voiced character monologues and dialogues play along with some character animations after every battle, and I'd like to hear your thoughts on if wording is presented to be realistic in speech patterns, how all the characters sound in personality, if they sound similar in personality to each other, and any feedback on anything. There are in-battle criteria to trigger certain quotes more often than others, but I won't bore you with them. I just wanted to put that out in case you were curious how the below criteria matters in probability.
      Every quote in the solo quotes spoiler box is an instance of the character having a monologue. It's not him/her saying every single quote after one battle, so RNG will decide which of the listed solo quotes will play alone after a battle. For example,
      "Yes! I won!"
      and
      "That was easy!"
      are randomly picked per battle. The person is not saying, "Yes! I won! That was easy!"
      Solo quotes:
      When there's an empty line of space between dialogue, that means the next line is the start of a new dialogue. For the example below, after the second quote from Alvis is a different instance of dialogue that plays if RNG picks that to be played instead of the previous instance.
      Alvis: “I wish I knew even half as much knowledge that you know.”
      Victoria: “In time, you will.”
      Alvis: “That’s a pretty optimistic view.”
      Victoria: “For a scholar, you did well in that fight.”
      Alvis: “I’m just lucky I got out of it with everything intact.”
      Victoria: “You should be more confident about your keen strategies.”
      Group quotes:
       
    • By lougv22
      I am currently an indie game developer and I am looking to get a job with a game company as a game programmer. I worked for a game studio 9 years ago, but at the time I decided to get a day job as a software developer (non-game development), while focusing (as an indie developer), during my free time, on a vision for a game I've had for some time.
      This was then, but i've recently found myself unsatisfied with my day job and I am now thinking of going back to the game industry. The drive to make games is just too strong in me and I can no longer justify spending my days making software I am not excited about. Which leads me to my questions about a game programmer portfolio. Before i first got a job at a game studio i had built a couple of small games, this was way back though, around the year 2006. Would those be too old to showcase on a portfolio?
      Second question, i'd like to make the indie game i am working on available for potential recruiters to play, but I am not sure how to do that. I tried to put it up on Shimmer.io (kind like itch.io, but not as popular), but i ran into issues with that. It's a Unity game and the Web build i created for it was about 190 MB and it ran slowly and was very choppy on my machine, at which point i kind of gave up on the idea of putting it up online. The other option is to simply send (through email or Google drive) game companies a regular Unity build and let them play it that way. The question is, should i try to go the Web build route again and if so, any tips on making it work well this time? And also, if the Web build doesn't work again, would it be acceptable to send companies i apply for a regular build?
    • By Naruto-kun
      Hi guys
      A while back I wrote a small app using C# and MoonSharp to execute a Lua script that would call functions from within my C# app, that would call GDI+ draw commands. This way I could see how my draw elements were being positioned in real time and adjust as I wished as can be seen in the image below. However, I am now looking at creating my own library that will use D3D11 instead of GDI+ to render 2D shapes, and I would like to create the same scripting tool, but this time I want it to use C#. Is there anything like this out there?

×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!