13 Replies - 5965 Views - Last Post: 04 December 2011 - 12:26 PM Rate Topic: -----

#1 Bivcala  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 31
  • Joined: 23-November 11

Help with Breakout/Brickout game

Posted 27 November 2011 - 11:34 AM

//I am trying to create a simple breakout game using windows form application.
So far I have managed to make the ball bounce around the form and I have created a bat. All I need is 1 brick and I don't know how to create it, I know that I need an array but I don't know where to start. Also the ball just randomly jumps around the form each time therefor I want to make it disappear when It Misses the bat at the bottom of the form. The thing I am stuck on right know is collision detection method which enables me to make the ball hit the bat and when the ball misses the bat at the bottom of the form it disappears.
// Here is my code.

 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 Ball_Break_out
 {
 public partial class Form1 : Form
 {
 Graphics paper;
 Pen pen;
 private int x, y;
 int xBat;
 private Random ranNum;
 private int xchange, ychange;
 int bricky;

public Form1()
 {
 InitializeComponent();

 
pen = new Pen(Color.White);

 
   paper = picDisplayBat.CreateGraphics(); 
   pen = new Pen(Color.DarkRed);

    pen.Width = 5;
    pen.Width = 10;
    ranNum = new Random(); // to create a random num
 picDisplayBat.MouseMove += new System.Windows.Forms.MouseEventHandler(p…
 paper = picDisplayBat.CreateGraphics();

 pen.Width = 3;

}

private void picdraw_MouseMove(object sender, MouseEventArgs e)
 {

 paper.Clear(Color.White);
 paper.DrawRectangle(pen, e.X + 10, picDisplayBat.Height - 20, 50, 10);
 xBat = e.X;

 }

 private void timer1_Tick(object sender, EventArgs e)
 {
 x = x + xchange;
 y = y + ychange;
 if (x >= picDisplayBat.Width)
 xchange = -xchange;
 if (y >= picDisplayBat.Height)
 ychange = -ychange;
 if (x <= 0)
 xchange = -xchange;
 if (y <= 0)
 ychange = -ychange;
 paper.Clear(Color.White);
 paper.DrawEllipse(pen, x, y, 10, 10);
 paper.DrawRectangle(pen, xBat + 10, picDisplayBat.Height - 20, 50, 10); // Drawing a rectangle shape for the xBat.

 paper.DrawRectangle(pen, bricky + 20, picDisplayBat.Height - 50, 30, 10); // A brick to check for coilision detection.

}

 private void btnStartBouncing_Click(object sender, EventArgs e)
 {
 timer1.Interval = 60;
 timer1.Enabled = true;
 x = ranNum.Next(1, picDisplayBat.Height);
 y = ranNum.Next(1, picDisplayBat.Width);
 xchange = ranNum.Next(1, 10);
 ychange = ranNum.Next(1, 10);

}

 private void btnStopBouncing_Click(object sender, EventArgs e)
 {
 timer1.Enabled = false;
 paper.Clear(Color.White);
 }

}

 }


This post has been edited by Martyr2: 27 November 2011 - 11:41 AM
Reason for edit:: Please use code tags in the future, thanks! :)


Is This A Good Question/Topic? 0
  • +

Replies To: Help with Breakout/Brickout game

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,088
  • Joined: 18-April 07

Re: Help with Breakout/Brickout game

Posted 27 November 2011 - 11:52 AM

So if the ball ever reaches a "y" value greater than the height of a form, that would mean it has dropped out the bottom right? Here is the circumstance where you write whatever code you want to happen (remove "a life", restart the game etc)

Now we have collision detection here which is going to check the bounds of the ball with the bounds of the bat and the blocks. If you can get the bounding rectangle of the ball and the bounding rectangle of the bat, you will see there is a method called "intersect" in class rectangle.

Intersect Method - Rectangle class - MSDN

You can also check if one point of an object is equal to another point or within the bounds of another object. For instance, is a point on the circumference of the ball equal to the bounds of the bat? Is that point inside the bounds of the bat's rectangle? If so, you know a collision has occurred.

Play with this idea a little. The goal here will be to keep it generic and you should be able to use it for both block, bat and wall collisions.

:)
Was This Post Helpful? 2
  • +
  • -

#3 Bivcala  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 31
  • Joined: 23-November 11

Re: Help with Breakout/Brickout game

Posted 27 November 2011 - 12:16 PM

Hi Martyr,

I seem to have difficulty working what code I will use for the coilision detection I am a begginer and I am trying to make fun game using C#.

If you can, plz show me wha code to use for colision detection.

Thank you
Was This Post Helpful? -1
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5433
  • View blog
  • Posts: 11,646
  • Joined: 02-June 10

Re: Help with Breakout/Brickout game

Posted 27 November 2011 - 01:28 PM

Martyr2 Gave you a link for that. When one rectangle intersects another rectangle they have collided. Look at the link he already gave you and make a good faith effort to incorporate it into your code. You at least have to make an effort of your own before giving up and asking someone else if they will write it for you.

The stages of asking for homework help on a forum

Quote

I have a little programming experience but I need to write ...


You need to start there. I can't say "I have little experience in speaking Russian, but I have been assigned to write a mystery novel in Russian. Can you help me?"

We can help you by saying "First learn basic programming and the language of C#. Then take on assignments." Could someone here write this program for you? Sure. Could someone here map out all the processes you need to follow and do the Software Design part of this in the slim hope you could code it from there? Sure. But we don't volunteer to do the job that you're either getting paid for, or getting a grade for. You may want to read this.

For now, just work on the lessons. Do a self-teaching book from cover to cover. Then consider writing a program.

