XNA实现不停循环的路的效果
XNA实现不停循环的路的效果
by wgscd 2015-06-03
using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Media; namespace WindowsGame1 { /// <summary> /// This is the main type for your game /// </summary> public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Texture2D bg; Texture2D bg2; Texture2D bg3; Point position = new Point(); Point position2 = new Point(); Point position3 = new Point(); int w = 0; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } /// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here base.Initialize(); } /// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); bg = Content.Load<Texture2D>("bg"); bg2 = Content.Load<Texture2D>("bg2"); bg3 = Content.Load<Texture2D>("bg3"); w = bg.Width; graphics.PreferredBackBufferWidth = w; // TODO: use this.Content to load your game content here } /// <summary> /// UnloadContent will be called once per game and is the place to unload /// all content. /// </summary> protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } /// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); // TODO: Add your update logic here base.Update(gameTime); position.X += 1; position2.X = position.X+w; position3.X = position.X - w; if (position.X >= graphics.PreferredBackBufferWidth){ position.X = 0; } } /// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: Add your drawing code here spriteBatch.Begin(); Rectangle rec1 = new Rectangle() { X = position.X, Y = 0, Width = bg.Width, Height = bg.Height }; Rectangle rec2 = new Rectangle() { X = position2.X, Y = 0, Width = bg.Width, Height = bg.Height }; Rectangle rec3 = new Rectangle() { X = position3.X, Y = 0, Width = bg.Width, Height = bg.Height }; spriteBatch.Draw(bg, rec1, Color.White); spriteBatch.Draw(bg2, rec2, Color.White); spriteBatch.Draw(bg3, rec3, Color.White); spriteBatch.End(); base.Draw(gameTime); } } }
Silverlight/WPF 版本:
<UserControl x:Class="WPFTestRun.UI.RoadBg" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="335.594" d:DesignWidth="669.152"> <UserControl.Resources> <Storyboard x:Key="sb1" x:Name="sb1"> <DoubleAnimation x:Name="d1" From=" 0" To="22" Duration="0:0:12" Storyboard.TargetName="trans1" Storyboard.TargetProperty="X" /> <DoubleAnimation x:Name="d2" From=" 0" To="22" Duration="0:0:12" Storyboard.TargetName="trans2" Storyboard.TargetProperty="X" /> </Storyboard> </UserControl.Resources> <Grid> <Grid.Background> <ImageBrush ImageSource="/WPFTestRun;component/Assets/bg/bgok.jpg"/> </Grid.Background> <Image x:Name="img1" Source="/WPFTestRun;component/Assets/bg/bgok.jpg" Stretch="Fill" RenderTransformOrigin="0.5,0.5"> <Image.RenderTransform> <TranslateTransform x:Name="trans1" X="0"/> </Image.RenderTransform> </Image> <Image x:Name="img2" Source="/WPFTestRun;component/Assets/bg/bgok.jpg" Stretch="Fill" RenderTransformOrigin="0.5,0.5"> <Image.RenderTransform> <TranslateTransform x:Name="trans2" X="0"/> </Image.RenderTransform> </Image> </Grid> </UserControl>
code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WPFTestRun.UI { /// <summary> /// RoadBg.xaml 的交互逻辑 /// </summary> public partial class RoadBg : UserControl { public RoadBg() { InitializeComponent(); Loaded += RoadBg_Loaded; } Storyboard sb1 ; Storyboard sb2; DoubleAnimation d1; DoubleAnimation d2; void RoadBg_Loaded(object sender, RoutedEventArgs e) { sb1 = (Resources["sb1"] as Storyboard); sb1.RepeatBehavior = RepeatBehavior.Forever; if (sb1 == null ) { return; } d1 = sb1.Children[0] as DoubleAnimation; d2 = sb1.Children[1] as DoubleAnimation; d1.From = 0; d1.To = -this.RenderSize.Width; d2.From = this.RenderSize.Width; d2.To = 0; sb1.Begin(); } } }
fffffffffffffffff
test red font.