source: 2013/26/AleksiP/pingpong/pingpong/pingpong/pingpong.cs @ 4220

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