继承课程本身
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);
}
}
}
答案 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);
}
当镜头消失时,您可以清除列表并重新绘制。