Need help with a game in c#

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 2232 Views - Last Post: 06 May 2009 - 07:20 AM Rate Topic: -----

#1 ho-ho  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 69
  • Joined: 04-May 09

Need help with a game in c#

Post icon  Posted 04 May 2009 - 06:20 AM

I am trying to develop a windows mobile game in c# in which when keys.up is pressed, a bullet should be fired.The code with which I am working is below.
When the game launches, once the bullet is going up.
What I want is the bullet should only be fired when the keys.up is pressed.(fighter rectangle also moves when the keys are pressed)
In the paint method, how can the bulletImage be drawn only when the fire method is called?
If someone can please help me correct my code.
Thanks

private void fire()
		{
			bulletRectangle.Y += bulletDirection;
			bulletRectangle.Y += bulletSpeed;
			bulletDirection = -1;
			bulletSpeed = -5;
  }
  if(keyArgs != null)
			{
				switch (keyArgs.KeyCode)
				{
					case Keys.Up: fighterRectangle.Y -= ySpeed;
						fire();
						break;
					case Keys.Down: fighterRectangle.Y += ySpeed;
						break;
					case Keys.Right: fighterRectangle.X += xSpeed;
						break;
					case Keys.Left: fighterRectangle.X -= xSpeed;
						break;
					default:
						return;

				}

			}
  private void Form1_Paint(object sender, PaintEventArgs e)
		{
		   g.DrawImage(bulletImage, bulletRectangle.X, bulletRectangle.Y);
	}

This post has been edited by ho-ho: 04 May 2009 - 06:25 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Need help with a game in c#

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Need help with a game in c#

Posted 04 May 2009 - 06:51 AM

View Postho-ho, on 4 May, 2009 - 05:20 AM, said:

I am trying to develop a windows mobile game in c# in which when keys.up is pressed, a bullet should be fired.The code with which I am working is below.
When the game launches, once the bullet is going up.
What I want is the bullet should only be fired when the keys.up is pressed.(fighter rectangle also moves when the keys are pressed)
In the paint method, how can the bulletImage be drawn only when the fire method is called?
If someone can please help me correct my code.
Thanks

private void fire()
		{
			bulletRectangle.Y += bulletDirection;
			bulletRectangle.Y += bulletSpeed;
			bulletDirection = -1;
			bulletSpeed = -5;
  }
  if(keyArgs != null)
			{
				switch (keyArgs.KeyCode)
				{
					case Keys.Up: fighterRectangle.Y -= ySpeed;
						fire();
						break;
					case Keys.Down: fighterRectangle.Y += ySpeed;
						break;
					case Keys.Right: fighterRectangle.X += xSpeed;
						break;
					case Keys.Left: fighterRectangle.X -= xSpeed;
						break;
					default:
						return;

				}

			}
  private void Form1_Paint(object sender, PaintEventArgs e)
		{
		   g.DrawImage(bulletImage, bulletRectangle.X, bulletRectangle.Y);
	}


You could add a bool to the program:

bool bulletVisible = false;



Then, in the fire method set it to true.

bulletVisible = true;



Finally, in the paint method:

if (bulletVisible)
	g.DrawImage(bulletImage, bulletRectangle.X, bulletRectangle.Y);



You should somewhere in your program determine if the bullet is outside the window or if there is a collision and set the visible variable to false. :)
Was This Post Helpful? 0
  • +
  • -

#3 ho-ho  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 69
  • Joined: 04-May 09

Re: Need help with a game in c#

Posted 04 May 2009 - 11:54 AM

