Changeset 1105 for 2010/27/Vekakart/Pong/Peli.cs
- Timestamp:
- 2010-07-06 14:58:03 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
2010/27/Vekakart/Pong/Peli.cs
r1072 r1105 6 6 public class Peli : PhysicsGame 7 7 { 8 Vector NopeusYlos = new Vector(0.0, 200.0); 9 Vector NopeusAlas = new Vector(0.0, -200.0); 10 8 11 PhysicsObject Pallo; 9 12 PhysicsObject Maila1; 10 13 PhysicsObject Maila2; 11 14 15 PhysicsObject oikeareuna; 16 PhysicsObject vasenreuna; 17 18 IntMeter Pelaajan1Pisteet; 19 IntMeter Pelaajan2Pisteet; 12 20 protected override void Begin() 13 21 { 14 22 LuoKentta(); 15 Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "poistu");23 AsetaOhjaimet(); 16 24 ALoitaPeli(); 25 LisaaLaskurit(); 26 } 17 27 18 19 }20 28 void LuoKentta() 21 29 { … … 26 34 Pallo.Restitution = 1.0; 27 35 Add(Pallo); 36 AddCollisionHandler(Pallo, KasittelePallonTormays); 28 37 29 38 Maila1 = LuoMaila(Level.Left + 20.0, 0.0); … … 31 40 32 41 33 34 Level.CreateBorders(1.0, false); 42 vasenreuna = Level.CreateLeftBorder (); 43 vasenreuna.Restitution = 1.0; 44 vasenreuna.IsVisible = false; 45 oikeareuna = Level.CreateRightBorder(); 46 oikeareuna.Restitution = 1.0; 47 oikeareuna.IsVisible = false; 48 PhysicsObject ylaReuna = Level.CreateTopBorder(); 49 ylaReuna.Restitution = 1.0; 50 ylaReuna.IsVisible = false; 51 PhysicsObject alaReuna = Level.CreateBottomBorder(); 52 alaReuna.Restitution = 1.0; 53 alaReuna.IsVisible = false; 54 35 55 Level.BackgroundColor = Color.Black; 36 56 37 57 Camera.ZoomToLevel(1.0); 58 } 38 59 39 40 41 }42 60 void ALoitaPeli() 43 61 { … … 45 63 Pallo.Hit (impulssi); 46 64 } 47 void AsetaOhjaimet()48 {49 Keyboard.Listen (Key.A, ButtonState.Pressed, LiikutaMailaaYlos, "Pelaaja1: Liikuta Mailaa Ylös", Maila1);50 Keyboard.Listen (Key.A, ButtonState.Released, PysaytaMaila, null);51 65 52 Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "poistu");53 }54 66 PhysicsObject LuoMaila(double x, double y) 55 67 { … … 62 74 return maila; 63 75 } 76 64 77 void LiikutaMailaaYlos(PhysicsObject maila) 65 78 { 66 Vector nopeus = new Vector(0, 200); 79 if (maila.Y >=Level.Top) 80 { 81 maila.Velocity = Vector.Zero; 82 return; 83 } 84 Vector nopeus = new Vector(0.0, 200.0); 85 maila.Velocity = nopeus; 86 } 67 87 68 88 void LiikutaMailaaAlas(PhysicsObject maila) 89 { 90 Vector nopeus = new Vector(0.0, -200.0); 91 maila.Velocity = nopeus; 69 92 } 93 70 94 void PysaytaMaila(PhysicsObject maila) 71 95 { 72 maila.Velocity = Vector.Zero; 96 maila.Velocity = Vector.Zero; 73 97 } 74 98 99 void AsetaOhjaimet() 100 { 101 Keyboard.Listen (Key.W, ButtonState.Down, AsetaNopeus, "pelaaja 1: Liikuta mailaa ylös", Maila1, NopeusYlos); 102 Keyboard.Listen(Key.W, ButtonState.Released, AsetaNopeus, null, Maila1, Vector.Zero); 103 Keyboard.Listen(Key.S, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", Maila1, NopeusAlas); 104 Keyboard.Listen(Key.S, ButtonState.Released, AsetaNopeus, null, Maila1, Vector.Zero); 105 106 Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "pelaaja 2: Liikuta mailaa ylös", Maila2, NopeusYlos); 107 Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, Maila2, Vector.Zero); 108 Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", Maila2, NopeusAlas); 109 Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, Maila2, Vector.Zero); 110 111 Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); 112 Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu"); 113 114 } 115 116 void AsetaNopeus(PhysicsObject maila, Vector nopeus) 117 { 118 if ((nopeus.Y < 0) && (maila.Y < Level.Bottom)) 119 { 120 maila.Velocity = Vector.Zero; 121 return; 122 } 123 if ((nopeus.Y > 0) && (maila.Y > Level.Top)) 124 { 125 maila.Velocity = Vector.Zero; 126 return; 127 } 128 129 maila.Velocity = nopeus; 130 131 } 132 133 void LisaaLaskurit() 134 { 135 Pelaajan1Pisteet = Luopistelaskuri(Screen.Left + 100.0, Screen.Top - 100.0); 136 Pelaajan2Pisteet = Luopistelaskuri(Screen.Right - 100.0, Screen.Top - 100.0); 137 } 138 139 IntMeter Luopistelaskuri(double x, double y) 140 { 141 IntMeter laskuri = new IntMeter(0); 142 laskuri.MaxValue = 10; 143 Label naytto = new Label (); 144 naytto.BindTo (laskuri); 145 naytto.X = x; 146 naytto.Y = y; 147 naytto.TextColor = Color.White; 148 Add(naytto); 149 return laskuri; 150 151 152 } 153 154 void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) 155 { 156 if (kohde == oikeareuna) 157 { 158 Pelaajan1Pisteet.Value += 1; 159 } 160 else if (kohde == vasenreuna) 161 { 162 Pelaajan2Pisteet.Value += 1; 163 } 164 } 165 166 167 75 168 }
Note: See TracChangeset
for help on using the changeset viewer.