2017-01-24 2 views
0

Ich versuche gerade, ein Spiel ähnlich dem Spiel "Solo Noble" zu machen, in dem du eine Anzahl von Bällen hast und du musst die niedrigste erreichbare Punktzahl erhalten. Ich versuche gerade, ein Array von schwarzen & weißen Tasten, die in Form einer Binärdatei in einer externen Textdatei geformt sind, zu machen. Im Moment habe ich dies:Binär aus einer Textdatei lesen und in Schaltflächen umwandeln C#

using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using WindowsFormsApplication9.Properties; 

namespace WindowsFormsApplication9 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      Marble(); 
     } 
     public void Marble() 
     { 
      int ButtonWidth = 40; 
      int ButtonHeight = 40; 
      int Distance = 20; 
      int start_x = 10; 
      int start_y = 10; 
      int y = 0; 
      int x = 0; 
      int delX = x + (y * 2); 

      for (x = 0; x < 8; x++) 
      {    
       for (y = 0; y < 8; y++) 
       { 
       GameButton tmpButton = new GameButton(); 
       tmpButton.BackColor = Color.Black; 
       tmpButton.Top = start_x + (x * ButtonHeight + Distance); 
       tmpButton.Left = start_y + (y * ButtonWidth + Distance); 
       tmpButton.Width = ButtonWidth; 
       tmpButton.Height = ButtonHeight; 
       tmpButton.Text = "X: " + x.ToString() + " Y: " + y.ToString(); 
       tmpButton.MouseUp += TmpButton_MouseUp; 
       tmpButton.Row = x; 
       tmpButton.Column = y; 
       tmpButton.Currentcolor = false; 

       if (x == 4 && y == 6) { 
        tmpButton.BackColor = Color.White; 
       }        
       else 
       {      
        this.Controls.Add(tmpButton); 
       }      
      } 
     } 
    } 

    private void TmpButton_MouseUp(object sender, MouseEventArgs e) 
    { 
     GameButton Mygamebutton = (GameButton) sender; 
     Mygamebutton.Currentcolor = !Mygamebutton.Currentcolor; 
     if (Mygamebutton.Currentcolor == true) 
     { 
      Mygamebutton.BackColor = Color.Black; 
     } 
     else 
     { 
      Mygamebutton.BackColor = Color.White; 
     } 
    } 
    } 
} 

Aber ich versuche, so etwas zu bekommen:

byte[] fileBytes = File.ReadAllBytes(inputFilename); 
StringBuilder sb = new StringBuilder(); 
foreach(byte b in fileBytes) 
{ 
    sb.Append(Convert.ToString(b, 2).PadLeft(8, '0')); 
} 
File.WriteAllText(outputFilename, sb.ToString()); 

Ich bin nicht ganz sicher, wie das binäre Array aus der TXT-Datei in Schaltflächen zu drehen, zum Beispiel Eine 0 ist keine Schaltfläche und eine 1 ist eine Schaltfläche.

+2

Wie erstellen Sie dieses binäre fie? – Artiom

+0

Sie beziehen sich auf Ihre Datei sowohl als "binär" als auch als "Text", was verwirrend ist. Sprechen Sie vielleicht über eine Textdatei, die nur "0" und "1" Zeichen enthält? Wie auch immer, mit was genau haben Sie Probleme? Die Datei lesen? Oder wie man den Dateilese- und Tastenerstellungscode verbindet? –

Antwort

1

Wenn Sie Informationen über Schaltflächen serialisieren und später als diese Schaltflächen in Ihrem Formular hinzufügen möchten, müssen Sie verstehen, was Sie in der Datei speichern möchten: Position (x- und y-Koordinaten), Größe, Text, Hintergrund?

private void LoadButtonsInformation() 
{ 
    using (var stream = new MemoryStream(File.ReadAllBytes(@"C:\Projects\info.bin"))) 
    { 
     var serializer = new BinaryFormatter(); 
     var buttonInformations = (ButtonInformation[]) serializer.Deserialize(stream); 

     var buttons= buttonInformations.Select(button => new Button 
     { 
      Location = new Point(button.X, button.Y), 
      Text = button.Text, 
      Width = button.Width, 
      Height = button.Height 
     }).ToArray(); 

     //add to form 
     foreach (var button in buttons) 
      Controls.Add(button); 
    } 
} 


private void SaveButtonsInformation(params Button[] buttons) 
{ 
    var buttonsInformation = buttons.Select(button => new ButtonInformation 
    { 
     X = button.Location.X, 
     Y = button.Location.Y, 
     Text = button.Text, 
     Width = button.Width, 
     Height = button.Height 
    }).ToArray(); 

    using (Stream stream = new FileStream(@"C:\Projects\info.bin", FileMode.Create)) 
    { 
     var serializer = new BinaryFormatter(); 
     serializer.Serialize(stream, buttonsInformation); 
    } 
} 

[Serializable] 
public class ButtonInformation 
{ 
    public int X { get; set; } 

    public int Y { get; set; } 

    public string Text { get; set; } 

    public int Width { get; set; } 

    public int Height { get; set; } 
} 
Verwandte Themen