Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Drawing;
- namespace Example
- {
- class Player
- {
- //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'.
- //We can't use a null value because it is literally nothing. So we have to make sure we create the rectangle somewhere.
- public Rectangle playerRect;
- //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>)".
- public Player()
- {
- //First we have to use the syntax to declare a new object, like this:
- playerRect = new Rectangle(100, 100, 100, 100);
- //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.
- }
- public void Move()
- {
- //ChangeRectangle or something
- }
- }
- class MainGame
- {
- //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
- //properties of a class, you first have to create the class. So let's do that again in the constructor.
- Player player1;
- public Rectangle mouseRect;
- public MainGame()
- {
- player1 = new Player();
- //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.
- //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.
- //After all, they will move, so their rectangle will change.
- }
- public void Draw()
- {
- //Now we can aks something like
- if (mouseRect == player1.playerRect)
- {
- //doshit
- }
- //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
- //you would also call something like mouseRect = new Rectangle();.
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement