source: 2013/30/TomiH/pong1/pong1/pong1/pong1.cs @ 4518

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