hey everyone
i'm a beginner c# programmer and i've been learning out of a tutorial book
i've been making a pong game but for some reason, the animation for the ball isn't working
could you tell me if there's anything wrong with the code?
(copy, paste into c#)
thx:)
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
int x = 46; //initial x position of ball
int y = 46; //initial y position of ball
int dx = 8; //position increment for ball on x axis
int dy = 5; //position increment for ball on y axis
Graphics g;
public Form1()
{
InitializeComponent();
this.Paint +=new PaintEventHandler(Form1_Paint);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
g = e.Graphics;
SolidBrush brush = new SolidBrush(Color.Black);
g.FillEllipse(brush, x, y, 10, 10);
}
private void timer1_Tick(object sender, EventArgs e)
{
moveBall();
}
private void moveBall()
{
if ((x + dx < 0) || (x + dx > 300)) dx = -dx;
if ((y + dy < 0) || (y + dy > 255)) dy = -dy;
x = x + dx;
y = y + dy;
Invalidate();
}
}
}