コード例 #1
0
        public void Update(GameTime gameTime)
        {
            if (runTimer)
            {
                timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
            }

            mouseState = Mouse.GetState();

            ButtonState currState = mouseState.LeftButton;
            if (currState == ButtonState.Pressed && currState != prevState)
            {
                mousePosition = new Point(mouseState.X, mouseState.Y);
                if (quitButtonRectangle.Contains(mousePosition)) // quit Button
                {
                    game.quitGame();
                }

                foreach (Rectangle r in rectangles)
                {
                    if (r.Contains(mousePosition))
                    {
                        foreach (Card c in cards)
                        {
                            //int a
                            if (c.getRect().Intersects(r) && !c.isFlipped())
                            {
                                if (cardsFlipped == 0)
                                {
                                    c.flip();
                                    cardsFlipped++;
                                    card1 = cards.IndexOf(c);
                                }
                                else if(cardsFlipped == 1 && cards.IndexOf(c) != card1)
                                {
                                    c.flip();
                                    cardsFlipped++;
                                    card2 = cards.IndexOf(c);
                                }
                            }
                        }
                    }
                }

                prevState = currState;

            } if (currState == ButtonState.Released)
            {
                prevState = currState;
            }

            if (cardsFlipped == 2)
            {
                elapsedTime += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

                if (elapsedTime >= 1000)
                {

                    if (cards[card1].getPairId() == cards[card2].getPairId())
                    {
                        cards[card1].setVisible(false);
                        cards[card2].setVisible(false);

                    }
                    else if (cards[card1].getPairId() != cards[card2].getPairId())
                    {
                        cards[card1].flip();
                        cards[card2].flip();
                    }

                    int count = 0;

                    foreach (Card c in cards)
                    {
                        if(c.isFlipped())
                        {
                          count++; // total cards flipped
                        }
                     }
                    if (count == 12)// user wins
                    {
                        runTimer = false;
                        string username = Environment.UserName;
                        float time = timer;
                        Player p = new Player(username, time);
                        //p.setName(username);
                       // p.setTime(time);
                        savePlayer(p);

                    }
                    cardsFlipped = 0;
                    elapsedTime = 0;

                }
            }
        }
コード例 #2
0
        public void savePlayer(Player p)
        {
            List<Player> players = loadPlayers();
            players.Add(p);

            FileStream stream = File.Open(@"C:\scores.xml", FileMode.Create);
            try
            {
                // Convert the object to XML data and put it in the stream
                XmlSerializer serializer = new XmlSerializer(typeof(List<Player>));
                serializer.Serialize(stream, players);
            }
            finally
            {

                stream.Close();
            }
        }