我怎样才能这样做,而不是重新绘制这个对象,它只是在多个点上多次绘制它?

时间:2016-08-27 19:29:54

标签: c#

嘿所以我试图让它成为当我"拍摄"它在每个地方留下了枪声。好吧,它只是不断重新绘制而不是留下多个。我知道它为什么这样做(因为它在绘制事件中并且每次重新计时定时器使其刷新)但我不知道如何实际修复它。

继承课程本身

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Media;
using System.Drawing;

namespace Paintball
{
    class PaintballClass
    {
    private SoundPlayer mySound;
    private Image myImage;

    public Point shotLocation; 

    public PaintballClass (System.IO.Stream aSound, Image anImage)
    {
        mySound = new SoundPlayer();
        mySound.Stream = aSound;
        mySound.Load();

        myImage = anImage;
    }

    public void playSound()
    {
        mySound.Play();
    }

    public void locateShot(Point location)
    {
        shotLocation = location;
    }

    public void displayShot(Graphics g)
    {
        g.DrawImage(myImage, shotLocation);
    }


    }
}

继承人的形式

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Paintball
{
public partial class Form1 : Form
{
    private Target targ;
    private PaintballClass shot;
    private static Random rand = new Random();
    private const int SPEED = 5;


    public Form1()
    {
        InitializeComponent();
        int x1 = rand.Next(pictureBox1.Width);
        int y1 = rand.Next(pictureBox1.Height);
        targ = new Target(new Rectangle(x1, y1, 15, 15), pictureBox1.ClientRectangle, Brushes.Black, SPEED);
        shot = new PaintballClass(Paintball.Properties.Resources.Gunshot, Paintball.Properties.Resources.Untitled);
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        shot.displayShot(e.Graphics);
        targ.paint(e.Graphics);
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        targ.move();
        Refresh();
    }

    private void btnStart_Click(object sender, EventArgs e)
    {
        timer1.Enabled = true;
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        shot.playSound();
        shot.locateShot(e.Location);
    }
}

}

1 个答案:

答案 0 :(得分:1)

将照片存储在列表中:public List<Point> shotLocations = new List<Point>(); 迭代它们并在draw方法中显示它们:

public void displayShots(Graphics g)
{
    foreach(var shotLocation in shotLocations)
        g.DrawImage(myImage, shotLocation);
}

locateShot方法中,将位置添加到shotLocations

public void locateShot(Point location)
{
    shotLocations.Add(location);
}

当镜头消失时,您可以清除列表并重新绘制。