Alphabet Aquarium

AlphabetAquarium

This was a class assignment where we had to come up with an alphabet aquarium. Sorta like a fish aquarium IRL but just with letters instead of fish. Then I had to animate the letters so they would move around like fish would in the tank. I started getting into is-a has-a relationships with this one. So you will see 3 classes. The AlphabetAquarium class which takes care of the interface tasks, a FishTank class to hold this fish, and a Fish class that creates the fish.

This was fun project messing with the various graphics methods to draw and animate the characters. I’m sure I’ll be coming back and tweaking things later as I learn.

AlphabetAquarium Class:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AlphabetAquarium
{
    public partial class AlphabetAquarium : Form
    {
        bool doAnimation = false;

        private FishTank _fishTank = new FishTank();

        public AlphabetAquarium()
        {
            InitializeComponent();
        }

        private void AlphabetAquarium_Load(object sender, EventArgs e)
        {
            //Populate "colors" combo box, select "Black: as the default.
            colorsComboBox.Items.Add("Black");
            colorsComboBox.Items.Add("Red");
            colorsComboBox.Items.Add("Green");
            colorsComboBox.Items.Add("Blue");
            colorsComboBox.SelectedIndex = colorsComboBox.FindString("Black");

            //Populate the "fish" combo box, select "A" as the default.
            fishComboBox.Items.Add("A");
            fishComboBox.Items.Add("B");
            fishComboBox.Items.Add("C");
            fishComboBox.Items.Add("D");
            fishComboBox.SelectedIndex = fishComboBox.FindString("A");
        }

        private void colorsComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Change the color of the colorPictureBox to match the selected color.
            colorPictureBox.BackColor = Color.FromName(colorsComboBox.SelectedItem.ToString());
        }

        private void addFishButton_Click(object sender, EventArgs e)
        {
            //Use the boundaries of the fishTankPanel to limit our random x, y location.
            Rectangle fishTankRect = fishTankPanel.Bounds;
            Random random = new Random();
            int x = random.Next(10, fishTankRect.Width - 10);
            int y = random.Next(10, fishTankRect.Height - 10);

            //Create a new fish object and add to our fish tank.
            Fish fish = new Fish(fishComboBox.SelectedItem.ToString(), x, y, Color.FromName(colorsComboBox.SelectedItem.ToString()));
            _fishTank.AddFish(fish);
            fishTankPanel.Invalidate();
        }

        private void fishTankPanel_Paint(object sender, PaintEventArgs e)
        {
            //Loop thru each fish in our fish tank and draw them.
            for (int i = 0; i < _fishTank.CountFish(); i++)
            {
                Fish fish = _fishTank.GetFish(i);
                e.Graphics.DrawString(fish.FishLetter, new Font("Arial", 10),
                    new SolidBrush(fish.FishColor), new Point(fish.XPosition, fish.YPosition));
            }
            fishCountLabel.Text = _fishTank.CountFish().ToString();
        }

        private void clearFishButton_Click(object sender, EventArgs e)
        {
            _fishTank.ClearFish();
            fishTankPanel.Invalidate();
        }

        private void moveFish()
        {
            //Getting the boundary of the screen. This will be used to limit where the fish move.
            Rectangle boundary = fishTankPanel.Bounds;

            for (int i = 0; i < _fishTank.CountFish(); i++)             {                 //Getting the fish that are on the screen.                 Fish fish = _fishTank.GetFish(i);                 //Checking to see if the 'R' direction has been assigned.                 if (fish.Direction == "R")                 {                     fish.XPosition++;                     fish.YPosition++;                     //If the direction exceeds then the direction will change.                     if (fish.XPosition >= boundary.Width - 20 || fish.YPosition >= boundary.Height - 20)
                    {
                        //Changing the direction of the fish.
                        fish.Direction = "L";
                        Console.WriteLine("R has changed");
                    }
                }

                //Checking to see if the 'L' direction has been assigned.
                if (fish.Direction == "L")
                {
                    fish.XPosition--;
                    fish.YPosition--;

                    //If the direction gets close to the border limit then the direction will change.
                    if (fish.XPosition <= 20 || fish.YPosition <= 20)
                    {
                        //Changing the direction of the fish.
                        fish.Direction = "R";
                        Console.WriteLine("L has changed");
                    }
                }

                //Checking the direction along with the X and Y positions.
                Console.WriteLine("L or R: " + fish.Direction);
                Console.WriteLine("X: " + fish.XPosition);
                Console.WriteLine("Y: " + fish.YPosition);

                fishTankPanel.Invalidate();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //Calling the moveFish method.
            moveFish();
        }

        private void animateCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            //Checking to see if the animate checkbox is checked.
            if (animateCheckBox.Checked)
            {
                timer1.Enabled = true;
                doAnimation = true;
            }
            else
            {
                timer1.Enabled = false;
                doAnimation = false;
            }
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //Making sure all animation activities have been stopped before closing.
        private void AlphabetAquarium_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (doAnimation)
            {
                MessageBox.Show("You need to uncheck the animation box before closing the program.", this.Text, MessageBoxButtons.OK);
                e.Cancel = true;
            }
        }
    }
}

Fish Class:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace AlphabetAquarium
{
    class Fish
    {
        private Color _fishColor;
        public Color FishColor
        {
            get { return _fishColor; }
            set { _fishColor = value; }
        }

        private int _xPosition;
        public int XPosition
        {
            get { return _xPosition; }
            set { _xPosition = value; }
        }

        private int _yPosition;
        public int YPosition
        {
            get { return _yPosition; }
            set { _yPosition = value; }
        }

        private string _fishLetter;
        public string FishLetter
        {
            get { return _fishLetter; }
            set { _fishLetter = value; }
        }

        private string _direction;
        public string Direction
        {
            get { return _direction; }
            set { _direction = value; }
        }

        public Fish(string fishLetter, int xPosition, int yPosition, Color fishColor)
        {   
            //If no letter specidied, use "X".
            if (fishLetter.Length == 0)
                fishLetter = "X";
            _fishLetter = fishLetter;

            //Ensure the position is >= 0.
            if (xPosition < 0)
                xPosition = 0;
            _xPosition = xPosition;

            if (yPosition < 0)
                yPosition = 0;
            _yPosition = yPosition;

            //Set the fish color.
            _fishColor = fishColor;

            //Generating a random number between 1 and 50.
            Random random = new Random();
            int direction = random.Next(1, 50);

            //Setting the direction to either L or R. From the random number.
            if (direction <= 25)
                _direction = "L";
            else
                _direction = "R";
        }

        //Test method to make sure the movement is working. Can remove this later.
        public Fish()
        {
            Random random = new Random();
            int direction = random.Next(1, 50);

            if (direction <= 25)
                _direction = "L";
            else
                _direction = "R";
        }
    }
}

FishTank Class:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AlphabetAquarium
{
    class FishTank
    {
        //Use a list collection to hold the fish.
        private List _fishTank = new List();

        public int CountFish()
        {
            return _fishTank.Count;
        }

        public Fish GetFish(int position)
        {
            return _fishTank[position];
        }

        public void AddFish(Fish fish)
        {
            _fishTank.Add(fish);
        }

        public void ClearFish()
        {
            _fishTank.Clear();
        }
    }
}
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s