Changeset 7285
- Timestamp:
- 2016-06-14 13:45:49 (7 years ago)
- Location:
- 2016/24/HenriH
- Files:
-
- 3 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
2016/24/HenriH/FysiikkaPeli1/FysiikkaPeli1/FysiikkaPeli1/FysiikkaPeli1.cs
r7269 r7285 11 11 PhysicsObject pallo; 12 12 const double PALLON_MIN_NOPEUS = 500; 13 PhysicsObject maila1; 14 PhysicsObject maila2; 13 15 16 PhysicsObject vasenReuna; 17 PhysicsObject oikeaReuna; 18 19 IntMeter pelaajan1Pisteet; 20 IntMeter pelaajan2Pisteet; 21 22 Vector nopeusYlos = new Vector(0, 400); 23 Vector nopeusAlas = new Vector(0, -400); 14 24 protected override void Update(Time time) 15 25 { … … 27 37 LuoKentta(); 28 38 AsetaOhjaimet(); 39 LisaaLaskurit(); 29 40 AloitaPeli(); 30 41 … … 39 50 pallo.Y = 0.0; 40 51 52 AddCollisionHandler(pallo, KasittelePallonTormays); 53 41 54 maila1 = LuoMaila(Level.Left + 20.0, 0.0); 42 maila2 = LuoMaila(Level.Right - 20.0, 0.0);55 maila2 = LuoMaila(Level.Right - 20.0, 0.0); 43 56 44 57 pallo.Restitution = 1.0; 45 Level.Background.Color = Color. DarkYellowGreen;58 Level.Background.Color = Color.Yellow; 46 59 Camera.ZoomToLevel(); 47 60 Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 48 49 Level.CreateBorders(1.0, false); 61 62 vasenReuna = Level.CreateLeftBorder(); 63 vasenReuna.Restitution = 1.0; 64 vasenReuna.IsVisible = false; 65 66 oikeaReuna = Level.CreateRightBorder(); 67 oikeaReuna.Restitution = 1.0; 68 oikeaReuna.IsVisible = false; 69 70 PhysicsObject alaReuna = Level.CreateBottomBorder(); 71 alaReuna.Restitution = 1.0; 72 alaReuna.IsVisible = false; 73 74 PhysicsObject yläReuna = Level.CreateTopBorder(); 75 yläReuna.Restitution = 1.0; 76 yläReuna.IsVisible = false; 50 77 } 51 78 void AloitaPeli() … … 54 81 pallo.Hit(impulssi); 55 82 } 56 83 57 84 PhysicsObject LuoMaila(double x, double y) 58 85 { 59 86 PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); 60 87 maila.Shape = Shape.Rectangle; 61 maila.X = x;88 maila.X = x; 62 89 maila.Y = y; 63 90 maila.Restitution = 1.0; … … 67 94 void AsetaOhjaimet() 68 95 { 69 96 Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); 97 Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 70 98 99 Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); 100 Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 101 102 Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); 103 Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 104 Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas); 105 Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 106 107 Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); 71 108 Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 72 Keyboard.Listen(Key.A, ButtonState.Down, LiikutaMaila1Ylos, "Pelaaja 1: Liikuta mailaa ylös"); 73 Keyboard.Listen(Key.A, ButtonState.Released, PysaytaMaila1, null); 109 110 74 111 } 75 112 void AsetaNopeus(PhysicsObject maila, Vector nopeus) 76 113 { 114 115 if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) 116 { 117 maila.Velocity = Vector.Zero; 118 return; 119 } 120 if ((nopeus.Y > 0) && (maila.Top > Level.Top)) 121 { 122 maila.Velocity = Vector.Zero; 123 return; 124 } 125 77 126 maila.Velocity = nopeus; 127 128 129 130 if ((nopeus.Y > 0) && (maila.Top > Level.Top)) 131 { 132 maila.Velocity = Vector.Zero; 133 return; 134 } 135 maila.Velocity = nopeus; 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 IntMeter LuoPisteLaskuri(double x, double y) 144 { 145 IntMeter laskuri = new IntMeter(0); 146 laskuri.MaxValue = 10; 147 148 Label naytto = new Label(); 149 naytto.BindTo(laskuri); 150 naytto.X = x; 151 naytto.Y = y; 152 naytto.TextColor = Color.Black; 153 naytto.BorderColor = Level.Background.Color; 154 naytto.Color = Level.Background.Color; 155 Add(naytto); 156 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 } 78 169 } 79 170 } … … 82 173 83 174 84
Note: See TracChangeset
for help on using the changeset viewer.