Advertisement
Mellowlicious

ClassExample

Feb 16th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6. namespace Example
  7. {
  8.  
  9.     class Player
  10.     {
  11.         //This rectangle declaration tells us that the player will have some kind of rectangle. However, it is not automatically declared: By default it will have the value 'null'.
  12.         //We can't use a null value because it is literally nothing. So we have to make sure we create the rectangle somewhere.
  13.         public Rectangle playerRect;
  14.  
  15.         //The best place to create the rectangle is in the constructor: This is the function that is called when you use syntax of the form "Class className = new Class(<insert parameters>)".
  16.         public Player()
  17.         {
  18.             //First we have to use the syntax to declare a new object, like this:
  19.             playerRect = new Rectangle(100, 100, 100, 100);
  20.             //The Rectangle struct has 3 different constructors. Here we use int x, int y, int width, int height. So we get a rectangle at coordinates (100,100) with width 100 and height 100.
  21.         }
  22.  
  23.         public void Move()
  24.         {
  25.             //ChangeRectangle or something
  26.         }
  27.     }
  28.     class MainGame
  29.     {
  30.         //Again here it says that we have some player in your game. However, we need to know what kind of specific player it is: We might not have a player, or we might have multiple players. Before you can use the
  31.         //properties of a class, you first have to create the class. So let's do that again in the constructor.
  32.         Player player1;
  33.         public Rectangle mouseRect;
  34.         public MainGame()
  35.         {
  36.             player1 = new Player();
  37.             //Now that we have constructed the player, we can ask its properties. We do this by className.property. For example, if we want to know player1's rectangle, we ask player1.playerRect.
  38.             //Note that the rectangle is specific to this implementation: We can't ask Player.playerRect, for example. This is because it is a property that will change with every new player you create.
  39.             //After all, they will move, so their rectangle will change.
  40.         }
  41.  
  42.         public void Draw()
  43.         {
  44.             //Now we can aks something like
  45.             if (mouseRect == player1.playerRect)
  46.             {
  47.                 //doshit
  48.             }
  49.             //Note that this won't work (the entire program is missing a main function anyway) because mouseRect is not instantized: it's still null. This will throw out a nullreferenceexception. Normally in the constructor
  50.             //you would also call something like mouseRect = new Rectangle();.
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement