the timer control does not work in a C# program.

  • Thread starter Thread starter Brian
  • Start date Start date
The OP just needs to learn how to ask a good question.

There are no 'good questions', only 'good answers'. Sounds like one of those motivational koans.

RL
 
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);
Hmmm.

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();
}
}
}

The example is not complete as you are missing the designer
generated stuff.

Below is a similar example without any hidden designer stuff.

Arne

==============

using System;
using System.Drawing;
using System.Windows.Forms;

public class MainForm : Form
{
private const int D = 20;
private const int W = 400;
private const int H = 400;
private static readonly Random rng = new Random();
private int x;
private int y;
private int dx;
private int dy;
private PictureBox pic;
private Timer t;
public MainForm()
{
x = rng.Next(0, W-D);
y = rng.Next(0, H-D);
dx = rng.Next(1, 5);
dy = rng.Next(1, 5);
pic = new PictureBox();
pic.Location = new Point(0, 0);
pic.Size = new Size(W, H);
pic.Paint += BallPaint;
Controls.Add(pic);
Resize += Mirror;
ClientSize = new Size(W, H);
t = new Timer();
t.Interval = 10;
t.Tick += BallMove;
t.Start();
}
private void Mirror(object sender, EventArgs evt)
{
pic.Size = this.ClientSize;
}
private void BallMove(object sender, EventArgs evt)
{
x += dx;
y += dy;
if(x < 0)
{
x = 0;
dx = -dx;
dy += rng.Next(-1, 1);
}
if(x > pic.Width - D)
{
x = pic.Width - D;
dx = -dx;
dy += rng.Next(-1, 1);
}
if(y < 0)
{
y = 0;
dx += rng.Next(-1, 1);
dy = - dy;
}
if(y > pic.Height - D)
{
y = pic.Height - D;
dx += rng.Next(-1, 1);
dy = - dy;
}
pic.Invalidate();
}
private void BallPaint(object sender, PaintEventArgs evt)
{
evt.Graphics.FillEllipse(new SolidBrush(Color.Blue), x, y, D, D);
}
[STAThread]
public static void Main(string[] args)
{
Application.Run(new MainForm());
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top