source: 2017/27/LottaH/Bong/Bong/Bong/Bong.cs @ 10337

Revision 8890, 4.7 KB checked in by npo17_48, 6 years ago (diff)
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    Vector nopeusYlos = new Vector(0, 200);
12    Vector nopeusAlas = new Vector(0, -200);
13
14    PhysicsObject pallo;
15    PhysicsObject maila1;
16    PhysicsObject maila2;
17
18    PhysicsObject vasenReuna;
19    PhysicsObject oikeaReuna;
20
21    IntMeter pelaajan1Pisteet;
22    IntMeter pelaajan2Pisteet;
23
24
25   
26    public override void Begin()
27    {
28        LuoKentta();
29        AsetaOhjaimet();
30        LisaaLaskurit();
31        AloitaPeli();
32
33
34
35
36
37
38
39    }
40
41    void LuoKentta()
42    {
43        pallo = new PhysicsObject(40.0, 40.0);
44        pallo.Shape = Shape.Circle;
45        pallo.Color = Color.BrightGreen;
46        pallo.X = -200.0;
47        pallo.Y = 0.0;
48        pallo.Restitution = 1.0;
49        Add(pallo);
50
51        AddCollisionHandler(pallo, KasittelePallonTormays);
52
53        maila1 = LuoMaila(Level.Left + 20.0, 0.0);
54        maila2 = LuoMaila(Level.Right - 20.0, 0.0);
55
56       vasenReuna = Level.CreateLeftBorder();
57        vasenReuna.Restitution = 1.0;
58        vasenReuna.KineticFriction = 0.0;
59        vasenReuna.IsVisible = false;
60
61         oikeaReuna = Level.CreateRightBorder();
62        oikeaReuna.Restitution = 1.0;
63        oikeaReuna.KineticFriction = 0.0;
64        oikeaReuna.IsVisible = false;
65
66        PhysicsObject ylaReuna = Level.CreateTopBorder();
67        ylaReuna.Restitution = 1.0;
68        ylaReuna.KineticFriction = 0.0;
69        ylaReuna.IsVisible = false;
70
71        PhysicsObject alaReuna = Level.CreateBottomBorder();
72        alaReuna.Restitution = 1.0;
73        alaReuna.IsVisible = false;
74        alaReuna.KineticFriction = 0.0;
75
76        Level.Background.Color = Color.Black;
77
78        Camera.ZoomToLevel();
79
80    }
81
82    PhysicsObject LuoMaila(double x, double y)
83    {
84        PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0);
85        maila.Shape = Shape.Rectangle;
86        maila.Color = Color.BrownGreen;
87        maila.X = x;
88        maila.Y = y;
89        maila.Restitution = 1.0;
90        Add(maila);
91        return maila;
92    }
93   
94 
95
96    IntMeter LuoPisteLaskuri(double x, double y)
97    {
98
99            IntMeter laskuri = new IntMeter(0);
100            laskuri.MaxValue = 10;
101
102            Label naytto = new Label();
103            naytto.BindTo(laskuri);
104            naytto.X = x;
105            naytto.Y = y;
106            naytto.TextColor = Color.White;
107            naytto.BorderColor = Level.Background.Color;
108            naytto.Color = Level.Background.Color;
109            Add(naytto);
110
111        return laskuri;
112
113
114
115
116    }
117
118    void LisaaLaskurit()
119    {
120        pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0);
121        pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0);
122    }
123    void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde)
124    {
125        if (kohde == oikeaReuna)
126        {
127            pelaajan1Pisteet.Value += 1;
128        }
129        else if (kohde ==vasenReuna)
130        {
131            pelaajan2Pisteet.Value += 1;
132        }
133    }
134    void AloitaPeli()
135    {
136        Vector impulssi = new Vector(500.0, 0.0);
137        pallo.Hit(impulssi);
138    }
139    void AsetaOhjaimet()
140    {
141        Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos);
142        Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero);
143        Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas);
144        Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero);
145
146        Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos);
147        Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero);
148        Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas);
149        Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero);
150
151        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet");
152
153        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
154
155
156
157    }
158    void AsetaNopeus(PhysicsObject maila, Vector nopeus)
159    {
160        if ((nopeus.Y > 0) && (maila.Top > Level.Top))
161        {
162            maila.Velocity = Vector.Zero;
163            return;
164        }
165        if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom))
166        {
167            maila.Velocity = Vector.Zero;
168            return;
169        }
170        maila.Velocity = nopeus;
171    }
172
173}
Note: See TracBrowser for help on using the repository browser.