source: 2016/30/LeoM/Pong/Pong/Pong/Pong.cs @ 8100

Revision 8100, 3.7 KB checked in by jotapoti, 7 years ago (diff)

pallo ja mailat liikkuu reunat on mutta puuttuu pistelasku

Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8
9public class Pong : PhysicsGame
10
11{
12    Vector nopeusYlos = new Vector(0, 1500);
13    Vector nopeusAlas = new Vector(0, -1500);
14
15    PhysicsObject pallo;
16    PhysicsObject maila1;
17    PhysicsObject maila2;
18    IntMeter pelaajan1Pisteet;
19    IntMeter pelaajan2Pisteet;
20
21    public override void Begin()
22    {
23
24        Luokentta();
25        AsetaOhjaimet();
26        LisaaLaskurit();
27        AloitaPeli();
28       
29
30
31
32    }
33    void Luokentta()
34    {
35        pallo = new PhysicsObject(60.0, 60.0);
36        pallo.Shape = Shape.Circle;
37        pallo.X = -200.0;
38        pallo.Y = 0.0;
39        Add(pallo);
40        pallo.Restitution = 1.0;
41        maila1 = LuoMaila(Level.Left + 20.0, 0.0);
42        maila2 = LuoMaila(Level.Right - 20.0, 0.0);
43        Level.CreateBorders(1.0, true);
44
45       
46
47        Level.Background.Color = Color.HotPink;
48        Camera.ZoomToLevel();
49    }
50    void AsetaOhjaimet()
51    {
52
53        Keyboard.Listen(Key.W, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos);
54        Keyboard.Listen(Key.W, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero);
55        Keyboard.Listen(Key.S, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas);
56        Keyboard.Listen(Key.S, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero);
57
58
59
60        Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos);
61        Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero);
62        Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas);
63        Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero);
64
65
66
67        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet");
68
69        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
70    }
71    void AloitaPeli()
72    {
73        Vector impulssi = new Vector (2000.0, 0.0);
74        pallo.Hit(impulssi);
75    }
76
77    PhysicsObject LuoMaila(double x, double y)
78    {
79        PhysicsObject maila = PhysicsObject.CreateStaticObject(2.0, 100.0);
80        maila.Shape = Shape.Rectangle;
81
82        maila.X = x;
83        maila.Y = y;
84
85        maila.Restitution = 1.0;
86        Add(maila);
87        return maila;
88    }
89    void AsetaNopeus(PhysicsObject maila, Vector nopeus)
90    {
91        if ((nopeus.Y > 0) && (maila.Top > Level.Top))
92
93
94        {
95            maila.Velocity = Vector.Zero;
96            return;
97        }
98        if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom))
99        {
100            maila.Velocity = Vector.Zero;
101            return;
102        }
103
104
105        maila.Velocity = nopeus;
106    }
107    void LisaaLaskurit()
108    {
109        pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0);
110        pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0);
111    }
112
113    IntMeter LuoPisteLaskuri(double x, double y)
114    {
115        IntMeter laskuri = new IntMeter(0);
116        laskuri.MaxValue = 10;
117        Label naytto = new Label();
118        naytto.BindTo(laskuri);
119        naytto.X = x;
120        naytto.Y = y;
121        naytto.TextColor = Color.White;
122        naytto.BorderColor = Level.Background.Color;
123        naytto.Color = Level.Background.Color;
124        Add(naytto);
125
126        return laskuri;
127    }
128    void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde)
129    {
130
131    }
132
133
134
135
136
137
138}
Note: See TracBrowser for help on using the repository browser.