绘制2个椭圆,但一个在移动时不显示

时间:2015-05-07 08:07:50

标签: c# wpf canvas ellipse

所以我需要投出两个正在移动的球,但目前只有一个出现并移动,另一个(先得到的)并没有显示出来。有人能解释一下为什么吗? (变量名是荷兰语,很抱歉)

public partial class Game : Window
{
    List<double> spelerEntiteiten = new List<double>();
    List<double> computerEntiteiten = new List<double>();
    private DispatcherTimer speelTijd = new DispatcherTimer();

    public Game()
    {
        InitializeComponent();
        speelTijd.Tick += speelTijd_Tick;
        startenMenuButton.Click += startenMenuButton_Click;
        objectenMenuSlider.ValueChanged += objectenMenuSlider_ValueChanged;
        speelTijd.Interval = TimeSpan.FromMilliseconds(1);
    }

    void objectenMenuSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        objectenMenuTextBlock.Text = ((int) objectenMenuSlider.Value).ToString();
    }

    void startenMenuButton_Click(object sender, RoutedEventArgs e)
    {
        int canvasX = (int)gameCanvas.Width;
        int canvasY = (int)gameCanvas.Height;
        Random getal = new Random();
        speelTijd.Start();
        //for (int i = 0; i <= objectenMenuSlider.Value; i = i + 2)
        //{
        coordinaten(ref spelerEntiteiten);
        coordinaten(ref computerEntiteiten);
        //}
    }

    public void coordinaten(ref List<double> coords) 
    {
        int canvasX = (int)gameCanvas.Width;
        int canvasY = (int)gameCanvas.Height;
        Random getal = new Random();

        coords.Add(getal.Next(20, canvasX - 20));
        coords.Add(getal.Next(20, canvasY - 20));
        coords.Add(2);
        coords.Add(2);
    }

    void speelTijd_Tick(object sender, EventArgs e)
    {
        //for (int i = 0; i <= objectenMenuSlider.Value; i = i + 2)
        //{
            gameCanvas.Children.Clear();
            objecten(ref spelerEntiteiten, 1);
            objecten(ref computerEntiteiten, 2);
        //}
    }

    public void objecten(ref List<double> entiteiten, int kleur)
    {
        Ellipse ball = new Ellipse();
        ball.Width = 20;
        ball.Height = 20;
        double breedte = entiteiten[0], hoogte = entiteiten[0+1];

        if ((breedte <= 0) || (breedte >= gameCanvas.Width-20))
        {
            entiteiten[0+2] = -entiteiten[0+2];
        }
        if ((hoogte <= 0) || (hoogte >= gameCanvas.Height-20))
        {
            entiteiten[0 + 3] = -entiteiten[0 + 3];
        }

        entiteiten[0] += entiteiten[0+2];
        entiteiten[0 + 1] += entiteiten[0 + 3];

        ball.Margin = new Thickness(entiteiten[0],entiteiten[0+1],0,0);

        if (kleur == 1)
        {
            ball.Fill = new SolidColorBrush(Colors.Blue);
        }
        else
        {
            ball.Fill = new SolidColorBrush(Colors.Red);
        }

        gameCanvas.Children.Add(ball);
    }
}

XAML代码

<Window x:Name="gameWindow" x:Class="Project_Challange.Game"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Game" Height="450" Width="750">
<Grid>
    <Canvas x:Name="gameCanvas" HorizontalAlignment="Left" Height="391" VerticalAlignment="Top" Width="742" Margin="0,29,0,0" Background="#FF3E3C3B"/>
    <Menu x:Name="gameMenu" HorizontalAlignment="Left" Height="29" VerticalAlignment="Top" Width="742">
        <TextBlock x:Name="objectenMenuTextBlock" Text=" 1" Margin="10,1,0,0" Width="25" Foreground="#FF319105" FontSize="16" FontFamily="Snap ITC"/>
        <TextBlock Text="Meer objecten:" Margin="0,3,0,0"/>
        <Slider x:Name="objectenMenuSlider" Maximum="20" Minimum="1" Width="389"/>
        <Button x:Name="startenMenuButton" Content="Starten" Width="75" />
        <Button Content="Stoppen" Width="75" HorizontalAlignment="Right"/>
    </Menu>

</Grid>

0 个答案:

没有答案