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