
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;



namespace WindowsGame1
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
int s = 0;
Texture2D mySpriteTexture, mySpriteTextureBG,myb,b,pig,b3,start,point,heart,block,over,prince,save;
int X, Y; // 全域變數

int Offset_X = 0;  // X 軸 偏移値
int Offset_Y = 0;  // Y 軸 偏移値
int Offset_X2 = 0;  // X 軸 偏移値
int Offset_Y2 = 0;  // Y 軸 偏移値
int Offset_X3 = 0;  // X 軸 偏移値
int Offset_Y3 = 0;  // Y 軸 偏移値
int Offset_X4 = 0;  // X 軸 偏移値
int Offset_Y4 = 0;  // Y 軸 偏移値
int Offset_X5 = 0;  // X 軸 偏移値
int Offset_Y5 = 0;  // Y 軸 偏移値
int Offset_X6 = 0;  // X 軸 偏移値
int Offset_Y6 = 0;  // Y 軸 偏移値
int Offset_X7 = 0;  // X 軸 偏移値
int enter = 0;
int end;
int con;
Song song;
KeyboardState oldState; // 宣告一個KeyboardState 結構的變數
KeyboardState newState; // 宣告一個KeyboardState 結構的變數
SpriteFont Font1; // 字型物件
//MyAudio audio;
//newState  //得到目前鍵盤每一個按鍵的狀況

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";

this.Window.Title = "SAVE PRINCE（↑↓←→ ）";
this.IsMouseVisible = true; // 呈現滑鼠游標
this.Window.AllowUserResizing = false; // 允許視窗縮放
this.graphics.PreferredBackBufferWidth = 576; //視窗的預設寬
this.graphics.PreferredBackBufferHeight = 832; //視窗的預設高
this.TargetElapsedTime = new TimeSpan(0,0,0,0,33);//更新的速度50→(1/20)
//audio = new MyAudio(); // 建立audio
//audio.PlayBgm();
X = 256;//公主起始點
Y = 768;//公主起始點
}


protected override void Initialize()
{
base.Initialize();
}


protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);

// TODO: use this.Content to load your game content here
mySpriteTexture = Content.Load<Texture2D>("princess2_new");//princess
mySpriteTextureBG = Content.Load<Texture2D>("back");//背景
myb = Content.Load<Texture2D>("b2_new");//怪物
b = Content.Load<Texture2D>("b");//紫人
b3 = Content.Load<Texture2D>("b3new");//ice
pig = Content.Load<Texture2D>("pignew");//pig
Font1 = Content.Load<SpriteFont>("SpriteFont1");//文字
start = Content.Load<Texture2D>("home");//首圖
point = Content.Load<Texture2D>("direction");//遊戲說明圖
heart = Content.Load<Texture2D>("heart");//命
block = Content.Load<Texture2D>("block");//蓋掉心
over = Content.Load<Texture2D>("over");//末頁
prince = Content.Load<Texture2D>("princenew");//王子
save = Content.Load<Texture2D>("save");//save
song = Content.Load<Song>("Mario");
MediaPlayer.Play(song);
}


protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}

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
//KeyboardState newState; // 宣告一個KeyboardState 結構的變數
newState = Keyboard.GetState(); //得到目前鍵盤每一個按鍵的狀況


    
    if (oldState.IsKeyUp(Keys.Right) && newState.IsKeyDown(Keys.Right)&& X<512) X += 64;
    
    if (oldState.IsKeyUp(Keys.Left) && newState.IsKeyDown(Keys.Left)&&X>0) X -= 64;
    
    if (oldState.IsKeyUp(Keys.Up) && newState.IsKeyDown(Keys.Up)&&Y>64) Y -= 64;
    
    if (oldState.IsKeyUp(Keys.Down) && newState.IsKeyDown(Keys.Down)&&Y<768) Y += 64;


if (newState.IsKeyDown(Keys.Enter) && oldState.IsKeyUp(Keys.Enter)) {//如果剛剛按了enter現在又按一次enter
    enter += 1;
    if (enter == 3) enter = 2;//防止玩家在遊戲中不小心按到enter
}
if (newState.IsKeyDown(Keys.Back) && oldState.IsKeyUp(Keys.Back)) {//按了back鍵後
    end = 1;
}


oldState = newState; // 這一次就是 下一次的上一次鍵盤狀況
Offset_X += 1; // 向右
Offset_Y += 0;  // 不增加

Offset_X2 += -2; // 向左
Offset_Y2 += 0;  // 不增加

Offset_X3 += 2; // 向左
Offset_Y3 += 0;  // 不增加

Offset_X4 += -1; // 向左
Offset_Y4 += 0;  // 不增加

Offset_X5 += 1; // 向左
Offset_Y5 += 0;  // 不增加

