Alle Beiträge und Antworten
Snowball - 28.10.2007, 18:20
XNA - Codeschnipsel - Move_Background-2
Hier mal nen wenig komplexer:
Code: #region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion
namespace x8
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
ContentManager content;
KeyboardState ksState;
SpriteBatch Sprite;
SpriteFont Arial;
int hSpeed = 10;
int vSpeed = 10;
Vector2 mousepos;
Vector2 spritePosition = Vector2.Zero;
Texture2D worldmap;
Texture2D player;
SpriteBatch spriteBatch;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(this.Services);
graphics.IsFullScreen = true;
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{
this.worldmap = Texture2D.FromFile(this.graphics.GraphicsDevice,
"worldmap.bmp");
this.player = Texture2D.FromFile(this.graphics.GraphicsDevice,
"player.bmp");
this.spriteBatch = new SpriteBatch(this.graphics.GraphicsDevice);
this.IsMouseVisible = true;
}
Arial = content.Load<SpriteFont>("Arial");
Sprite = new SpriteBatch(graphics.GraphicsDevice);
}
protected override void UnloadGraphicsContent(bool unloadAllContent)
{
if (unloadAllContent == true)
{
this.content.Unload();
}
}
protected override void Update(GameTime gameTime)
{
MouseState mouse = Mouse.GetState();
if (mouse.LeftButton == ButtonState.Pressed)
{
mousepos = new Vector2(mouse.X, mouse.Y);
}
ksState = Keyboard.GetState();
if (ksState.IsKeyDown(Keys.Up))
{
this.spritePosition.Y += this.vSpeed;
}
if (ksState.IsKeyDown(Keys.Down))
{
this.spritePosition.Y -= this.vSpeed;
}
if (ksState.IsKeyDown(Keys.Left))
{
this.spritePosition.X += this.hSpeed;
}
if (ksState.IsKeyDown(Keys.Right))
{
this.spritePosition.X -= this.hSpeed;
}
if (ksState.IsKeyDown(Keys.Escape))
{
this.Exit();
}
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
this.graphics.GraphicsDevice.Clear(Color.White);
this.spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
this.spriteBatch.Draw(this.worldmap, this.spritePosition, Color.White);
this.spriteBatch.Draw(this.player, new Vector2(350, 200), Color.White);
this.spriteBatch.End();
Sprite.Begin();
Sprite.DrawString(Arial, "Maus Position: " + mousepos, new Vector2(250, 285), Color.Green);
Sprite.End();
base.Draw(gameTime);
}
}
}
Mit folgendem Code, können Sie den Beitrag ganz bequem auf ihrer Homepage verlinken