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! :)

New Topic/Question
Reply



MultiQuote






|