Offset_X6 += -1; // 向左
Offset_Y6 += 0;  // 不增加

Offset_X7 += 64;
base.Update(gameTime);
}


protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.White); //.CornflowerBlue);

// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw(mySpriteTextureBG, new Vector2(0,0), Color.White);


Vector2 sposition = new Vector2(0, 0);//起始位置在(0,0)
Vector2 hposition = new Vector2(0, 0);//心
Vector2 hposition2 = new Vector2(64, 0);//心
Vector2 hposition3 = new Vector2(128, 0);//心
Vector2 blockp ;//失命(蓋掉一顆心)
Vector2 p = new Vector2(64, 64);//王子初始位置
Vector2 position;//怪獸
Vector2 position2;//馬力歐
Vector2 position3;//ice
Vector2 position4;//pig

spriteBatch.Draw(prince, p, Color.White);
if (enter == 0)
    spriteBatch.Draw(start, sposition, Color.White);//畫出首頁
if (enter == 1)
    spriteBatch.Draw(point, sposition, Color.White);//畫出說明頁
if (enter == 2)
{
    
    Vector2 position1 = new Vector2(X, Y);//公主起始位置
    spriteBatch.Draw(heart, hposition, Color.White);
    spriteBatch.Draw(heart, hposition2, Color.White);
    spriteBatch.Draw(heart, hposition3, Color.White);
    spriteBatch.Draw(mySpriteTexture, position1, Color.White);
   
    
    position = new Vector2(8* (Offset_X % 64), 128);
    spriteBatch.Draw(myb, position, Color.White);
    if (position.X == position1.X && position.Y == position1.Y)
    {
        s = s - 1;//扣分
    }
    position2 = new Vector2(16*(Offset_X2 % 64) + 576, 192);
    spriteBatch.Draw(b, position2, Color.White);
    if (position2.X == position1.X && position2.Y == position1.Y)
    {
        s = s - 1;
    }
    position3 = new Vector2(8 * (Offset_X % 64 + 12), 320);
    spriteBatch.Draw(b3, position3, Color.White);
    if (position3.X == position1.X && position3.Y == position1.Y)
    {
        s = s - 1;
    }
    position4 = new Vector2(8 * (Offset_X2 % 64) + 576, 448);
    spriteBatch.Draw(pig, position4, Color.White);
    if (position4.X == position1.X && position4.Y == position1.Y)
    {
        s = s - 1;
    }
    position = new Vector2(8 * (Offset_X5 % 32), 576);
    spriteBatch.Draw(myb, position, Color.White);
    if (position.X == position1.X && position.Y == position1.Y)
    {
        s = s - 1;//扣分
    }
    position2 = new Vector2(16* (Offset_X6 % 64)+576, 704);
    spriteBatch.Draw(b, position2, Color.White);
    if (position2.X == position1.X && position2.Y == position1.Y)
    {
        s = s - 1;
    }
   


    if (p.X == position1.X && p.Y == position1.Y)//公主找到王子
    {
        //spriteBatch.Draw(save, overp, Color.White);
        con = 1;
    }
    
    if (s == -1 )//失一命
    {
        blockp = new Vector2(128,0);
        spriteBatch.Draw(block, blockp, Color.White);
       
    }
    if (s == -2 )//失兩命
    {
        blockp = new Vector2(128, 0);
        spriteBatch.Draw(block, blockp, Color.White);
        blockp = new Vector2(64, 0);
        spriteBatch.Draw(block, blockp, Color.White);
        
    }
    if (s <=-3)//失三命
    {
        blockp = new Vector2(128, 0);
        spriteBatch.Draw(block, blockp, Color.White);
        blockp = new Vector2(64, 0);
        spriteBatch.Draw(block, blockp, Color.White);
        blockp = new Vector2(0, 0);
        spriteBatch.Draw(block, blockp, Color.White);
        spriteBatch.Draw(over, sposition, Color.White);
        
    }
      
    if (end == 1)//按了back之後
    {
        enter = 0;
        end = 0;
        con = 0;
        s = 0;
        X = 0;
        Y = 768;
    }
    if (con == 1) {
        spriteBatch.Draw(save, sposition, Color.White);       
    }

    //Vector2 FontPos = new Vector2(450, 32);
    //string message = " Score " + s.ToString();
    //Vector2 FontOrigin = Font1.MeasureString(message) / 2; // 字串中心

    //spriteBatch.DrawString(Font1, // 字型
    //message, // 字串
    //FontPos, // 位置
    //Color.Red, // 字的顏色
    //0, // 旋轉角度
    //FontOrigin,// 字串中心點
    //1.5f, // 縮放倍數
    //SpriteEffects.None,// 旋轉校果
    //0); // 圖層深度 0.0 ~ 1.0 (後)

    

}   
    spriteBatch.End();
base.Draw(gameTime);
}
}
}
