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