Thank you for pointing out the problem in my code. Your code really helped. I was able to fire a bullet once but when I press keys.up again , only one time the bullet image goes up and is not visible. After that every time keys.up is pressed, the bullet seems to flicker at the top of the screen and it's not going up from the x and y axis that are set for it. How can I fix that?
(Every time keys. up is pressed the bullet should go up and when it touches the top of screen, bullet image should not be visible)
Below is the code I am working with
I really appreciate your help.
private void Form1_Paint(object sender, PaintEventArgs e)
		{
   if (bulletVisible)
				{
					g.DrawImage(bulletImage, bulletRectangle.X, bulletRectangle.Y);
					fire();
				}
 
	 if (bulletRectangle.Top <= 0)
				{
					bulletVisible = false;
	   // this part of the code makes the image disappears when it reaches the top of screen but If keys.up is pressed again the image just flickers at the top of screen
					
				}
  private bool bulletVisible = false;
		private void fire()
		{
			bulletVisible = true;
			bulletRectangle.Y += bulletDirection;
			bulletRectangle.Y += bulletSpeed;
			bulletDirection = -1;
			bulletSpeed = -5;
		   
		}

   if(keyArgs != null)
			{
				switch (keyArgs.KeyCode)
				{
					case Keys.Up: fighterRectangle.Y -= ySpeed;
						fire();
						break;
					case Keys.Down: fighterRectangle.Y += ySpeed;
						break;
					case Keys.Right: fighterRectangle.X += xSpeed;
						break;
					case Keys.Left: fighterRectangle.X -= xSpeed;
						break;
					default:
						return;

				}


Was This Post Helpful? 0
  • +
  • -

#4 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Need help with a game in c#

Posted 04 May 2009 - 12:19 PM

View Postho-ho, on 4 May, 2009 - 10:54 AM, said:

Thank you for pointing out the problem in my code. Your code really helped. I was able to fire a bullet once but when I press keys.up again , only one time the bullet image goes up and is not visible. After that every time keys.up is pressed, the bullet seems to flicker at the top of the screen and it's not going up from the x and y axis that are set for it. How can I fix that?
(Every time keys. up is pressed the bullet should go up and when it touches the top of screen, bullet image should not be visible)
Below is the code I am working with
I really appreciate your help.
private void Form1_Paint(object sender, PaintEventArgs e)
		{
   if (bulletVisible)
				{
					g.DrawImage(bulletImage, bulletRectangle.X, bulletRectangle.Y);
					fire();
				}
 
	 if (bulletRectangle.Top <= 0)
				{
					bulletVisible = false;
	   // this part of the code makes the image disappears when it reaches the top of screen but If keys.up is pressed again the image just flickers at the top of screen
					
				}
  private bool bulletVisible = false;
		private void fire()
		{
			bulletVisible = true;
			bulletRectangle.Y += bulletDirection;
			bulletRectangle.Y += bulletSpeed;
			bulletDirection = -1;
			bulletSpeed = -5;
		   
		}

   if(keyArgs != null)
			{
				switch (keyArgs.KeyCode)
				{
					case Keys.Up: fighterRectangle.Y -= ySpeed;
						fire();
						break;
					case Keys.Down: fighterRectangle.Y += ySpeed;
						break;
					case Keys.Right: fighterRectangle.X += xSpeed;
						break;
					case Keys.Left: fighterRectangle.X -= xSpeed;
						break;
					default:
						return;

				}



It is flickering at the top because you do not reset the position of the bullet every time you fire the bullet. You would want to try something like this:

		private void fire()
		{
			bulletVisible = true;
                        bulletRectangle.Y = fighterRectangle.Y;
                        bulletRectangle.X = fighterRectangle.X;
			bulletRectangle.Y += bulletDirection;
			bulletRectangle.Y += bulletSpeed;
			bulletDirection = -1;
			bulletSpeed = -5;
		   
		}



That would make the bullet start at the position of the fighter and then travel up the screen. Does that help?
Was This Post Helpful? 0
  • +
  • -

#5 ho-ho  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 69
  • Joined: 04-May 09

Re: Need help with a game in c#

Posted 04 May 2009 - 12:48 PM

Hi, the code does exactly what you said it would accomplish. The bullet now moves in the same position as the fighter plane. Is there any way that once the keys.up is pressed, the bullet travels in the upward direction, leaving the fighter plane behind.
(Right now it seems to be traveling with the fighter plane everywhere).
Thank you so much for your help, I just dived into game programming and I am learning as I try new codes.
Was This Post Helpful? 0
  • +
  • -

#6 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Need help with a game in c#

Posted 04 May 2009 - 01:01 PM

View Postho-ho, on 4 May, 2009 - 11:48 AM, said:

Hi, the code does exactly what you said it would accomplish. The bullet now moves in the same position as the fighter plane. Is there any way that once the keys.up is pressed, the bullet travels in the upward direction, leaving the fighter plane behind.
(Right now it seems to be traveling with the fighter plane everywhere).
Thank you so much for your help, I just dived into game programming and I am learning as I try new codes.


Games typically work in loops. Each time through the loop is a frame. In each frame the game you will update the objects in the game, draw the objects in the game and respond to events.

This is what a game loop looks like:

while (true)
{
	Update Objects;
	Draw Objects;
	Process Events;
	add timing logic here;
}



It looks like in your code you are not doing that, you are just responding to the key press events. Maybe if you posted all of your code I could better understand what you are doing. :)
Was This Post Helpful? 0
  • +
  • -

#7 ho-ho  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 69
  • Joined: 04-May 09

Re: Need help with a game in c#

Posted 04 May 2009 - 01:18 PM

code for the game is below

 public partial class Form1 : Form
	{
		private Image fighterImage = null;
		private Rectangle fighterRectangle;

	  private Image bulletImage = null;
		private Rectangle bulletRectangle;

   public Form1()
		{
			InitializeComponent();
			System.Reflection.Assembly execAssem = System.Reflection.Assembly.GetExecutingAssembly();
			fighterImage = new System.Drawing.Bitmap(execAssem.GetManifestResourceStream(@"TargetGame.FighterJet104.gif"));
			fighterRectangle = new Rectangle((this.ClientSize.Width - fighterImage.Width) / 2, this.ClientSize.Height - fighterImage.Height,60,80);
  
  bulletImage = new System.Drawing.Bitmap(execAssem.GetManifestResourceStream(@"TargetGame.bullets.gif"));
			bulletRectangle = new Rectangle(160, 110, 20, 30);

			timer1.Enabled = true;

  }
	private Bitmap backBuffer = null;
		private void Form1_Paint(object sender, PaintEventArgs e)
		{
			if (backBuffer == null)
			{
				backBuffer = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
			}
			using (Graphics g = Graphics.FromImage(backBuffer))
			{
				g.Clear(Color.Blue);
				g.DrawImage(fighterImage, fighterRectangle.X, fighterRectangle.Y);

   fighterRectangle.X = Math.Max(0, Math.Min(ClientSize.Width - fighterImage.Width, fighterRectangle.X));
				fighterRectangle.Y = Math.Max(0, Math.Min(ClientSize.Height - fighterImage.Height, fighterRectangle.Y));

  bulletRectangle.X = Math.Max(0, Math.Min(ClientSize.Width - bulletImage.Width, bulletRectangle.X));
				bulletRectangle.Y = Math.Max(0, Math.Min(ClientSize.Height - bulletImage.Height, bulletRectangle.Y));
 
	 if (bulletVisible)
				{
					g.DrawImage(bulletImage, bulletRectangle.X, bulletRectangle.Y);
					fire();
				}
				if (bulletRectangle.Top <= 0)
				{
					bulletVisible = false;
					
				}

				e.Graphics.DrawImage(backBuffer, 0, 0);
			}
		}
	protected override void OnPaintBackground(PaintEventArgs pevent)
		{
			//Don't allow the background to paint
		}
		int bulletDirection = -1;
		int bulletSpeed = -5;

		private bool bulletVisible = false;
		private void fire()
		{
			bulletVisible = true;
			bulletRectangle.Y = fighterRectangle.Y;
			bulletRectangle.X = fighterRectangle.X;
			bulletRectangle.Y += bulletDirection;
			bulletRectangle.Y += bulletSpeed;
			bulletDirection = -1;
			bulletSpeed = -5;
		   
		} 

   private System.Windows.Forms.KeyEventArgs keyArgs = null;
		private int xSpeed = 10;
		private int ySpeed = 10;
		int direction = 1;
		 int speed = 5;
		 

		private void updatePositions()
		{
	 // there is another image saucer image which travels to and fro
			saucerRectangle.X += direction;
			saucerRectangle.X += speed;

			if ((saucerRectangle.X + saucerImage.Width) >= this.Width)
			{
				direction = -1;
				speed = -5;
			}
			if (saucerRectangle.X <= 0)
			{
				direction = 1;
				speed = 5;
			}

  if(keyArgs != null)
			{
				switch (keyArgs.KeyCode)
				{
					case Keys.Up: fighterRectangle.Y -= ySpeed;
						fire();
						break;
					case Keys.Down: fighterRectangle.Y += ySpeed;
						break;
					case Keys.Right: fighterRectangle.X += xSpeed;
						break;
					case Keys.Left: fighterRectangle.X -= xSpeed;
						break;
					default:
						return;

				}

			}
	 private void timer1_Tick(object sender, EventArgs e)
		{
			
			updatePositions();
			Invalidate();
		}

		private void Form1_KeyDown(object sender, KeyEventArgs e)
		{

			keyArgs = e;
		}

		private void Form1_KeyUp(object sender, KeyEventArgs e)
		{
			keyArgs = null;

		}
   

Was This Post Helpful? 0
  • +
  • -

#8 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Need help with a game in c#

Posted 04 May 2009 - 01:52 PM

View Postho-ho, on 4 May, 2009 - 12:18 PM, said:

code for the game is below


That is actually very helpful. I think I can help you out. What you would want to do is separate the logic of firing a bullet with updating the bullet. You would modify the fire bullet method like this:


		private void fire()
		{
                        if (bulletVisible == false)
                        {
			        bulletVisible = true;
	        		bulletRectangle.Y = fighterRectangle.Y;
         			bulletRectangle.X = fighterRectangle.X;
		        }
		} 



Then modify the updatePositions method to update the position of the bullet:

		private void updatePositions()
		{
	                // there is another image saucer image which travels to and fro
			saucerRectangle.X += direction;
			saucerRectangle.X += speed;

			if ((saucerRectangle.X + saucerImage.Width) >= this.Width)
			{
				direction = -1;
				speed = -5;
			}
			if (saucerRectangle.X <= 0)
			{
				direction = 1;
				speed = 5;
			}

			bulletRectangle.Y += bulletDirection;
			bulletRectangle.Y += bulletSpeed;
			bulletDirection = -1;
			bulletSpeed = -5;

                        if (bulletRectangle.Y < 0)
                            bulletAlive = false;

                        if(keyArgs != null)
			{
				switch (keyArgs.KeyCode)
				{
					case Keys.Up: fighterRectangle.Y -= ySpeed;
						fire();
						break;
					case Keys.Down: fighterRectangle.Y += ySpeed;
						break;
					case Keys.Right: fighterRectangle.X += xSpeed;
						break;
					case Keys.Left: fighterRectangle.X -= xSpeed;
						break;
					default:
						return;

				}

			}



This will allow you to fire one bullet at a time and it will be set to the starting point of the fighter when you press the Keys.Up.
Was This Post Helpful? 0
  • +
  • -

#9 ho-ho  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 69
  • Joined: 04-May 09

Re: Need help with a game in c#

Posted 04 May 2009 - 02:23 PM

Thank you so much for your time and all the replies. Your logic for the code works perfect and I am so glad that I joined this forum.
I had been struggling with it for days.
I have one quick question, if the bullet strikes with saucer object, saucer disappears but after a sec. it needs to appear again so the game continues and so does the score. How can I make saucer appear after each strike. Would I have to create another timer object or the existing one will do? I don't have much experience with timers?
I appreciate your input on this.
Thanks once again
Was This Post Helpful? 0
  • +
  • -

#10 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Need help with a game in c#

Posted 04 May 2009 - 02:55 PM

View Postho-ho, on 4 May, 2009 - 01:23 PM, said:

Thank you so much for your time and all the replies. Your logic for the code works perfect and I am so glad that I joined this forum.
I had been struggling with it for days.

(*HINT*)
So, This Post Was Helpful!

Quote

I have one quick question, if the bullet strikes with saucer object, saucer disappears but after a sec. it needs to appear again so the game continues and so does the score. How can I make saucer appear after each strike. Would I have to create another timer object or the existing one will do? I don't have much experience with timers?
I appreciate your input on this.
Thanks once again


To tell you the truth using timer controls is not the best solution but they seem to be working for you so I'll see if I can help you with that. Add another timer to the form, set the interval to the length you want to wait before the saucer will reappear. Like you did with the bullet you may want to create a variable if the saucer is visible and if it is not visible don't draw it. If the bullet and the saucer collide set the saucer visible to false and make a call:

saucerVisible = false;
timer2.Start();



In the tick event of the second timer you would put this code:

timer2.Stop();
saucerVisible = true;



This would be easier if you didn't use timer controls but they seem to work for you. :)
Was This Post Helpful? 1
  • +
  • -

#11 ho-ho  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 69
  • Joined: 04-May 09

Re: Need help with a game in c#

Posted 04 May 2009 - 08:13 PM

I have seen other people's code and they seemed to be using timers so I thought may be that was the best way of doing it. But when I saw your logic for bulletVisible , I wanted to stick with that instead of using timer with saucer visible.
The code for saucerVisible is below, when bullet hits the saucer, saucer disappears but when I am adding (saucerVisible = true) at the end, saucer image seems to be flickering when the image comes in the center of screen.
If you can tell me if I am doing it right or there should be something other than the last else statement.

private bool saucerVisible = true;
		private void Form1_Paint(object sender, PaintEventArgs e)
		{

   if (saucerVisible)
				{
					
					g.DrawImage(saucerImage, saucerRectangle.X, saucerRectangle.Y);
				}

				if (saucerRectangle.IntersectsWith(bulletRectangle))
				{
					saucerVisible = false;
				}
				else
					saucerVisible = true;
   // I think with the else condition here the image seems to be flickering


Was This Post Helpful? 0
  • +
  • -

#12 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1009
  • View blog
  • Posts: 4,209
  • Joined: 14-February 08

Re: Need help with a game in c#

Posted 05 May 2009 - 12:51 AM

Hi there

You could try

private bool saucerVisible = true;
	private void Form1_Paint(object sender, PaintEventArgs e)
   {
			if (saucerVisible )
			{   
				   g.DrawImage(saucerImage, saucerRectangle.X, saucerRectangle.Y);
			 }
	 }   

	 // then in update positions
	 private void updatePositions()
	 {   
			 if (saucerRectangle.IntersectsWith(bulletRectangle))
			{
					saucerVisible = false;
			 }
			 else
			 {
					saucerVisible = true;
			  }

			  // the rest......
		}


Was This Post Helpful? 0
  • +
  • -

#13 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Need help with a game in c#

Posted 05 May 2009 - 06:04 AM

View Poststayscrisp, on 4 May, 2009 - 11:51 PM, said:

Hi there

You could try

private bool saucerVisible = true;
	private void Form1_Paint(object sender, PaintEventArgs e)
   {
			if (saucerVisible )
			{   
				   g.DrawImage(saucerImage, saucerRectangle.X, saucerRectangle.Y);
			 }
	 }   

	 // then in update positions
	 private void updatePositions()
	 {   
			 if (saucerRectangle.IntersectsWith(bulletRectangle))
			{
					saucerVisible = false;
			 }
			 else
			 {
					saucerVisible = true;
			  }

			  // the rest......
		}



That should fix the flicker problem.
Was This Post Helpful? 0
  • +
  • -

#14 ho-ho  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 69
  • Joined: 04-May 09

Re: Need help with a game in c#

Posted 05 May 2009 - 07:46 AM

Hi, I added the visibility condition of saucer in updatePositions(),the saucer goes from right to left and left to right and it disappears as the bullet hits it but it's not going smoothly ( I mean it sometimes becomes invisible even when the bullet doesn't touches it). I'll paste the code. I really appreciate your help.
private bool saucerVisible = true;
		private void Form1_Paint(object sender, PaintEventArgs e)
		{
   if (bulletVisible)
				{
					g.DrawImage(bulletImage, bulletRectangle.X, bulletRectangle.Y);
					fire();
				}
				if (bulletRectangle.Top <= 0)
				{
					bulletVisible = false;
					
				}
				if (saucerVisible)
				{
					
					g.DrawImage(saucerImage, saucerRectangle.X, saucerRectangle.Y);
				}
 protected override void OnPaintBackground(PaintEventArgs pevent)
		{
			//Don't allow the background to paint
		}
		

		private bool bulletVisible = false;
		
		private void fire()
		{
			
			if (bulletVisible == false)
			{
				bulletVisible = true;
				bulletRectangle.Y = fighterRectangle.Y;
				bulletRectangle.X = fighterRectangle.X;
			}
			
			
		   
		}

		private System.Windows.Forms.KeyEventArgs keyArgs = null;
		private int xSpeed = 10;
		private int ySpeed = 10;
		int direction = 1;
		 int speed = 5;
		 int bulletDirection = -1;
		 int bulletSpeed = -5;
		 

		private void updatePositions()
		{
			saucerRectangle.X += direction;
			saucerRectangle.X += speed;
			bulletRectangle.Y += bulletDirection;
			bulletRectangle.Y += bulletSpeed;
			bulletDirection = -1;
			bulletSpeed = -5;

			if ((saucerRectangle.X + saucerImage.Width) >= this.Width)
			{
				direction = -1;
				speed = -5;
			}
			if (saucerRectangle.X <= 0)
			{
				direction = 1;
				speed = 5;
			}

			if (saucerRectangle.IntersectsWith(bulletRectangle))
			{
				saucerVisible = false;
			}
			else
			{
				saucerVisible = true;
			}
			  
				 if(keyArgs != null)
			{
				switch (keyArgs.KeyCode)
				{
					case Keys.Up: fighterRectangle.Y -= ySpeed;
						fire();
						break;
					case Keys.Down: fighterRectangle.Y += ySpeed;
						break;
					case Keys.Right: fighterRectangle.X += xSpeed;
						break;
					case Keys.Left: fighterRectangle.X -= xSpeed;
						break;
					default:
						return;

				}

			}


		}

		private void timer1_Tick(object sender, EventArgs e)
		{
			
			updatePositions();
			Invalidate();
		}


Was This Post Helpful? 0
  • +
  • -

#15 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1009
  • View blog
  • Posts: 4,209
  • Joined: 14-February 08

Re: Need help with a game in c#

Posted 05 May 2009 - 07:54 AM

Hmm there could be another problem but whenever dealing with velocity and speed it is best to use floats as opposed to ints.

I'm still looking at your code but give that a try.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2