[1292] | 1 | using System; |
---|
| 2 | using Jypeli; |
---|
| 3 | using Jypeli.Widgets; |
---|
| 4 | using Jypeli.Assets; |
---|
| 5 | |
---|
| 6 | public class Peli : PhysicsGame |
---|
| 7 | { |
---|
[1315] | 8 | Vector nopeusYlos = new Vector(0, 200); |
---|
| 9 | Vector nopeusAlas = new Vector(0, -200); |
---|
| 10 | |
---|
[1292] | 11 | PhysicsObject pallo; |
---|
| 12 | |
---|
[1315] | 13 | PhysicsObject maila1; |
---|
| 14 | PhysicsObject maila2; |
---|
| 15 | |
---|
[1292] | 16 | protected override void Begin() |
---|
| 17 | { |
---|
| 18 | LuoKentta(); |
---|
[1315] | 19 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu"); |
---|
| 20 | AloitaPeli(); |
---|
| 21 | AsetaOhjaimet(); |
---|
[1292] | 22 | } |
---|
| 23 | |
---|
| 24 | void LuoKentta() |
---|
| 25 | { |
---|
| 26 | pallo = new PhysicsObject(40.0, 40.0); |
---|
| 27 | pallo.Shape = Shapes.Circle; |
---|
[1315] | 28 | pallo.Color = Color.Green; |
---|
[1292] | 29 | pallo.X = -200.0; |
---|
| 30 | pallo.Y = 0.0; |
---|
| 31 | pallo.Restitution = 1.0; |
---|
| 32 | Add(pallo); |
---|
| 33 | |
---|
[1315] | 34 | maila1 = LuoMaila(Level.Left + 20.0, 0.0); |
---|
| 35 | maila2 = LuoMaila(Level.Right - 20.0, 0.0); |
---|
[1292] | 36 | |
---|
| 37 | Level.CreateBorders(1.0, false); |
---|
| 38 | Level.BackgroundColor = Color.Black; |
---|
| 39 | |
---|
| 40 | Camera.ZoomToLevel(); |
---|
| 41 | } |
---|
[1315] | 42 | |
---|
[1292] | 43 | void AloitaPeli() |
---|
| 44 | { |
---|
| 45 | Vector impulssi = new Vector(600.0, 0.0); |
---|
[1315] | 46 | pallo.Hit(impulssi); |
---|
[1292] | 47 | } |
---|
| 48 | |
---|
[1315] | 49 | PhysicsObject LuoMaila(double x, double y) |
---|
[1292] | 50 | { |
---|
| 51 | PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); |
---|
| 52 | maila.Shape = Shapes.Rectangle; |
---|
| 53 | maila.X = x; |
---|
| 54 | maila.Y = y; |
---|
| 55 | maila.Restitution = 1.0; |
---|
| 56 | Add(maila); |
---|
[1315] | 57 | |
---|
| 58 | return maila; |
---|
[1292] | 59 | } |
---|
[1315] | 60 | |
---|
| 61 | void AsetaOhjaimet() |
---|
| 62 | { |
---|
| 63 | Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa Ylos", maila1, nopeusYlos); |
---|
| 64 | Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); |
---|
| 65 | Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa Ylos", maila1, nopeusAlas); |
---|
| 66 | Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); |
---|
| 67 | |
---|
| 68 | Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa Ylos", maila2, nopeusYlos); |
---|
| 69 | Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); |
---|
| 70 | Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa Ylos", maila2, nopeusAlas); |
---|
| 71 | Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); |
---|
| 72 | |
---|
| 73 | Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); |
---|
| 74 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu"); |
---|
| 75 | } |
---|
| 76 | void AsetaNopeus(PhysicsObject maila, Vector nopeus) |
---|
| 77 | { |
---|
| 78 | if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) |
---|
| 79 | { |
---|
| 80 | maila.Velocity = Vector.Zero; |
---|
| 81 | return; |
---|
| 82 | } |
---|
| 83 | if ((nopeus.Y > 0) && (maila.Top > Level.Top)) |
---|
| 84 | { |
---|
| 85 | maila.Velocity = Vector.Zero; |
---|
| 86 | return; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | maila.Velocity = nopeus; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | } |
---|