1 | using System; |
---|
2 | using Jypeli; |
---|
3 | using Jypeli.Widgets; |
---|
4 | using Jypeli.Assets; |
---|
5 | |
---|
6 | public class Peli : PhysicsGame |
---|
7 | { |
---|
8 | PhysicsObject pallero; |
---|
9 | Vector yhaylosyrittaa = new Vector(0, 200); |
---|
10 | Vector maanalainen = new Vector(0, -200); |
---|
11 | PhysicsObject vasen; |
---|
12 | PhysicsObject oikia; |
---|
13 | PhysicsObject pelaaja1; |
---|
14 | PhysicsObject pelaaja2; |
---|
15 | IntMeter Ykkospisteet; |
---|
16 | IntMeter Kakkospisteet; |
---|
17 | |
---|
18 | protected override void Begin() |
---|
19 | { |
---|
20 | Kentta(); |
---|
21 | Ohjustukikohta(); |
---|
22 | Pluslaskut(); |
---|
23 | Alota(); |
---|
24 | |
---|
25 | } |
---|
26 | |
---|
27 | void Kentta() |
---|
28 | { |
---|
29 | pallero = new PhysicsObject(39.9, 40.0); |
---|
30 | pallero.Shape = Shapes.Circle; |
---|
31 | pallero.Y = 100.82346277835; |
---|
32 | Add(pallero); |
---|
33 | pallero.Restitution = 1.0; |
---|
34 | AddCollisionHandler(pallero, Tyrmäys); |
---|
35 | vasen = Level.CreateLeftBorder(); |
---|
36 | oikia = Level.CreateRightBorder(); |
---|
37 | PhysicsObject katto = Level.CreateTopBorder(); |
---|
38 | PhysicsObject lattia = Level.CreateBottomBorder(); |
---|
39 | Level.BackgroundColor = Color.ForestGreen; |
---|
40 | Camera.ZoomToLevel(); |
---|
41 | pelaaja1 = PingPong(Level.Left + 20, 0.0); |
---|
42 | pelaaja2 = PingPong(Level.Right - 20, 0.0); |
---|
43 | } |
---|
44 | |
---|
45 | void Alota() |
---|
46 | { |
---|
47 | Vector newton = new Vector(-1000.0, 500.0); |
---|
48 | pallero.Hit(newton); |
---|
49 | } |
---|
50 | PhysicsObject PingPong(double x, double y) |
---|
51 | { |
---|
52 | PhysicsObject pong = PhysicsObject.CreateStaticObject(20.0, 50.0); |
---|
53 | pong.Position = new Vector(x, y); |
---|
54 | pong.Restitution = 1.0; |
---|
55 | Add(pong); |
---|
56 | return pong; |
---|
57 | } |
---|
58 | |
---|
59 | void Tyrmäys(PhysicsObject pallero, PhysicsObject uhri) |
---|
60 | { |
---|
61 | if (uhri == oikia) |
---|
62 | { |
---|
63 | Ykkospisteet.Value += 1; |
---|
64 | } |
---|
65 | if (uhri == vasen) |
---|
66 | { |
---|
67 | Kakkospisteet.Value += 1; |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | void Ohjustukikohta() |
---|
72 | { |
---|
73 | Keyboard.Listen(Key.A, ButtonState.Down, Vauhdikkuus, "Pelaaja 1: Liikuta mailaa ylös", pelaaja1, yhaylosyrittaa); |
---|
74 | Keyboard.Listen(Key.A, ButtonState.Released, Vauhdikkuus, null, pelaaja1, Vector.Zero); |
---|
75 | Keyboard.Listen(Key.Z, ButtonState.Down, Vauhdikkuus, "Pelaaja 1: Liikuta mailaa alas", pelaaja1, maanalainen); |
---|
76 | Keyboard.Listen(Key.Z, ButtonState.Released, Vauhdikkuus, null, pelaaja1, Vector.Zero); |
---|
77 | Keyboard.Listen(Key.Up, ButtonState.Down, Vauhdikkuus, "Pelaaja 2: Liikuta mailaa ylös", pelaaja2, yhaylosyrittaa); |
---|
78 | Keyboard.Listen(Key.Up, ButtonState.Released, Vauhdikkuus, null, pelaaja2, Vector.Zero); |
---|
79 | Keyboard.Listen(Key.Down, ButtonState.Down, Vauhdikkuus, "Pelaaja 2: Liikuta mailaa alas", pelaaja2, maanalainen); |
---|
80 | Keyboard.Listen(Key.Down, ButtonState.Released, Vauhdikkuus, null, pelaaja2, Vector.Zero); |
---|
81 | Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Ohje"); |
---|
82 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu"); |
---|
83 | |
---|
84 | } |
---|
85 | |
---|
86 | void Pluslaskut() |
---|
87 | { |
---|
88 | Ykkospisteet = Laskin(Screen.Left + 100.0, Screen.Top - 100.0); |
---|
89 | Kakkospisteet = Laskin(Screen.Right - 100.0, Screen.Top - 100.0); |
---|
90 | } |
---|
91 | |
---|
92 | IntMeter Laskin(double x, double y) |
---|
93 | { |
---|
94 | IntMeter laskin = new IntMeter(0); |
---|
95 | laskin.MaxValue = 5; |
---|
96 | |
---|
97 | Label tausta = new Label(); |
---|
98 | tausta.BindTo(laskin); |
---|
99 | tausta.X = x; |
---|
100 | tausta.Y = y; |
---|
101 | Add(tausta); |
---|
102 | return laskin; |
---|
103 | } |
---|
104 | |
---|
105 | void Vauhdikkuus(PhysicsObject pong, Vector vauhti) |
---|
106 | { |
---|
107 | if ((vauhti.Y < 0) && (pong.Bottom < Level.Bottom)) |
---|
108 | { |
---|
109 | pong.Velocity = Vector.Zero; |
---|
110 | return; |
---|
111 | } |
---|
112 | if ((vauhti.Y > 0) && (pong.Top > Level.Top)) |
---|
113 | { |
---|
114 | pong.Velocity = Vector.Zero; |
---|
115 | return; |
---|
116 | } |
---|
117 | pong.Velocity = vauhti; |
---|
118 | } |
---|
119 | } |
---|