pacman tworzenie labiryntu za pomoca array2d

0

Witam , mam problem z przedstawieniem tego labiryntu za pomoca tablicy , to niestety poczatek calego zadania ale bez tablicy nie ruszam dalej ;)

###################
#.................#
#.#.###.#.#.###.#.#
#.#.#...#.#...#.#.#
#.#.###.#.#.###.#.#
#.................#
#.#.#.### ###.#.#.#
#.................#
#.#.###.#.#.###.#.#
#.#.#...#.#...#.#.#
#.#.###.#.#.###.#.#
#.................#
###################>

program musze wczytac z wiersza polecenia i przedstawic go graficznie, wywolanie programu (game.exe < maze.dat) powinno przedstawiac taki oto labirynt

,
screenshot-20171107151945.png

nastepnie zasady jak w pacmanie , chodzi tylko o nieruchome sciany i poruszanie klawiatura i kropki do zebrania .
Chetnie pomoc przez skypa na zasadzie korkow ( € )
dzieki za pomoc

0
using System;
using System.IO;

class Program
{
  public static void Main(string [] args)
  {
	char [][] tablica;
    string path = args[0];
    string[] readText = File.ReadAllLines(path);
    
    int wysokosc = readText.Length;
    int szerokosc = readText[0].Length;
    
    tablica = new char[wysokosc][];
    
    for(int i=0;i<wysokosc;i++)
    {
		tablica[i] = readText[i].ToCharArray();
	}
	
	// -------------------------------------------------
    for(int i=0;i<wysokosc;i++)
    {
		for(int j=0;j<szerokosc;j++)
		{
			Console.Write(tablica[i][j]);
		}
		Console.WriteLine();
	}
    
  }
}

0

Labirynt wyswietla sie na konsoli , teraz musze z tego stworzyc gre , tak , by wyswietalala sie w oknie jak wyzej .
ma ktos jakis plan albo godzinke czasu na korki skype ?

0

O chłopaku, takie rysowanie na formie to trochę cięższa sprawa. Jeśli nie ogarniasz dziedziczenia i polimorfizmu, to godzinka będzie za mało. Odezwij się na priv.

1

Posiedziałem trochę nad tym i wyszło trochę g**no kodu jako implementacja chorego pomysłu który nawet mnie przeraża
aż sam siebie zapytałem "to działa xD?".

Dwie tablice 2d. Jedna trzyma znaczki, a druga odległości od góry i lewej strony formsa i cała logika się na tym opiera.

Niech mi ktoś teraz powie, jak to zrobić normalnie i szybko bez takiego bajzlu.

PS: Random użyty, bo nazwy labeli kolidowały, a tak było najszybciej :P

x.PNG

x1.PNG

