/// <summary> /// Allows the game component to update itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> public override void Update(GameTime gameTime) { // TODO: Add your update code here KeyboardState ks = Keyboard.GetState(); //Left car if (ks.IsKeyDown(Keys.Right)) { rightcar.CarRight(); } if (ks.IsKeyDown(Keys.Left)) { rightcar.CarLeft(); } //Right car if (ks.IsKeyDown(Keys.S)) { leftcar.CarRight(); } if (ks.IsKeyDown(Keys.A)) { leftcar.CarLeft(); } //Left movement if (leftcar.Position.X < 130) { leftcar.position.X = 130; } if (leftcar.Position.X > 389) { leftcar.position.X = 389; } //Right movement if (rightcar.Position.X < 503) { rightcar.position.X = 503; } if (rightcar.Position.X > 762) { rightcar.position.X = 762; } if (start) { rightroad.speed.Y = 10; leftroad.speed.Y = 10; } //Set cars //Left if (leftc1.position.Y < -100) { leftCarRandomizer(); leftc1.tex = leftrandomcar; if (start) { leftc1.posRandomizer(); leftc1.spdRandomizer(); } } if (leftc2.position.Y < -100) { leftCarRandomizer(); leftc2.tex = leftrandomcar; if (start) { leftc2.posRandomizer(); leftc2.spdRandomizer(); } } if (leftc3.position.Y < -100) { leftCarRandomizer(); leftc3.tex = leftrandomcar; if (start) { leftc3.posRandomizer(); leftc3.spdRandomizer(); } } //Right if (rightc1.position.Y < -100) { rightCarRandomizer(); rightc1.tex = rightrandomcar; if (start) { rightc1.posRandomizer(); rightc1.spdRandomizer(); } } if (rightc2.position.Y < -100) { rightCarRandomizer(); rightc2.tex = rightrandomcar; if (start) { rightc2.posRandomizer(); rightc2.spdRandomizer(); } } if (rightc3.position.Y < -100) { rightCarRandomizer(); rightc3.tex = rightrandomcar; if (start) { rightc3.posRandomizer(); rightc3.spdRandomizer(); } } Console.WriteLine("leftcar" + leftcm.carhit); Console.WriteLine("rightcar" + rightcm.carhit); //Speed up if ((leftscorecount >= 10 && rightscorecount >= 10) && (leftcm.carhit == false && rightcm.carhit == false)) { //Roads leftroad.speed.Y = 20; rightroad.speed.Y = 20; baseCarSpd = 5; maxCarSpd = 10; speedSetter(); } if (leftcm.Hit == true) { if (leftcm.Hitnum == 1) { leftCarRandomizer(); leftc1.tex = leftrandomcar; leftcm.Hit = false; leftcm.Hitnum = 0; } if (leftcm.Hitnum == 2) { leftCarRandomizer(); leftc2.tex = leftrandomcar; leftcm.Hit = false; leftcm.Hitnum = 0; } if (leftcm.Hitnum == 3) { leftCarRandomizer(); leftc3.tex = leftrandomcar; leftcm.Hit = false; leftcm.Hitnum = 0; } if (leftcm.carhit == true) { rightroad.speed.Y = 0; leftroad.speed.Y = 0; leftcar.Visible = false; winString.Position = new Vector2(315, 230); winString.Color = Color.Red; winString.Message = "Red Wins!"; explosion.Position = leftcar.Position; explosion.start(); exploSound.Play(); gameReset(); } } if (rightcm.Hit == true) { if (rightcm.Hitnum == 1) { rightCarRandomizer(); rightc1.tex = leftrandomcar; rightcm.Hit = false; rightcm.Hitnum = 0; } if (rightcm.Hitnum == 2) { rightCarRandomizer(); rightc2.tex = leftrandomcar; rightcm.Hit = false; rightcm.Hitnum = 0; } if (rightcm.Hitnum == 3) { rightCarRandomizer(); rightc3.tex = leftrandomcar; rightcm.Hit = false; rightcm.Hitnum = 0; } if (rightcm.carhit == true) { rightroad.speed.Y = 0; leftroad.speed.Y = 0; rightcar.Visible = false; winString.Position = new Vector2(275, 230); winString.Color = Color.Green; winString.Message = "Green Wins!"; explosion.Position = rightcar.Position; explosion.start(); exploSound.Play(); gameReset(); } } if (leftcm.carhit == true || rightcm.carhit == true) { gameOverTwoPlayer.Message = "Game Over"; pressSpaceBar.Position = new Vector2(346, 270); pressSpaceBar.Message = "Press SpaceBar to play again"; } if (ks.IsKeyDown(Keys.Space)) { if (rightroad.speed.Y == 0 && leftroad.speed.Y == 0) { start = true; scoreAndMsgReset(); baseCarSpd = 1; maxCarSpd = 4; speedSetter(); pressSpaceBar.Message = ""; winString.Message = ""; } } if (ks.IsKeyDown(Keys.W)) { if (lefthornPlay == false) { lefthorn.Play(); lefthornPlay = true; } } //Horn if (lefthornPlay == true) { lefthornDelay++; if (lefthornDelay >= 140) { lefthornPlay = false; lefthornDelay = 0; } } if (ks.IsKeyDown(Keys.Up)) { if (righthornPlay == false) { righthorn.Play(); righthornPlay = true; } } if (righthornPlay == true) { righthornDelay++; if (righthornDelay >= 140) { righthornPlay = false; righthornDelay = 0; } } leftscorecount = leftc1.score + leftc2.score + leftc3.score; leftscore.Message = "Left Score\n" + leftscorecount.ToString(); rightscorecount = rightc1.score + rightc2.score + rightc3.score; rightscore.Message = "Right Score\n" + rightscorecount.ToString(); base.Update(gameTime); }
/// <summary> /// Allows the game component to update itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> public override void Update(GameTime gameTime) { // TODO: Add your update code here KeyboardState ks = Keyboard.GetState(); if (enterpressed == false) { if (ks.IsKeyDown(Keys.Enter)) { gameOverReset(); start = false; enterpressed = true; road.speed.Y = 0; roadspeedfactor = 0; car.Visible = true; } } { Console.WriteLine("mrs: " + maxroadspeed); Console.WriteLine("start: " + start); if (ks.IsKeyDown(Keys.Up)) { if (delaystart == false) { horn.Play(); delaystart = true; } } if ((car1.position.Y >= 100) && (car1.tex == policecar) && (delaystart == false)) { policesound.Play(); delaystart = true; } if ((car2.position.Y >= 100) && (car2.tex == policecar) && (delaystart == false)) { policesound.Play(); delaystart = true; } if ((car3.position.Y > 100) && (car3.tex == policecar) && (delaystart == false)) { policesound.Play(); delaystart = true; } if (delaystart) { delay++; if (delay >= 140) { delaystart = false; delay = 0; } } if (ks.IsKeyDown(Keys.Right)) { car.CarRight(); } if (ks.IsKeyDown(Keys.Left)) { car.CarLeft(); } if (car.Position.X < road.Tex.Width - 247) { car.position.X = road.Tex.Width - 247; } if (car.Position.X > road.Tex.Width + 12) { car.position.X = road.Tex.Width + 12; } //if (start) { if (car1.position.Y < -100) { CarRandomizer(); car1.tex = randomcar; if (start) { car1.posRandomizer(); car1.spdRandomizer(); } } if (car2.position.Y < -100) { CarRandomizer(); car2.tex = randomcar; if (start) { car2.posRandomizer(); car2.spdRandomizer(); } } if (car3.position.Y < -100) { CarRandomizer(); car3.tex = randomcar; if (start) { car3.posRandomizer(); car3.spdRandomizer(); } } } if (start) { if (cm.Hit == true) { if (cm.Hitnum == 1) { CarRandomizer(); car1.tex = randomcar; car1.posRandomizer(); car1.spdRandomizer(); cm.Hit = false; cm.Hitnum = 0; } if (cm.Hitnum == 2) { CarRandomizer(); car2.tex = randomcar; car2.posRandomizer(); car2.spdRandomizer(); cm.Hit = false; cm.Hitnum = 0; } if (cm.Hitnum == 3) { CarRandomizer(); car3.tex = randomcar; car3.posRandomizer(); car3.spdRandomizer(); cm.Hit = false; cm.Hitnum = 0; } } } if (road.speed.Y < maxroadspeed) { road.speed.Y += roadspeedfactor; } if (road.speed.Y >= maxroadspeed) { start = true; } if (scorecount >= 10) { maxroadspeed = 20; car1.basespeed = 5; car2.basespeed = 5; car3.basespeed = 5; car1.maxspeed = 8; car2.maxspeed = 8; car3.maxspeed = 8; } if (cm.carhit == true) { boom.Position = car.Position; Console.WriteLine("carhit " + cm.carhit); road.speed.Y = 0; maxroadspeed = 0; if (gamestarted == true) { car.Visible = false; if (enterpressed) { gameOver.Message = "GAME OVER"; pressSpaceBarOne.Message = "Press SpaceBar to play again"; } if (crash == false) { boomSound.Play(); boom.start(); crash = true; } } gameReset(); } } if (ks.IsKeyDown(Keys.Space) && road.speed.Y == 0) { start = false; cm.carhit = false; pressSpaceBarOne.Message = ""; car.Position = carpos; gamestarted = true; car.Visible = true; crash = false; gameOverReset(); roadspeedfactor = 0.01f; maxroadspeed = 10; } scorecount = car1.score + car2.score + car3.score; score.Message = "Score \n" + scorecount.ToString(); base.Update(gameTime); }