在C#中的键上绘制不同的精灵
本文关键字:精灵 绘制 | 更新日期: 2023-09-27 18:27:17
im试图用enums在按键上绘制不同的精灵,遵循了我在这个网站上找到的一个例子,但我的代码似乎只呈现Sprite_1。我最初在bools上设置了这个,但这样我也只能加载一个精灵,奇怪的是,尽管它是一个不同的精灵(精灵_2)。我做错了什么?
using System;
using System.Text;
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.Storage;
using Microsoft.Xna.Framework.Media;
namespace WindowsGame3
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
//Screen Stuff
const int Screen_Height = 1280;
const int Screen_Width = 720;
Rectangle Screen_Bounds;
//Sprite stuff
int Player_Size = 60;
Rectangle Player1;
Rectangle Player2;
Rectangle Player3;
Rectangle Player4;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
Screen_Bounds = new Rectangle(0, 0, Screen_Width, Screen_Height);
}
/// <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();
}
enum Spritestate
{
Player_Left,
Player_Right,
Player_Up,
Player_Down
}
Spritestate RealTime_Player;
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
///
Texture2D Sprite_1;//Facing Right
Vector2 Sprite_1Position = new Vector2(100, 100);
Texture2D Sprite_2;//Facing Left
Vector2 Sprite_2Position = new Vector2(100, 100);
Texture2D Sprite_3;//Facing Up
Vector2 Sprite_3Position = new Vector2(100, 100);
Texture2D Sprite_4;//Facing Down
Vector2 Sprite_4Position = new Vector2(100, 100);
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
Sprite_1 = Content.Load<Texture2D>("Tank_Right");
Sprite_2 = Content.Load<Texture2D>("Tank_Left");
Sprite_3 = Content.Load<Texture2D>("Tank_Up");
Sprite_4 = Content.Load<Texture2D>("Tank_Down");
// 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();
KeyboardState keyboard = Keyboard.GetState();
if (keyboard.IsKeyDown(Keys.W))
{
Spritestate RealTime_Player = Spritestate.Player_Up;
Sprite_1Position.Y -= 5;
Sprite_2Position.Y -= 5;
Sprite_3Position.Y -= 5;
Sprite_4Position.Y -= 5;
}
if (keyboard.IsKeyDown(Keys.A))
{
Spritestate RealTime_Player = Spritestate.Player_Right;
Sprite_1Position.X -= 5;
Sprite_2Position.X -= 5;
Sprite_3Position.X -= 5;
Sprite_4Position.X -= 5;
}
if (keyboard.IsKeyDown(Keys.S))
{
Spritestate RealTime_Player = Spritestate.Player_Down;
Sprite_1Position.Y += 5;
Sprite_2Position.Y += 5;
Sprite_3Position.Y += 5;
Sprite_4Position.Y += 5;
}
if (keyboard.IsKeyDown(Keys.D))
{
Spritestate RealTime_Player = Spritestate.Player_Left;
Sprite_1Position.X += 5;
Sprite_2Position.X += 5;
Sprite_3Position.X += 5;
Sprite_4Position.X += 5;
}
Player1 = new Rectangle((int)Sprite_1Position.X, (int)Sprite_1Position.Y, Player_Size, Player_Size);
Player2 = new Rectangle((int)Sprite_2Position.X, (int)Sprite_2Position.Y, Player_Size, Player_Size);
Player3 = new Rectangle((int)Sprite_3Position.X, (int)Sprite_3Position.Y, Player_Size, Player_Size);
Player4 = new Rectangle((int)Sprite_4Position.X, (int)Sprite_4Position.Y, Player_Size, Player_Size);
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <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();
switch (RealTime_Player)
{
case Spritestate.Player_Right:
spriteBatch.Draw(Sprite_1, Player1, Color.White);
Vector2 Sprite_1Position = new Vector2(100, 100);
break;
case Spritestate.Player_Left:
spriteBatch.Draw(Sprite_2, Player2, Color.White);
Vector2 Sprite_2Position = new Vector2(100, 100);
break;
case Spritestate.Player_Up:
spriteBatch.Draw(Sprite_3, Player3, Color.White);
Vector2 Sprite_3Position = new Vector2(100, 100);
break;
case Spritestate.Player_Down:
spriteBatch.Draw(Sprite_4, Player4, Color.White);
Vector2 Sprite_4Position = new Vector2(100, 100);
break;
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}
在每个if
:中创建一个新变量
if (keyboard.IsKeyDown(Keys.W))
{
Spritestate RealTime_Player = Spritestate.Player_Up;
这不是类变量。更改为:
if (keyboard.IsKeyDown(Keys.W))
{
RealTime_Player = Spritestate.Player_Up;