using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        static char[,] tablica;
        static Point[,] coords;
        static int wysokosc;
        static int szerokosc;
        static int score = 0;
        static int removedCounter = 0;
        enum directions
        {
            North,
            South,
            West,
            East
        }

        public Form1()
        {
            InitializeComponent();
            Initialize();
            Rysuj();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Initialize()
        {
            string path = @"C:\test\input.txt";
            string[] readText = File.ReadAllLines(path);

            wysokosc = readText.Length;
            szerokosc = readText[0].Length;

            tablica = new char[wysokosc,szerokosc];
            coords = new Point[wysokosc,szerokosc];

            for (int i = 0; i < wysokosc; i++)
            {
                for (int j = 0; j < szerokosc; j++)
                {
                    tablica[i,j] = readText[i][j];
                }
            }
            int left = 0;
            int top = 150;
            for (int i =0; i<wysokosc; i++)
            {
                for (int j=0; j<szerokosc; j++)
                {
                    left = 50 + (j + 1) * 75;
                    coords[i,j] = new Point(top, left);
                }
                top +=55;
            }
        }

        private void Rysuj()
        {
            for (int i=0; i<wysokosc; i++)
            {
                for (int j = 0; j < szerokosc; j++)
                {
                    Random rnd = new Random();
                    
                    string current = tablica[i,j].ToString();
                    Label label = new Label();

                    label.Font = new Font("Arial", 16, FontStyle.Bold);
                    if (current=="#")
                    {
                        label.Text = current;
                        label.Name = "label" + j + i + rnd.Next(100);
                        label.ForeColor = Color.LawnGreen;
                        label.Size=new Size(20,20);
                    }
                    else if (current==".")
                    {
                        label.Text = "O";
                        label.Name = "label" + j + i+rnd.Next(100);
                        label.ForeColor = Color.Blue;
                        label.Size = new Size(30, 30);
                    }
                    else
                    {
                        label.Text = "X";
                        label.Name = "Player";
                        label.ForeColor = Color.Red;
                    }
                    label.Top = coords[i,j].X;
                    label.Left = coords[i,j].Y;
                    this.Controls.Add(label);
                }   
            }
        }

        private int[] GetIndex_OfPlayersPosition_InCoords2DArray(int currentX, int currentY)
        {
            for (int i = 0; i < wysokosc; i++)
            {
                for (int j = 0; j < szerokosc; j++)
                {
                    if (coords[i,j].X == currentX && coords[i,j].Y == currentY)
                    {
                        return new int[] { i, j };
                    }
                }
            }
            return new int[] { 0, 0 };
        }

        private string GetNameOfTextLabel_OnCoords(int currentTop, int currentLeft)
        {
            foreach (Label x in this.Controls.OfType<Label>())
            {
                if (x.Top == currentTop && x.Left ==currentLeft && x.Name != "DisplayingScore")
                {
                    return x.Name;
                }
            }
            return "";
        }

        private void MovementHelper(string name, Control player)
        {
            Control newlabel = this.Controls[name];
            if (newlabel.Text == "#")
            {
                MessageBox.Show("You cannot go there");
            }
            else
            {
                if (newlabel.Text.ToLower() == "o")
                {
                    score++;
                    DisplayingScore.Text = $"Score: {score.ToString()}";
                }
                Control currentlabel = this.Controls[name];
                currentlabel.Text = "";
                newlabel.Text = "X";
                newlabel.Font = new Font("Arial", 16, FontStyle.Bold);
                newlabel.ForeColor = Color.Red;
                newlabel.Name = "Player";
                player.Text = "";
                player.Name = "removed" + removedCounter;
                removedCounter++;
            }
        }
        private void Movement(directions direction)
        {
            Control player = this.Controls["Player"];
            int[] pos = GetIndex_OfPlayersPosition_InCoords2DArray(player.Top, player.Left);
            string name="";
            switch (direction)
            {
                case directions.North:
                   name = GetNameOfTextLabel_OnCoords(coords[pos[0] - 1, pos[1]].X, coords[pos[0]-1, pos[1]].Y);
                   break;

                case directions.South:
                   name = GetNameOfTextLabel_OnCoords(coords[pos[0] + 1, pos[1]].X, coords[pos[0]+1, pos[1]].Y);
                   break;
                
                case directions.East:
                   name = GetNameOfTextLabel_OnCoords(coords[pos[0], pos[1]+1].X, coords[pos[0], pos[1]+1].Y);
                  break;
                
                case directions.West:
                   name = GetNameOfTextLabel_OnCoords(coords[pos[0], pos[1]-1].X, coords[pos[0], pos[1]-1].Y);
                   break;

                default:
                  MessageBox.Show("shit happens");
                  break;
            }
            MovementHelper(name, player);
        }

        private void North_Click(object sender, EventArgs e)
        {
            Movement(directions.North);
        }

        private void East_Click(object sender, EventArgs e)
        {
            Movement(directions.East);
        }

        private void South_Click(object sender, EventArgs e)
        {
            Movement(directions.South);
        }

        private void West_Click(object sender, EventArgs e)
        {
            Movement(directions.West);
        }
    }
}
1

A moze 2 wymiarowa tablica zer i jedynek. 0 to ściana, 1 to wolne pole. Pqcman ma pozycje x i y. Przy ruchu np w lewo odejmujemy od pozycji x pacmana to co jest na lewo od pacmana ( 0 lub jedynke) z tablicy labiryntu. Jak jest 0 to pozycja pacmana sie nie zmieni. A jak 1 to pozycja x pacmana zmieni się o 1.

@Daniel M jak skończysz to wrzuć tu linka do projektu. Pogramy :)

2

Ech. Nie dawało mi to spokoju i zrobiłem :)

https://github.com/JacekCzapla/Pacman.git

Jest tam trochę magicznych liczb i przydałoby się jeszcze trochę kodu do skalowania, i rysowanie miga ale da się grać :)

titlescreenshot-20171109112454.png

0
jacek.placek napisał(a):

private void Form1_KeyDown(object sender, KeyEventArgs e)
Keys.Right

Ehh. Myślałem, że to byłoby zbyt proste i trzeba będzie się bawić z windows hookami

Dzięki :P

0

Program.cs

using System;
using System.IO;
using System.Windows.Forms;

namespace Pacman
{
    static class Program
    {
        /// <summary>
        /// Start
        /// </summary>
        [STAThread]
        static void Main(string[]args)
        {

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);


            char[][] arr;
            string path = "schemat.txt";
            string[] readText = File.ReadAllLines(path);

            int height = readText.Length;

            arr = new char[height][];
            for (int i = 0; i < height; i++)
            {
                arr[i] = readText[i].ToCharArray();
            }

            
            Application.Run(new Form1(arr));
        }
    }
}

