Skocz do zawartości

Zarchiwizowany

Ten temat jest archiwizowany i nie można dodawać nowych odpowiedzi.

KillerBee

Tetris wyjasnienie

Polecane posty

Witajcie. Czy moglibyście wyjasnic mi co sie po kolei dzieje w tym kodzie?

Prosze o wklejenie kodu z komentarzem.

C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace tetris
{
    public partial class Form1 : Form
    {
        public bool appRunning = false;
        public Tile activeTile = null;
        Random rnd = new Random();
        public bool[,] tilesMatrix;
        public Form1()
        {
            InitializeComponent();
            tilesMatrix = new bool[
                arenaPanel.Width / Tile.TileWidth,
                arenaPanel.Height / Tile.TileHeight];
        }

        private void startButton_Click(object sender, EventArgs e)
        {
            if (appRunning)
            {
                appRunning = false;
                startButton.Text = "START";
            }
            else
            {
                appRunning = true;
                startButton.Text = "STOP";
                timer1.Enabled = true;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (appRunning)
            {
                if (activeTile == null) //create a new tile to fall down
                {
                    int pos = rnd.Next(1,tilesMatrix.GetLength(0)-1);
                    if (tilesMatrix[pos, 0])
                    {  // end of game
                        appRunning = false;
                        MessageBox.Show("Game Over");
                        arenaPanel.Controls.Clear();
                        for (int i = 0; i < tilesMatrix.GetLength(0); ++i)
                            for (int j = 0; j < tilesMatrix.GetLength(1); ++j)
                            {
                                tilesMatrix[i, j] = false;
                            }
                        startButton.Text = "START";
                    }
                    else
                    {
                        tilesMatrix[pos, 0] = true;
                        activeTile = new Tile();
                        arenaPanel.Controls.Add(activeTile);
                        
                        activeTile.Location = new Point(pos * Tile.TileWidth, 0);
                        activeTile.UseVisualStyleBackColor = true;
                        activeTile.Enabled = true;
                        //activeTile.BackColor = Color.Red;
                        int x = rnd.Next(3);
                        if (x == 1)
                        {
                            activeTile.BackColor = Color.Red;
                        }
                        else if (x == 2)
                        {
                            activeTile.BackColor = Color.Yellow;
                        }
                        else
                        {
                            activeTile.BackColor = Color.Green;
                        }

                    }
                }
                else
                { //continue falling down
                    if (activeTile.Location.Y + 2 * Tile.TileHeight >= arenaPanel.Height)
                    {  // the tile is at the bottom
                        activeTile = null;
                    }
                    else
                    {
                        int posx = activeTile.Location.X / activeTile.Width;
                        int posy = activeTile.Location.Y / activeTile.Height;

                        if (!tilesMatrix[posx, posy + 1])
                        {
                            tilesMatrix[posx, posy + 1] = true;
                            activeTile.Location = new Point(posx * Tile.TileWidth, (posy + 1) * Tile.TileHeight);
                            tilesMatrix[posx, posy] = false;
                        }
                        else
                        { //tile blocked
                            activeTile = null;
                        }

                    }
                }
            }
        }

        private void leftButton_Click(object sender, EventArgs e)
        {
            if (appRunning)
            {
                if (activeTile != null)
                {
                    int posx = activeTile.Location.X / activeTile.Width;
                    int posy = activeTile.Location.Y / activeTile.Height;

                    if ((posx - 1) >=0 && tilesMatrix[posx - 1, posy] == false)
                    {
                        activeTile.Location = new Point((posx -1) * Tile.TileWidth, posy * Tile.TileHeight);
                        tilesMatrix[posx, posy] = false;
                    }
                }
            }
        }

        private void rightButton_Click(object sender, EventArgs e)
        {
            if (appRunning)
            {
                if (activeTile != null)
                {
                    int posx = activeTile.Location.X / activeTile.Width;
                    int posy = activeTile.Location.Y / activeTile.Height;

                    if ((posx + 1) < tilesMatrix.GetLength(0) && tilesMatrix[posx + 1, posy] == false)
                    {
                        activeTile.Location = new Point((posx + 1) * Tile.TileWidth, posy * Tile.TileHeight);
                        tilesMatrix[posx, posy] = false;
                    }
                }
            }
        }

        private void downButton_Click(object sender, EventArgs e)
        {
            if (appRunning)
            {
                if (activeTile != null)
                {
                    int posx = activeTile.Location.X / activeTile.Width;
                    int posy = activeTile.Location.Y / activeTile.Height;
                    int buf =0;
                    for (int i = posy; i < (arenaPanel.Height / Tile.TileHeight);i++ )
                    {
                        if (tilesMatrix[posx, i] == false) buf = i;
                    }
                    tilesMatrix[posx, posy] = false;
                    activeTile.Location = new Point(posx * Tile.TileWidth, buf * Tile.TileHeight);
                    tilesMatrix[posx, buf] = true;
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }

}

Dziekuje za pomoc.

Link do komentarza
Udostępnij na innych stronach

Chcesz, żeby dodać linijkę z komentarzem, do każdej linii kodu ? To dość nudne i tak naprawdę niewiele da.

Postaraj się "odpalić" program w głowie/na kartce, zobacz co się po kolei dzieje, a jak będziesz miał problem z jakimś miejscem w kodzie to zaznacz o który fragment kodu, to ja (albo ktoś inny) ci to wyjaśni.

Link do komentarza
Udostępnij na innych stronach



  • Kto przegląda   0 użytkowników

    • Brak zalogowanych użytkowników przeglądających tę stronę.
×
×
  • Utwórz nowe...