2 Replies - 787 Views - Last Post: 19 February 2009 - 09:38 PM Rate Topic: -----

#1 flaming_reign  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-October 08

BeachBall

Posted 31 January 2009 - 11:29 PM

Hi,

I am making the beach ball project. Well, recently I just found hte problem. Now I' thinking that does anyone know how to add an easy and difficulty level to the game and a textbox will show the score, how many points the player receives from the balls popped? anyone want to help me get started?


namespace BeachBalls
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}
		int ballSize;

		int[] ballX = new int[5];

		int[] ballY = new int[5];

		int[] ballSpeed = new int[5];

		int daggerSize;

		int daggerX;

		Graphics myGraphics;

		Brush blankBrush;

		Random myRandom = new Random();
		private void Form1_Load(object sender, EventArgs e)
		{
			int x;

			// Have the balls spread across the panel with 20 pixels borders

			ballSize = (int)((pnlBeachBalls.Width - 6 * 20) / 5);

			x = 10;

			for (int i = 0; i < 5; i++)
			{

				ballX[i] = x;

				x = x + ballSize + 20;

			}

			// Make dagger one-half the ball size

			daggerSize = (int)(ballSize / 2);

			myGraphics = pnlBeachBalls.CreateGraphics();

			blankBrush = new SolidBrush(pnlBeachBalls.BackColor);

			// Give form focus

			this.Focus();
		}
		private void Form1_FormClosing(object sender, FormClosingEventArgs e)
		{

			myGraphics.Dispose();

			blankBrush.Dispose();

		}
		private void Form1_KeyDown(object sender, KeyEventArgs e)
		{

			// Erase dagger at old location

			myGraphics.FillRectangle(blankBrush, daggerX, pnlBeachBalls.Height - daggerSize, daggerSize, daggerSize);

			// Check for F key (left) and J key (right) and compute arrow position

			if (e.KeyCode == Keys.F)
			{

				daggerX = daggerX - 5;

			}

			else if (e.KeyCode == Keys.J)
			{

				daggerX = daggerX + 5;

			}

			// Position dagger

			myGraphics.DrawImage(picDagger.Image, daggerX, pnlBeachBalls.Height - daggerSize, daggerSize, daggerSize);

		}
		private void btnExit_Click(object sender, EventArgs e)
		{

			this.Close();

		}
		private void btnStart_Click(object sender, EventArgs e)
		{

			if (btnStart.Text == "Start")
			{

				// New Game

				myGraphics.Clear(pnlBeachBalls.BackColor);

				txtOver.Visible = false;

				btnStart.Text = "Stop";

				btnExit.Enabled = false;

				txtScore.Text = "0";

				// set each ball off top of panel and give new speed

				for (int i = 0; i < 5; i++)
				{

					ballY[i] = -ballSize;

					ballSpeed[i] = myRandom.Next(4) + 3;

				}

				// Set dagger near center

				daggerX = (int)(pnlBeachBalls.Width / 2);

				myGraphics.DrawImage(picDagger.Image, daggerX, pnlBeachBalls.Height - daggerSize, daggerSize, daggerSize);

				// Give form focus so it can accept KeyDown events

				this.Focus();

			}

			else
			{

				// Game stopped

				txtOver.Visible = true;

				btnStart.Text = "Start";

				btnExit.Enabled = true;

			}

			// Toggle timers

			timBalls.Enabled = !(timBalls.Enabled);

			timGame.Enabled = !(timGame.Enabled);

		}
		private void timGame_Tick(object sender, EventArgs e)
		{

			// 60 seconds have elapsed - stop game

			timBalls.Enabled = false;

			timGame.Enabled = false;

			txtOver.Visible = true;

			btnStart.Text = "Start";

			btnExit.Enabled = true;

		}
		private void timBalls_Tick(object sender, EventArgs e)
		{

			for (int i = 0; i < 5; i++)
			{

				// erase ball

				myGraphics.FillRectangle(blankBrush, ballX[i], ballY[i], ballSize, ballSize);

				// move ball

				ballY[i] = ballY[i] + ballSpeed[i];

				// check if ball has popped 

				if ((ballY[i] + ballSize) > (pnlBeachBalls.Height - daggerSize))
				{

					if (ballX[i] < daggerX)
					{

						if ((ballX[i] + ballSize) > (daggerX + daggerSize))
						{

							// Ball has popped

							// Increase score - move back to top

							Console.Beep();

							txtScore.Text = Convert.ToString(Convert.ToInt32(txtScore.Text) + 1);

							ballY[i] = -ballSize;

							ballSpeed[i] = myRandom.Next(4) + 3;

						}

					}

				}

				// check for moving off bottom

				if ((ballY[i] + ballSize) > pnlBeachBalls.Height)
				{

					// Ball reaches bottom without popping

					// Move back to top with new speed

					ballY[i] = -ballSize;

					ballSpeed[i] = myRandom.Next(4) + 3;

				}

				// redraw ball at new location

				myGraphics.DrawImage(picBall.Image, ballX[i], ballY[i], ballSize, ballSize);

			}

		}
	}
}

