Exemple #1
0
        public void Update(GameTime gameTime)
        {
            //if(Program.sPort.BytesToRead  == 1 )
            //{
            //    int data = (int)Program.sPort.ReadByte();
            //    //string trash = sPort.ReadExisting();
            //    if (data >= 128)
            //        data -= 255;
            //    kierunek = data;
            //}else
            //    if (Program.sPort.BytesToRead > 1)
            //{

            //    string trash = Program.sPort.ReadExisting();

            //}

            #region Ruchome krawędzie
            foreach (Krawedz k in listaKrawedzi)
            {
                Vector2 kr = new Vector2(0, k.prostokat.Y);
                if (k.ruchoma == true && k.kierunek == "prawo")
                {
                    kr.X = 500 * (float)gameTime.ElapsedGameTime.TotalSeconds;
                    k.prostokat.X += (int)kr.X;
                }
                if (k.ruchoma == true && k.kierunek == "lewo")
                {
                    kr.X = 500 * (float)gameTime.ElapsedGameTime.TotalSeconds;
                    k.prostokat.X -= (int)kr.X;
                }
                if (k.prostokat.X > MyStaticValues.WinSize.X - k.prostokat.Width)
                {
                    k.kierunek = "lewo";
                }
                if (k.prostokat.X < 0)
                {
                    k.kierunek = "prawo";
                }
            }
            #endregion
            if (position.Y < maxPosition.Y)
            {
                maxPosition = position;
                maxPosition.X = 0;
            }
            else
                if (maxPosition.Y + 700 < position.Y)
                {
                    koniecGry = true;
                }
            totalWynik = (int)((maxPosition.Y - 2 * maxPosition.Y) / 100) + wynik;

            //string test;

            //sport.DataReceived() += test;

            positionBefore = position;
            keyState = Keyboard.GetState();

            czas = gameTime.TotalGameTime.ToString();

            #region Sterowanie

            if (keyState.IsKeyDown(Keys.Enter) || keyState.IsKeyDown(Keys.Space))
                ScreenManager.Instance.AddScreen(new PlayScreen());
            gracz.IsActive = true;
            if (opadanie)
            {
                gracz.CurrentFrame = new Vector2(gracz.CurrentFrame.X, 2);
            }
            else
            {
                gracz.CurrentFrame = new Vector2(gracz.CurrentFrame.X, 0);
            }
            if (kierunek > 0)
            {
                gracz.CurrentFrame = new Vector2(0, gracz.CurrentFrame.Y);
                position.X += moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds * kierunek/48;
            }
            else if (kierunek < 0)
            {
                gracz.CurrentFrame = new Vector2(1, gracz.CurrentFrame.Y);
                position.X += moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds * kierunek/48;
            }
            if (jump)
            {
                gracz.CurrentFrame = new Vector2(gracz.CurrentFrame.X, 1);
                velocity = Vector2.Zero;
                velocity.Y -= jumpSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                jump = false;
            }

            if (!jump)
                velocity.Y += gravity * (float)gameTime.ElapsedGameTime.TotalSeconds;
            else
            {
                velocity.Y = 100 * (float)gameTime.ElapsedGameTime.TotalSeconds;
                opadanie = false;
            }
            position += velocity;

            if (position.Y >= MyStaticValues.WinSize.Y - gracz.FrameHeight)
            {
                jump = true;
            }
            #endregion

            #region colision
            CheckBorders();

            foreach (Krawedz k in listaKrawedzi)
            {
                if (playerRect.Intersects(k.prostokat) && opadanie)
                {
                    gracz.CurrentFrame = new Vector2(gracz.CurrentFrame.X, 1);
                    position.Y = k.prostokat.Y - gracz.FrameHeight;
                    inter = true;
                    jump = true;
                }
                else
                    inter = false;
            }

            if (playerRect.Intersects(podloga))
            {
                position.Y = podloga.Y - gracz.FrameHeight;
                inter = true;
                jump = true;
            }
            else
                inter = false;

            foreach (Coin c in listaCoinow)
            {
                if (playerRect.Intersects(c.prostokat))
                {
                    inter = true;
                    wynik += 20;
                    iloscCoinow++;
                    listaCoinowDoUsuniecia.Add(c);
                }
                else
                    inter = false;
            }
            #endregion
            #region Rysowanie bloków
            dlugoscListy = listaKrawedzi.Count;
            foreach (Krawedz k in listaKrawedziDoUsuniecia)
            {
                for (int i = 0; i < dlugoscListy - 1; i++)
                {
                    badanaKrawedz = listaKrawedzi[i];
                    if (k == badanaKrawedz)
                    {
                        listaKrawedzi.Remove(k);
                    }
                }
            }
            listaKrawedziDoUsuniecia.RemoveRange(0, listaKrawedziDoUsuniecia.Count);

            dlugoscListy = listaCoinow.Count;
            foreach (Coin c in listaCoinowDoUsuniecia)
            {
                for (int i = 0; i < dlugoscListy - 1; i++)
                {
                    try
                    {
                        badanyCoin = listaCoinow[i];
                    }
                    catch
                    {
                        return;
                    }
                    if (c == badanyCoin)
                    {
                        listaCoinow.Remove(c);
                    }
                }
            }
            listaCoinowDoUsuniecia.RemoveRange(0, listaCoinowDoUsuniecia.Count);
            #endregion
            gracz.Position = position;
            gracz.Update(gameTime);
            playerRect = new Rectangle((int)position.X, (int)position.Y, gracz.FrameWidth, gracz.FrameHeight);
            gracz.IsActive = false;
            /*
            if (licznik == 30)
            {
                updateKierunek();
                licznik = 0;
            }
            else
                licznik++;*/
        }
Exemple #2
0
        public void Initialize()
        {
            for (int i = 0; i < 600; i++)
            {
                Krawedz k = new Krawedz(poprzednieX, poprzednieWidth);
                k.prostokat.Y += (i * -200) + (MyStaticValues.WinSize.Y - 50);
                listaKrawedzi.Add(k);
                poprzednieX = k.prostokat.X;
                poprzednieWidth = k.prostokat.Width;
            }
            for (int i = 0; i < 100; i++)
            {
                Coin c = new Coin();
                listaCoinow.Add(c);
            }

            position = new Vector2(((MyStaticValues.WinSize.X - gracz.FrameWidth) / 2), (MyStaticValues.WinSize.Y - gracz.FrameHeight));
            velocity = Vector2.Zero;

            coin = new Rectangle(100, 100, 16, 16);

            //watek.Start();
        }