标题:碰碰球小游戏求助
只看楼主
mdlq910526
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-5-13
结帖率:0
已结贴  问题点数:20 回复次数:2 
碰碰球小游戏求助
小弟做了碰碰球这个小东西,主要是画图。现在遇到的问题是当游戏开始后,改变窗口大小以后,球的运行无法正常了,代码在下面,大家帮解决一下:

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

    }
}
搜索更多相关主题的帖子: 小游戏 
2011-05-13 15:48
mdlq910526
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-5-13
得分:0 
咋没人给指点一下啊~~
2011-05-16 10:12
vxbb
Rank: 6Rank: 6
等 级:侠之大者
帖 子:163
专家分:440
注 册:2008-8-18
得分:20 
传个附件多好, 又不是贴到vs里就能调试,懒得建项目。


绘图代码应该写在  Form_Paint()事件里。

窗体大小改变引发重绘事件,所以你的球被擦掉了

[ 本帖最后由 vxbb 于 2011-5-16 11:04 编辑 ]

QQ:491697374  Mail:vxbb@
2011-05-16 11:00



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-339735-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.084824 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved