source: 2012/24/EeroF/Bong/Bong/Bong/Bong.cs @ 3003

Revision 3003, 4.4 KB checked in by eefadjuk, 11 years ago (diff)

Talletus.

Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8
9public class Bong : PhysicsGame
10{   
11
12    Vector nopeusYlos = new Vector (0, 400 );
13    Vector nopeusAlas = new Vector(0, -400);
14    PhysicsObject pelaaja;
15    PhysicsObject pelaaja2;
16
17    PhysicsObject pallo = new PhysicsObject(40, 40);
18
19    IntMeter pelaajan1Pisteet;
20    IntMeter pelaajan2Pisteet;
21    PhysicsObject vasenReuna;
22    PhysicsObject oikeaReuna;
23    public override void Begin()
24    {
25
26
27
28        LuoKentta();
29        AloitaPeli();
30        AsetaOhjaimet();
31
32
33
34
35
36
37       
38
39
40    }
41    void LuoKentta()
42    {
43       pelaaja = LuoMaila(-500, 0, Color.Violet);
44        pelaaja2 = LuoMaila(500, 0, Color.Lime);
45
46        Add(pallo);
47        pallo.Shape = Shape.Circle;
48        pallo.Color = Color.OrangeRed;
49        pallo.Restitution = 1;
50        AddCollisionHandler(pallo,tormaus);
51
52        vasenReuna = Level.CreateLeftBorder();
53        vasenReuna.Restitution = 1.0;
54        vasenReuna.IsVisible = false;
55
56        oikeaReuna = Level.CreateRightBorder();
57        oikeaReuna.Restitution = 1.0;
58        oikeaReuna.IsVisible = false;
59
60        PhysicsObject ylaReuna = Level.CreateTopBorder();
61        ylaReuna.Restitution = 1.0;
62        ylaReuna.IsVisible = false;
63
64        PhysicsObject alaReuna = Level.CreateBottomBorder();
65        alaReuna.Restitution = 1.0;
66        alaReuna.IsVisible = false;
67
68        Level.BackgroundColor = Color.Cyan;
69        Camera.ZoomToLevel();
70        LisaaLaskurit();
71
72
73    }
74   
75
76    void AloitaPeli()
77    {
78        Vector impulssi = new Vector(500, 300);
79        pallo.Hit(impulssi);
80
81
82
83
84    }
85
86    PhysicsObject LuoMaila(double x, double y, Color vari)
87    {
88        PhysicsObject pelaaja = PhysicsObject.CreateStaticObject (15, 70);
89        Add(pelaaja);
90        pelaaja.Shape = Shape.Ellipse;
91        pelaaja.Color = vari;
92        pelaaja.X = x;
93        pelaaja.Y = y;
94        pelaaja.Restitution = 1;
95        return pelaaja;
96       
97
98
99    }
100   
101    void AsetaOhjaimet()
102    {
103        Keyboard.Listen(Key.S, ButtonState.Down, AsetaNopeus, null, pelaaja, nopeusAlas);
104        Keyboard.Listen(Key.S, ButtonState.Released, AsetaNopeus, null, pelaaja, Vector.Zero);
105        Keyboard.Listen(Key.W, ButtonState.Down, AsetaNopeus, null, pelaaja, nopeusYlos);
106        Keyboard.Listen(Key.W, ButtonState.Released, AsetaNopeus, null, pelaaja, Vector.Zero);
107        Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, null, pelaaja2, nopeusYlos);
108        Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, pelaaja2, Vector.Zero);
109        Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, null, pelaaja2, nopeusAlas);
110        Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, pelaaja2, Vector.Zero);
111       
112
113        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
114    }
115
116
117    void AsetaNopeus(PhysicsObject pelaaja,Vector nopeus  )
118    {
119        pelaaja.Velocity = nopeus;
120
121        if ( (nopeus.Y > 0 ) && (pelaaja.Top > Level.Top) )
122        {
123            pelaaja.Velocity = Vector.Zero;
124            return;
125        }
126
127   
128                if ((nopeus.Y < 0) && (pelaaja.Bottom < Level.Bottom))
129        {
130                    pelaaja.Velocity = Vector.Zero;
131                    return;
132        }
133
134       }
135    void LisaaLaskurit()
136    {
137       pelaajan1Pisteet = LuoPisteLaskuri ( Screen.Left + 100.0, Screen.Top - 100.0 );
138       pelaajan2Pisteet = LuoPisteLaskuri ( Screen.Right - 100.0, Screen.Top - 100.0 );
139}
140
141
142
143   
144    IntMeter LuoPisteLaskuri (double x, double y)
145    {
146        IntMeter laskuri = new IntMeter(0);
147       
148        laskuri.MaxValue = 10;
149
150       
151        Label naytto = new Label ();
152        naytto.BindTo(laskuri);
153        naytto.X = x;
154        naytto.Y = y;
155        naytto.TextColor = Color.White;
156        naytto.BorderColor = Level.BackgroundColor;
157        naytto.Color = Level.BackgroundColor;
158        Add(naytto);
159        return laskuri;
160
161    }
162    void tormaus(PhysicsObject pallo, PhysicsObject kohde)
163    {
164        if (kohde == oikeaReuna)
165        {
166            pelaajan1Pisteet.Value += 1;
167        }
168        else if (kohde == vasenReuna)
169        {
170            pelaajan2Pisteet.Value += 1;
171        }
172
173
174
175    }
176}
177
Note: See TracBrowser for help on using the repository browser.