- Timestamp:
- 2016-06-27 14:56:52 (7 years ago)
- Location:
- 2016/26/JonniN/Pong8)
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
2016/26/JonniN/Pong8)/Pong8)/Pong8_/Pong8_.cs
r7515 r7529 9 9 public class Pong8_ : 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; 12 24 13 25 public override void Begin() 14 26 { 15 27 LuoKentta(); 28 AsetaOhjaimet(); 29 LisaaLaskurit(); 16 30 AloitaPeli(); 17 31 18 Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");19 32 } 20 33 void LuoKentta() 21 34 { 22 35 pallo = new PhysicsObject(40.0, 40.0); 23 pallo.Shape = Shape. Circle;36 pallo.Shape = Shape.Star; 24 37 pallo.X = -200; 25 38 pallo.Y = 0.0; 26 39 Add(pallo); 27 40 28 LuoMaila(Level.Left + 20.0, 0.0); 29 LuoMaila(Level.Right - 20.0, 0.0); 41 vasenReuna = Level.CreateLeftBorder(); 42 vasenReuna.Restitution = 1.0; 43 vasenReuna.IsVisible = false; 30 44 31 Level.CreateBorders(1.0, false); 45 oikeaReuna = Level.CreateRightBorder(); 46 oikeaReuna.Restitution = 1.0; 47 oikeaReuna.IsVisible = false; 48 49 PhysicsObject alaReuna = Level.CreateBottomBorder(); 50 alaReuna.Restitution = 1.0; 51 alaReuna.IsVisible = false; 52 PhysicsObject yläReuna = Level.CreateTopBorder(); 53 yläReuna.Restitution = 1.0; 54 yläReuna.IsVisible = false; 55 56 maila1 = LuoMaila(Level.Left + 20.0, 0.0); 57 maila2 = LuoMaila(Level.Right - 20.0, 0.0); 58 32 59 pallo.Restitution = 1.0; 33 60 Level.Background.Color = Color.Black; 34 61 pallo.Color = Color.Yellow; 62 63 AddCollisionHandler(pallo, KasittelePallonTormays); 35 64 36 65 Camera.ZoomToLevel(); … … 41 70 pallo.Hit(suunta); 42 71 } 43 voidLuoMaila(double x, double y)72 PhysicsObject LuoMaila(double x, double y) 44 73 { 45 74 PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); … … 50 79 maila.Color = Color.Magenta; 51 80 Add(maila); 81 return maila; 82 } 83 void AsetaOhjaimet() 84 { 85 Keyboard.Listen(Key.W, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); 86 Keyboard.Listen(Key.W, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 87 Keyboard.Listen(Key.S, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); 88 Keyboard.Listen(Key.S, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 89 90 Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); 91 Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 92 Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas); 93 Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 94 95 Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); 96 Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 97 } 98 void AsetaNopeus(PhysicsObject maila, Vector nopeus) 99 { 100 if ((nopeus.Y > 0) && (maila.Top > Level.Top)) 101 { 102 maila.Velocity = Vector.Zero; 103 return; 104 } 105 if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) 106 { 107 maila.Velocity = Vector.Zero; 108 return; 109 } 110 maila.Velocity = nopeus; 111 } 112 IntMeter LuoPisteLaskuri(double x, double y) 113 { 114 IntMeter laskuri = new IntMeter(0); 115 laskuri.MaxValue = 10; 116 117 Label naytto = new Label(); 118 naytto.BindTo(laskuri); 119 naytto.X = x; 120 naytto.Y = y; 121 naytto.TextColor = Color.Cyan; 122 naytto.BorderColor = Level.Background.Color; 123 naytto.Color = Level.Background.Color; 124 Add(naytto); 125 126 return laskuri; 127 } 128 void LisaaLaskurit() 129 { 130 pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); 131 pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); 52 132 53 133 } 134 void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) 135 { 136 if(kohde == oikeaReuna) 137 { 138 pelaajan1Pisteet.Value += 1; 139 } 140 else if (kohde == vasenReuna) 141 { 142 pelaajan2Pisteet.Value += 1; 143 } 144 } 54 145 }
Note: See TracChangeset
for help on using the changeset viewer.