public Game(SocketHandler socketHandler, Random _random) { InitializeControlPanel(socketHandler); showImageFeedBack.CheckedChanged += ShowImageFeedBack_CheckedChanged; Reset(_random); Update(true); }
private void InitializeControlPanel(SocketHandler socketHandler) { controlPanel = new MyTableLayoutPanel(3, 1, "AAP", "A"); { { imageFeedBack = new MyCheckBox("Image Feed Back"); imageFeedBack.Checked = true; controlPanel.AddControl(imageFeedBack, 0, 0); } { showImageFeedBack = new MyCheckBox("Show Image Feed Back"); showImageFeedBack.Checked = false; controlPanel.AddControl(showImageFeedBack, 0, 0); } { Panel pnl = new Panel(); { pnl.Dock = DockStyle.Fill; pnl.AutoScroll = true; { generalSettings = new MyInputField(); generalSettings.Dock = DockStyle.Top; generalSettings.AddField("Image Feed Back Width", imageFeedBackSize.Width.ToString()).TextChanged += (o, e) => { try { imageFeedBackSize.Width = int.Parse((o as MyTextBox).Text); } catch (Exception) { MessageBox.Show("格式不正確"); } }; generalSettings.AddField("Image Feed Back Height", imageFeedBackSize.Height.ToString()).TextChanged += (o, e) => { try { imageFeedBackSize.Height = int.Parse((o as MyTextBox).Text); } catch (Exception) { MessageBox.Show("格式不正確"); } }; generalSettings.AddField("Port", socketHandler.port.ToString()).TextChanged += (o, e) => { try { socketHandler.port = int.Parse((o as MyTextBox).Text); } catch (Exception) { MessageBox.Show("格式不正確"); } }; generalSettings.AddField("darryMode", darryMode.ToString()).TextChanged += (o, e) => { try { darryMode = bool.Parse((o as MyTextBox).Text); } catch (Exception) { MessageBox.Show("格式不正確"); } }; generalSettings.AddField("max_upward_speed", max_upward_speed.ToString()).TextChanged += (o, e) => { try { max_upward_speed = double.Parse((o as MyTextBox).Text); } catch (Exception) { MessageBox.Show("格式不正確"); } }; generalSettings.AddField("alpha", alpha.ToString()).TextChanged += (o, e) => { try { alpha = double.Parse((o as MyTextBox).Text); } catch (Exception) { MessageBox.Show("格式不正確"); } }; generalSettings.AddField("重利", gravity.ToString()).TextChanged += (o, e) => { try { gravity = double.Parse((o as MyTextBox).Text); } catch (Exception) { MessageBox.Show("格式不正確"); } }; generalSettings.AddField("升力", liftForce.ToString()).TextChanged += (o, e) => { try { liftForce = double.Parse((o as MyTextBox).Text); } catch (Exception) { MessageBox.Show("格式不正確"); } }; generalSettings.AddField("可見障礙物數量", obstacleCount.ToString()).TextChanged += (o, e) => { try { obstacleCount = int.Parse((o as MyTextBox).Text); } catch (Exception) { MessageBox.Show("格式不正確"); } }; generalSettings.AddField("存活區的範圍", rangeY.ToString()).TextChanged += (o, e) => { try { rangeY = Interval.Parse((o as MyTextBox).Text); } catch (Exception) { MessageBox.Show("格式不正確"); } }; generalSettings.AddField("障礙物距離的範圍", obstacleDistance.ToString()).TextChanged += (o, e) => { try { obstacleDistance = Interval.Parse((o as MyTextBox).Text); } catch (Exception) { MessageBox.Show("格式不正確"); } }; generalSettings.AddField("障礙物寬度(通過時間)的範圍", obstacleWidth.ToString()).TextChanged += (o, e) => { try { obstacleWidth = Interval.Parse((o as MyTextBox).Text); } catch (Exception) { MessageBox.Show("格式不正確"); } }; //generalSettings.AddField("障礙物通道底部高度的範圍", obstacleY.ToString()).TextChanged += (o, e) => { try { obstacleY = Interval.Parse((o as MyTextBox).Text); } catch (Exception) { MessageBox.Show("格式不正確"); } }; generalSettings.AddField("障礙物通道寬度的範圍", obstacleHeight.ToString()).TextChanged += (o, e) => { try { obstacleHeight = Interval.Parse((o as MyTextBox).Text); } catch (Exception) { MessageBox.Show("格式不正確"); } }; pnl.Controls.Add(generalSettings); } } controlPanel.AddControl(pnl, 2, 0); } } }
public GamePanel(int _port, double fps) { this.Dock = DockStyle.Fill; port = _port; socketHandler = new SocketHandler(port); game = new Game(socketHandler, new Random(Form1.public_random)); //MessageBox.Show(Color.FromArgb(127,127,127).ToString()); //this.Size = new Size(1200, 750); { TLPmain = new MyTableLayoutPanel(1, 2, "A", "S1P"); { TLPctrl = new MyTableLayoutPanel(2, 1, "PP", "P"); TLPctrl.AutoSize = false; { TLPctrl.AddControl(game.controlPanel, 0, 0); } { TXB = new MyTextBox(true); TLPctrl.AddControl(TXB, 1, 0); } TLPmain.AddControl(TLPctrl, 0, 0); } { PictureBox pbx = new PictureBox(); pbx.Image = new Bitmap(pbx.Width, pbx.Height); pbx.Dock = DockStyle.Fill; pbx.SizeMode = PictureBoxSizeMode.Zoom; /*pbx.SizeChanged += (sender, e) => * { * SocketHandler_logAppended(pbx.Size.ToString()); * pbx.Image.Dispose(); * pbx.Image = new Bitmap(pbx.Width, pbx.Height); * };*/ bool humanFriendly = true; pbx.Click += (object sender, EventArgs e) => { humanFriendly ^= true; }; pbx.DoubleClick += Pbx_DoubleClick; { Thread thread = new Thread(() => { while (true) { Thread.Sleep((int)Math.Ceiling(1000.0 / fps)); var bmp = (humanFriendly ? new Bitmap(pbx.Width, pbx.Height) : new Bitmap(game.imageFeedBackSize.Width, game.imageFeedBackSize.Height)); game.drawImage(bmp, humanFriendly); Do(() => { //this.Text = bmp.Size.ToString(); //{ // BitmapData bd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); // unsafe // { // byte *p=(byte*)bd.Scan0.ToPointer // } //} var img = pbx.Image; pbx.Image = bmp; img.Dispose(); }); } }); thread.IsBackground = true; thread.Start(); } TLPmain.AddControl(pbx, 0, 1); } this.Controls.Add(TLPmain); } socketHandler.logAppended += SocketHandler_logAppended; socketHandler.msgReceived += SocketHandler_msgReceived; socketHandler.Start(); { Thread thread = new Thread(() => { int pre_count = 0; while (true) { Thread.Sleep(5000); if (socketHandler.dataConnectionCounter != pre_count) { SocketHandler_logAppended((pre_count = socketHandler.dataConnectionCounter).ToString() + " communications"); } } }); thread.IsBackground = true; thread.Start(); } }