WP 一个判断角度的场景

WP版打小鸟游戏炮台瞄准逻辑
本文介绍了一个Windows Phone版本的小鸟游戏中的炮台瞄准逻辑实现方式。通过使用鼠标点击屏幕,炮台能够根据点击位置调整其旋转角度,以实现精确瞄准。该逻辑涉及数学计算,包括三角函数的应用。

准备写个WP版打小鸟的游戏。
当鼠标点击屏幕的时候要炮火方向旋转的角度适应点击的角度;

如图:
105032-20151125215106031-1546899369.jpg

逻辑代码:
double stageHeight = 60;//炮底座高度
Point touchPoint = new Point();//记录点击是点坐标
Size PanelSize = new Size();//保存舞台的大小
private void ContentPanel_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
PanelSize = ContentPanel.RenderSize;
touchPoint = e.GetPosition(ContentPanel);
//弧度转角度:除以Math.PI乘以180
double res = 0;
if (touchPoint.X >( PanelSize.Width/2))
{
res = Math.Atan(((PanelSize.Height - stageHeight - touchPoint.Y) / ( touchPoint.X- PanelSize.Width/2))) / Math.PI * 180d;

            t1.Rotation =(90- res);
        }
        else
        {
            res = Math.Atan(((PanelSize.Height - stageHeight - touchPoint.Y) / ( PanelSize.Width / 2- touchPoint.X))) / Math.PI * 180d;

            t1.Rotation = -(90d-res );
        }

         txt.Text = "" + res;//弧度抓角度:处以Math.PI乘以180

    }

AXML代码
<phone:PhoneApplicationPage
x:Class="Birds.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">

<!--LayoutRoot 是包含所有页面内容的根网格-->
<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>


    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="我的应用程序" 
                   Style="{StaticResource PhoneTextNormalStyle}" 
                   Margin="12,0"
                   Name="txt"
                   />
    </StackPanel>

    <!--ContentPanel - 在此处放置其他内容-->
    <Grid x:Name="ContentPanel"
          Grid.Row="1" Background="Aqua"
          MouseLeftButtonDown="ContentPanel_MouseLeftButtonDown" >

        <Rectangle VerticalAlignment="Bottom" 
                Fill="Red"
                Height="60" 
                Width="20"
                Margin="0,0,0,50"
                RenderTransformOrigin="0.5,1">
            <Rectangle.RenderTransform>
                <CompositeTransform x:Name="t1" 
                                    Rotation="11"/>
            </Rectangle.RenderTransform>
        </Rectangle>
        <Rectangle VerticalAlignment="Bottom" 
                Fill="Red"
                Height="60" 
                   RenderTransformOrigin="0.5,0.5">
          
        </Rectangle>

    </Grid>

    <!--取消注释,以显示对齐网格,从而帮助确保
        控件在公用边界上对齐。图像在系统栏中显示时的边距
        上为 -32px。如果隐藏了系统栏,则将此值设为 0
        (或完全删除边距)。

        在发送之前删除此 XAML 和图像本身。-->
    <!--<Image Source="/Assets/AlignmentGrid.png" VerticalAlignment="Top" Height="800" Width="480" Margin="0,-32,0,0" Grid.Row="0" Grid.RowSpan="2" IsHitTestVisible="False" />-->
</Grid>

转载于:https://www.cnblogs.com/wgscd/articles/4996052.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值