ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 13주차 Snake bite
    카테고리 없음 2023. 6. 10. 23:22

     

    디자인코드 

    플레이 하면 시작하는버튼 배경화면 이미지 불러오기 나가기 버튼 구현 

    <Window x:Class="SnakeBiteWPF.MainWindow“
            ...
            xmlns:local="clr-namespace:SnakeBiteWPF"
            mc:Ignorable="d"
            Title="SnakeBite WPF" Height="400" Width="525">
      <Grid>
        <Grid.Background>
          <ImageBrush ImageSource="Images/tulip.jpg" Stretch="Fill"/>
        </Grid.Background>    
        <Grid.RowDefinitions>
          <RowDefinition Height="Auto"/>
          <RowDefinition Height="Auto"/>
          <RowDefinition Height="Auto"/>
          <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" Margin="20 30 0 0" Text="How to Play!"/>
        <TextBlock Grid.Row="1" Margin="20 0 0 0" Text="Eat 25 Eggs using Arrow Keys"/>
        <TextBlock Grid.Row="2" Margin="20 0 0 0" Text="Press ESC to pause while playing"/>
        <StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
          <Button x:Name="btnPlay" FontSize="18" FontWeight="Bold"
                Content="Play" Width="100" Height="50" Margin="0 50 60 0 " Click="btnPlay_Click" />
          <Button x:Name="btnQuit" FontSize="18" FontWeight="Bold"
                Content="Quit" Width="100" Height="50" Margin="0 50 0 0" Click="btnQuit_Click" />
        </StackPanel>    
      </Grid>
    </Window>

     

     

     

     

    게임 페이지 디자인코드 

     

    <Window x:Class="SnakeBiteWPF.Game"
            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:SnakeBiteWPF"
            mc:Ignorable="d"
            Title="SnakeBite" Height="340" Width="420" KeyDown="Window_KeyDown" >
      <Canvas x:Name="field" Width="420" Height="320" Background="LightBlue">
        <TextBlock x:Name="txtScore" Margin="10" Canvas.Left="200" Text="Socre = 0"/>
        <TextBlock x:Name="txtTimer" Margin="10" Canvas.Left="280" Text="Time = 0"/>
      </Canvas>
    </Window>

     

     

    뱀과 알 CS 코드 

     

      public partial class Game : Window
      {
        Ellipse[] snakes = new Ellipse[30];
        Ellipse egg;
        Random r = new Random();
    
        public Game()
        {
          InitializeComponent();
          InitEgg(); // InitSnakes();
        }
    
        private void InitEgg()
        {
          egg = new Ellipse();
          egg.Width = 10; egg.Height = 10;
          egg.Stroke = Brushes.Black;
          egg.Fill = Brushes.Red;
    
          // Canvas의 크기: Width="420" Height="320"
          int x = r.Next(0, 42);
          int y = r.Next(0, 32);
    
          Point p = new Point(x * 10, y * 10); // Point 구조체
          egg.Tag = p;
    
          field.Children.Add(egg);
          Canvas.SetLeft(egg, x * 10);
          Canvas.SetTop(egg, y * 10);
        }

    snake 관련 CS코드 

        private void InitSnakes()
        {
          int x = r.Next(0, 42);
          int y = r.Next(0, 32);
    
          for (int i = 0; i < 30; i++)
          {
            snakes[i] = new Ellipse();
            snakes[i].Width = 10;
            snakes[i].Height = 10;
            if (i % 5 == 0)
              snakes[i].Fill = Brushes.Green;
            else
              snakes[i].Fill = Brushes.Gold;
            snakes[i].Stroke = Brushes.Black;
            field.Children.Add(snakes[i]);
            snakes[i].Tag = new Point(x * 10, (y + i) * 10);
            Canvas.SetLeft(snakes[i], x * 10);
            Canvas.SetTop(snakes[i], (y + i) * 10);
          }
          snakes[0].Fill = Brushes.Chocolate;
          for (int i = visibleCount; i < 30; i++)
            snakes[i].Visibility = Visibility.Hidden;
        }

     

     

    뱀이 알을 먹으면 발생하게 하는 선언

    private void EatEgg()
            {
                Point pS = (Point)snakes[0].Tag;
                Point pE = (Point)egg.Tag;
    
                if (pS.X == pE.X && pS.Y == pE.Y)
                {
                    egg.Visibility = Visibility.Hidden;
                    visibleCount++;
                    // 꼬리를 하나 늘림
                    snakes[visibleCount - 1].Visibility = Visibility.Visible;
                    txtScore.Text = "Eggs = " + (++eaten).ToString();
    
                    if (visibleCount == 30)
                    {
                        timer.Stop();
                        sw.Stop();
                        DrawSnakes();
                        TimeSpan ts = sw.Elapsed;
                        string tElapsed = String.Format("Time = {0:00}:{1:00}.{2:00}",
                            ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
                        MessageBox.Show("Success!!!  " + tElapsed + " sec");
                        this.Close();
                    }
    
                    Point p = new Point(r.Next(1, 480 / size) * size,
                      r.Next(1, 380 / size) * size);
                    egg.Tag = p;
                    egg.Visibility = Visibility.Visible;
                    Canvas.SetLeft(egg, p.X);
                    Canvas.SetTop(egg, p.Y);
                }
Designed by Tistory.