C# 猜拳遊戲

      在〈C# 猜拳遊戲〉中尚無留言

圖片

在專案按右鍵/加入現在項目, 再選影像圖片, 將三張圖加入

resource

XAML

<Window x:Class="Game.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Game"
mc:Ignorable="d"
Title="MainWindow" Height="550" Width="600">
  <Grid>
     <StackPanel Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Center">
     <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
     <StackPanel Orientation="Vertical" Margin="5,0,5,0">
     <Label Content="電腦" HorizontalAlignment="Center"></Label>
     <Border BorderThickness="1" BorderBrush="Black">
       <Image Width="200" Height="200" Name="imgComputer"></Image>
     </Border>
 </StackPanel>
 <StackPanel Orientation="Vertical" Margin="5,0,5,0">
 <Label Content="玩家" HorizontalAlignment="Center"></Label>
 <Border BorderThickness="1" BorderBrush="Black">
 <Image Width="200" Height="200" Name="imgPlayer"></Image>
 </Border>
 </StackPanel>
 </StackPanel>
 <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,5,0,5">
 <Border BorderBrush="Blue" BorderThickness="1">
 <Label Width="200" Height="30" Name="lblResult" HorizontalContentAlignment="Center"></Label>
 </Border>
 </StackPanel>
 <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
 <Button Width="150" Height="150" Name="btnScissors" Click="btnClick">
 <Image Name="img_btn_scissors" Source="Resources/scissors.png" ></Image>
 </Button>
 <Button Width="150" Height="150" Name="btnRock" Click="btnClick">
 <Image Name="img_btn_rock" Source="Resources/rock.png" ></Image>
 </Button>
 <Button Width="150" Height="150" Name="btnPaper" Click="btnClick">
 <Image Name="img_btn_paper" Source="Resources/paper.png" ></Image>
 </Button>
 </StackPanel>
 </StackPanel>
 </Grid>
</Window>

CS檔

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnClick(object sender, RoutedEventArgs e)
        {
            Random r = new Random();
            int cp = r.Next(0, 3);
            switch (cp)
            {
                case 0:
                    //imgComputer.Source = new BitmapImage(new Uri("c:/scissors.jpg"));
                    imgComputer.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/scissors.png"));
                    break;
                case 1:
                    imgComputer.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/rock.png"));
                    break;
                case 2:
                    imgComputer.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/paper.png"));
                    break;
            }
            Button b = (Button)sender;
            int player;
            if (b.Name.Equals("btnScissors"))
            {
                player = 0;
                imgPlayer.Source = new BitmapImage(new Uri("pack://application:,,,/scissors.png"));
            }
            else if (b.Name.Equals("btnRock"))
            {
                player = 1;
                imgPlayer.Source = new BitmapImage(new Uri("pack://application:,,,/rock.png"));
            }
            else
            {
                player = 2;
                imgPlayer.Source = new BitmapImage(new Uri("pack://application:,,,/paper.png"));
            }
            switch (cp - player)
            {
                case 0:
                    lblResult.Content = "平手";
                    break;
                case -1:
                case 2:
                    lblResult.Content = "You Win";
                    break;
                case -2:
                case 1:
                    lblResult.Content = "You Loss";
                    break;
            }
        }
    }

csharp_game

下載

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *