/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); IsMouseVisible = true; m_levelsTextPosition = new Vector2(GraphicsDevice.Viewport.Width / 1.35f, GraphicsDevice.Viewport.Height / 100); m_View = new View(GraphicsDevice, Content); m_soundEffect = Content.Load <SoundEffect>("plong1"); m_destroyedEffect = Content.Load <SoundEffect>("test"); m_waterEffect = Content.Load <SoundEffect>("watereffect"); m_BombEffect = Content.Load <SoundEffect>("BombEffect"); m_hahaEfect = Content.Load <SoundEffect>("haha"); m_SongLevel1 = Content.Load <Song>("BackgroundSong"); m_songLevel2 = Content.Load <Song>("BackgroundSong2"); m_songLevel3 = Content.Load <Song>("backgroundSong3"); m_warningSong = Content.Load <SoundEffect>("LeaveNow"); m_NrOfLifes = Content.Load <SpriteFont>("SpriteFont1"); m_LevelsFont = Content.Load <SpriteFont>("LevelsFont"); m_Camera = new Camera(GraphicsDevice.Viewport); m_MenuController = new MenuControlls(GraphicsDevice); m_MenuView = new MenuView(GraphicsDevice, Content, spriteBatch, m_MenuController); m_splitterView = new SplitterView(GraphicsDevice, Content); model = new gameModel(); model.LoadLevel(Level.Maps(CurrentLevel)); m_rainParticle = new RainParticle(Content, GraphicsDevice, 15); }
/*===============================-Creates instances of the important game classes-=========================*/ /*======================================+Awake Code+===================================*/ /* * If the no copies of game manager exist then * create a copy and set the game to running * else Destroy any un-need gameObjects */ void Awake() { if (instance == null) { instance = this; gameRunning = true; Debug.Log("Game has started"); gameModel = new gameModel(); } else { Destroy(gameObject); } }
private void handleConnection() { done = false; while (!done) { string response = DataHandler.ReadString(client); Console.WriteLine(response); string code = response.Substring(0, 2); response = response.Replace(code, ""); string[] param = response.Split(':'); switch (code) { case "03": //player ready 3:"amount" if (form.AmountConnectedLabel.InvokeRequired) { Action act = () => form.AmountConnectedLabel.Text = param[0] + "/2 players ready"; form.AmountConnectedLabel.Invoke(act); } else form.AmountConnectedLabel.Text = param[0] + "/2 players ready"; if (form.gameButton.InvokeRequired) { Action act = () => form.gameButton.Enabled = true; form.gameButton.Invoke(act); } else form.gameButton.Enabled = true; if (form.gameButton.InvokeRequired) { Action act = () => form.gameButton.Text = "Start game"; form.gameButton.Invoke(act); } else form.gameButton.Text = "Start game"; break; case "04": //start the game gameModel gameModel = new gameModel(new GameScreenView(), client); if (form.InvokeRequired) { Action hide = () => form.Hide(); Action close = () => form.Close(); form.Invoke(hide); form.Invoke(close); } else form.Hide(); if (gameModel.GameScreenView.InvokeRequired) { Action sho = () => gameModel.GameScreenView.Show(); gameModel.GameScreenView.Invoke(sho); } else gameModel.GameScreenView.Show(); done = true; gameModel.startTimers(); Application.Run(gameModel.GameScreenView); break; } } }
/// <summary> /// draw player and level. /// </summary> /// <param name="a_graphicsDevice"></param> /// <param name="a_level"></param> /// <param name="a_camera"></param> /// <param name="postion"></param> /// <param name="model"></param> public void DrawLevel(Level a_level, Camera a_camera, gameModel model) { viewportSize = new Vector2(m_graphics.Viewport.Width, m_graphics.Viewport.Height); scale = a_camera.GetScale(); m_camera = a_camera; m_model = model; m_spriteBatch.Begin(); if (Level.CurrentLevel == 1) { m_spriteBatch.Draw(m_Level1Background, new Rectangle(0, 0, (int)viewportSize.X, (int)viewportSize.Y), Color.White); } else if (Level.CurrentLevel == 2) { m_spriteBatch.Draw(m_Level2Background, new Rectangle(0, 0, (int)viewportSize.X, (int)viewportSize.Y), Color.White); } else if (Level.CurrentLevel == 3) { m_spriteBatch.Draw(m_Level3Background, new Rectangle(0, 0, (int)viewportSize.X, (int)viewportSize.Y), Color.DarkRed); } for (int x = 0; x < Level.g_levelWidth; x++) { for (int y = 0; y < Level.g_levelHeight; y++) { Vector2 viewPos = a_camera.GetViewPosition(x, y, viewportSize); //Destination rectangle in windows coordinates only scaling Rectangle destRect = new Rectangle((int)viewPos.X, (int)viewPos.Y, (int)scale, (int)scale); if (a_level.m_tiles[x, y] == TileType.BLOCKED) { m_spriteBatch.Draw(m_blockTexture, destRect, Color.White); } else if (a_level.m_tiles[x, y] == TileType.Background) { m_spriteBatch.Draw(m_backgroundTexture, destRect, Color.White); } else if (a_level.m_tiles[x, y] == TileType.Water) { m_spriteBatch.Draw(m_WaterTexture, destRect, Color.White); } } } Color color; if (model.hasCollidedWidthTheLeft()) { color = Color.Red; } else { color = Color.White; } Vector2 viewpos = a_camera.GetViewPosition(model.GetPlayerPosition().X, model.GetPlayerPosition().Y, viewportSize); destrect = new Rectangle((int)(viewpos.X - scale / 2), (int)(viewpos.Y - scale), (int)scale, (int)scale); Rectangle animationRect = new Rectangle(m_Texture.Width / 8 * (int)model.getFrame(), 0, m_Texture.Width / 8, m_Texture.Height / 2); m_spriteBatch.Draw(m_Texture, destrect, animationRect, color); m_spriteBatch.End(); }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { isSongPlayed(); MouseState m_mouse = Mouse.GetState(); //if exit button is clicked then exit the game. if (m_MenuController.isClickedToQuit) { this.Exit(); } // TODO: Add your update logic here elapased += (float)gameTime.ElapsedGameTime.TotalMilliseconds; float elapasedSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds; // Update the game when is not paused. if (paused == false) { if (CurrentLevel == 1) { //Cheack keyboard input. if (m_View.IsCharacterMovingToRight()) { model.characterMovingFasterToRight(); } else if (m_View.IsCharacterMovingToLeft()) { model.charcterMovingSlowlyToRight(); } else { model.characterAutoMovingToRight(); } } else if (CurrentLevel == 2) { //Cheack keyboard input. if (m_View.IsCharacterMovingToRight()) { model.characterMovingFasterToRight2(); } else if (m_View.IsCharacterMovingToLeft()) { model.charcterMovingSlowlyToRight2(); } else { model.characterAutoMovingToRight2(); } } else if (CurrentLevel == 3) { model.ghostMovingToRight(); //Cheack keyboard input. if (m_View.IsCharacterMovingToRight()) { model.characterMovingFasterToRight3(); model.ghostMovingFasterToRight(); } else if (m_View.IsCharacterMovingToLeft()) { model.charcterMovingSlowlyToRight3(); } else { model.characterAutoMovingToRight3(); } } if (m_View.IsCharacterJumping() && model.getHasJumped()) { model.charcterIsJumping(); m_soundEffect.Play(); } model.UpdatePlayer(elapasedSeconds); } // swith stats to take care about which button is clicked for menu. switch (CurrentGameState) { case GameState.MainMenu: if (m_MenuController.isCklickedToPlay) { CurrentGameState = GameState.Playing; paused = false; m_MenuController.isCklickedToReturn = false; m_MenuController.isClickedToCMenu = false; } if (m_MenuController.isCklickedToSeInfo) { CurrentGameState = GameState.Options; m_MenuController.isCklickedToSeInfo = true; m_MenuController.isCklickedToPlay = false; m_MenuController.isCklickedToReturn = false; } if (m_MenuController.isClickedToRePlay) { bombs.Clear(); space = 0; model.newLifes(); CurrentGameState = GameState.Playing; model.getPlayerDefaultPosition(); model.GhostDefaultPosition(); paused = false; m_MenuController.isCklickedToReturn = false; m_MenuController.isClickedToCMenu = false; } break; case GameState.Options: Menu(); break; case GameState.Playing: model.gameOver(); model.endOfTheGame(); m_rainParticle.Update(elapasedSeconds); if (CurrentLevel == 1) { if (isPlayed == false && isPlayed1 == false) { m_song = m_SongLevel1; isPlayed = true; isPlayed1 = true; isPlayed2 = false; isPlayed3 = false; } } if (CurrentLevel == 2) { if (isPlayed == false && isPlayed2 == false) { m_song = m_songLevel2; isPlayed = true; isPlayed2 = true; isPlayed1 = false; isPlayed3 = false; } Bombs(elapasedSeconds); } if (CurrentLevel == 3) { if (isPlayed == false && isPlayed3 == false) { m_song = m_songLevel3; isPlayed = true; isPlayed3 = true; isPlayed2 = false; isPlayed1 = false; } Bombs(elapasedSeconds); if (m_View.ghostRectangle().Intersects(m_View.playerRectangle())) { bombs.Clear(); m_warningSong.Play(); model.getPlayerDefaultPosition(); model.GhostDefaultPosition(); model.m_positionLagerThanTheLevel = true; } else if (model.isGameOver) { m_hahaEfect.Play(); } } if (m_MenuController.isClickedToCMenu == true) { CurrentGameState = GameState.Pause; paused = true; m_MenuController.isCklickedToPlay = false; m_MenuController.isCklickedToSeInfo = false; m_MenuController.isClickedToResume = false; m_MenuController.isClickedToCMenu = false; m_MenuController.isClickedToRePlay = false; m_MenuController.IsClickedToPlayAgain = false; } Menu(); if (model.isGameOver) { space = 0; bombs.Clear(); CurrentGameState = GameState.GameOver; m_MenuController.isCklickedToPlay = false; m_MenuController.IsClickedToPlayAgain = false; model.isGameOver = false; paused = true; } if (model.isLevelCompleted) { bombs.Clear(); space = 0; CurrentGameState = GameState.Finish; paused = true; m_MenuController.isClickedToContinue = false; } if (model.m_positionLagerThanTheLevel && model.hasCollidedWidthTheLeft() == false) { bombs.Clear(); m_waterEffect.Play(); } break; case GameState.Pause: if (m_MenuController.isClickedToResume) { CurrentGameState = GameState.Playing; paused = false; m_MenuController.isCklickedToPlay = true; m_MenuController.isClickedToResume = true; m_MenuController.isCklickedToReturn = false; m_MenuController.isClickedToCMenu = false; m_MenuController.isClickedToRePlay = false; } break; case GameState.GameOver: if (m_MenuController.IsClickedToPlayAgain || m_View.IsClickedToPlayAgain()) { model.getPlayerDefaultPosition(); model.GhostDefaultPosition(); model.isGameOver = false; paused = false; model.newLifes(); CurrentLevel = 1; model = new gameModel(); model.LoadLevel(Level.Maps(CurrentLevel)); CurrentGameState = GameState.Playing; } Menu(); break; case GameState.Finish: if (m_MenuController.isClickedToContinue) { space = 0; model.getPlayerDefaultPosition(); model.GhostDefaultPosition(); model.isLevelCompleted = false; m_MenuController.isClickedToRePlay = false; m_MenuController.IsClickedToPlayAgain = false; m_MenuController.isClickedToContinue = false; paused = true; if (CurrentLevel == 1 || CurrentLevel == 2) { model = new gameModel(); CurrentGameState = GameState.Playing; model.LoadLevel(Level.Maps(CurrentLevel += 1)); } } break; } m_MenuController.mousePosition(m_mouse); base.Update(gameTime); }