4 Replies - 1939 Views - Last Post: 05 December 2009 - 03:50 AM Rate Topic: -----

#1 Develine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-December 09

Making picturebox faster

Posted 05 December 2009 - 02:07 AM

I'm making a game in VC# and I'm having a problem with the picturebox movement. Im using a timer (set to 33 mls) to change the location of the picturebox so that it seems like its moving on the screen. here is my code:
public partial class Form1 : Form
	{
		public int countBallons = 0, Cash = 0;
		public PictureBox[] ballons = new PictureBox[100];
		private Wave[] waves = new Wave[5];

		public static bool checkHit(PictureBox missle, PictureBox ballon)
		{
			if (missle.Location.X == ballon.Location.X && missle.Location.Y == ballon.Location.Y)
				return true;
			for (int i = 1; i < 100; i++)
			{
				if (missle.Location.X + i == ballon.Location.X && missle.Location.Y == ballon.Location.Y)
					return true;
				if (missle.Location.X - i == ballon.Location.X && missle.Location.Y == ballon.Location.Y)
					return true;
				if (missle.Location.X == ballon.Location.X && missle.Location.Y + i == ballon.Location.Y)
					return true;
				if (missle.Location.X == ballon.Location.X && missle.Location.Y - i == ballon.Location.Y)
					return true;
				if (missle.Location.X + i == ballon.Location.X && missle.Location.Y + i == ballon.Location.Y)
					return true;
				if (missle.Location.X - i == ballon.Location.X && missle.Location.Y + i == ballon.Location.Y)
					return true;
				if (missle.Location.X + i == ballon.Location.X && missle.Location.Y - i == ballon.Location.Y)
					return true;
				if (missle.Location.X - i == ballon.Location.X && missle.Location.Y - i == ballon.Location.Y)
					return true;
			}
			return false;
		}

		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			waves[0] = new Wave(2, 0);
			MoveBallon.Enabled = true;
			SpawnBallon.Enabled = true;
		}

		private void timer1_Tick(object sender, EventArgs e)
		{
			for (int i = 0; i < waves[0].GetRed() + waves[0].GetBlue(); i++)
			{
				if (ballons[i] != null)
				{
					if (ballons[i].Location.X < 350)
						ballons[i].Location = new Point(ballons[i].Location.X + 10, ballons[i].Location.Y);
					if (ballons[i].Location.X == 350 && ballons[i].Location.Y < 260)
						ballons[i].Location = new Point(ballons[i].Location.X, ballons[i].Location.Y + 10);
					if (ballons[i].Location.X < 740 && ballons[i].Location.Y == 260)
						ballons[i].Location = new Point(ballons[i].Location.X + 10, ballons[i].Location.Y);
					if (ballons[i].Location.X == 740 && ballons[i].Location.Y < 440)
						ballons[i].Location = new Point(ballons[i].Location.X, ballons[i].Location.Y + 10);
					if (ballons[i].Location.X < 790 && ballons[i].Location.Y < 450 && ballons[i].Location.Y >= 440)
						ballons[i].Location = new Point(ballons[i].Location.X + 4, ballons[i].Location.Y + 1);
					if (ballons[i].Location.X < 1010 && ballons[i].Location.Y == 450)
						ballons[i].Location = new Point(ballons[i].Location.X + 10, ballons[i].Location.Y);
				}
				if(ballons[i].Location.X > 0)
					pictureBox2.Location = new Point(Convert.ToInt32(pictureBox2.Location.X + (ballons[i].Location.X - pictureBox2.Location.X) / 4), Convert.ToInt32(pictureBox2.Location.Y + (ballons[i].Location.Y - pictureBox2.Location.Y) / 4));
				if (checkHit(pictureBox2, ballons[i]))
					ballons[i] = null;
				/*if (ballons[i + 1] == null)
					break;*/
			}
		}

		private void SpawnBallon_Tick(object sender, EventArgs e)
		{
			ballons[countBallons] = new PictureBox();
			ballons[countBallons].Height = 10;
			ballons[countBallons].Width = 10;
			//ballons[countBallons].BackColor = Color.Red; (For test proposal only)
			ballons[countBallons].ImageLocation = "(you don't really need the location, the picture is in the resource directory of the game)";
			ballons[countBallons].Location = new Point(-50, 150);
			ballons[countBallons].Enabled = true;
			ballons[countBallons].Visible = true;
			Controls.Add(ballons[countBallons]);
			if (countBallons < waves[0].GetBlue() + waves[0].GetRed())
				countBallons++;
			else
				SpawnBallon.Enabled = false;
		}
	}



timer1_tick = changes the location of the picturebox (33 mls).
SpawnBallon_Tick = spawn a new balloon every 1 sec.

how do I make the picturebox move faster? every time i spawn another one its just making the game slower and slower.

Is This A Good Question/Topic? 0
  • +

Replies To: Making picturebox faster

#2 MentalFloss  Icon User is offline

  • .
  • member icon

Reputation: 577
  • View blog
  • Posts: 1,500
  • Joined: 02-September 09

Re: Making picturebox faster

Posted 05 December 2009 - 02:42 AM

You might want to look into XNA for your games. PictureBoxes are kinda supposed to be stationary.

Yeah, I know you can make a game with whatever, but I'm just saying.

As for your performance issue, I don't know. I just think you're gonna fight an uphill battle with this one.
Was This Post Helpful? 0
  • +
  • -

#3 Develine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-December 09

Re: Making picturebox faster

Posted 05 December 2009 - 02:49 AM

XNA does look like a good engine for C#, do I need to learn some functions to use it, and if so could you give me a link to any tutorial about it?
Was This Post Helpful? 0
  • +
  • -

#4 MentalFloss  Icon User is offline

  • .
  • member icon

Reputation: 577
  • View blog
  • Posts: 1,500
  • Joined: 02-September 09

Re: Making picturebox faster

Posted 05 December 2009 - 02:58 AM

getting started:
http://creators.xna....gintro/chapter1

creators club:
http://creators.xna.com

I can't find the project I started with (and admittedly I have not gotten very far in XNA) but it was really good. It was a video walkthrough of making a 2D space shooter with a fixed rotating cannon on the left hand side that shot red cannonballs. Try to find that. It was very concise and informative.

Also, buy a book on the subject. Books trump web by miles. Internet should be your supplement - not your sole resource.
Was This Post Helpful? 0
  • +
  • -

#5 Develine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-December 09

Re: Making picturebox faster

Posted 05 December 2009 - 03:50 AM

checked XNA out and it looks great. thank you very much m8!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1