别踩白块的两种模式

本节主要讲解别踩白块,游戏采用的方法较为简单,主要学习C#中如何创建动态数组,来存放相应的空间,游戏分为两个模式,模式一通过点击来实现移动,模式二(也就是常见的模式)方块移动,手动点击,并且方块速度会加快,代码如下:
编辑器界面

<Window x:Class="别猜百块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="600" WindowStyle="None" Background="Pink" AllowsTransparency="False" Loaded="Window_Loaded" Left="400" Top="0">
    <Canvas Name="back" Margin="0,0,200,0" Height="800" Width="400" >
        <Button Content="模式一" Canvas.Left="469" Canvas.Top="130" Width="75" Click="Button_Click"/>
        <Button Content="结束游戏" Canvas.Left="469" Canvas.Top="324" Width="75" Click="Button_Click_1"/>
        <Button Content="模式二" Canvas.Left="469" Canvas.Top="230" Width="75" Click="Button_Click_2"/>
        <Label Content="总分:" Canvas.Left="469" Canvas.Top="499" Width="40" Height="30" Background="Bisque" HorizontalContentAlignment="Center" Tag="plus"/>
        <Label Content="0" Canvas.Left="504" Canvas.Top="499" Background="Aquamarine" Width="50" Height="30" Name="g" Tag="plus"/>
    </Canvas>
</Window>

