C#의 탐식사

33254 단어 C# 모듈
본고는 주로 탐식하는 뱀을 설명하여 두 마리의 뱀을 실현하는데 코드는 다음과 같다. 편집기 인터페이스
"   1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="800" Width="1000" WindowStyle="None" AllowsTransparency="False" Background="Pink" Loaded="Window_Loaded">
    "back" Margin="0,0,200,0" Background="Goldenrod" Height="800" Width="800">
        <Button Content="    " Canvas.Left="868" Canvas.Top="199" Width="75" Click="Button_Click" />
        <Button Content="    " Canvas.Left="868" Canvas.Top="107" Width="75" Click="Button_Click_1"/>
    


테마 코드
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace    1
{
    /// 
    /// MainWindow.xaml      
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        //  
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        //  
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            t1.Interval = TimeSpan.FromMilliseconds(100);
            t1.Start();
        }
        int SnakeSize = 20;
        int rows;
        int column;
        List snake = new List();
        List snake1 = new List();
        Random r = new Random();
        DispatcherTimer t1 = new DispatcherTimer();

        Border food = new Border();
        //  
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            rows = (int)back.Width / SnakeSize;
            column = (int)back.Height / SnakeSize;
            this.Left = this.Top = 0;
            CreateSnake();
            CreateSnake1();
            t1.Tick += t1_Tick;
            KeyDown += MainWindow_KeyDown;
            food.Width = food.Height = SnakeSize;
            food.CornerRadius = new CornerRadius(90);
            food.Background = new RadialGradientBrush(Color.FromRgb((byte)r.Next(0, 256), (byte)r.Next(0, 256), (byte)r.Next(0, 256)), Color.FromRgb((byte)r.Next(0, 256), (byte)r.Next(0, 256), (byte)r.Next(0, 256)));
            back.Children.Add(food);
            Canvas.SetLeft(food, r.Next(0, 20) * rows);
            Canvas.SetTop(food, r.Next(0, 20) * column);
            Canvas.SetZIndex(food, 3);
        }
        //     
        void MainWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key.ToString() == "W" && snake[0].Tag.ToString() != "S")
            {
                snake[0].Tag = "W";
            }
            if (e.Key.ToString() == "A" && snake[0].Tag.ToString() != "D")
            {
                snake[0].Tag = "A";
            }
            if (e.Key.ToString() == "S" && snake[0].Tag.ToString() != "W")
            {
                snake[0].Tag = "S";
            }
            if (e.Key.ToString() == "D" && snake[0].Tag.ToString() != "A")
            {
                snake[0].Tag = "D";
            }
            // 
            if (e.Key.ToString() == "Up" && snake1[0].Tag.ToString() != "Down")
            {
                snake1[0].Tag = "W";
            }
            if (e.Key.ToString() == "Left" && snake1[0].Tag.ToString() != "Right")
            {
                snake1[0].Tag = "A";
            }
            if (e.Key.ToString() == "Down" && snake1[0].Tag.ToString() != "Up")
            {
                snake1[0].Tag = "S";
            }
            if (e.Key.ToString() == "Right" && snake1[0].Tag.ToString() != "Left")
            {
                snake1[0].Tag = "D";
            }
        }
        //     
        void t1_Tick(object sender, EventArgs e)
        {
            for (int i = snake.Count - 1; i > 0; i--)
            {
                Canvas.SetLeft(snake[i], Canvas.GetLeft(snake[i - 1]));
                Canvas.SetTop(snake[i], Canvas.GetTop(snake[i - 1]));
            }
            switch (snake[0].Tag.ToString())
            {
                case "W":
                    Canvas.SetTop(snake[0], Canvas.GetTop(snake[0]) - SnakeSize);
                    break;
                case "A":
                    Canvas.SetLeft(snake[0], Canvas.GetLeft(snake[0]) - SnakeSize);
                    break;
                case "S":
                    Canvas.SetTop(snake[0], Canvas.GetTop(snake[0]) + SnakeSize);
                    break;
                case "D":
                    Canvas.SetLeft(snake[0], Canvas.GetLeft(snake[0]) + SnakeSize);
                    break;
                default: break;
            }
            //   
            if (Canvas.GetTop(food) == Canvas.GetTop(snake[0]) && Canvas.GetLeft(food) == Canvas.GetLeft(snake[0]))
            {
                Border b = new Border();
                b.Width = b.Height = SnakeSize;
                b.CornerRadius = new CornerRadius(90);
                b.Background = food.Background;
                back.Children.Add(b);
                Canvas.SetLeft(b, Canvas.GetLeft(snake[snake.Count - 1]));
                Canvas.SetTop(b, Canvas.GetTop(snake[snake.Count - 1]));
                Canvas.SetZIndex(b, 3);
                snake.Add(b);

                food.Background = new RadialGradientBrush(Color.FromRgb((byte)r.Next(0, 256), (byte)r.Next(0, 256), (byte)r.Next(0, 256)), Color.FromRgb((byte)r.Next(0, 256), (byte)r.Next(0, 256), (byte)r.Next(0, 256)));
                Canvas.SetLeft(food, r.Next(0, 20) * rows);
                Canvas.SetTop(food, r.Next(0, 20) * column);
            }
            //    
            if (Canvas.GetLeft(snake[0]) >= 800)
            {
                Canvas.SetLeft(snake[0], 0);
            }
            if (Canvas.GetLeft(snake[0]) <= -20)
            {
                Canvas.SetLeft(snake[0], 780);
            } if (Canvas.GetTop(snake[0]) <= -20)
            {
                Canvas.SetTop(snake[0], 780);
            } if (Canvas.GetTop(snake[0]) >= 800)
            {
                Canvas.SetTop(snake[0], 0);
            }
            //    
            for (int i = snake1.Count - 1; i > 0; i--)
            {
                Canvas.SetLeft(snake1[i], Canvas.GetLeft(snake1[i - 1]));
                Canvas.SetTop(snake1[i], Canvas.GetTop(snake1[i - 1]));
            }
            switch (snake1[0].Tag.ToString())
            {
                case "W":
                    Canvas.SetTop(snake1[0], Canvas.GetTop(snake1[0]) - SnakeSize);
                    break;
                case "A":
                    Canvas.SetLeft(snake1[0], Canvas.GetLeft(snake1[0]) - SnakeSize);
                    break;
                case "S":
                    Canvas.SetTop(snake1[0], Canvas.GetTop(snake1[0]) + SnakeSize);
                    break;
                case "D":
                    Canvas.SetLeft(snake1[0], Canvas.GetLeft(snake1[0]) + SnakeSize);
                    break;
                default: break;
            }
            //   
            if (Canvas.GetTop(food) == Canvas.GetTop(snake1[0]) && Canvas.GetLeft(food) == Canvas.GetLeft(snake1[0]))
            {
                Border b = new Border();
                b.Width = b.Height = SnakeSize;
                b.CornerRadius = new CornerRadius(90);
                b.Background = food.Background;
                back.Children.Add(b);
                Canvas.SetLeft(b, Canvas.GetLeft(snake1[snake1.Count - 1]));
                Canvas.SetTop(b, Canvas.GetTop(snake1[snake1.Count - 1]));
                Canvas.SetZIndex(b, 3);
                snake1.Add(b);

                food.Background = new RadialGradientBrush(Color.FromRgb((byte)r.Next(0, 256), (byte)r.Next(0, 256), (byte)r.Next(0, 256)), Color.FromRgb((byte)r.Next(0, 256), (byte)r.Next(0, 256), (byte)r.Next(0, 256)));
                Canvas.SetLeft(food, r.Next(0, 20) * rows);
                Canvas.SetTop(food, r.Next(0, 20) * column);
            }
            //    
            if (Canvas.GetLeft(snake1[0]) >= 800)
            {
                Canvas.SetLeft(snake1[0], 0);
            }
            if (Canvas.GetLeft(snake1[0]) <= -20)
            {
                Canvas.SetLeft(snake1[0], 780);
            } if (Canvas.GetTop(snake1[0]) <= -20)
            {
                Canvas.SetTop(snake1[0], 780);
            } if (Canvas.GetTop(snake1[0]) >= 800)
            {
                Canvas.SetTop(snake1[0], 0);
            }
        }
        //  
        private void CreateSnake()
        {
            for (int i = 0; i < 3; i++)
            {
                Border kk = new Border();
                kk.Width = kk.Height = SnakeSize;
                kk.CornerRadius = new CornerRadius(90);
                kk.Background = new RadialGradientBrush(Color.FromRgb((byte)r.Next(0, 256), (byte)r.Next(0, 256), (byte)r.Next(0, 256)), Color.FromRgb((byte)r.Next(0, 256), (byte)r.Next(0, 256), (byte)r.Next(0, 256)));
                back.Children.Add(kk);
                Canvas.SetLeft(kk, (2 - i) * SnakeSize);
                Canvas.SetTop(kk, rows / 2 * SnakeSize);
                Canvas.SetZIndex(kk, 3);
                snake.Add(kk);
                snake[0].Tag = "D";
            }
        }
        //    
        private void CreateSnake1()
        {
            for (int i = 0; i < 3; i++)
            {
                Border kk = new Border();
                kk.Width = kk.Height = SnakeSize;
                kk.CornerRadius = new CornerRadius(90);
                kk.Background = new RadialGradientBrush(Color.FromRgb((byte)r.Next(0, 256), (byte)r.Next(0, 256), (byte)r.Next(0, 256)), Color.FromRgb((byte)r.Next(0, 256), (byte)r.Next(0, 256), (byte)r.Next(0, 256)));
                back.Children.Add(kk);
                Canvas.SetLeft(kk, i * SnakeSize + 740);
                Canvas.SetTop(kk, rows / 2 * SnakeSize);
                Canvas.SetZIndex(kk, 3);
                snake1.Add(kk);
                snake1[0].Tag = "A";
            }
        }

    }
}

버튼 이벤트와 Dispatcher Timer를 통해 뱀의 이동을 제어하고, Dispatcher Timer는 뱀이 먹이를 먹고 먹이가 먹힌 후 위치를 바꾸어 뱀의 몸을 늘리는 것을 실현했다.

좋은 웹페이지 즐겨찾기