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