This post has been edited by flaming_reign: 01 February 2009 - 01:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: BeachBall

#2 flaming_reign  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-October 08

Re: BeachBall

Posted 19 February 2009 - 04:12 PM

Hi,

Can anyone please help me? I could do the countdown..but uhm, I don't know how to active the time for the countdown to start when you press the start button for the game. Like..when you start the game, I want the countdown (timer) to start at the same time..ahh, and for some reason, my timer doesn't work when I press start...could anyone help?


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 BeachBalls
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}
		int ballSize;

		int[] ballX = new int[5];

		int[] ballY = new int[5];

		int[] ballSpeed = new int[5];

		int daggerSize;

		int daggerX;

		Graphics myGraphics;

		Brush blankBrush;

		Random myRandom = new Random();

		public int seconds; // Seconds.
		public int minutes; // Minutes.
		public int hours;   // Hours.
		public bool paused; // State of the timer [PAUSED/WORKING].


		private void Form1_Load(object sender, EventArgs e)
		{
			int x;

			// Have the balls spread across the panel with 20 pixels borders

			ballSize = (int)((pnlBeachBalls.Width - 6 * 20) / 5);

			x = 10;

			for (int i = 0; i < 5; i++)
			{

				ballX[i] = x;

				x = x + ballSize + 20;

			}

			// Make dagger one-half the ball size

			daggerSize = (int)(ballSize / 2);

			myGraphics = pnlBeachBalls.CreateGraphics();

			blankBrush = new SolidBrush(pnlBeachBalls.BackColor);

			// Give form focus

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

		private void btnStart_Click(object sender, EventArgs e)
		{
			if (btnStart.Text == "Start")
			{

				// New Game

				myGraphics.Clear(pnlBeachBalls.BackColor);

				txtOver.Visible = false;

				btnStart.Text = "Stop";

				btnExit.Enabled = false;

				txtScore.Text = "0";

				// set each ball off top of panel and give new speed

				for (int i = 0; i < 5; i++)
				{

					ballY[i] = -ballSize;

					ballSpeed[i] = myRandom.Next(4) + 3;

				}

				// Set dagger near center

				daggerX = (int)(pnlBeachBalls.Width / 2);

				myGraphics.DrawImage(picDagger.Image, daggerX, pnlBeachBalls.Height - daggerSize, daggerSize, daggerSize);

				// Give form focus so it can accept KeyDown events

				this.Focus();

				if (paused != true)
				{
					if ((textBox1.Text != "") && (textBox2.Text != "") && (textBox3.Text != ""))
					{
						timer1.Enabled = true;
						btnStart.Enabled = false;
						textBox1.Enabled = false;
						textBox2.Enabled = false;
						textBox3.Enabled = false;
						textBox4.Enabled = false;
						try
						{
							minutes = System.Convert.ToInt32(textBox2.Text);
							seconds = System.Convert.ToInt32(textBox3.Text);
							hours = System.Convert.ToInt32(textBox1.Text);
						}
						catch (Exception ex)
						{
							MessageBox.Show(ex.Message);
						}
					}
					else
					{
						MessageBox.Show("Incomplete settings!");
					}
				}
				else
				{
					timer1.Enabled = true;
					paused = false;
					btnStart.Enabled = false;
				}

			}

			else
			{

				// Game stopped

				txtOver.Visible = true;

				btnStart.Text = "Start";

				btnExit.Enabled = true;

			}

			// Toggle timers

			timBalls.Enabled = !(timBalls.Enabled);

			timGame.Enabled = !(timGame.Enabled);

			// Stop the timer.
			paused = false;
			timer1.Enabled = false;
			btnStart.Enabled = true;
			textBox4.Clear();
			textBox3.Clear();
			textBox2.Clear();
			textBox1.Clear();
			textBox1.Enabled = true;
			textBox4.Enabled = true;
			textBox3.Enabled = true;
			textBox2.Enabled = true;
			textBox1.Enabled = true;
			lblHr.Text = "00";
			lblMin.Text = "00";
			lblSec.Text = "00";

		}

		private void timGame_Tick(object sender, EventArgs e)
		{
			// 60 seconds have elapsed - stop game

			timBalls.Enabled = false;

			timGame.Enabled = false;

			txtOver.Visible = true;

			btnStart.Text = "Start";

			btnExit.Enabled = true;
		}

		private void timBalls_Tick(object sender, EventArgs e)
		{
			for (int i = 0; i < 5; i++)
			{

				// erase ball

				myGraphics.FillRectangle(blankBrush, ballX[i], ballY[i], ballSize, ballSize);

				// move ball

				ballY[i] = ballY[i] + ballSpeed[i];

				// check if ball has popped

				if ((ballY[i] + ballSize) > (pnlBeachBalls.Height - daggerSize))
				{

					if (ballX[i] < daggerX)
					{

						if ((ballX[i] + ballSize) > (daggerX + daggerSize))
						{

							// Ball has popped

							// Increase score - move back to top

							Console.Beep();

							txtScore.Text = Convert.ToString(Convert.ToInt32(txtScore.Text) + 1);

							ballY[i] = -ballSize;

							ballSpeed[i] = myRandom.Next(4) + 3;

						}

					}

				}

				// check for moving off bottom

				if ((ballY[i] + ballSize) > pnlBeachBalls.Height)
				{

					// Ball reaches bottom without popping

					// Move back to top with new speed

					ballY[i] = -ballSize;

					ballSpeed[i] = myRandom.Next(4) + 3;

				}

				// redraw ball at new location

				myGraphics.DrawImage(picBall.Image, ballX[i], ballY[i], ballSize, ballSize);
			}
		}

		private void Form1_FormClosing(object sender, FormClosingEventArgs e)
		{
			myGraphics.Dispose();

			blankBrush.Dispose();
		}

		private void Form1_KeyDown(object sender, KeyEventArgs e)
		{
			// Erase dagger at old location

			myGraphics.FillRectangle(blankBrush, daggerX, pnlBeachBalls.Height - daggerSize, daggerSize, daggerSize);

			// Check for Z key (left) and X key (right) and compute arrow position

			if (e.KeyCode == Keys.Z)
			{

				daggerX = daggerX - 5;

			}

			else if (e.KeyCode == Keys.X)
			{

				daggerX = daggerX + 5;

			}

			// Position dagger

			myGraphics.DrawImage(picDagger.Image, daggerX, pnlBeachBalls.Height - daggerSize, daggerSize, daggerSize);
		}

		private void timer1_Tick(object sender, EventArgs e)
		{
			// Verify if the time didn't pass.
			if ((minutes == 0) && (hours == 0) && (seconds == 0))
			{
				// Start time when the game starts.
				// If the time is over, clear all settings and fields.
				// Also, show the message, notifying that the time is over.
				timer1.Enabled = false;
				MessageBox.Show(textBox4.Text);
				btnStart.Enabled = true;
				textBox4.Clear();
				textBox3.Clear();
				textBox2.Clear();
				textBox1.Enabled = true;
				textBox4.Enabled = true;
				textBox3.Enabled = true;
				textBox2.Enabled = true;
				textBox1.Enabled = true;
				lblHr.Text = "00";
				lblMin.Text = "00";
				lblSec.Text = "00";
			}
			else
			{
				// Else continue counting.
				if (seconds < 1)
				{
					seconds = 59;
					if (minutes == 0)
					{
						minutes = 59;
						if (hours != 0)
							hours -= 1;

					}
					else
					{
						minutes -= 1;
					}
				}
				else
					seconds -= 1;
				// Display the current values of hours, minutes and seconds in
				// the corresponding fields.
				lblHr.Text = hours.ToString();
				lblMin.Text = minutes.ToString();
				lblSec.Text = seconds.ToString();
			}

		}
	   
	}
	
}

This post has been edited by flaming_reign: 19 February 2009 - 04:14 PM

Was This Post Helpful? 0
  • +
  • -

#3 luigiplumbersinc  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 82
  • Joined: 16-February 09

Re: BeachBall

Posted 19 February 2009 - 09:38 PM

maybe call the timers Start method after enabling it?

timer1.Enabled = true;
timer1.Start();


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1