• 学习c#桌面应用编程 --- 我的第一个游戏


    场景

    我需要做一个c#桌面窗口软件,但是我曾经都是专职于java开发,但是java对windows并不是特别友好(awt除外),于是必须需要掌握c#桌面编程,所以我需要手动做一个小游戏,来学习c#的一些基本桌面应用的知识。

    开始

    这是一个连连看的小游戏

    namespace game
    {
        public partial class Form1 : Form
        {
            // Use this Random object to choose random icons for the squares
            Random random = new Random();
    
            // Each of these letters is an interesting icon
            // in the Webdings font,
            // and each icon appears twice in this list
            List<string> icons = new List<string>()
    
        {
            "!", "!", "N", "N", ",", ",", "k", "k",
            "b", "b", "v", "v", "w", "w", "z", "z"
        };
    
    
            // firstClicked points to the first Label control 
            // that the player clicks, but it will be null 
            // if the player hasn't clicked a label yet
            Label firstClicked = null;
    
            // secondClicked points to the second Label control 
            // that the player clicks
            Label secondClicked = null;
    
            /// 
            /// Assign each icon from the list of icons to a random square
            /// 
            private void AssignIconsToSquares()
            {
                // The TableLayoutPanel has 16 labels,
                // and the icon list has 16 icons,
                // so an icon is pulled at random from the list
                // and added to each label
                foreach (Control control in tableLayoutPanel1.Controls)
                {
                    Label iconLabel = control as Label;
                    if (iconLabel != null)
                    {
                        int randomNumber = random.Next(icons.Count);
                        iconLabel.Text = icons[randomNumber];
                        iconLabel.ForeColor = iconLabel.BackColor;
                        icons.RemoveAt(randomNumber);
                    }
                }
            }
    
            public Form1()
            {
                InitializeComponent();
                AssignIconsToSquares();
    
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            /// 
            /// Every label's Click event is handled by this event handler
            /// 
            /// The label that was clicked
            /// 
            private void label1_Click(object sender, EventArgs e)
            {
                // The timer is only on after two non-matching 
                // icons have been shown to the player, 
                // so ignore any clicks if the timer is running
                if (timer1.Enabled == true)
                    return;
    
                Label clickedLabel = sender as Label;
    
                if (clickedLabel != null)
                {
                    // If the clicked label is black, the player clicked
                    // an icon that's already been revealed --
                    // ignore the click
                    if (clickedLabel.ForeColor == Color.Black)
                        return;
    
                    // If firstClicked is null, this is the first icon
                    // in the pair that the player clicked, 
                    // so set firstClicked to the label that the player 
                    // clicked, change its color to black, and return
                    if (firstClicked == null)
                    {
                        firstClicked = clickedLabel;
                        firstClicked.ForeColor = Color.Black;
                        return;
                    }
    
                    // Check to see if the player won
                    CheckForWinner();
    
    
                    // If the player gets this far, the timer isn't
                    // running and firstClicked isn't null,
                    // so this must be the second icon the player clicked
                    // Set its color to black
                    secondClicked = clickedLabel;
                    secondClicked.ForeColor = Color.Black;
    
                    // If the player clicked two matching icons, keep them 
                    // black and reset firstClicked and secondClicked 
                    // so the player can click another icon
                    if (firstClicked.Text == secondClicked.Text)
                    {
                        firstClicked = null;
                        secondClicked = null;
                        return;
                    }
    
                    // If the player gets this far, the player 
                    // clicked two different icons, so start the 
                    // timer (which will wait three quarters of 
                    // a second, and then hide the icons)
                    timer1.Start();
                    CheckForWinner();
                }
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
    
                // Stop the timer
                timer1.Stop();
    
                // Hide both icons
                firstClicked.ForeColor = firstClicked.BackColor;
                secondClicked.ForeColor = secondClicked.BackColor;
    
                // Reset firstClicked and secondClicked 
                // so the next time a label is
                // clicked, the program knows it's the first click
                firstClicked = null;
                secondClicked = null;
            }
    
            private void CheckForWinner()
            {
                // Go through all of the labels in the TableLayoutPanel, 
                // checking each one to see if its icon is matched
                foreach (Control control in tableLayoutPanel1.Controls)
                {
                    Label iconLabel = control as Label;
    
                    if (iconLabel != null)
                    {
                        if (iconLabel.ForeColor == iconLabel.BackColor)
                            return;
                    }
                }
    
                // If the loop didn’t return, it didn't find
                // any unmatched icons
                // That means the user won. Show a message and close the form
                MessageBox.Show("You matched all the icons!", "Congratulations");
                Close();
            }
        }
    
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167

    效果

    在这里插入图片描述

    在这里插入图片描述

    结束

    有个大致的了解,可以开始我的开发了。

    对于新的技术,我的总结是:
    文档生demo,demo生一,一生万物

  • 相关阅读:
    ROS中结合C++语言实现HelloWorld
    大厂外包,值得拥有吗?
    (rabbitmq的高级特性)死信交换机
    Cyanine5 NHS ester免疫荧光染色146368-14-1星戈瑞
    网络—网络通信基础(理论)
    【论文阅读】 Dimensionality reduction for large-scale neural recordings
    5.javase_循环语句
    大家都能看得懂的源码 - 如何封装 cookie/localStorage/sessionStorage hook?
    Bean 的作用域和生命周期
    深入理解Python中的布尔值:真与假
  • 原文地址:https://blog.csdn.net/weixin_45487988/article/details/133909495