Don't try to create a useful working program to fit a need of yours (or a for-pay contract) as your introduction to coding project. When you are learning to code you don't know enough to code a program, let alone know how to engineer the architecture of a program. It would be like saying "I don't know how to read sheet music, or play an instrument. I think I'll write a 3 act opera as my first learning experience."

I don't say this to be mean. We've seen lots of new coders take this approach and we know it doesn't work. Trying to design your own programs before you understand the basics of the code language you've chosen just leads to problems, frustrations, and 'swiss-cheese' education (lots of holes).


Resources, references and suggestions for new programmers. - Updated Nov 2011
Spoiler

Was This Post Helpful? 2
  • +
  • -

#5 Bivcala  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 31
  • Joined: 23-November 11

Re: Help with Breakout/Brickout game

Posted 27 November 2011 - 02:28 PM

I started College in september 2011 on a Computer Science course.
Before the Computer Science course I didn't do any programming before. As you can see I am a complete begginer so I am not making something really complex, just simple things I have my own code to work on.
Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,088
  • Joined: 18-April 07

Re: Help with Breakout/Brickout game

Posted 27 November 2011 - 02:31 PM

Video games tend to be the most difficult to start with for beginners. Why not try one of these projects? They are of all skill levels and you can pick one that you think might be easy enough to tackle. I suggest something from the "numbers" section or from the "text" section.

Martyr2's Mega Project Idea List

:)
Was This Post Helpful? 4
  • +
  • -

#7 Bivcala  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 31
  • Joined: 23-November 11

Re: Help with Breakout/Brickout game

Posted 03 December 2011 - 12:44 PM

Hello,

I am creating a breakout game using C# winform app however, I don't have a clue what method to use to draw 5 bricks which should be placed at the top of the form so that the ball can hit it and distroy it.

Any body have any idea of what to do to create the bricks?
Was This Post Helpful? 0
  • +
  • -

#8 MATTtheSEAHAWK  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 137
  • View blog
  • Posts: 782
  • Joined: 11-September 10

Re: Help with Breakout/Brickout game

Posted 03 December 2011 - 02:01 PM

Well what you could do is create 5 picture boxes in the code and then maybe do some sort of collision detection for it. Also why use WinForms when you could use XNA which would make it much easier. WinForms can be used for games.

But if you really wanted to do it this way then you could just create a timer and use the tick event as a game loop and on every tick of the timer you could just check for one of the bricks to be destroyed and if it is then you could take it off the screen.
Was This Post Helpful? 1
  • +
  • -

#9 brep  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 166
  • Joined: 26-August 11

Re: Help with Breakout/Brickout game

Posted 03 December 2011 - 02:52 PM

In reference to this very similar thread Here, the following might also be of interest:

Rectangle class:
Click Here

Hope this helps,
preb
Was This Post Helpful? 1
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5433
  • View blog
  • Posts: 11,646
  • Joined: 02-June 10

Re: Help with Breakout/Brickout game

Posted 03 December 2011 - 03:22 PM

Trying to create this game using off-the-shelf objects makes life hard though.

If you make a Brick class you can then give it all the code you would expect for a brick:

PlayBlinkieSound();
Evaporate();// used when hit

int NumberOfHitsToEvaporate = 3;


And so on.

The tutorials below walk through making an application including inheritance, custom events and custom controls, object serialization and more.
Bulding an application - Part 1
Building an application - Part 2
Quick and easy custom events
Was This Post Helpful? 3
  • +
  • -

#11 MATTtheSEAHAWK  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 137
  • View blog
  • Posts: 782
  • Joined: 11-September 10

Re: Help with Breakout/Brickout game

Posted 03 December 2011 - 05:00 PM

To add on to what said, the main reason you would want to use the Rectangle class is for collision detection.

How you would use it for that is, to create a rectangle object in your brick class and ball class (thanks tlhIn`toq for pointing that out) that would be instantiated with the x y location of the object (and in the case of the ball, updated every time it moves) and then the size of the object.

After you did that, you would use the Intersect() method in Rectangle to test if the two collide. If they do then you would take one away from the hits it needs to disappear. If that number is 1 then you would just delete it.

One last thing, if you're this new to programming (I read your other thread). You shouldn't start out making fun games that you would like to play, you should start off with other projects that are smaller and easier to do. You will get better with time. I remember trying to do the same thing though so I don't have much room to say anything haha.
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5433
  • View blog
  • Posts: 11,646
  • Joined: 02-June 10

Re: Help with Breakout/Brickout game

Posted 04 December 2011 - 09:25 AM

Trusting in Matt's assessment that you should probably work more on *learning* before you try your hand at *developing* here are some resources for books and tutorials that you can follow to get you up to speed while making the best use of your time.



Resources, references and suggestions for new programmers. - Updated Nov 2011
Spoiler

Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5433
  • View blog
  • Posts: 11,646
  • Joined: 02-June 10

Re: Help with Breakout/Brickout game

Posted 04 December 2011 - 09:31 AM

I've just realized that you opened two threads for this same topic.

Please don't do that. It divides the help, makes people duplicate advice, is a waste of resources and so on.

There was no reason to do this as you were getting help on the first thread.

http://www.dreaminco...9&#entry1501849

http://www.dreaminco...6&#entry1496236

The two threads have been merged

This post has been edited by tlhIn`toq: 04 December 2011 - 09:33 AM

Was This Post Helpful? 1
  • +
  • -

#14 Bivcala  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 31
  • Joined: 23-November 11

Re: Help with Breakout/Brickout game

Posted 04 December 2011 - 12:26 PM

:bigsmile: :bigsmile: Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1