碰碰球小游戏求助
小弟做了碰碰球这个小东西,主要是画图。现在遇到的问题是当游戏开始后,改变窗口大小以后,球的运行无法正常了,代码在下面,大家帮解决一下:using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
ball qiu;
public Form1()
{
InitializeComponent();
}
public class ball
{
public int x, y;
public Color c = Color.Blue;
public int dx, dy;
public int r = 50;
public Graphics g;
public void move(int w, int h)
{
Brush b0 = new SolidBrush(Color.White);
Brush b1 = new SolidBrush(c);
g.FillEllipse(b0, x, y, r, r);
x = x + dx;
y = y + dy;
if (x < 0 || x> w)
dx = -1 * dx;
if (y < 0 || y > h)
dy = -1 * dy;
g.FillEllipse(b1, x, y, r, r);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
qiu.move(this.Size.Width - 50, this.Size.Height - 50);
}
private void Form1_Load(object sender, EventArgs e)
{
System.Random r = new Random();
int a = r.Next(0, 500);
System.Random n = new Random();
int b = n.Next(0, 400);
System.Random m = new Random();
int dx = m.Next(-100, 50);
System.Random h = new Random();
int dy = h.Next(-50, 100);
this.timer1.Interval = 10;
this.timer1.Enabled = false;
qiu = new ball();
qiu.x = a;
qiu.y = b;
qiu.dx = dx;
qiu.dy = dy;
qiu.c = Color.Black;
qiu.g = this.CreateGraphics();
}
private void ButtonStart_Click_1(object sender, EventArgs e)
{
this.timer1.Enabled = true;
}
private void ButtonStop_Click(object sender, EventArgs e)
{
this.timer1.Enabled = false;
}
}
}