首页 > 解决方案 > Powershell GUI 猜数字游戏

问题描述

所以基本上,我需要制作一个数字猜谜游戏,但需要一个 gui。我不知道如何使用户在文本框中编写的答案成为一个变量,以及如何将该变量与我的其余脚本集成。

这是我到目前为止所拥有的:

图形用户界面:

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

#GUI

$gamefrm                         = New-Object system.Windows.Forms.Form
$gamefrm.ClientSize              = New-Object System.Drawing.Point(573,376)
$gamefrm.text                    = "Deviner le nombre"
$gamefrm.TopMost                 = $true
$gamefrm.BackColor               = [System.Drawing.ColorTranslator]::FromHtml("#bd10e0")

$ruletxt                         = New-Object system.Windows.Forms.TextBox
$ruletxt.multiline               = $false
$ruletxt.width                   = 262
$ruletxt.height                  = 20
$ruletxt.location                = New-Object System.Drawing.Point(150,39)
$ruletxt.Font                    = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$ruletxt.Text                    = "Deviner le nombre!"
$ruletxt.AutoSize                = $true
$ruletxt.ReadOnly                = $true

$intxt                           = New-Object system.Windows.Forms.TextBox
$intxt.multiline                 = $false
$intxt.width                     = 263
$intxt.height                    = 20
$intxt.location                  = New-Object System.Drawing.Point(149,110)
$intxt.Font                      = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$intxt.Text                      = "1"
$intxt.AutoSize                  = $true
$intxt.ReadOnly                  = $true

$outtxt                          = New-Object system.Windows.Forms.TextBox
$outtxt.multiline                = $false
$outtxt.width                    = 263
$outtxt.height                   = 20
$outtxt.location                 = New-Object System.Drawing.Point(149,190)
$outtxt.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$outtxt.text                     = ""
$outtxt.AutoSize                 = $true


$gamefrm.controls.AddRange(@($ruletxt,$intxt,$outtxt,$imagepb,$Button1,)) 

$Button1                         = New-Object system.Windows.Forms.Button
$Button1.text                    = "button"
$Button1.width                   = 60
$Button1.height                  = 30
$Button1.location                = New-Object System.Drawing.Point(86,89)
$Button1.Font                    = New-Object System.Drawing.Font('Microsoft Sans Serif',10)


逻辑代码:


#Write your logic code here

[void]$gamefrm.ShowDialog()

[int]$nombre = Get-Random -Minimum 1 -Maximum 101

$essai = 0


$output = $intxt.Text

$input = $outtxt.Text



 while ($input -ne $nombre){
  
  
  if ($input -gt $nombre) {$output = " trop haut "}

if ($input -lt $nombre) {$intxt.Text = " trop bas "}

if ( ( $input -gt 100 ) -or ($input -lt 0 ) ) {$intxt.Text = " entre 1 et 100! "}

  $essai++

 } $intxt.Text = "Correct! Nombre d'essai: $essai"

如果有人可以帮助请告诉我谢谢。

标签: powershelluser-interface

解决方案


推荐阅读