{{{ #!html
}}} {{{ #!html }}} [[BR]][[BR]][[BR]][[BR]][[BR]][[BR]] [[PageOutline]] = Pong-game, part 2 = This is Pong-game tutorial part 2/7. on this part * '''We make ball to move''' * '''Make borders to field''' * '''Chance colour of backround''' * '''Zoom in to the whole field''' [[Image(Pong/Liitteet:pong2.png, 550, nolink)]] = 1. Coordinatesystem = Ball, Like many others game creatures, are in coordinatesystem. It means, that ball has `x`-cordinate, what tells balls place in horisontal and `y`-cordinate, what tells balls place on vertically . Cordinates can get also positive and negative values. Previous part we didn't give cordinates to ball, so that place come default origo (The point that has zero values in both `x`-and `y`). Nexe picture shows idea of balls `x`-cordinate. When `x`-cordinate is zero, ball is horizontaly center of the game field . if `x` has value -200, balls position is horizontaly on left side of origo. `y`-cordinates idea is same, it only tels place on vertically. [[Image(Pong/Liitteet:x_akseli.png, nolink)]] Gameobject `x`- and `y`-cordinates can set chansing physicsobjects `X`- ja `Y`-atributes: {{{ #!C# ball.X = -200.0; ball.Y = 0.0; }}} add previos lines of code behind creating a ball . try different `X` and `Y` cordinates where ball comes on screen. [[Image(source:/trunk/help_symbols/try_to_run.png, nolink)]][[BR]]^(paina F5)^ = 2. Vectors = When making game you need commonly vectors. Vector isn't a strange thing, than it keeps inside both `x`- and `y`-cordinate. We could picture vector whit cordinates (10, 20). First number is `x`- and second `y`-cordinate. In code similar vector would be created call `new Vector( 10.0, 20.0 )`. Vector can picture for example oblects place in coordinatesystem or objects place chance in relation to a previous place. Exsample vector (100, 50) will be picture `x`-cordinate chance 100 units to right and `y`-cordinates chance 50 units to up. In next picture there is demostrated before pictured vector. Vectors are pictured normaly whit arrow. Becouse vectors `x`-value is 100, in picture arrows lenght horizontaly is 100 units. In proportion aroows height in vertically is 50 units. If ball is moved acording to vector, it moves to pointed direction. [[Image(Pong/Liitteet:vektori.png, nolink)]] = 3. putting ball to move = Now we put ball to move. Becouse we have an physicmotor, this is pretty easy. Physicobject can be prod to movement by `Hit`-call. we need to put vector to parameter, what tells ''What way and how fast ball goes''. add these next lines to code, but '''only then''', when ball is added to game (so behind `Add`-call): {{{ #!C# Vector impulssi = new Vector( 500.0, 0.0 ); ball.Hit( impulssi ); }}} What biger values you give to vector, that faster ball goes kovempaa. try to do vector whit different `x`- and `y`-values and look what direktion ball goes. [[Image(source:/trunk/help_symbols/try_to_run.png, nolink)]] [[Image(source:/trunk/help_symbols/question.png, nolink)]] Miten laittaisit pallon liikkumaan yläviistoon? = 4. Creating a border = When ball is made to move, it escapes out from screen . So lets do borders to field then see how ball bounces . add next subprogramcall (we tell you more about subprograms in next part) to code then, when ball created and added to game . {{{ #!C# Level.CreateBorders(); }}} When you now drive the game, in gamefield ther must be borders now, where ball collides and bounces back. [[Image(source:/trunk/help_symbols/try_to_run.png, nolink)]] But why ball collides , but we havn't procramed it anyway? becouse, when we created project we created physicgame, in start of the code there is next line: {{{ #!C# public class game : PhysicsGame }}} Phusicsgame (`PhysicsGame`) ''physicsmotor'' calculates ball's movement for us . ---- if you try to run the game now ('''F5'''-button), we can notice, that balls collising haves a litle broblem: it slows down when it hits to the wall, not good. physicsobjects lukily have many other properties not only `x`- ja `y`-cordinates. one of them is `Restitution`. To restitution you can give numbers betveen `0.0` - `1.0`. What closer that value is to one, that less its velocity drops from impact. For example superball's bouncingvalue is maybe close to one , but screwed up paper's bouncing value is almost zero, becouse it dosn't bounse very hight if you throw it to ground. So lets put its `Restitution`- arvoksi `1.0`. Write this line right behind setting the cordinates of ball. {{{ #!C# ball.Restitution = 1.0; }}} Becouse in impact there is alvays two sides, you need to put value to target of impact, like this. To borders of gamefield this will be done by using `CreateBorders`-subprogram "versin", what takes parameters. First parameter is bouncing, what you have putted to the ball, and another parameter tells, can you see the wall or not . Type of it is true value `bool`, so it haves two posible values: * `true` ("yes") * `false` ("no") Becouse the borders are outside of screen (when you get to that point), values of another parameter aren't so important. So put it value to `false`. '''Find''' Next line from your code: {{{ #!C# Level.CreateBorders(); }}} and '''chanse''' it to next. {{{ #!C# Level.CreateBorders( 1.0, false ); }}} [[Image(source:/trunk/help_symbols/try_to_run.png, nolink)]] = 5. Chansing the backround color = [http://en.wikipedia.org/wiki/Pong Alkuperäisen Pong-pelin] backround color was black not lightblue. Lets fix that litle missind '''behind of last line ''' next line, what gives a new color to the backround. {{{ #!C# Level.BackgroundColor = Color.Black; }}} [[Image(source:/trunk/help_symbols/try_to_run.png, nolink)]] [[Image(source:/trunk/help_symbols/question.png, nolink)]] Miten asettaisit taustavärin vihreäksi? Remember to save your project. = 6. Zooming the camera = Now we need put an camera to show whole game field. In our gamefield there is always camera, `Camera`. You can zoom in or out from the field. So lets zoom it, to show whole field. It happens when you add next line: {{{ Camera.ZoomToLevel(); }}} = 7. Result = When you have done theese adds your code must look like in next link: https://trac.cc.jyu.fi/projects/npo/browser/trunk/esimerkit/Pong/Vaihe2/Peli.cs Notice, that C#-language isn't very accurasy about "emphty" marks, like space's or emphty lines, about. But its not important, if you have emphty lines in your code, or if there comes space behind the bracet. you can write emphty lines to your code, if it gets reading of your code easier. Its not important if your lines aren't on order. For example its not a big deal if you give first cordinates to ball or bounce. But you need to create it before you give bounce to it, becouse you need an physicsobject to give bounce. Notice, in ready code there can be lines what starts `using`, what you dont maybe have. Its not a harm. If you dont have `using`-lines, there comes an clear error . {{{ #!html }}} {{{ #!html
}}} [[BR]][[BR]][[BR]][[BR]][[BR]][[BR]]