{{{ #!html
}}} {{{ #!html }}} [[BR]][[BR]][[BR]][[BR]][[BR]][[BR]] [[PageOutline]] = Pong-game, part 4 = Tässä oppaassa teemme toisenkin mailan. [[Image(Pong/Liitteet:pong4.png, nolink)]] = 1. Creating the second paddle = Baddle was created in last part , whit method `CreateField`, Like this: {{{ #!C# PhysicsObject maila = PhysicsObject.CreateStaticObject( 20.0, 100.0 ); paddle.Shape = Shape.Rectangle; paddle.X = Level.Left + 20.0; paddle.Y = 0.0; paddle.Restitution = 1.0; Add( paddle ); }}} We could do second one like first , but '''don't write this''': {{{ #!C# PhysicsObject maila2 = PhysicsObject.CreateStaticObject( 20.0, 100.0 ); paddle2.Shape = Shape.Rectangle; paddle2.X = Level.Right - 20.0; paddle2.Y = 0.0; paddle2.Restitution = 1.0; Add( paddle2 ); }}} In here we make another paddle to his own variable, `paddle2`. you give parameters and attributes same way like to first and finaly added to field. you need to only change another paddle's X-cordinate to right side of the field (`Level.Right`) minus 20. But. When we are creating paddles with same way, so we are doing same twice? So can we programme that way, so that is only double it to both paddles to different sides? Of course. Lets do method where we do a paddle. Think, what is good name to that method... = 2. Method and Parameters = On method we can do only once all that what needs to create one paddles. becouse different paddles haves different cordinates, we need to tell that to method. And it happens so , cordinates are parameters of method. so lets write this next method: {{{ #!C# void CreatePaddle( double x, double y ) { } }}} When whriting parameters of method you need to tell type of them, in here we use `double` to both parameters. type `double` is suitable to produce decimals. you need to give names to parameters like any other variable, in here names of parameters are `x` and `y`. Notice, that method's parameter's and vairiable's names starts whit a litle letter, object's variables (esim. `paddle.X`) big again. '''move''' creating paddle code what is marked whit red to `CreatePadle`-method: {{{ #!html
    void CreateField()
    {
        ball = new PhysicsObject( 40.0, 40.0 );
        ball.Shape = Shape.Circle;
        ball.X = -200.0;
        ball.Y = 0.0;
        ball.Restitution = 1.0;
        Add( ball );

        PhysicsObject paddle = PhysicsObject.CreateStaticObject( 20.0, 100.0 );
        paddle.Shape = Shape.Rectangle;
        paddle.X = Level.Left + 20.0;
        paddle.Y = 0.0;
        paddle.Restitution = 1.0;
        Add( paddle );

        Level.CreateBorders( 1.0, false );
        Level.BackgroundColor = Color.Black;

        Camera.ZoomToLevel();
    }
}}} Whit changes `CreatePaddle` looks like this: {{{ #!C# void CreatePaddle( double x, double y ) { PhysicsObject paddle = PhysicsObject.CreateStaticObject( 20.0, 100.0 ); paddle.Shape = Shape.Rectangle; paddle.X = Level.Left + 20.0; paddle.Y = 0.0; paddle.Restitution = 1.0; Add( maila ); } }}} Lets take parameters to use. Lets place parameters ´x´ and ´y´ to cordinates of paddle `paddle.X` and `paddle.Y` whit next way: {{{ #!C# void CreatePaddle( double x, double y ) { PhysicsObject paddle = PhysicsObject.CreateStaticObject( 20.0, 100.0 ); paddle.Shape = Shape.Rectangle; paddle.X = x; paddle.Y = y; paddle.Restitution = 1.0; Add( paddle ); } }}} You can test it now, but there isn't any paddles yet . method `CreatePaddle` isn't called anywhere, so its code isn't running . [[Image(source:/trunk/help_symbols/try_to_run.png, nolink)]] = 3. Calling methods = Whit methods you can easily do more than one paddles. to there where we moved add there these two lines : {{{ #!html
    CreateField()
    {
        ball = new PhysicsObject( 40.0, 40.0 );
        ball.Shape = Shape.Circle;
        ball.X = -200.0;
        ball.Y = 0.0;
        ball.Restitution = 1.0;
        Add( ball );

        CreatePaddle( Level.Left + 20.0, 0.0 );
        CreatePaddle( Level.Right - 20.0, 0.0 );

        Level.CreateBorders( 1.0, false );
        Level.BackgroundColor = Color.Black;

        Camera.ZoomToLevel();
    }
}}} In method call parameters what you give to method you need to give them __in same order __ than method takes them . In call you don't list types of parameters (like `double`). This picture demostrates that, how parameters relays from method call to cordinates of paddle: [[Image(Pong/Liitteet:parametrit.png, nolink)]] Notice, that parameter can also be result of addition or subtraction. For example sum `Level.Left + 20.0` runs before method call and addition what it gets is given to parameter of method. Now there should be two paddles in your game! [[Image(source:/trunk/help_symbols/try_to_run.png, nolink)]] = 4. Result = Pong-game whit two paddles looks like this: [[Include(source:/trunk/esimerkit/Pong/Vaihe4/Peli.cs, svn:mime-type=text/plain)]] {{{ #!html }}} {{{ #!html
}}} [[BR]][[BR]][[BR]][[BR]][[BR]][[BR]]