source: 2010/23/kajysail/Ping Pong/Peli.cs @ 724

Revision 724, 5.3 KB checked in by kajysail, 13 years ago (diff)

Lisäsin animaation ilmapalloon.

Line 
1using System;
2using Jypeli;
3using Jypeli.ScreenObjects;
4using Jypeli.Assets;
5
6namespace Ping_Pong
7{
8    public class Peli : PhysicsGame
9    {
10        PhysicsObject pallo;
11        PhysicsObject pelihahmo;
12        Timer ajastin;
13        Timer aikalaskuri;
14        ValueDisplay aikanaytto;       
15               
16        protected override void Begin()
17        {
18           
19           
20            Alku();
21           
22        }
23        void Alku()
24        {
25            LuoPallo();
26            LuoPeliHahmo();
27            AsetaOhjaimet();
28           
29
30            AddCollisionHandler(pelihahmo, KasitteleOsumama);
31
32            Level.Background.Image = LoadImage("tausta2");
33            Level.CreateBorders( 1.03, false);
34
35            ajastin = new Timer();
36            ajastin.Interval = 10;
37            ajastin.Trigger += Ajastinapu;
38
39            Add(ajastin);
40            ajastin.Reset();
41           
42
43            aikalaskuri = new Timer();
44            Add(aikalaskuri);
45            aikalaskuri.Start();
46
47            ajastin.Start();
48
49            aikanaytto = new ValueDisplay();
50            aikanaytto.Text = "Aikaa kulunut: ";
51            aikanaytto.FormatDouble(1, true);
52            aikanaytto.BindTo(aikalaskuri.SecondCounter);
53            aikanaytto.X = -500;
54            aikanaytto.Y = 420;
55           
56            Add(aikanaytto);
57
58           
59
60            Camera.ZoomToLevel(160);
61            MessageDisplay.TextColor = Color.Red;
62           
63        }
64
65        void Ajastinapu(Timer sender)
66        {
67            LuoPallo();
68        }
69
70        void LuoPeliHahmo()
71        {
72            pelihahmo = new PhysicsObject(50.0, 50.0);
73            pelihahmo.Shape = Shapes.Rectangle;
74            pelihahmo.Image = LoadImage("Ilmapallo");
75            Image[] IlmapalloKuvat = LoadImages("Ilmapallo", "Ilmapallo2", "Ilmapallo3","Ilmapallo2");
76            pelihahmo.Animation = new Animation(IlmapalloKuvat);
77            pelihahmo.Animation.FPS = 8;
78            Add(pelihahmo);
79            pelihahmo.Animation.Start();
80
81        }
82
83        void LuoPallo()
84        {
85            pallo = new PhysicsObject(100.0, 100.0);
86            pallo.Shape = Shapes.Circle;
87            pallo.X = -350;
88            pallo.Y = 390;
89            pallo.Restitution = 1.03;
90            pallo.Image = LoadImage("piikkipallo2");           
91
92            Add(pallo);
93           
94
95            Vector impulssi = new Vector(400.0, 300.0);
96            pallo.Hit(impulssi);
97        }
98
99        void AsetaOhjaimet()
100        {
101            Keyboard.Listen(Key.Up, ButtonState.Down, LiikutaPelihahmoYlos, "Pelaaja 2: Liikuta pelihahmoa ylös", pelihahmo);
102            Keyboard.Listen(Key.Up, ButtonState.Released, PysaytaPelihahmo, null, pelihahmo);
103            Keyboard.Listen(Key.Down, ButtonState.Down, LiikutaPelihahmoAlas, "Pelaaja 1: Liikuta pelihahmoa alas", pelihahmo);
104            Keyboard.Listen(Key.Down, ButtonState.Released, PysaytaPelihahmo, null, pelihahmo);
105            Keyboard.Listen(Key.Right, ButtonState.Down, LiikutaPelihahmoOikea, "Pelaaja 1: Liikuta pelihahmoa oikealle", pelihahmo);
106            Keyboard.Listen(Key.Right, ButtonState.Released, PysaytaPelihahmo, null, pelihahmo);
107            Keyboard.Listen(Key.Left, ButtonState.Down, LiikutaPelihahmoVasen, "Pelaaja 1: Liikuta pelihahmoa vasemmalle", pelihahmo);
108            Keyboard.Listen(Key.Left, ButtonState.Released, PysaytaPelihahmo, null, pelihahmo);
109         
110            Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu");
111            Keyboard.Listen(Key.U, ButtonState.Pressed, Lopeta, "Uusi peli");
112        }
113
114        void LiikutaPelihahmoYlos(PhysicsObject pelihahmo)
115        {
116            Vector nopeus = new Vector(0, 500);
117            pelihahmo.Velocity = nopeus;
118        }
119
120        void LiikutaPelihahmoAlas(PhysicsObject pelihahmo)
121        {
122            Vector nopeus = new Vector(0, -500);
123            pelihahmo.Velocity = nopeus;
124        }
125
126        void LiikutaPelihahmoVasen(PhysicsObject pelihahmo)
127        {
128            Vector nopeus = new Vector(-500, 0);
129            pelihahmo.Velocity = nopeus;
130        }
131
132        void LiikutaPelihahmoOikea(PhysicsObject pelihahmo)
133        {
134            Vector nopeus = new Vector(500, 0);
135            pelihahmo.Velocity = nopeus;
136        }
137
138        void PysaytaPelihahmo(PhysicsObject pelihahmo)
139        {
140            pelihahmo.Velocity = Vector.Zero;
141        }
142
143        void KasitteleOsumama(PhysicsObject osuja, PhysicsObject osuttu)
144        {
145            if (osuja == pelihahmo)
146            {
147                MessageDisplay.Clear();
148                MessageDisplay.X = -300;
149                MessageDisplay.Y = 20;             
150                MessageDisplay.Add("Hävisit! aloita uusi peli painamalla U:ta.");             
151                aikalaskuri.Pause();                           
152            }
153            Explosion rajahdys = new Explosion(100);
154            rajahdys.Position = osuja.Position;
155            Add(rajahdys);
156            osuja.Destroy();           
157        }
158
159       
160
161       
162        void Lopeta()
163        {                       
164            ClearAll();
165            Alku();                                                           
166        }
167    }
168}
Note: See TracBrowser for help on using the repository browser.