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