自学XNA路(二)新手上路-移动背景图片
2013-03-01 02:41
190 查看
导入一个图片格式任意。
这里我导入了自己做的一个V.JPG
把文件拉到资源管理器的Content里面然后添加下面代码
接着就要移动图片了.
这样就完成自己第一个小程序,效果如下
这里我导入了自己做的一个V.JPG
把文件拉到资源管理器的Content里面然后添加下面代码
Texture2D backgroundTexture; SpriteBatch sprites; protected override void Initialize() { backgroundTexture = Content.Load<Texture2D>("V");//V为文件名 sprites = new SpriteBatch(graphics.GraphicsDevice); base.Initialize(); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.Goldenrod); sprites.Begin(); sprites.Draw(backgroundTexture, Vector2.Zero, Color.White); sprites.End(); base.Draw(gameTime); }运行后就得到结果:
接着就要移动图片了.
float scrollPosition = 0; protected override void Update(GameTime gameTime) { // Allows the game to exit GamePadState gamePad=GamePad.GetState(PlayerIndex.One); KeyboardState keyboard=Keyboard.GetState(); if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed|| keyboard.IsKeyDown(Keys.Escape)) this.Exit(); float moveFactorPerSecond=200*(float)gameTime.ElapsedRealTime.TotalMilliseconds/500.0f;//每帧移动像素 if (gamePad.DPad.Up == ButtonState.Pressed || keyboard.IsKeyDown(Keys.Up))//判断按键上 scrollPosition += moveFactorPerSecond; if (gamePad.DPad.Down == ButtonState.Pressed || keyboard.IsKeyDown(Keys.Down))//判断按键下 scrollPosition -= moveFactorPerSecond; // TODO: Add your update logic here base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.Goldenrod); sprites.Begin(); int resolutionWidth = graphics.GraphicsDevice.Viewport.Width;//图像宽 int resolutionHeight = graphics.GraphicsDevice.Viewport.Height;//图像高 for (int x = 0; x <= resolutionWidth / backgroundTexture.Width; x++)//填充背景图片,目前会出现刷新有问题的BUG按下会出现半截没有刷新图片露出背景 { for (int y = -1; y <= resolutionHeight / backgroundTexture.Height; y++) { Vector2 position = new Vector2(x * backgroundTexture.Width, y * backgroundTexture.Height + ((int)scrollPosition) % backgroundTexture.Height); sprites.Draw(backgroundTexture, position, Color.White); } } sprites.End(); base.Draw(gameTime); }
这样就完成自己第一个小程序,效果如下
相关文章推荐
- 自学XNA路(一)新手上路
- jQuery鼠标移动到Div上后切换背景,或更换图片
- 用Qt实现窗体的无边框、可移动、透明不规则显示(需背景透明的图片)
- 【Cocos2d-x】新手自学(七)百分比读取进度加载图片资源到缓存(2.0.1版本)
- 当鼠标移动到图片上会显示 不同的背景
- 鼠标移动,修改背景图片
- 背景图片随滚动轴移动
- 用Qt实现窗体的无边框、可移动、透明不规则显示(需背景透明的图片)
- 【实例】html5-canvas中实现背景图片的移动
- 在IE6,7下输入类型的表单控件输入文字的时候,背景图片会跟着一块移动
- 按钮的背景图片变换。。。。(button移动到上面的时候是手型,几个去除链接/按钮虚线框的方法 )
- 新手上路Servlet下载图片出现乱码--练习笔记
- 【实例】html5-canvas中实现背景图片的移动
- 貌似Keep登录首页 图片在背景移动
- 简单背景图片,鼠标移动特效
- js根据鼠标移动速度背景图片自动旋转的方法
- Android 仿QQ菜单背景图片的左右上下移动属性动画实现
- 2015-6-18 按钮菜单图片(正常,按下);scheduleUpdate;移动背景
- 鼠标移动更改图片背景
- 【Cocos2d-x】新手自学(七)百分比读取进度加载图片资源到缓存(2.0.1版本)