1 | using System; |
---|
2 | using Jypeli; |
---|
3 | using Jypeli.Widgets; |
---|
4 | using Jypeli.Assets; |
---|
5 | using Jypeli.Effects; |
---|
6 | |
---|
7 | |
---|
8 | class Tasohyppely : PhysicsGame |
---|
9 | { |
---|
10 | const int rulev = 50; |
---|
11 | const int rukor = 50; |
---|
12 | const double nopeus = 200; |
---|
13 | const double hyppyVoima = 3000; |
---|
14 | |
---|
15 | IntMeter isku1; |
---|
16 | IntMeter isku2; |
---|
17 | |
---|
18 | PlatformCharacter pelaaja1; |
---|
19 | PlatformCharacter pelaaja2; |
---|
20 | |
---|
21 | PhysicsObject reuna1; |
---|
22 | PhysicsObject reuna2; |
---|
23 | PhysicsObject reuna3; |
---|
24 | PhysicsObject reuna4; |
---|
25 | |
---|
26 | |
---|
27 | protected override void Begin() |
---|
28 | { |
---|
29 | |
---|
30 | |
---|
31 | isku1 = new IntMeter(0); |
---|
32 | isku2 = new IntMeter(0); |
---|
33 | |
---|
34 | // Zoomataan lähemmäksi |
---|
35 | |
---|
36 | seuraavaKentta(); |
---|
37 | Camera.StayInLevel = true; |
---|
38 | Camera.ZoomToLevel(); |
---|
39 | } |
---|
40 | |
---|
41 | void seuraavaKentta() |
---|
42 | { |
---|
43 | |
---|
44 | ClearAll(); |
---|
45 | MediaPlayer.Play("sm26"); |
---|
46 | |
---|
47 | isku1 = new IntMeter(20); |
---|
48 | isku2 = new IntMeter(20); |
---|
49 | |
---|
50 | |
---|
51 | |
---|
52 | // Asetetaan painovoima |
---|
53 | Gravity = new Vector(0, -1000); |
---|
54 | |
---|
55 | luoKentta(); |
---|
56 | lisaaNappaimet(); |
---|
57 | } |
---|
58 | |
---|
59 | void luoKentta() |
---|
60 | { |
---|
61 | TileMap ruudut = TileMap.FromFile("kennta.txt"); |
---|
62 | ruudut['#'] = Luotaso; |
---|
63 | ruudut.Insert(rulev, rukor); |
---|
64 | |
---|
65 | reuna1 = Level.CreateLeftBorder(); |
---|
66 | reuna2 = Level.CreateRightBorder(); |
---|
67 | reuna3 = Level.CreateTopBorder(); |
---|
68 | reuna4 = Level.CreateBottomBorder(); |
---|
69 | |
---|
70 | reuna1.Tag = "reuna"; |
---|
71 | reuna2.Tag = "reuna"; |
---|
72 | reuna3.Tag = "reuna"; |
---|
73 | reuna4.Tag = "reuna"; |
---|
74 | |
---|
75 | |
---|
76 | Level.Background.CreateGradient(Color.White, Color.SkyBlue); |
---|
77 | Level.Background.Image = LoadImage("aa"); |
---|
78 | |
---|
79 | |
---|
80 | lisaaPelaajat(); |
---|
81 | |
---|
82 | Timer ajastin = new Timer(); |
---|
83 | ajastin.Interval = 10; |
---|
84 | ajastin.Trigger += HeitaKranaatti; |
---|
85 | Add(ajastin); |
---|
86 | ajastin.Start(); |
---|
87 | |
---|
88 | } |
---|
89 | |
---|
90 | PhysicsObject Luotaso() |
---|
91 | { |
---|
92 | PhysicsObject taso = PhysicsObject.CreateStaticObject(50.0, 50.0); |
---|
93 | taso.Image = LoadImage("seina"); |
---|
94 | return taso; |
---|
95 | } |
---|
96 | |
---|
97 | void lisaaPelaajat() |
---|
98 | { |
---|
99 | pelaaja1 = new PlatformCharacter(40, 40); |
---|
100 | pelaaja1.Mass = 4.0; |
---|
101 | pelaaja1.Image = LoadImage("aa"); |
---|
102 | pelaaja1.X = 100; |
---|
103 | pelaaja1.Y = 120; |
---|
104 | |
---|
105 | pelaaja2 = new PlatformCharacter(40, 40); |
---|
106 | pelaaja2.Mass = 4.0; |
---|
107 | pelaaja2.Image = LoadImage("ss"); |
---|
108 | pelaaja2.X = -100; |
---|
109 | pelaaja2.Y = 120; |
---|
110 | |
---|
111 | AddCollisionHandler(pelaaja1, kasitteletormays); |
---|
112 | AddCollisionHandler(pelaaja2, kasitteletormays); |
---|
113 | |
---|
114 | |
---|
115 | Add(pelaaja1); |
---|
116 | Add(pelaaja2); |
---|
117 | } |
---|
118 | |
---|
119 | void lisaaNappaimet() |
---|
120 | { |
---|
121 | Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); |
---|
122 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu pelistä"); |
---|
123 | |
---|
124 | Keyboard.Listen(Key.Left, ButtonState.Down, liikuta, "Liikkuu vasemmalle", pelaaja1, -nopeus); |
---|
125 | Keyboard.Listen(Key.Right, ButtonState.Down, liikuta, "Liikkuu oikealle", pelaaja1, nopeus); |
---|
126 | Keyboard.Listen(Key.Up, ButtonState.Pressed, hyppaa, "Hyppää", pelaaja1, hyppyVoima); |
---|
127 | Keyboard.Listen(Key.Down, ButtonState.Pressed, iske1, "hit"); |
---|
128 | |
---|
129 | |
---|
130 | |
---|
131 | |
---|
132 | Keyboard.Listen(Key.A, ButtonState.Down, liikuta, "Liikkuu vasemmalle", pelaaja2, -nopeus); |
---|
133 | Keyboard.Listen(Key.D, ButtonState.Down, liikuta, "Liikkuu oikealle", pelaaja2, nopeus); |
---|
134 | Keyboard.Listen(Key.W, ButtonState.Pressed, hyppaa, "Hyppää", pelaaja2, hyppyVoima); |
---|
135 | Keyboard.Listen(Key.S, ButtonState.Pressed, iske2, "hit"); |
---|
136 | |
---|
137 | Keyboard.Listen(Key.Enter, ButtonState.Pressed, seuraavaKentta , "restart"); |
---|
138 | } |
---|
139 | |
---|
140 | void liikuta(PlatformCharacter hahmo, double nopeus) |
---|
141 | { |
---|
142 | hahmo.Walk(nopeus); |
---|
143 | } |
---|
144 | |
---|
145 | void hyppaa(PlatformCharacter hahmo, double voima) |
---|
146 | { |
---|
147 | hahmo.Jump(voima); |
---|
148 | PlaySound("power beam"); |
---|
149 | |
---|
150 | |
---|
151 | } |
---|
152 | |
---|
153 | void kasitteletormays(PhysicsObject pelaaja, PhysicsObject kohde) |
---|
154 | { |
---|
155 | if (kohde.Tag.ToString() == "reuna") |
---|
156 | { |
---|
157 | pelaaja.Destroy(); |
---|
158 | if (pelaaja.Equals(pelaaja1)) |
---|
159 | { |
---|
160 | MessageDisplay.Add("turkoosi voitti"); |
---|
161 | MessageDisplay.Add("enter = uusi peli"); |
---|
162 | return; |
---|
163 | } |
---|
164 | MessageDisplay.Add("punainen voitti"); |
---|
165 | MessageDisplay.Add("enter = uusi peli"); |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | void iske1() |
---|
170 | { |
---|
171 | Vector aba = new Vector(pelaaja2.X - pelaaja1.X, pelaaja2.Y - pelaaja1.Y); |
---|
172 | if (aba.Magnitude<75) |
---|
173 | { |
---|
174 | isku1.Value +=RandomGen.NextInt(1,10); |
---|
175 | pelaaja2.Hit(new Vector(aba.X * isku1, aba.Y * isku1)); |
---|
176 | PlaySound("plzbmfire"); |
---|
177 | } |
---|
178 | |
---|
179 | } |
---|
180 | void iske2() |
---|
181 | { |
---|
182 | |
---|
183 | Vector aba = new Vector(pelaaja1.X - pelaaja2.X, pelaaja1.Y - pelaaja2.Y); |
---|
184 | if (aba.Magnitude < 75) |
---|
185 | { |
---|
186 | isku2.Value += RandomGen.NextInt(1, 10); |
---|
187 | pelaaja1.Hit(new Vector(aba.X * isku2, aba.Y * isku2)); |
---|
188 | PlaySound("icebfire"); |
---|
189 | } |
---|
190 | |
---|
191 | } |
---|
192 | void HeitaKranaatti(Timer sender) |
---|
193 | { |
---|
194 | for (int i = 0; i < RandomGen.NextInt(1, 100); i++) |
---|
195 | { |
---|
196 | |
---|
197 | |
---|
198 | Grenade kranaatti = new Grenade(RandomGen.NextDouble(0.5,4)); |
---|
199 | kranaatti.X = RandomGen.NextDouble(Level.Left, Level.Right); |
---|
200 | kranaatti.Y = Level.Top - 10; |
---|
201 | kranaatti.FuseTime = TimeSpan.FromSeconds(RandomGen.NextDouble(0,3.0)); |
---|
202 | kranaatti.ExplosionForce = 2500; |
---|
203 | kranaatti.ExplosionRadius = 50; |
---|
204 | kranaatti.ExplosionSpeed = 100; |
---|
205 | |
---|
206 | Add(kranaatti); |
---|
207 | Vector heittoVoima = Vector.FromLengthAndAngle(RandomGen.NextDouble(5000, 10000), Angle.Degrees(RandomGen.NextDouble(180, 360))); |
---|
208 | kranaatti.Hit(heittoVoima); |
---|
209 | } |
---|
210 | |
---|
211 | |
---|
212 | } |
---|
213 | |
---|
214 | |
---|
215 | |
---|
216 | } |
---|