⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gameclass.cs

📁 Beginning C# Game Programming 的源代码
💻 CS
📖 第 1 页 / 共 2 页
字号:
				case MessageType.Add2ToScore: {
					rea.Message.ReceiveData.Dispose();
					ship.Score += 2;
					break;
				}
				case MessageType.GamePaused: {
					rea.Message.ReceiveData.Dispose();
					gameState = GameStates.Paused;
					break;
				}
				case MessageType.GameRunning: {
					rea.Message.ReceiveData.Dispose();
					if (gameState == GameStates.Paused)
						gameState = GameStates.Running;
					break;
				}
			}
		}

		public void SendMyPlayerUpdate() {

			PlayerUpdate update = new PlayerUpdate();
			update.ShipPosition		= ship.Position;
			update.Outline			= ship.Outline;
			update.ShipVelocity		= ship.Velocity;
			update.State			= ship.State;
			update.WaitCount		= ship.WaitCount;
			update.DeathCount		= ship.DeathCount;
			update.FlameIndex		= ship.FlameIndex;
			update.Sounds			= (int)ship.Sounds;
			update.Score			= ship.Score;

			ShotUpdate shotUpdate = new ShotUpdate();
			shotUpdate.ShotAge = new int[Constants.NumShots];
			shotUpdate.ShotPosition = new Vector2[Constants.NumShots];

			Shot[] shotArray = ship.ShotHandler.GetShotArray();
			for (int i = 0; i < Constants.NumShots; i++) {
				shotUpdate.ShotPosition[i] = shotArray[i].Position;
				shotUpdate.ShotAge[i] = shotArray[i].Age;
			}
			netPeer.SendPlayerUpdate(update, shotUpdate);
		}

		public void SendGameParamUpdate() {
			if ((null == netPeer) || (otherPlayers.Count < 1))
				return;
			GameParamUpdate update = new GameParamUpdate();
			update.Gravity = gravity;
			update.GameSpeed = gameSpeed;
			if (bounceBack)
				update.BounceBack = 1;
			else
				update.BounceBack = 0;

			if (inverseGravity)
				update.InverseGravity = 1;
			else
				update.InverseGravity = 0;

			if (blackHole)
				update.BlackHole = 1;
			else
				update.BlackHole = 0;

			update.WindowSize = windowBounds.Size;

			netPeer.SendGameParamUpdate(update);
		}

		public void SendPointToAllPlayers() {
			netPeer.SendScorePointToAll();
		}

		public void SendTwoPointsToPlayer(int player) {
			netPeer.SendTwoPointsToPlayer(player);
		}


		#endregion //Network


		#region Input and Audio
		private void HandleKeys() {
			KeyboardState keyboardState = input.GetKBState();

			
			if (null == keyboardState)
				return;

			if (keyboardState[Key.LeftArrow])
				ship.RotateLeft();
			if (keyboardState[Key.RightArrow])
				ship.RotateRight();
			
			ship.SetThrust(keyboardState[Key.UpArrow]);

			if (keyboardState[Key.LeftControl]  || keyboardState[Key.RightControl])
				ship.Shoot();

			if (keyboardState[Key.Space])
				ship.EnterHyper();

			// Game configuration / Pause key.  The configuration controls are disabled if we are connected to another host.
			if (keyboardState[Key.F2]) {
				Pause();

				if (!netPeer.InSession || netPeer.IsHost)
					gameSettings.ControlsEnabled = true;
				else
					gameSettings.ControlsEnabled = false;

				gameSettings.Show();
			}

			// Sound keys
			if (keyboardState[Key.F5]) {
				ship.Sounds |= Sounds.Taunt;
			}

			if (keyboardState[Key.F6]) {
				ship.Sounds |= Sounds.Dude1;
			}

			if (keyboardState[Key.F7]) {
				ship.Sounds |= Sounds.Dude2;
			}

			if (keyboardState[Key.F8]) {
				ship.Sounds |= Sounds.Dude3;
			}

			if (keyboardState[Key.F9]) {
				ship.Sounds |= Sounds.Dude4;
			}

			if (keyboardState[Key.F10]) {
				ship.Sounds |= Sounds.Dude5;
			}	

			//Exit if escape is pressed
			if (keyboardState[Key.Escape]) {
				End();				
			}
			
		}

		private void PlaySounds(Sounds otherShipSounds) {
			if (soundHandler != null) {
				Sounds soundsToPlay = ship.Sounds;

				if ((otherShipSounds & Sounds.ShipExplode) != 0)
					soundsToPlay |= Sounds.OtherShipExplode;

				if ((otherShipSounds & Sounds.ShipFire) != 0)
					soundsToPlay |= Sounds.OtherShipFire;

				if ((otherShipSounds & Sounds.ShipThrust) != 0)
					soundsToPlay |= Sounds.OtherShipThrust;

				if ((otherShipSounds & Sounds.ShipAppear) != 0)
					soundsToPlay |= Sounds.OtherShipAppear;

				Sounds directMap =
					Sounds.ShipHyper |
					Sounds.Taunt |
					Sounds.Dude1 |
					Sounds.Dude2 |
					Sounds.Dude3 |
					Sounds.Dude4 |
					Sounds.Dude5;

				Sounds soundsToCopy = otherShipSounds & directMap;

				soundsToPlay |= soundsToCopy;

				soundHandler.Play(soundsToPlay);
			}
		}

		#endregion Input and Audio


		#region Main gameloop and rendering
		public void WriteScores() {
			Point location = new Point(Constants.ScoreXOrigin + windowBounds.X, 
				Constants.ScoreYOrigin + windowBounds.Y);
			Point scoreLocation = location;
			scoreLocation.X += Constants.ScoreOffset;

			ship.DrawHostName(surfaceSecondary, location, 0xFFFFFF);	// this ship is white...

			Word word = GetWordFromScore(ship.Score.ToString());
			int color = 0xFFFFFF;
			word.Draw(surfaceSecondary, color, Constants.LetterSpacing, scoreLocation);

			int shipIndex = 0;
			lock (otherPlayers) {
				foreach (RemotePlayer player in otherPlayers.Values) {
					if (!player.Active)
						continue;

					location.Y += Constants.ScoreVerticalSpacing;
					scoreLocation.Y += Constants.ScoreVerticalSpacing;
					player.Ship.DrawHostName(surfaceSecondary, location, shipColors[shipIndex].ToArgb());
					word = GetWordFromScore(player.Ship.Score.ToString());
					word.Draw(surfaceSecondary, shipColors[shipIndex].ToArgb(), Constants.LetterSpacing, scoreLocation);

					shipIndex = (shipIndex + 1) % shipColors.Length;
				}
			}
		}

		Word GetWordFromScore(string score) {
			Word word;

			word = (Word) wordCache[score];
			if (word != null)
				return word;

			word = new Word(score, Constants.LetterSize);
			wordCache.Add(score, word);
			return word;
		}

		public void MainLoop() {
			float minFrameTime = gameSettings.GameSpeed * 0.005f;

			if (lastFrameTime < minFrameTime) {
				lastFrameTime += DXUtil.Timer(DirectXTimer.GetElapsedTime);
				return;
			}
			lastFrameTime = 0.0f;
			try {				
				if (gameState == GameStates.Paused) {
					Word paused = new Word("PAUSED", Constants.LetterSize * 1.5f);
					paused.Draw(surfaceSecondary, Color.White.ToArgb(), 
						Constants.LetterSpacing * 2, 
						new Point(windowBounds.Left + 50, windowBounds.Top + 50));
					surfacePrimary.Draw(surfaceSecondary, DrawFlags.DoNotWait);
				}
				// Clear the ship's sound flags
				ship.Sounds = (Sounds) 0;
				
				// process input
				HandleKeys();

				
				if (gameState != GameStates.Running) return;

				surfaceSecondary.ColorFill(Color.Black);
				surfaceSecondary.DrawWidth = 1;
				
				// update my position, and tell others about it...
				ship.UpdatePosition();

				// update my state, and draw myself...
				ship.UpdateState();

				//If there are other players, send them our ship info
				if (netPeer.InSession  && otherPlayers.Count > 0)
					SendMyPlayerUpdate();

				WriteScores();
				stars.Draw(surfaceSecondary);

				int shipColor = Color.White.ToArgb();
				int shotColor = Color.White.ToArgb();
				ship.Draw(surfaceSecondary, shipColor, shotColor);

				// Handle other ships
				// walk through all other players. For each player
				// 1) draw the ship
				// 2) check to see whether the other ship has killed us
				// 3) figure the score
				// 4) see if we need to time-out this ship
				int shipIndex = 0;
				Sounds otherShipSounds = (Sounds) 0;
				DateTime now = DateTime.Now;
				lock (otherPlayers) {
					foreach (RemotePlayer player in otherPlayers.Values) {
						if (!player.Active)
							continue;

						player.Ship.Draw(surfaceSecondary, shipColors[shipIndex].ToArgb(), shotColor);
						shipIndex = (shipIndex + 1) % shipColors.Length;
						ship.TestShip(player);
						otherShipSounds |= player.Ship.Sounds;

						// if we haven't gotten an update in a while,
						// mark the player as inactive...
						TimeSpan delta = now - player.UpdateTime;
						if (delta.Seconds > Constants.RemoteTickTimeout) {
							player.Active = false;
						}
					}
				}

				// Draw the sun only if the "Black Hole" option isn't enabled
				if (!blackHole)
					sun.Draw(surfaceSecondary);

				surfacePrimary.Draw(surfaceSecondary, DrawFlags.DoNotWait);
				PlaySounds(otherShipSounds);
			}

			catch(SurfaceLostException) {
				// The surface can be lost if power saving
				// mode kicks in, or any other number of reasons.
				CreateSurfaces();
			}
		}

		#endregion


		#region Cleanup

		public void End() {
			gameState = GameStates.Exiting;
			mainClass.EndGame();
		}

		#endregion //cleanup
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -