Peter Duniho said:
[...]
I have posted the code in hope that an error can be found.
You have posted the code where? I don't see it anywhere here. Keep in mind
that newsgroups not specifically marked as for binaries typically won't
allow attachments. Some ISPs may allow them through, but many will not.
Finally, it sounds as though you are using the Visual Studio Designer to
create your program. Which is great, but it means you need to put a little
more effort in if you want to post the code. To post a proper
concise-but-complete code example for a program created using the Designer,
you need to remember to copy the code out from the *.Designer.cs file as
well.
Pete
Sorry I hit e-mail reply instead of feedback reply
Here is the full code
Here is the complete code for the Ping Pong game.
The ball starts at the top left of the form instead of moving.
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 Pong_Game
{
public partial class Form1 : Form
{
int paddle_x = 0;
int paddle_y = 255;
int paddle_width = 35;
int paddle_height = 20;
Graphics g;
int x = 0;
int y = 0;
int dx = 8;
int dy = 5;
public Form1()
{
InitializeComponent();
this.MouseMove += new MouseEventHandler(MovePaddle);
this.Paint += new PaintEventHandler(paint1);
}
private void paint1(Object sender, PaintEventArgs e)
{
g = e.Graphics;
SolidBrush blueBrush = new SolidBrush(Color.Red);
g.FillRectangle(blueBrush, paddle_x, paddle_y, paddle_width,
paddle_height);
SolidBrush brush = new SolidBrush(Color.Blue);
g.FillEllipse(brush, x, y, 10, 10);
}
private void MovePaddle(Object sender, MouseEventArgs e)
{
paddle_y = e.Y;
paddle_x = e.X;
Invalidate();
}
private void timer1_Tick(object sender, EventArgs e)
{
MoveBall();
}
private void MoveBall()
{
int newBall_x = x + dx;
int newBall_y = y + dy;
if ((newBall_x < 0 || (newBall_x > 300))) dx = - dx;
if ((newBall_y < 0 || (newBall_y > 255))) dy = - dy;
if (((newBall_x > paddle_x) && (newBall_x < (paddle_x +
paddle_width)))&&((newBall_y > paddle_y) && (newBall_y < (paddle_y +
paddle_height))))
{
if (dx > 0 && dy > 0)
{
dx = -dy;
}
if (dx < 0 && dy > 0)
{
dy = -dy;
}
}
x = x + dx;
y = y + dy;
Invalidate();
}
}
}
Regards Brian
------------- Begin Message -------------
The code for the timer is
private void timer1_Tick(object sender, EventArgs e)
{
MoveBall();
}
I have activated the timer.
I checked the code for the MoveBall.
Is there something else I have not done?
Is timer1_Tick getting called?
Arne
PS: It would be easier to help if we have some more
complete code.