主体代码

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
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        DispatcherTimer t1 = new DispatcherTimer();
        Random r = new Random();
        List<Label> list = new List<Label>();
        List<Label> list1 = new List<Label>();
        Image pic = new Image();
        int speed=2;
        int total=0;
        int k = 0;
        //欢迎界面
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            t1.Tick += t1_Tick;
            t1.Interval = TimeSpan.FromMilliseconds(10);
            pic.Source = new BitmapImage(new Uri("images/582b47eaa0142442528b1e4e7e1ff2c1_u=1095098125,3201950109&fm=21&gp=0.jpg", UriKind.Relative));
            pic.Stretch = Stretch.Fill;
            back.Children.Add(pic);
            pic.Height = back.Height;
            pic.Width = back.Width;
            Canvas.SetLeft(pic,0);
            Canvas.SetTop(pic,0);
        }

        //模式一
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //通过k来防止连续按同一个按钮而造成的错误,因为数组是全局的,不可能实例化两次
            if(k==1)
            {
                return;
            }
            else if(k==2||k==0)
            {
                total = 0;
                g.Content = total.ToString();
                for (int i = k*10-1; i >= 0;i-- )
                {
                    t1.Stop();
                    back.Children.Remove(list1[i]);
                    list1.Remove(list1[i]);
                }
                k = 1;
                for (int i = 0; i < 20; i++)
                {
                    Label body = new Label();
                    body.Width = 100;
                    body.Height = 200;
                    list.Add(body);
                    list[i].Tag = i;
                    list[i].BorderBrush = Brushes.DarkOrange;
                    list[i].BorderThickness = new Thickness(1);
                    back.Children.Add(list[i]);
                    Canvas.SetLeft(list[i], (i % 4) * body.Width);
                    Canvas.SetTop(list[i], (i / 4 - 1) * body.Height);
                }
                for (int i = 0; i < 5; i++)
                {
                    list[r.Next(0, 4) + i * 4].Background = Brushes.Black;
                }
                for (int i = 0; i < 20; i++)
                {
                   //点击事件
                    list[i].MouseLeftButtonDown += MainWindow_MouseLeftButtonDown;
                    if (list[i].Background != Brushes.Black)
                    {
                        list[i].Background = Brushes.White;
                    }
                }
            }
        }
        //模式一点击
        void MainWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Label body = sender as Label;
            if (body.Background == Brushes.Black && (int)body.Tag > 15)
            {
                total++;
                g.Content = total.ToString();
                //点击之后更换颜色,并且向下移动
                for (int i = 19; i > 3; i--)
                {
                    list[i].Background = list[i - 4].Background;
                }
                for (int i = 0; i < 4; i++)
                {
                    list[i].Background = Brushes.White;
                }
                list[r.Next(0, 4)].Background = Brushes.Black;
                for (int i = 0; i < 4; i++)
                {
                    if (list[i].Background != Brushes.Black)
                    {
                        list[i].Background = Brushes.White;
                    }
                }
            }
        }
        //模式二
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            if(k==1||k==0)
            {
                total = 0;
                g.Content = total.ToString();
                for (int i = k*20-1; i >= 0; i--)
                {
                    back.Children.Remove(list[i]);
                    list.Remove(list[i]);
                }
                k = 2;
                for (int i = 0; i < 20; i++)
                {
                    Label body = new Label();
                    body.Width = 100;
                    body.Height = 200;
                    list1.Add(body);
                    list1[i].BorderBrush = Brushes.DarkOrange;
                    list1[i].BorderThickness = new Thickness(1);
                    back.Children.Add(list1[i]);
                    Canvas.SetLeft(list1[i], (i % 4) * body.Width);
                    Canvas.SetTop(list1[i], (i / 4 - 1) * body.Height);
                }
                for (int i = 0; i < 5; i++)
                {
                    list1[r.Next(0, 4) + i * 4].Background = Brushes.Black;
                }
                for (int i = 0; i < 20; i++)
                {
                    list1[i].MouseLeftButtonDown += MainWindow1_MouseLeftButtonDown;
                    if (list1[i].Background != Brushes.Black)
                    {
                        list1[i].Background = Brushes.White;
                    }
                    if (i > 15)
                    {
                        list1[i].Tag = "black";
                    }
                    else
                        list1[i].Tag = "else";
                }
                t1.Start();
            }
            else if(k==2)
            {
                return ;
            }
        }
        //模式二点击
        void MainWindow1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Label body = sender as Label;
            if(body.Background==Brushes.Black&&body.Tag.ToString()=="black")
            {
               //计分
                total++;
                g.Content = total.ToString();
                if (total%10 == 0)
                {
                    speed += 2;
                }
                for (int i = 0; i < 20;i++ )
                {
                    if(list1[i]==body)
                    {
                        for (int j = (i / 4) * 4; j < (i / 4 + 1) * 4;j++ )
                        {
                            list1[j].Tag = "else";
                            list1[j].Background = Brushes.White;
                        }
                        list1[i].Background = Brushes.ForestGreen;
                        if(i>3)
                        {
                            for (int j = (i / 4-1) * 4; j < (i / 4) * 4; j++)
                            {
                                list1[j].Tag = "black";
                            }
                        }
                        else for(int j=16;j<20;j++)
                            {
                                list1[j].Tag = "black";
                            }
                    }
                }
            }
        }
        //模式二移动
        void t1_Tick(object sender, EventArgs e)
        {
            for (int i = 19; i >= 0; i--)
            {
                if (Canvas.GetTop(list1[i]) >= 800)
                {
                    for (int j = i - 3; j <= i;j++ )
                    {
                        if(list1[j].Background==Brushes.Black)
                        {
                            for (int k = (i / 4) * 4; k < (i / 4 + 1) * 4; k++)
                            {
                                list1[k].Tag = "else";
                                list1[k].Background = Brushes.White;
                            }
                            if (i > 3)
                            {
                                for (int k = (i / 4 - 1) * 4; k < (i / 4) * 4; k++)
                                {
                                    list1[k].Tag = "black";
                                }
                            }
                            else for (int k = 16; k < 20; k++)
                                {
                                    list1[k].Tag = "black";
                                }
                        }
                    }
                    for (int j = i - 3; j <= i; j++)
                    {
                        list1[j].Background = Brushes.White;
                        Canvas.SetTop(list1[j], -list1[j].Height);
                    }
                    list1[r.Next(0, 4) + (i / 4) * 4].Background = Brushes.Black;
                    for (int j = i - 3; j <= i;j++ )
                    {
                        if(list1[i].Background!=Brushes.Black)
                        {
                            list1[i].Background=Brushes.White;
                        }
                    }
                }
                Canvas.SetTop(list1[i], Canvas.GetTop(list1[i]) + speed);
            }
        }
        //结束
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}

本节主要讲解C#中动态数组的使用方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值