All pastes #278148 Raw Edit

Miscellany

public text v1 · immutable
·published 2006-12-13 21:47 UTC
rendered paste body
#region Using directives

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.IO;

#endregion


namespace BloodyBees
{
    /// <summary>
    /// Summary description for form.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        /// <summary>
        /// Main menu for the form.
        /// </summary>
        private System.Windows.Forms.MainMenu mainMenu1;


        public Form1()
        {
            InitializeComponent();

            start = DateTime.Now;
            backBuffer                  = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
            objects                     = new ScreenObjects(new Rectangle(0,0,this.ClientSize.Width, this.ClientSize.Height));
            objects.BackgroundBitmap    = getBitmapResource(GameConstants.background);

            hive = new Hive(getBitmapResource(GameConstants.player));
           // CreateEnemies();
            for (int i = 0; i < 5; i++ )
            {
                objects.SetEnemy();
            }

            using (Graphics g = Graphics.FromImage(backBuffer))
            {
                objects.Clear(g);
            }

            this.Invalidate();
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.mainMenu1 = new System.Windows.Forms.MainMenu();
            this.timer1 = new System.Windows.Forms.Timer();
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.menuItem2 = new System.Windows.Forms.MenuItem();
            // 
            // mainMenu1
            // 
            this.mainMenu1.MenuItems.Add(this.menuItem1);
            this.mainMenu1.MenuItems.Add(this.menuItem2);
            // 
            // timer1
            // 
            this.timer1.Enabled = true;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // menuItem1
            // 
            this.menuItem1.Text = "New Game";
            this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
            // 
            // menuItem2
            // 
            this.menuItem2.Text = "Instructions";
            this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
            // 
            // Form1
            // 
            this.ClientSize = new System.Drawing.Size(176, 180);
            this.Menu = this.mainMenu1;
            this.Text = "Player Score: ";
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);

        }

        #endregion

        private Bitmap background;
        private Bitmap backBuffer;
        private Timer timer1;
        private System.DateTime start;
        private System.TimeSpan current;
        private Hive hive;
        private ScreenObjects objects;
        private MenuItem menuItem1;
        private MenuItem menuItem2;
        readonly System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();


        public Bitmap getBitmapResource(string name)
        {
            string full_name = GameConstants.ImageDir + name;
            Stream bitmapStream = assembly.GetManifestResourceStream(full_name);
            return new Bitmap(bitmapStream);
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            Application.Run(new Form1());            
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            if ((GameConstants.GameOver == false)&&(GameConstants.startGame==false))
            {
                e.Graphics.DrawImage(backBuffer, 0, 0);
            }
            else if (GameConstants.startGame == true)
            {
                // When the game is complete, draw an appropriate image to the screen
                e.Graphics.DrawImage(getBitmapResource(GameConstants.startscreen), 0, 0);
            }
            else
            {
                // When the game is complete, draw an appropriate image to the screen
                e.Graphics.DrawImage(getBitmapResource(GameConstants.CurrentGameOverImg), 0, 0);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            // Only update game bjects whilst game is running.
            if (GameConstants.GameOver == false)
            {
                hive.Update();
                //Bitmap playerBMP = hive.Draw(getBitmapResource(GameConstants.player));
                Bitmap playerBMP = hive.Draw(new Bitmap(30,30));
                using (Graphics g = Graphics.FromImage(backBuffer))
                {
                    //objects.UpdateObjects(g, getBitmapResource(GameConstants.player));
                    objects.UpdateObjects(g, playerBMP,getBitmapResource(GameConstants.currentHealth));
                }

                this.Invalidate();
                current = System.DateTime.Now - start;
            }
            this.Text = "Player Score: " + current;//GameConstants.PlayerScore;
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == System.Windows.Forms.Keys.F1))
            {
               
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.F2))
            {

            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Up))
            {
                // Up
                
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Down))
            {
                // Down
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Left))
            {
                // Left
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Right))
            {
                // Right
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
            {
                // Enter

            }
            if ((e.KeyCode == System.Windows.Forms.Keys.D1))
            {
                // 1
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.D2))
            {
                // 2
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.D3))
            {
                // 3
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.D4))
            {
                // 4
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.D5))
            {
                // 5
                objects.PerformShoot();
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.D6))
            {
                // 6
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.D7))
            {
                // 7
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.D8))
            {
                // 8
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.D9))
            {
                // 9
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.F8))
            {
                // *
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.D0))
            {
                // 0
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.F9))
            {
                // #
            }

        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            //base.OnPaintBackground(e);
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            //if started

            if (GameConstants.startGame == true)
            {
                GameConstants.startGame = false;
                menuItem1.Text = "Pause";
                menuItem2.Text = "Quit";
            }

            else if( GameConstants.startGame == false && GameConstants.GameOver == false )
            {
                if (timer1.Enabled == true)
                {
                    menuItem1.Text = "Resume";
                    timer1.Enabled = false;
                }

                else
                {
                    start.Add( new TimeSpan(DateTime.Now.Ticks - current.Ticks));
                    menuItem1.Text = "Pause";
                    timer1.Enabled = true;
                }
            }
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            //if started
            //quit
            //GameConstants.GameOver = true;
            if (GameConstants.startGame == true)
            {
                string message = "Game Objective:\nDefend your hive from falling rocks.\n";
                message += "Use the arrow keys to move left and right and the";
                message += " enter key to fire a member of the swarm to destroy the enemy.\n";
                message += "Do not let you hive health deplete or the game will be over\n\n";

                message += "Enemies: Pollen and nectar will increase the health of your hive";
                message += "but are not regularly available.\nStones will do damage,";
                message += "and large stones will damage more\n\n";

                message += "Score: Your score and game time appear at the top.\nTry get the ";
                message += "best score possible in the time,\n remember you only have 2 and a half minutes.";
                MessageBox.Show(message);
            }

            else if (GameConstants.startGame == false && GameConstants.GameOver == false)
            {
                GameConstants.startGame = true;
                menuItem1.Text = "New Game";
                menuItem2.Text = "Instructions";
            }
        }

    }
}