source: 2013/30/ViljamiV/PerusPeli4/PerusPeli4/PerusPeli4/PerusPeli4.cs @ 5545

Revision 5545, 4.6 KB checked in by koannak, 9 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 PerusPeli4 : Game
10{
11    Image shrek = LoadImage("shrek");
12    Image talo = LoadImage("talo");
13    Image aloitus = LoadImage("sreks guest");
14
15    SoundEffect ovi = LoadSoundEffect("oviaani");
16    EasyHighScore topLista = new EasyHighScore();
17
18    SoundEffect havisit = LoadSoundEffect("havisitaani");
19
20    List<int> listaVaaristaOvista = new List<int>();
21
22    int kenttaNro = 50;
23
24    IntMeter pisteLaskuri;
25
26    public override void Begin()
27    {
28        ////////////////////////////////////////////////////////////////////////////////////////////////////////SetWindowSize(1024, 768);
29        // Kirjoita ohjelmakoodisi tähän
30        IsMouseVisible = true;
31        MediaPlayer.Play("mouthi");
32
33        MediaPlayer.Volume = 0.5;
34        MediaPlayer.IsRepeating = true;
35
36        Level.Background.Image = aloitus;
37        Level.Background.ScaleToLevel();
38        Level.Background.Color = Color.Black;
39
40        Timer.SingleShot(2, LuoAlkuvalikko);
41    }
42
43
44    void LuoAlkuvalikko()
45    {
46        MultiSelectWindow alkuValikko = new MultiSelectWindow("Pelin alkuvalikko", "Aloita peli", "Parhaat pisteet", "Lopeta");
47        Add(alkuValikko);
48        alkuValikko.AddItemHandler(0, AloitaPeli);
49        alkuValikko.AddItemHandler(1, ParhaatPisteet);
50        alkuValikko.AddItemHandler(2, Exit);
51    }
52
53
54    public void AloitaPeli(Window sender)
55    {
56        Begin();
57    }
58
59
60    void AloitaPeli()
61    {
62        ClearAll();
63
64        for (int i = 0; i < kenttaNro; i++)
65        {
66            listaVaaristaOvista.Add(RandomGen.NextInt(1,4));
67        }
68
69        MediaPlayer.Play("musikki");
70        LuoPistelaskuri();
71
72        Level.Size = Screen.Size;
73       
74        Level.Background.Image = talo;
75        Level.Background.ScaleToLevel();
76        Level.Background.Color = Color.Black;
77
78        GameObject ovi = LuoOvi(6.0);
79        GameObject ovi2 = LuoOvi(35.0);
80        GameObject ovi3 = LuoOvi(8.0);
81
82        Mouse.ListenOn(ovi, MouseButton.Left, ButtonState.Pressed, delegate(GameObject o) { OviValittu(1); }, null, ovi);
83        Mouse.ListenOn(ovi2, MouseButton.Left, ButtonState.Pressed, delegate(GameObject o) { OviValittu(2); }, null, ovi);
84        Mouse.ListenOn(ovi3, MouseButton.Left, ButtonState.Pressed, delegate(GameObject o) { OviValittu(3); }, null, ovi);
85
86
87        Keyboard.Listen(Key.Escape, ButtonState.Pressed, shrexit, "Lopeta peli");
88    }
89
90
91    GameObject LuoOvi(double jakaja)
92    {
93        GameObject ovi = new GameObject(Window.ClientBounds.Width / 9.0, Window.ClientBounds.Height / 5.0);
94        ovi.X = -Window.ClientBounds.Width / jakaja;
95        ovi.Y = -50;
96        ovi.IsVisible = false;
97        Add(ovi);
98        return ovi;
99    }
100
101
102    void ParhaatPisteet()
103    {
104        topLista.Show();
105    }
106
107
108    void LuoPistelaskuri()
109    {
110        pisteLaskuri = new IntMeter(0);
111
112        Label pisteNaytto = new Label();
113        pisteNaytto.X = Screen.Left + 100;
114        pisteNaytto.Y = Screen.Top - 100;
115        pisteNaytto.TextColor = Color.Black;
116        pisteNaytto.Color = Color.White;
117        pisteNaytto.Title = "Huone";
118
119        pisteNaytto.BindTo(pisteLaskuri);
120        Add(pisteNaytto);
121    }
122
123
124    void OviValittu(int numero)
125    {
126        if (listaVaaristaOvista[pisteLaskuri.Value].Equals(numero))
127        {
128            havisit.Play();
129
130            GameObject shreks = new GameObject(Screen.Width, Screen.Height);
131            shreks.Image = shrek;
132            Add(shreks, 3);
133
134            Timer.SingleShot(3, delegate
135            {
136                topLista.EnterAndShow(pisteLaskuri.Value);
137                topLista.HighScoreWindow.Closed += PeliLoppui;
138            });
139        }
140        else
141        {
142            ovi.Play(0.8, 0.0, 0.0);
143            pisteLaskuri.Value++;
144
145            MultiSelectWindow valikko = new MultiSelectWindow("Pääsit seuraavaan huoneeseen.", "Jatka");
146            Add(valikko);
147        }
148    }
149
150
151    void PeliLoppui(Window sender)
152    {
153        shrexit();
154    }
155
156
157    void shrexit()
158    {
159        ClearAll();
160
161        MultiSelectWindow exitwd = new MultiSelectWindow("Do you want to shrexit?", "Shrek", "Drek");
162        exitwd.ItemSelected += shrelect;
163        Add(exitwd);
164    }
165
166
167    void shrelect(int selection)
168    {
169        switch (selection)
170        {
171            case 0:
172
173                Exit();
174                break;
175
176            case 1:
177                AloitaPeli();
178                break;
179        }
180    }
181
182}
Note: See TracBrowser for help on using the repository browser.