Form1.cs

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Pacman
{
    public partial class Form1 : Form
    {
        private Font myfont = new Font("Arial", 16, FontStyle.Bold);
        private const int labelSize = 35;
        private const int positionFromTop = 20;
        private const int positionFromLeft = 20;

        private Point playerPosition;
        private readonly int height;
        private readonly int width;
        private const int additionalSpace = 200;
        private int points = 0;
        private const string food = ".";
        private Label[,] labels;
        private Label pointsLabel;
        private readonly Color playerColor = Color.White;
        private readonly Color foodColor = Color.Red;
        private readonly Color floorColor = Color.Blue;
        private readonly Color wallColor = Color.Orange;
        private readonly Color pointsLabelBackColor = Color.Orange;
        private readonly Color pointsLabelForeColor = Color.Purple;
        
        private void UpdatePoints()
        {
            pointsLabel.Text = "Punkty: " + points;
        }
        public Form1(char[][]tablica)
        {
            InitializeComponent();
            BackColor = Color.Black;
            
            width = tablica[0].Length;
            height = tablica.Length;
            Width = width * labelSize + additionalSpace;
            Height = height * labelSize + additionalSpace;

            //find and set player position
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    if (tablica[i][j] == 'X')
                    {
                        playerPosition = new Point(j,i);
                        break;
                    }
                }
            }
            labels = new Label[height, width];

            //add all labels
            //fields
            for(int j=0,positionY= positionFromTop; j<height;j++,positionY+=labelSize)
            {
                for (int i = 0, positionX = positionFromLeft; i < width; i++, positionX += labelSize)
                {
                    Label tmp = new Label
                    {
                        Width = labelSize,
                        Height = labelSize,
                        BackColor = floorColor,
                        Font = myfont,
                        Text = Convert.ToString(tablica[j][i])
                    };
                    switch (tmp.Text)
                    {
                        case "X":
                            tmp.ForeColor = playerColor;
                            break;
                        case "#":
                            tmp.ForeColor = wallColor;
                            tmp.BackColor = wallColor;
                            break;
                        case ".":
                            tmp.ForeColor = foodColor;
                            break;
                        default:
                            break;
                    }
                    tmp.Top = positionY;
                    tmp.Left = positionX;

                    this.Controls.Add(tmp);
                    labels[j, i] = tmp;
                }
            }

            //points
            pointsLabel = new Label
            {
                Width = 200,
                BackColor = pointsLabelBackColor,
                Height = labelSize,
                Font = myfont,
                ForeColor = pointsLabelForeColor,
                Text = "Punkty: " + points,
                Top = labels[height - 1, 0].Top + 40,
                Left = positionFromLeft
            };
            this.Controls.Add(pointsLabel);
        }

        private void MoveUp()
        {
            labels[playerPosition.Y, playerPosition.X].Text = " ";
            

            labels[playerPosition.Y - 1, playerPosition.X].Text = "X";
            labels[playerPosition.Y - 1, playerPosition.X].ForeColor = playerColor;
            playerPosition.Y--;
        }

        private void MoveDown()
        {
            labels[playerPosition.Y, playerPosition.X].Text = " ";
            
            labels[playerPosition.Y + 1, playerPosition.X].Text = "X";
            labels[playerPosition.Y + 1, playerPosition.X].ForeColor = playerColor;
            playerPosition.Y++;
        }

        private void MoveLeft()
        {
            labels[playerPosition.Y, playerPosition.X].Text = " ";
            
            labels[playerPosition.Y, playerPosition.X-1].Text = "X";
            labels[playerPosition.Y, playerPosition.X-1].ForeColor = playerColor;
            playerPosition.X--;
        }

        private void MoveRight()
        {
            labels[playerPosition.Y, playerPosition.X].Text = " ";

            labels[playerPosition.Y, playerPosition.X + 1].Text = "X";
            labels[playerPosition.Y, playerPosition.X + 1].ForeColor = playerColor;
            playerPosition.X++;
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            switch(e.KeyCode)
            {
                case Keys.Up:
                    if (labels[playerPosition.Y - 1, playerPosition.X].Text == " ")
                    {
                        MoveUp();
                    }
                    else if (labels[playerPosition.Y - 1, playerPosition.X].Text == food)
                    {
                        MoveUp();
                        points++;
                    }
                        break;
                case Keys.Down:
                    if (labels[playerPosition.Y + 1, playerPosition.X].Text == " ")
                    {
                        MoveDown();
                    }
                    else if (labels[playerPosition.Y + 1, playerPosition.X].Text == food)
                    {
                        MoveDown();
                        points++;
                    }
                    break;
                case Keys.Left:
                    if (labels[playerPosition.Y, playerPosition.X-1].Text == " ")
                    {
                        MoveLeft();
                    }
                    else if (labels[playerPosition.Y, playerPosition.X - 1].Text == food)
                    {
                        MoveLeft();
                        points++;
                    }
                    break;
                case Keys.Right:
                    if (labels[playerPosition.Y, playerPosition.X + 1].Text == " ")
                    {
                        MoveRight();
                    }
                    else if (labels[playerPosition.Y, playerPosition.X + 1].Text == food)
                    {
                        MoveRight();
                        points++;
                    }
                        break;
                default:
                    break;
            }
            UpdatePoints();
        }
    }
}



1 użytkowników online, w tym zalogowanych: 0, gości: 1