/// <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); //加载背景音乐和按钮音效 music = Content.Load <Song>("Music"); click = Content.Load <SoundEffect>("Click"); //默认设置为不静音 isMuted = false; //播放背景音乐 MediaPlayer.Play(music); //设置背景音乐的大小 MediaPlayer.Volume = 0.1f; MediaPlayer.IsRepeating = true;//重复播放背景音乐 //加载鼠标纹理及坐标大小 mouseCursorTexture2D = Content.Load <Texture2D>("MouseCursor"); mouseCursorRectangle = new Rectangle(0, 0, 45, 45); currentScence = GameScence.MainMenu; // TODO: use this.Content to load your game content here //加载游戏背景图片纹理 gameBackgroundTexture2D = Content.Load <Texture2D>("GameBackground"); //游戏界面是480x800,全屏覆盖 gameBackgroundRectangle = new Rectangle(0, 0, 480, 800); //加载标题图片纹理 gameTitleTexture2D = Content.Load <Texture2D>("GameTitle"); //初始化标题位置和大小 gameTitleRectangle = new Rectangle((GameWidth - gameTitleTexture2D.Bounds.Width) / 2, 100, gameTitleTexture2D.Bounds.Width, gameTitleTexture2D.Bounds.Height); //加载开始那妞纹理 playNormalTexture2D = Content.Load <Texture2D>("PlayNormal"); //初始化开始按钮位置和大小 playNormalRectangle = new Rectangle((GameWidth - playNormalTexture2D.Bounds.Width) / 2, 280, playNormalTexture2D.Bounds.Width, playNormalTexture2D.Bounds.Height); //加载声效开关和关于按钮纹理和位置大小 soundOnTexture2D = Content.Load <Texture2D>("SoundOn"); soundOnRectangle = new Rectangle(100, 580, 75, 75); aboutTexture2D = Content.Load <Texture2D>("About"); aboutRectangle = new Rectangle(320, 580, 75, 75); //加载静音纹理 soundOffTexture2D = Content.Load <Texture2D>("SoundOff"); //加载关于字体位置和大小以及内容 aboutFont = Content.Load <SpriteFont>("AboutFont"); Vector2 aboutTextVector2 = aboutFont.MeasureString(aboutText); aboutFontPosition = new Vector2((GameWidth - (int)aboutTextVector2.X) / 2, 300); //加载返回按钮的图片纹理和位置大小 backTexture2D = Content.Load <Texture2D>("Back"); backRectangle = new Rectangle(20, 20, 50, 50); //加载图片列表 wifeTextureList = new List <Texture2D>(); for (int i = 1; i < 12; i++) { Texture2D wifeTexture = Content.Load <Texture2D>("Wife" + i); wifeTextureList.Add(wifeTexture); } //定义图片列表位置 wifeListRectangle = new Rectangle((GameWidth - 295) / 2, 100, 295, 221); //加载选老婆按钮和初始化位置大小 marryHerTexture2D = Content.Load <Texture2D>("MarryHer"); marryHerRectangle = new Rectangle((GameWidth - marryHerTexture2D.Bounds.Width) / 2, 300, marryHerTexture2D.Bounds.Width, marryHerTexture2D.Bounds.Height); //加载双喜图片纹理和初始化位置大小 doubleHappinessTexture2D = Content.Load <Texture2D>("DoubleHappiness"); doubleHappinessRectangle = new Rectangle((GameWidth - doubleHappinessTexture2D.Bounds.Width) / 2, 330, doubleHappinessTexture2D.Bounds.Width, doubleHappinessTexture2D.Bounds.Height); //加载离婚图片纹理和初始化位置大小 divorceTexture2D = Content.Load <Texture2D>("Divorce"); divorceRectangle = new Rectangle(50, 640, divorceTexture2D.Bounds.Width, divorceTexture2D.Bounds.Height); //加载主菜单图片纹理和初始化位置大小 mainMenuTexture2D = Content.Load <Texture2D>("MainMenu"); mainMenuRectangle = new Rectangle(280, 640, mainMenuTexture2D.Bounds.Width, mainMenuTexture2D.Bounds.Height); //加载结婚进行曲 weddingMarch = Content.Load <Song>("WeddingMarch"); }
/// <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) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } // TODO: Add your update logic here //获取当前鼠标状态 MouseState mouseState = Mouse.GetState(); //修改自定义鼠标位置的X,Y轴坐标为鼠标当前X,Y位置 mouseCursorRectangle.X = mouseState.X; mouseCursorRectangle.Y = mouseState.Y; //判断当前场景是否是主菜单 if (currentScence == GameScence.MainMenu) { //判断是否点击了关于按钮 if (mouseState != prevMouseState && mouseState.LeftButton != ButtonState.Released && aboutRectangle.Contains(mouseState.X, mouseState.Y)) { //将当前场景切换关于场景 currentScence = GameScence.About; //按钮点击音效 if (!isMuted) { click.Play(); } } //判断是否点击关闭音效音乐按钮 if (mouseState != prevMouseState && mouseState.LeftButton != ButtonState.Released && soundOnRectangle.Contains(mouseState.X, mouseState.Y)) { isMuted = !isMuted; MediaPlayer.IsMuted = isMuted; //按钮点击音效 if (!isMuted) { click.Play(); } } //判断是否点击了开始游戏按钮 if (mouseState != prevMouseState && mouseState.LeftButton != ButtonState.Released && playNormalRectangle.Contains(mouseState.X, mouseState.Y)) { //跳转到游戏场景 currentScence = GameScence.GamePlaying; starting = true; //按钮点击音效 if (!isMuted) { click.Play(); } } } //判断当前场景是否是游戏场景 else if (currentScence == GameScence.GamePlaying) { //判断是否点击了关于按钮 if (mouseState != prevMouseState && mouseState.LeftButton != ButtonState.Released && backRectangle.Contains(mouseState.X, mouseState.Y)) { //将当前场景切换到主菜单 currentScence = GameScence.MainMenu; //按钮点击音效 if (!isMuted) { click.Play(); } } //判断是否点击了选老婆按钮 if (mouseState != prevMouseState && mouseState.LeftButton != ButtonState.Released && marryHerRectangle.Contains(mouseState.X, mouseState.Y)) { //停止切换图片 starting = false; //切换到游戏结束页面 currentScence = GameScence.GameOver; if (!isMuted) { //停止之前的游戏音乐 MediaPlayer.Stop(); //播放结婚进行曲 MediaPlayer.Play(weddingMarch); } //按钮点击音效 if (!isMuted) { click.Play(); } } if (starting) { timeLastFrame = timeLastFrame + gameTime.ElapsedGameTime.Milliseconds;//图片切换后经过的时间(毫秒) if (timeLastFrame > timePerFame) { timeLastFrame = timeLastFrame - timePerFame;//将图片切换后经过的时间恢复到小于每秒切换时间,保证下面代码执行一次 if (currentFrame >= wifeTextureList.Count - 1) { currentFrame = 0; } else { currentFrame += 1; } } } } //判断当前场景是否是关于 else if (currentScence == GameScence.About) { //判断是否点击了关于按钮 if (mouseState != prevMouseState && mouseState.LeftButton != ButtonState.Released && backRectangle.Contains(mouseState.X, mouseState.Y)) { //将当前场景切换到主菜单 currentScence = GameScence.MainMenu; //按钮点击音效 if (!isMuted) { click.Play(); } } } else if (currentScence == GameScence.GameOver) { //判断是否点击了离婚 if (mouseState != prevMouseState && mouseState.LeftButton != ButtonState.Released && divorceRectangle.Contains(mouseState.X, mouseState.Y)) { //停止切换图片 starting = true; //切换到游戏页面 currentScence = GameScence.GamePlaying; if (!isMuted) { //停止之前的结婚进行曲 MediaPlayer.Stop(); //播放结婚进行曲 MediaPlayer.Play(music); } //按钮点击音效 if (!isMuted) { click.Play(); } } //判断是否点击了主菜单 if (mouseState != prevMouseState && mouseState.LeftButton != ButtonState.Released && mainMenuRectangle.Contains(mouseState.X, mouseState.Y)) { //切换到游戏页面 currentScence = GameScence.MainMenu; if (!isMuted) { //停止之前的结婚进行曲 MediaPlayer.Stop(); //播放结婚进行曲 MediaPlayer.Play(music); } //按钮点击音效 if (!isMuted) { click.Play(); } } } //鼠标状态赋值给前鼠标状态 prevMouseState = mouseState; base.Update(gameTime); }
private void Awake() { Instance = this; audioSource = transform.GetComponent <AudioSource>(); EventCenter.AddListener <bool>(EventDefine.isMusic, isMusic); }