- Timestamp:
- 2010-07-06 14:57:24 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
2010/27/oteehann/Pong/Peli.cs
r1066 r1102 6 6 public class Peli : PhysicsGame 7 7 { 8 Vector nopeusYlos = new Vector(0, 200); 9 Vector nopeusAlas = new Vector(0, -200); 10 8 11 PhysicsObject pallo; 12 PhysicsObject maila1; 13 PhysicsObject maila2; 14 15 PhysicsObject vasenReuna; 16 PhysicsObject oikeaReuna; 17 18 IntMeter pelaajan1Pisteet; 19 IntMeter pelaajan2Pisteet; 20 21 22 9 23 protected override void Begin() 10 24 { 11 25 12 26 //TODO: Alusta peli tässä 13 27 14 28 LuoKentta(); 15 Vector impulssi = new Vector(700.0, 700.0); 29 AsetaOhjaimet(); 30 LisaaLaskurit(); 31 AloitaPeli(); 32 Vector impulssi = new Vector(700.0, 250.0); 16 33 pallo.Hit(impulssi); 17 34 18 35 } 19 36 20 37 void LuoKentta() 21 38 { 22 39 pallo = new PhysicsObject(40.0, 40.0); 23 40 pallo.Color = Color.White; 24 41 pallo.Shape = Shapes.Circle; … … 28 45 Level.CreateBorders(); 29 46 pallo.Restitution = 1.0; 30 Level.CreateBorders(1.0, false); 47 vasenReuna = Level.CreateLeftBorder(); 48 vasenReuna.Restitution = 1.0; 49 vasenReuna.IsVisible = false; 50 PhysicsObject alaReuna = Level.CreateBottomBorder(); 51 alaReuna.Restitution = 1.0; 52 alaReuna.IsVisible = false; 53 oikeaReuna = Level.CreateRightBorder(); 54 oikeaReuna.Restitution = 1.0; 55 oikeaReuna.IsVisible = false; 56 PhysicsObject ylaReuna = Level.CreateTopBorder(); 57 ylaReuna.Restitution = 1.0; 58 ylaReuna.IsVisible = false; 59 60 vasenReuna = Level.CreateLeftBorder(); 61 oikeaReuna = Level.CreateRightBorder(); 62 63 31 64 Level.BackgroundColor = Color.Red; 32 65 Camera.ZoomToLevel(); 33 PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); 34 maila.Shape = Shapes.Rectangle; 35 maila.X = Level.Left + 20.0; 36 maila.Y = 0.0; 37 maila.Restitution = 1.0; 38 Add(maila); 66 maila1 = LuoMaila(Level.Left + 20.0, 0.0); 67 maila2 = LuoMaila(Level.Right - 20.0, 0.0); 68 AddCollisionHandler(pallo, KasittelePallonTormays); 69 70 39 71 40 72 41 73 } 42 74 43 void AloitaPeli; 75 void AloitaPeli() 76 { 77 Vector impulssi = new Vector(500.0, 0.0); 78 pallo.Hit(impulssi); 79 80 } 81 82 PhysicsObject LuoMaila(double x, double y) 83 { 84 PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); 85 maila.Shape = Shapes.Rectangle; 86 maila.X = x; 87 maila.Y = y; 88 maila.Restitution = 1.0; 89 Add(maila); 90 return maila; 91 } 92 void AsetaOhjaimet() 93 { 94 95 Keyboard.Listen(Key.A, ButtonState.Down, Asetanopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); 96 Keyboard.Listen(Key.A, ButtonState.Released, Asetanopeus, null, maila1, Vector.Zero); 97 98 Keyboard.Listen(Key.Z, ButtonState.Down, Asetanopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); 99 100 Keyboard.Listen(Key.Z, ButtonState.Released, Asetanopeus, null, maila1, Vector.Zero); 101 102 Keyboard.Listen(Key.Up, ButtonState.Down, Asetanopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); 103 Keyboard.Listen(Key.Up, ButtonState.Released, Asetanopeus, null, maila2, Vector.Zero); 104 Keyboard.Listen(Key.Down, ButtonState.Down, Asetanopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas); 105 Keyboard.Listen(Key.Down, ButtonState.Released, Asetanopeus, null, maila2, Vector.Zero); 106 107 Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); 108 Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu"); 109 } 110 void LiikutaMailaaYlos(PhysicsObject maila) 111 { 112 if (maila.Y < Level.Top) 113 { 114 115 116 Vector nopeus = new Vector(0, 200); 117 maila.Velocity = nopeus; 118 } 119 } 120 void PysaytaMaila(PhysicsObject maila) 121 { 122 maila.Velocity = Vector.Zero; 123 } 124 void LiikutaMailaaAlas(PhysicsObject maila) 125 { 126 Vector nopeus = new Vector(0, -200); 127 maila.Velocity = nopeus; 128 129 } 130 void Asetanopeus(PhysicsObject maila, Vector nopeus) 131 { 132 if ((nopeus.Y < 0) && (maila.Y < Level.Bottom)) 133 { 134 maila.Velocity = Vector.Zero; 135 return; 136 } 137 if ((nopeus.Y > 0) && (maila.Y > Level.Top)) 138 { 139 maila.Velocity = Vector.Zero; 140 return; 141 } 142 143 maila.Velocity = nopeus; 144 145 146 } 147 void LisaaLaskurit() 148 {//... 149 pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); 150 pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); 151 } 152 153 IntMeter LuoPisteLaskuri(double x, double y) 154 { 155 IntMeter laskuri = new IntMeter(0); 156 laskuri.MaxValue = 10; 157 Label naytto = new Label(); 158 naytto.BindTo(laskuri); 159 naytto.X = x; 160 naytto.Y = y; 161 naytto.TextColor = Color.White; 162 Add(naytto); 163 164 return laskuri; 165 166 } 167 void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) 168 { 169 if (kohde == oikeaReuna) 170 { 171 pelaajan1Pisteet.Value += 1; 172 } 173 else if (kohde == vasenReuna) 174 { 175 pelaajan2Pisteet.Value += 1; 176 177 } 178 179 180 } 44 181 } 182 183 184 185 186 187 188
Note: See TracChangeset
for help on using the changeset viewer.