1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using Jypeli; |
---|
4 | using Jypeli.Assets; |
---|
5 | using Jypeli.Controls; |
---|
6 | using Jypeli.Effects; |
---|
7 | using Jypeli.Widgets; |
---|
8 | |
---|
9 | public class FysiikkaPeli1 : PhysicsGame |
---|
10 | { |
---|
11 | Vector VelocityUp = new Vector(0, 200); |
---|
12 | Vector velocityDown = new Vector(0, -200); |
---|
13 | |
---|
14 | PhysicsObject ball; |
---|
15 | PhysicsObject paddle1; |
---|
16 | PhysicsObject paddle2; |
---|
17 | |
---|
18 | PhysicsObject leftBorder; |
---|
19 | PhysicsObject rightBorder; |
---|
20 | |
---|
21 | IntMeter player1Score; |
---|
22 | IntMeter player2Score; |
---|
23 | |
---|
24 | public override void Begin() |
---|
25 | { |
---|
26 | CreateLevel(); |
---|
27 | StartGame(); |
---|
28 | SetControls(); |
---|
29 | AddCounters(); |
---|
30 | } |
---|
31 | void CreateLevel() |
---|
32 | { |
---|
33 | ball = new PhysicsObject(40.0, 40.0); |
---|
34 | ball.Shape = Shape.Circle; |
---|
35 | ball.X = 100.0; |
---|
36 | ball.Y = -0.0; |
---|
37 | ball.Restitution = 1.0; |
---|
38 | Add(ball); |
---|
39 | |
---|
40 | AddCollisionHandler(ball, Handleballcollison); |
---|
41 | |
---|
42 | paddle1 =CreatePaddle(Level.Right -20, 0.0); |
---|
43 | paddle2 =CreatePaddle(Level.Left +20, 0.0); |
---|
44 | |
---|
45 | |
---|
46 | Camera.ZoomToLevel(); |
---|
47 | |
---|
48 | Level.Background.Color = Color.LightGray; |
---|
49 | Level.CreateBorders(1.0, true); |
---|
50 | ball.Color = Color.DarkForestGreen; |
---|
51 | |
---|
52 | leftBorder = Level.CreateLeftBorder(); |
---|
53 | leftBorder.Restitution = 1.0; |
---|
54 | leftBorder.IsVisible = false; |
---|
55 | |
---|
56 | rightBorder = Level.CreateRightBorder(); |
---|
57 | rightBorder.Restitution = 1.0; |
---|
58 | rightBorder.IsVisible = false; |
---|
59 | |
---|
60 | PhysicsObject bottomBorder = Level.CreateBottomBorder(); |
---|
61 | bottomBorder.Restitution = 1.0; |
---|
62 | bottomBorder.IsVisible = false; |
---|
63 | |
---|
64 | PhysicsObject topBorder = Level.CreateTopBorder(); |
---|
65 | topBorder.Restitution = 1.0; |
---|
66 | topBorder.IsVisible = false; |
---|
67 | |
---|
68 | } |
---|
69 | void StartGame() |
---|
70 | { |
---|
71 | Vector impulse = new Vector(600.0, 0.0); |
---|
72 | ball.Hit(impulse); |
---|
73 | } |
---|
74 | |
---|
75 | PhysicsObject CreatePaddle(double x, double y) |
---|
76 | { |
---|
77 | PhysicsObject paddle = PhysicsObject.CreateStaticObject(20.0, 100.0); |
---|
78 | paddle.Shape = Shape.Rectangle; |
---|
79 | paddle.Color = Color.DarkRed; |
---|
80 | paddle.X = x; |
---|
81 | paddle.Y = y; |
---|
82 | paddle.Restitution = 1.1; |
---|
83 | Add(paddle); |
---|
84 | return paddle; |
---|
85 | |
---|
86 | } |
---|
87 | |
---|
88 | void SetControls() |
---|
89 | { |
---|
90 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); |
---|
91 | Keyboard.Listen(Key.Up, ButtonState.Down, SetVelocity, "The Paddle1 will go up", paddle1, VelocityUp); |
---|
92 | Keyboard.Listen(Key.Up, ButtonState.Released, SetVelocity, "Releasing A will stop Paddle1", paddle1, Vector.Zero); |
---|
93 | Keyboard.Listen(Key.Down, ButtonState.Down, SetVelocity, "Paddle1 Will go down",paddle1, velocityDown); |
---|
94 | Keyboard.Listen(Key.Down, ButtonState.Released, SetVelocity, "Paddle will stop from going down", paddle1, Vector.Zero); |
---|
95 | |
---|
96 | Keyboard.Listen(Key.A, ButtonState.Down, SetVelocity,"Paddle2 will go up", paddle2, VelocityUp); |
---|
97 | Keyboard.Listen(Key.A, ButtonState.Released, SetVelocity,"Paddle2 will stop moving", paddle2, Vector.Zero); |
---|
98 | Keyboard.Listen(Key.D, ButtonState.Down, SetVelocity, "Paddle 2 will go down", paddle2, velocityDown); |
---|
99 | Keyboard.Listen(Key.D, ButtonState.Released, SetVelocity, "Paddle 2 wills stop moving", paddle2, Vector.Zero); |
---|
100 | |
---|
101 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); |
---|
102 | Keyboard.Listen(Key.Tab,ButtonState.Pressed, ShowControlHelp, "It will show control panel"); |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | } |
---|
107 | void SetVelocity(PhysicsObject paddle, Vector velocity) |
---|
108 | { |
---|
109 | if ((velocity.Y > 0) &&(paddle.Top > Level.Top)) |
---|
110 | { |
---|
111 | paddle.Velocity = Vector.Zero; |
---|
112 | return; |
---|
113 | } |
---|
114 | if ((velocity.Y < 0) && (paddle.Bottom < Level.Bottom)) |
---|
115 | { |
---|
116 | paddle.Velocity = Vector.Zero; |
---|
117 | } |
---|
118 | paddle.Velocity = velocity; |
---|
119 | } |
---|
120 | |
---|
121 | void AddCounters() |
---|
122 | { |
---|
123 | player1Score = CreateScoreCounter(Screen.Right -100, Screen.Top -100); |
---|
124 | player2Score = CreateScoreCounter(Screen.Left + 100, Screen.Top -100); |
---|
125 | } |
---|
126 | |
---|
127 | IntMeter CreateScoreCounter(double x, double y) |
---|
128 | { |
---|
129 | IntMeter counter = new IntMeter(0); |
---|
130 | counter.MaxValue = 10; |
---|
131 | |
---|
132 | Label display = new Label(); |
---|
133 | display.BindTo(counter); |
---|
134 | display.X = x; |
---|
135 | display.Y = y; |
---|
136 | display.TextColor = Color.GreenYellow; |
---|
137 | display.BorderColor = Color.YellowGreen; |
---|
138 | display.BorderColor = Level.Background.Color; |
---|
139 | display.Color = Level.Background.Color; |
---|
140 | Add(display); |
---|
141 | |
---|
142 | return counter; |
---|
143 | |
---|
144 | } |
---|
145 | void Handleballcollison(PhysicsObject ball, PhysicsObject target) |
---|
146 | { |
---|
147 | if (target == rightBorder) |
---|
148 | { |
---|
149 | player2Score.Value += 1; |
---|
150 | } |
---|
151 | else if (target == leftBorder) |
---|
152 | { |
---|
153 | player1Score.Value += 1; |
---|
154 | } |
---|
155 | |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | } |
---|