WPF 타자 게임

32013 단어 C# 모듈
본고는 C#를 이용하여 WPF를 이용하여 타자 게임을 편집하여 지정된 버튼을 누르면 비행기에서 총알을 쏘아 혐오감을 일으키고 자모를 맞힌 후 총알과 자막이 사라진다. 코드는 다음과 같다. 편집기 인터페이스에서 전체적인 배경과 크기에 대한 설정을 실현하고 버튼 공간을 가져와 시작과 끝을 실시간으로 보여준다.
"    2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="700" Width="1000" WindowStyle="None" Background="Pink" AllowsTransparency="False" Loaded="Window_Loaded" Left="0" Top="0">
    "back" Background="GreenYellow" Width="800" Height="700" Margin="0,0,200,0" >
        <Button Content="    " Canvas.Left="855" Canvas.Top="70" Width="75" Click="Button_Click"/>
        <Button Content="    " Canvas.Left="855" Canvas.Top="152" Width="76" RenderTransformOrigin="0.499,2.358" Click="Button_Click_1"/>
    


주체의 코드는 실례화된 DispatcherTimer를 통해 모든 기능을 실현하고 for와foreach순환을 결합하여if판단을 통해 해당하는 대상을 찾는다
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;
using System.Windows.Media.Animation;

namespace     2
{
    /// 
    /// MainWindow.xaml      
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        //    
        DispatcherTimer t1 = new DispatcherTimer();
        DispatcherTimer t2 = new DispatcherTimer();
        DispatcherTimer t3 = new DispatcherTimer();
        DispatcherTimer t4 = new DispatcherTimer();
        DispatcherTimer t5 = new DispatcherTimer();
        MyImage plane = new MyImage();
        Random r = new Random();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            t1.Tick += t1_Tick;
            t2.Tick += t2_Tick;
            t3.Tick += t3_Tick; 
            t4.Tick += t4_Tick;
            t5.Tick += t5_Tick;
            KeyDown += MainWindow_KeyDown;
            MyImage ma = new MyImage();
            ma.type = "plane";
            ma.index = 0;
            plane.Tag = ma;
            plane.Width = 60;
            plane.Height = 60;
            plane.Source = new BitmapImage(new Uri("images/plane/plane1.png",UriKind.Relative));
            plane.Stretch = Stretch.UniformToFill;
            back.Children.Add(plane);
            Canvas.SetLeft(plane,back.Width/2-plane.Width/2);
            Canvas.SetTop(plane,back.Height-plane.Height-20);
            Canvas.SetZIndex(plane,5);
        }
        //    
        void MainWindow_KeyDown(object sender, KeyEventArgs e)
        {
            for (int i = 0; i < back.Children.Count; i++)
            {
                if (back.Children[i].GetType().Name == "Label"&&((Label)back.Children[i]).Tag.ToString()=="cha")
                {
                    if(e.Key.ToString().ToLower()==((Label)back.Children[i]).Content.ToString())
                    {
                        ((Label)back.Children[i]).Tag = "chara";
                        Canvas.SetLeft(plane,Canvas.GetLeft(back.Children[i])+((Label)back.Children[i]).Width/2-plane.Width/2);
                        MyImage bullet = new MyImage();
                        MyImage ma = new MyImage();
                        ma.type = "bullet";
                        ma.index = 0;
                        bullet.Tag = ma;
                        bullet.Width = 12;
                        bullet.Height = 28;
                        bullet.Source = new BitmapImage(new Uri("images/bullet/0.png", UriKind.Relative));
                        bullet.Stretch = Stretch.UniformToFill;
                        back.Children.Add(bullet);
                        Canvas.SetLeft(bullet,Canvas.GetLeft(plane)+ plane.Width / 2 - bullet.Width / 2);
                        Canvas.SetTop(bullet,Canvas.GetTop(plane));
                        Canvas.SetZIndex(bullet, 3);
                        break;
                    }
                }
            }
        }
        //   
        void t5_Tick(object sender, EventArgs e)
        {
            foreach(UIElement bullet in back.Children)
            {
                if (bullet.GetType().Name == "MyImage" && ((MyImage)((MyImage)bullet).Tag).type == "bullet")
                {
                    MyImage smoke = new MyImage();
                    MyImage ma = new MyImage();
                    ma.type = "smoke";
                    ma.index = 0;
                    smoke.Tag = ma;
                    smoke.Width = 20;
                    smoke.Height = 20;
                    smoke.Source = new BitmapImage(new Uri("images/blue/0.png", UriKind.Relative));
                    smoke.Stretch = Stretch.UniformToFill;
                    back.Children.Add(smoke);
                    Canvas.SetLeft(smoke, Canvas.GetLeft(bullet) + ((MyImage)bullet).Width / 2 - smoke.Width / 2);
                    Canvas.SetTop(smoke, Canvas.GetTop(bullet) + ((MyImage)bullet).Height);
                    Canvas.SetZIndex(smoke, 4);
                    return;
                }
            }
        }
        //    
        int q;
        void t4_Tick(object sender, EventArgs e)
        {
            foreach(UIElement smoke in back.Children)
            {
                if (smoke.GetType().Name == "MyImage" && ((MyImage)((MyImage)smoke).Tag).type == "smoke")
                {
                    q = ((MyImage)((MyImage)smoke).Tag).index++;
                    if (((MyImage)((MyImage)smoke).Tag).index > 10)
                    {
                        back.Children.Remove(smoke);
                        return;
                    }
                    ((MyImage)smoke).Source = new BitmapImage(new Uri("images/blue/" + q + ".png", UriKind.Relative));
                }
            }
        }
        //        
        int x, y;
        void t3_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < back.Children.Count;i++ )
            {
                if(back.Children[i].GetType().Name=="MyImage"&&((MyImage)((MyImage)back.Children[i]).Tag).type=="bullet")
                {
                    x = ((MyImage)((MyImage)back.Children[i]).Tag).index++;
                    if (((MyImage)((MyImage)back.Children[i]).Tag).index>2)
                    {
                        ((MyImage)((MyImage)back.Children[i]).Tag).index = 0;
                        continue;
                    }
                    ((MyImage)back.Children[i]).Source = new BitmapImage(new Uri("images/bullet/"+x+".png",UriKind.Relative));
                }
            }
            for (int i = 0; i < back.Children.Count; i++)
            {
                if (back.Children[i].GetType().Name == "MyImage" && ((MyImage)((MyImage)back.Children[i]).Tag).type == "bomb")
                {
                    y = ((MyImage)((MyImage)back.Children[i]).Tag).index++;
                    if (((MyImage)((MyImage)back.Children[i]).Tag).index > 10)
                    {
                        back.Children.Remove(back.Children[i]);
                        return;
                    }
                    ((MyImage)back.Children[i]).Source = new BitmapImage(new Uri("images/bomb/" + y + ".png", UriKind.Relative));
                }
            }
        }
        //      
        void t2_Tick(object sender, EventArgs e)
        {
            foreach(UIElement cha in back.Children)
            {
                if (cha.GetType().Name == "Label")
                {
                    Canvas.SetTop(cha, Canvas.GetTop(cha) + 1);
                    Canvas.SetBottom(cha, Canvas.GetTop(cha) + ((Label)cha).Height);
                    if (Canvas.GetBottom(cha) >= Canvas.GetTop(plane))
                    {
                        back.Children.Remove(cha);
                    }
                }
                foreach(UIElement bullet in back.Children)
                {
                    if (bullet.GetType().Name == "MyImage" && ((MyImage)((MyImage)bullet).Tag).type == "bullet")
                    {
                        Canvas.SetTop(bullet,Canvas.GetTop(bullet)-1);
                        if (cha.GetType().Name == "Label" && ((Label)cha).Tag.ToString() == "chara")
                        {
                            if (Canvas.GetBottom(cha) >= Canvas.GetTop(bullet))
                            {
                                MyImage bomb = new MyImage();
                                MyImage ma = new MyImage();
                                ma.type = "bomb";
                                ma.index = 0;
                                bomb.Tag = ma;
                                bomb.Width = 40;
                                bomb.Height = 40;
                                bomb.Source = new BitmapImage(new Uri("images/bomb/0.png", UriKind.Relative));
                                bomb.Stretch = Stretch.UniformToFill;
                                back.Children.Add(bomb);
                                Canvas.SetLeft(bomb, Canvas.GetLeft(cha));
                                Canvas.SetTop(bomb, Canvas.GetTop(cha));
                                Canvas.SetZIndex(bomb, 4);
                                back.Children.Remove(bullet);
                                back.Children.Remove(cha);
                                return;
                            }
                        }
                    }
                }
            }
        }
        //   
        void t1_Tick(object sender, EventArgs e)
        {
            Label cha = new Label();
            cha.Tag = "cha";
            cha.Height = 70;
            cha.Width = 70;
            cha.Content = ((char)r.Next(97, 123)).ToString();
            cha.Foreground = new LinearGradientBrush(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)),45);
            cha.FontSize = r.Next(30,40);
            cha.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
            cha.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
            back.Children.Add(cha);
            Canvas.SetLeft(cha,r.Next(0,(int)(back.Width-cha.Width)));
            Canvas.SetTop(cha,0);
            Canvas.SetZIndex(cha,1);
        }
        //  
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            t1.Start();
            t1.Interval = TimeSpan.FromMilliseconds(1000);
            t2.Start();
            t2.Interval = TimeSpan.FromMilliseconds(10);
            t3.Start();
            t3.Interval = TimeSpan.FromMilliseconds(300);
           // t4.Start();
            t4.Interval = TimeSpan.FromMilliseconds(100);
            t5.Interval = TimeSpan.FromMilliseconds(100);
            t5.Start();
        }
        //  
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}

총알, 지연, 폭발 효과가 모두 Image 유형이기 때문에 서로를 잘 구분하고 동료가 애니메이션 재생을 실현하기 위해서는 라벨과 번호를 포함하는 새로운 종류를 도입해야 한다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace     2
{
 //      Image
    class MyImage:System.Windows.Controls.Image
    {
        public string type;
        public int index;
    }
}

이 게임은 주로 Dispatcher Timer를 통해 프로그램의 실행을 실현하고 for와foreach 순환과if판단을 통해 해당하는 기능을 실현한다.최종적으로 컨트롤의 구체적인 사용 방법을 배울 수 있기 때문에 관심 있는 것은 더 많은 새로운 기능을 추가할 수 있다

좋은 웹페이지 즐겨찾기