/// <summary> /// creates all buttons /// </summary> /// <param name="animal_type"></param> /// <param name="count"></param> /// <param name="use_type"></param> private List <Button> initAnimals(int animal_type, int count, string use_type) { //save all buttons initially created List <Button> btns = new List <Button>(); for (int i = 0; i < count; i++) { //creates button Button btn = new Button(); btn.Height = edge; btn.Width = edge; btn.BackColor = Color.Lavender; //initialize animals animal a = new animal(animal_type, use_type); //link animal to button btn.Tag = a; btns.Add(btn); } return(btns); }
/// <summary> /// left click /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void animalClick(object sender, EventArgs e) { Button btn = sender as Button; // achieve animal animal a = btn.Tag as animal; //if status is not start, then return if (status != "start") { return; } string color = this_user == 1 ? user1_color : user2_color; if (!a.isUse) { //Version 1: display text // btn.Text = AnimalType.getText(a.animal_type); //set blank background is white btn.BackColor = Color.White; if (a.user_type == "blue") { //version 1: change text color //btn.ForeColor = Color.Blue; //version 2:add picture btn.BackgroundImage = Image.FromFile(AnimalType.getImageBlue(a.animal_type)); btn.BackgroundImageLayout = ImageLayout.Stretch; } else { //version 1: change text color //btn.ForeColor = Color.Red; //version 2:add picture btn.BackgroundImage = Image.FromFile(AnimalType.getImageRed(a.animal_type)); btn.BackgroundImageLayout = ImageLayout.Stretch; } //change status to uncovered a.isUse = true; //initial click to determine which color is next if (user1_color == null) { user1_color = a.user_type; if (user1_color == "blue") { user2_color = "red"; //switch color this.this_user = 2; displayUser(); } else { user2_color = "blue"; //switch color this.this_user = 2; displayUser(); } } else { //switch this.this_user = this_user == 1 ? 2 : 1; displayUser(); } selectButton = null; } else if (color == a.user_type) { //select button this.selectButton = btn; } else if (selectButton != null) { //attack //determine distance Point p1 = (btn.Parent as Panel).Location; Point p2 = (selectButton.Parent as Panel).Location; //can only move up down left or right if (p1.X != p2.X && p1.Y != p2.Y) { return; } if (p1.Y == p2.Y) { if (Math.Abs(p1.X - p2.X) > this.edge) { return; } } else { if (Math.Abs(p1.Y - p2.Y) > this.edge) { return; } } //compare animal a2 = this.selectButton.Tag as animal; //cannot beat your own animals if (a2.user_type == a.user_type) { return; } if (!AnimalType.canEat(a2.animal_type, a.animal_type)) { //cannot beat return; } //can beat selectButton.Controls.Remove(selectButton); btn.Parent.Controls.Add(selectButton); btn.Parent.Controls.Remove(btn); //remove beaten ones if (a.user_type == "blue") { Blue_buttons.Remove(btn); } else { Red_buttons.Remove(btn); } bool ove = isOver(); if (ove) { MessageBox.Show("Congrats! You Won!"); status = "over"; return; } else { //switch player this.this_user = this_user == 1 ? 2 : 1; displayUser(); } this.selectButton = null; } }