Changeset 6514 for 2015/27/KarriK/Pong/Pong/Pong/Pong.cs
- Timestamp:
- 2015-06-29 14:26:41 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
2015/27/KarriK/Pong/Pong/Pong/Pong.cs
r6498 r6514 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; 15 16 PhysicsObject maila1; 17 PhysicsObject maila2; 18 19 PhysicsObject vasenReuna; 20 PhysicsObject oikeaReuna; 21 22 IntMeter pelaajan1Pisteet; 23 IntMeter pelaajan2Pisteet; 24 25 const double PALLON_MIN_NOPEUS = 500; 12 26 13 27 public override void Begin() … … 17 31 LuoKentta(); 18 32 AsetaOhjaimet(); 33 LisaaLaskurit(); 19 34 AloitaPeli(); 20 35 … … 32 47 pallo.Y = 0.0; 33 48 pallo.Restitution = 1.0; 49 pallo.Color = Color.Orange; 50 vasenReuna = Level.CreateLeftBorder(); 51 vasenReuna.Restitution = 1.0; 52 vasenReuna.IsVisible = false; 34 53 35 LuoMaila(Level.Left + 20.0, 0.0); 36 LuoMaila(Level.Right - 20.0, 0.0); 54 oikeaReuna = Level.CreateRightBorder(); 55 oikeaReuna.Restitution = 1.0; 56 oikeaReuna.IsVisible = false; 37 57 38 Level.CreateBorders(1.0, false); 58 PhysicsObject ylaReuna = Level.CreateTopBorder(); 59 ylaReuna.Restitution = 1.0; 60 ylaReuna.IsVisible = false; 39 61 40 Level.Background.Color = Color.Black; 62 PhysicsObject alaReuna = Level.CreateBottomBorder(); 63 alaReuna.Restitution = 1.0; 64 alaReuna.IsVisible = false; 65 66 maila1 = LuoMaila(Level.Left + 20.0, 0.0); 67 maila2 = LuoMaila(Level.Right - 20.0, 0.0); 68 69 70 71 Level.Background.Color = Color.Blue; 72 41 73 Camera.ZoomToLevel(); 74 AddCollisionHandler(pallo, KasittelePallonTormays); 75 42 76 } 43 77 … … 46 80 Vector impulssi = new Vector(500.0, 0.0); 47 81 pallo.Hit(impulssi); 48 } 82 } 49 83 50 voidLuoMaila(double x, double y)84 PhysicsObject LuoMaila(double x, double y) 51 85 { 52 86 PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); … … 56 90 maila.Restitution = 1.0; 57 91 Add(maila); 92 return maila; 58 93 59 94 60 95 } 61 96 void AsetaOhjaimet() 62 97 { 63 Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");64 Keyboard.Listen(Key.A, ButtonState.Down, LiikutaMaila1Ylos, "Pelaaja 1: Liikuta mailaa ylös");65 98 66 Keyboard.Listen(Key.A, ButtonState.Released, PysaytaMaila1, null); 99 Keyboard.Listen(Key.W, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); 100 Keyboard.Listen(Key.W, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 101 Keyboard.Listen(Key.S, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); 102 Keyboard.Listen(Key.S, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 103 104 Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 105 Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); 106 Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 107 Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas); 108 Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 109 Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); 67 110 } 111 void AsetaNopeus(PhysicsObject maila, Vector nopeus) 112 { 113 if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) 114 { 115 maila.Velocity = Vector.Zero; 116 return; 117 } 118 if ((nopeus.Y > 0) && (maila.Top > Level.Top)) 119 { 120 maila.Velocity = Vector.Zero; 121 122 return; 123 } 124 maila.Velocity = nopeus; 125 126 } 127 void LisaaLaskurit() 128 { 129 pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); 130 pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); 131 } 132 IntMeter LuoPisteLaskuri(double x, double y) 133 { 134 IntMeter laskuri = new IntMeter(0); 135 laskuri.MaxValue = 25; 136 Label naytto = new Label(); 137 naytto.BindTo(laskuri); 138 naytto.X = x; 139 naytto.Y = y; 140 naytto.TextColor = Color.White; 141 naytto.BorderColor = Level.Background.Color; 142 naytto.Color = Level.Background.Color; 143 Add(naytto); 144 return laskuri; 145 } 146 void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) 147 { 148 if (kohde == oikeaReuna) 149 { 150 pelaajan1Pisteet.Value += 1; 151 } 152 else if (kohde == vasenReuna) 153 { 154 pelaajan2Pisteet.Value += 1; 155 } 156 157 158 } 159 protected override void Update(Time time) 160 { 161 if (pallo != null && Math.Abs(pallo.Velocity.X) < PALLON_MIN_NOPEUS) 162 { 163 pallo.Velocity = new Vector(pallo.Velocity.X * 1.1, pallo.Velocity.Y); 164 } 165 base.Update(time); 166 } 167 168 68 169 }
Note: See TracChangeset
for help on using the changeset viewer.