Здравствуйте, в данном уроке я хотел бы продемонстрировать создание простенькой игры на Visual Basic. Сразу хотелось бы оговориться, что наш вариант игры ориентирован на 2-х игроков, т.е. поддержки так называемого ИИ (искусстевнного интелекта) не будет. Итак, открываем Excel и запускаем там редактор Visual Basic. На главную форму UserForm1 поместим 11 элементов TextBox, 4 элемента Label и 1 элемент CommandButton. Первые 9 элементов TextBox - это те ячейки, куда мы будем ставить крестики и нолики, далее еще 2 оставшихся элемента TextBox будут расположены слева (для 1-го игрока) и справа (для 2-го игрока) от основных девяти игровых ячеек TextBox. В эти две ячейки мы будем вписывать имена (ники) 1-го и 2-го игроков соответственно. Далее, помещаем элемент Label1 над элементом TextBox10 и в графе свойств Caption элемента Label1 вписываем "Игрок 1". Помещаем элемент Label2 над элементом TextBox11 и в графе свойств Caption элемента Label2 вписываем "Игрок 2". Помещаем элементы Label3 и Label4 под элементами TextBox10 и TextBox11 соответственно. Здесь при выигрыше одного из игроков будет появляться надпись WIN и имя одного из игроков. Допустим, если победил 1-й игрок, а его ник был ВАСЯ, то при выигрыше появится слева надпись "ВАСЯ WIN". Под этими элементами помещаем еще 4 метки, так, чтобы Label7 была под Label5, а Label8 под Label6. В значение Caption элементов Label5 и Label6 вписываем слово "Победы:" - ниже будут прибавляться сами победы. Т.е. если победил 1-й игрок, то при нажатии на кнопку "Новая игра", в эл-те Label7 появится единичка, означающая, что у игрока 1 одна победа, если он бобедил еще раз, то будет уже 2 и т.д. Элемент CommandButton1 - это кнопка. Она нужна нам для того, чтобы обнулить все результаты и начать новую игру. В графе Caption элемента CommandButton1 впишите "Новая игра". Итак, теперь приступим непосредственно к написанию самого кода игры. Дважды щелкнем на элементе CommandButton1 и пропишем следующий код: If Label3.Caption = TextBox10.Text + " WIN" Then Label7.Caption = Label7.Caption + 1 End If If Label4.Caption = TextBox11.Text + " WIN" Then Label8.Caption = Label8.Caption + 1 End If TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = "" TextBox4.Text = "" TextBox5.Text = "" TextBox6.Text = "" TextBox7.Text = "" TextBox8.Text = "" TextBox9.Text = "" Label3.Caption = "" Label4.Caption = "" Проверьте как работает кнопка. Впишите в поля, перечисленные в коде какие- нибудь символы, если при нажатии на кнопку, все убралось, то код прописан правильно. Теперь, разберемся с основными ячейками, куда будем ставить "крестик" и "нолик". Крестик у нас будет заменять английская буква "x", а нолик - "o". Всего у нас восемь вариантов победы - это 3 варианта сверху-вниз, 3 варианта слева-направо и 2 варианта наискосок. Крестики - это всегда 1-й игрок, нолики - всегда 2-й. Щелкаем дважды на элементе TextBox1 и вписываем туда следующий код-условие: If TextBox1.Text = "x" And TextBox2.Text = "x" And TextBox3.Text = "x" Then Label3.Caption = TextBox10.Text + " WIN" End If Этот код означает, что если три крестика есть в элементах 1-2-3, то в элементе Label3 появится надпись ИМЯ + "WIN". Если условие, задействующие первые 3 элемента вписано в элемент TextBox1, то это же условие должно быть вписано и в элемент TextBox2 и в элемент TextBox3. И так же со всеми остальными элементами. Теперь пропишем сразу несколько условий, в которых у нас будет задействован элемент TextBox1, т.е. ячейка 1 (сразу для крестиков и ноликов). Вместе с предыдущим кодом все это будет выглядеть так: If TextBox1.Text = "x" And TextBox2.Text = "x" And TextBox3.Text = "x" Then Label3.Caption = TextBox10.Text + " WIN" End If If TextBox1.Text = "x" And TextBox4.Text = "x" And TextBox7.Text = "x" Then Label3.Caption = TextBox10.Text + " WIN" End If If TextBox1.Text = "x" And TextBox5.Text = "x" And TextBox9.Text = "x" Then Label3.Caption = TextBox10.Text + " WIN" End If If TextBox1.Text = "o" And TextBox2.Text = "o" And TextBox3.Text = "o" Then Label4.Caption = TextBox11.Text + " WIN" End If If TextBox1.Text = "o" And TextBox4.Text = "o" And TextBox7.Text = "o" Then Label4.Caption = TextBox11.Text + " WIN" End If If TextBox1.Text = "o" And TextBox5.Text = "o" And TextBox9.Text = "o" Then Label4.Caption = TextBox11.Text + " WIN" End If Теперь прописываем код-условие для других ячеек. Элемент TextBox2: If TextBox1.Text = "x" And TextBox2.Text = "x" And TextBox3.Text = "x" Then Label3.Caption = TextBox10.Text + " WIN" End If If TextBox2.Text = "x" And TextBox5.Text = "x" And TextBox8.Text = "x" Then Label3.Caption = TextBox10.Text + " WIN" End If If TextBox1.Text = "o" And TextBox2.Text = "o" And TextBox3.Text = "o" Then Label4.Caption = TextBox11.Text + " WIN" End If If TextBox2.Text = "o" And TextBox5.Text = "o" And TextBox8.Text = "o" Then Label4.Caption = TextBox11.Text + " WIN" End If Элемент TextBox3: If TextBox1.Text = "x" And TextBox2.Text = "x" And TextBox3.Text = "x" Then Label3.Caption = TextBox10.Text + " WIN" End If If TextBox3.Text = "x" And TextBox6.Text = "x" And TextBox9.Text = "x" Then Label3.Caption = TextBox10.Text + " WIN" End If If TextBox3.Text = "x" And TextBox5.Text = "x" And TextBox7.Text = "x" Then Label3.Caption = TextBox10.Text + " WIN" End If If TextBox1.Text = "o" And TextBox2.Text = "o" And TextBox3.Text = "o" Then Label4.Caption = TextBox11.Text + " WIN" End If If TextBox3.Text = "o" And TextBox6.Text = "o" And TextBox9.Text = "o" Then Label4.Caption = TextBox11.Text + " WIN" End If If TextBox3.Text = "o" And TextBox5.Text = "o" And TextBox7.Text = "o" Then Label4.Caption = TextBox11.Text + " WIN" End If Элемент TextBox4: If TextBox1.Text = "x" And TextBox4.Text = "x" And TextBox7.Text = "x" Then Label3.Caption = TextBox10.Text + " WIN" End If If TextBox4.Text = "x" And TextBox5.Text = "x" And TextBox6.Text = "x" Then Label3.Caption = TextBox10.Text + " WIN" End If If TextBox1.Text = "o" And TextBox4.Text = "o" And TextBox7.Text = "o" Then Label4.Caption = TextBox11.Text + " WIN" End If If TextBox4.Text = "o" And TextBox5.Text = "o" And TextBox6.Text = "o" Then Label4.Caption = TextBox11.Text + " WIN" End If Элемент TextBox5: If TextBox1.Text = "x" And TextBox5.Text = "x" And TextBox9.Text = "x" Then Label3.Caption = TextBox10.Text + " WIN" End If If TextBox4.Text = "x" And TextBox5.Text = "x" And TextBox6.Text = "x" Then Label3.Caption = TextBox10.Text + " WIN" End If If TextBox2.Text = "x" And TextBox5.Text = "x" And TextBox8.Text = "x" Then Label3.Caption = TextBox10.Text + " WIN" End If If TextBox3.Text = "x" And TextBox5.Text = "x" And TextBox7.Text = "x" Then Label3.Caption = TextBox10.Text + " WIN" End If If TextBox1.Text = "o" And TextBox5.Text = "o" And TextBox9.Text = "o" Then Label4.Caption = TextBox11.Text + " WIN" End If If TextBox4.Text = "o" And TextBox5.Text = "o" And TextBox6.Text = "o" Then Label4.Caption = TextBox11.Text + " WIN" End If If TextBox2.Text = "o" And TextBox5.Text = "o" And TextBox8.Text = "o" Then Label4.Caption = TextBox11.Text + " WIN" End If If TextBox3.Text = "o" And TextBox5.Text = "o" And TextBox7.Text = "o" Then Label4.Caption = TextBox11.Text + " WIN" End If Элемент TextBox6: If TextBox4.Text = "x" And TextBox5.Text = "x" And TextBox6.Text = "x" Then Label3.Caption = TextBox10.Text + " WIN" End If If TextBox3.Text = "x" And TextBox6.Text = "x" And TextBox9.Text = "x" Then Label3.Caption = TextBox10.Text + " WIN" End If If TextBox4.Text = "o" And TextBox5.Text = "o" And TextBox6.Text = "o" Then Label4.Caption = TextBox11.Text + " WIN" End If If TextBox3.Text = "o" And TextBox6.Text = "o" And TextBox9.Text = "o" Then Label4.Caption = TextBox11.Text + " WIN" End If Элемент TextBox7: If TextBox1.Text = "x" And TextBox4.Text = "x" And TextBox7.Text = "x" Then Label3.Caption = TextBox10.Text + " WIN" End If If TextBox7.Text = "x" And TextBox8.Text = "x" And TextBox9.Text = "x" Then Label3.Caption = TextBox10.Text + " WIN" End If If TextBox3.Text = "x" And TextBox5.Text = "x" And TextBox7.Text = "x" Then Label3.Caption = TextBox10.Text + " WIN" End If If TextBox1.Text = "o" And TextBox4.Text = "o" And TextBox7.Text = "o" Then Label4.Caption = TextBox11.Text + " WIN" End If If TextBox7.Text = "o" And TextBox8.Text = "o" And TextBox9.Text = "o" Then Label4.Caption = TextBox11.Text + " WIN" End If If TextBox3.Text = "o" And TextBox5.Text = "o" And TextBox7.Text = "o" Then Label4.Caption = TextBox11.Text + " WIN" End If Элемент TextBox8: If TextBox7.Text = "x" And TextBox8.Text = "x" And TextBox9.Text = "x" Then Label3.Caption = TextBox10.Text + " WIN" End If If TextBox2.Text = "x" And TextBox5.Text = "x" And TextBox8.Text = "x" Then Label3.Caption = TextBox10.Text + " WIN" End If If TextBox7.Text = "o" And TextBox8.Text = "o" And TextBox9.Text = "o" Then Label4.Caption = TextBox11.Text + " WIN" End If If TextBox2.Text = "o" And TextBox5.Text = "o" And TextBox8.Text = "o" Then Label4.Caption = TextBox11.Text + " WIN" End If Элемент TextBox9: If TextBox1.Text = "x" And TextBox5.Text = "x" And TextBox9.Text = "x" Then Label3.Caption = TextBox10.Text + " WIN" End If If TextBox7.Text = "x" And TextBox8.Text = "x" And TextBox9.Text = "x" Then Label3.Caption = TextBox10.Text + " WIN" End If If TextBox3.Text = "x" And TextBox6.Text = "x" And TextBox9.Text = "x" Then Label3.Caption = TextBox10.Text + " WIN" End If If TextBox1.Text = "o" And TextBox5.Text = "o" And TextBox9.Text = "o" Then Label4.Caption = TextBox11.Text + " WIN" End If If TextBox7.Text = "o" And TextBox8.Text = "o" And TextBox9.Text = "o" Then Label4.Caption = TextBox11.Text + " WIN" End If If TextBox3.Text = "o" And TextBox6.Text = "o" And TextBox9.Text = "o" Then Label4.Caption = TextBox11.Text + " WIN" End If Ну вот впринципе и всё. Проверьте как все работает. При 3-х крестиках надпись должна появляться слева, а при 3-х ноликах - справа. Надеюсь, вам понравилось
|