菜单按钮不显示c#
Menu button not displayed c#
我正在利用业余时间制作一个 2D 太空射击游戏,以便在大学开始之前练习一些 c#。但是,我 运行 遇到了一个我似乎无法弄清楚的问题。对于菜单,我遵循了关于如何制作菜单的 Youtube 教程,但没有显示按钮。在过去的几天里,我一直在努力弄清楚,但没有运气,因此,我们将不胜感激任何帮助。如果代码令人困惑,我深表歉意,我仍在学习。当我 运行 玩游戏时,我没有遇到任何错误,我只是看到一个空白的蓝屏,没有显示任何按钮。
按钮Class
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Game1
{
class Button : Game1
{
Texture2D texture;
Vector2 position;
Rectangle rectangle;
Color colour = new Color(255, 255, 255, 255);
public Vector2 size;
bool down;
public bool isClicked;
public Button(Texture2D newText, GraphicsDevice graphics)
{
texture = newText;
size = new Vector2(graphics.Viewport.Width / 20, graphics.Viewport.Height / 10);
}
public void Update(MouseState mouse)
{
rectangle = new Rectangle((int)position.X, (int)position.Y,
(int)size.X, (int)size.Y);
Rectangle mouseRect = new Rectangle(mouse.X, mouse.Y, 1, 1);
if (mouseRect.Intersects(rectangle))
{
if (colour.A == 255) // if only one thing is in an if statement curly braces aren't needed
down = false;
if (colour.A == 0)
down = true;
if (down) colour.A += 3;
else colour.A -= 3;
if (mouse.LeftButton == ButtonState.Pressed)
isClicked = true;
}
// Once the mouse cursor has moved away from the button, the opacity will begin to increase.
else if (colour.A < 255)
{
colour.A += 3;
isClicked = false;
}
}
public void setPosition(Vector2 newPosition)
{
position = newPosition;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, rectangle, colour);
}
}`
}
相关Game1.CS代码:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System;
using System.Collections.Generic;
namespace Game1
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Texture2D mTexture;
public Vector2 mPosition = new Vector2(400, 0);
Sprite enemyShip;
public Vector2 Position;
player mPlayer;
player p;
MouseState previousMouseState;
public List<enemies> enemies = new List<enemies>();
Random random = new Random();
float spawn = 0; // delcaring the spawn variable as 0;
SpriteFont livesFont;
public float lives = 5;
SpriteFont scoreFont;
SpriteFont ammoFont;
public int ammoCount = 30;
Button playButton;
KeyboardState previousKeyboardState;
enum GameState
{
startMenu,
Loading,
Playing,
Paused,
}
// GameState gameState = new GameState();
GameState gameState = GameState.startMenu; // this is the state that it will load first.
enum MusicState
{
musicPause,
musicPlay,
}
MusicState musicState = MusicState.musicPlay;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
previousKeyboardState = Keyboard.GetState();
mPlayer = new player();
enemyShip = new Sprite();
p = new player();
IsMouseVisible = true; // this allows the mouse to be visible within the game.
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
playButton = new Button(Content.Load<Texture2D>("startButton"), graphics.GraphicsDevice);
playButton.setPosition(new Vector2(300, 200)); // setting position of the playButton.
mPlayer.LoadContent(this.Content);
mPlayer.bulletText = Content.Load<Texture2D>("bullet1"); // can load the texture this way, similiar to the line of code above.
mPlayer.LoadContent(this.Content, "spaceship");
enemyShip.LoadContent(this.Content, "enemyShip");
// loading SpriteFonts
scoreFont = Content.Load<SpriteFont>("NewSpriteFont");
livesFont = Content.Load<SpriteFont>("NewSpriteFont");
ammoFont = Content.Load<SpriteFont>("NewSpriteFont");
}
protected override void UnloadContent()
{
}
/// <param name="theGameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime theGameTime)
{
// keyboard and mouseStates
KeyboardState currentKeyboardState = Keyboard.GetState();
MouseState mouseState = Mouse.GetState();
previousMouseState = mouseState;
// switch case for gameStates
switch (gameState)
{
case GameState.startMenu:
if (playButton.isClicked == true)
{
gameState = GameState.Playing;
playButton.Update(mouseState);
}
break;
case GameState.Playing:
break;
}
// Pausing and playing the game and music
if (gameState == GameState.Playing)
{
if (currentKeyboardState.IsKeyDown(Keys.P) && previousKeyboardState.IsKeyUp(Keys.P))
{
gameState = GameState.Paused;
}
}
else if (gameState == GameState.Paused)
{
if (currentKeyboardState.IsKeyDown(Keys.P) && previousKeyboardState.IsKeyUp(Keys.P))
{
gameState = GameState.Playing;
}
}
if (musicState == MusicState.musicPlay)
{
if (currentKeyboardState.IsKeyDown(Keys.M) && previousKeyboardState.IsKeyUp(Keys.M))
{
MediaPlayer.Pause();
musicState = MusicState.musicPause;
}
}
else if (musicState == MusicState.musicPause)
{
if (currentKeyboardState.IsKeyDown(Keys.M)&& previousKeyboardState.IsKeyUp(Keys.M))
{
MediaPlayer.Play(song);
musicState = MusicState.musicPlay;
}
}
previousKeyboardState = currentKeyboardState;
colliitionDetection();
respawn();
loadEnemies(); // loads enemies through update.
spawn += (float)theGameTime.ElapsedGameTime.TotalSeconds;
foreach (enemies enemy in enemies) // foreach loop
enemy.Update(graphics.GraphicsDevice); // updating the enemy.
foreach (enemies enemy in enemies)
{
for (int b = 0; b < p.mbullets.Count; b++)
{
if (enemy.boundingBoxEnemy.Intersects(p.mbullets[b].boundingBoxBullet))
{
Console.WriteLine("please fucking work");
enemy.isVisible = false;
}
}
}
Vector2 aDirection = new Vector2(-1, 0); //aDirection - as the sprites are moving to the left we are decreasing the X direction.
Vector2 aSpeed = new Vector2(100, 0);
mPlayer.Update(theGameTime); // updating the player through here.
}
public void colliitionDetection()
{
for (int i = 0; i < enemies.Count; i++)
{
if (mPlayer.boundingBox.Intersects(enemies[i].boundingBoxEnemy))
{
mPlayer.Position.X = mPlayer.Position2.X;
/* Basicallyy what is happening, if the player hits the enemy it will stpawn him at position2, this is beacuase the player will always start the game at the start position, it is a 'const' unchangeable, but by saying that
* 'mPlayer.Position.X = mPlayer.Position2.X;' you are saying stating new coordinates for the player to spawn too, basically simple as that can't make it easier to understand you understand? ... needs to be a vector 2 aswell to use the xy
* */
if (mPlayer.Position.X == mPlayer.Position2.X)
{
//p.playerHealth -
lives = lives - 1;
respawn();
}
}
}
}
protected override void Draw(GameTime theGameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(); // sprite batch objects are what is used to draw the 2D images on the screen
// switch statement
// a switch statement that will draw sprites and fonts depending on the gameState
switch (gameState)
{
case GameState.startMenu: // the line below isn't being displayed on the screen...
playButton.Draw(this.spriteBatch); // SOMETHING WRONG WITH THIS.
// spriteBatch.Draw(Content.Load<Texture2D>("menuScreen"), new Rectangle (0,0 , 800, 600), Color.White);
break;
case GameState.Playing:
// spriteBatch.Draw(mTexture, mPosition, Color.White);
mBackground1.Draw(this.spriteBatch);
mBackground2.Draw(this.spriteBatch);
mBackground3.Draw(this.spriteBatch);
mBackground4.Draw(this.spriteBatch);
mBackground5.Draw(this.spriteBatch);
foreach (enemies enemy in enemies)
enemy.Draw(spriteBatch);
mPlayer.Draw(this.spriteBatch);
enemyShip.Draw(this.spriteBatch);
spriteBatch.DrawString(livesFont, lives.ToString(+lives + " lives remaining: "), new Vector2(600, 20), Color.Red);
// spriteBatch.DrawString(scoreFont, score.ToString("score " + score + " :"), new Vector2(100, 20), Color.Red);
spriteBatch.DrawString(ammoFont, ammoCount.ToString("ammo " + ammoCount + ":"), new Vector2(600, 100), Color.Green);
break;
case GameState.Loading:
break;
case GameState.Paused:
break;
}
spriteBatch.End(); // the end is used so once you've added everything you wanted to add to the screen the end will draw them all for you.
base.Draw(theGameTime);
}
}
我删除了一些与当前问题无关的代码。如果您更容易解决问题,请告诉我,我将 post 游戏 1 class.
的所有代码
非常感谢。
虽然这段代码还有很多改进的余地(举个例子,为什么你的按钮继承自 Game1 class)我相信你的问题是因为你的按钮从未初始化矩形 属性。您在构造函数中设置了大小,但在调用更新方法之前,您的矩形永远不会 initialized/set 。意思是在 button.Update 方法被调用之前,您的矩形的宽度和高度为 0, 0.. 在单击按钮之前,您的 button.Update 方法不会被调用。这就是按钮不可见的原因。因此,要解决您眼前的问题,请在构造函数中初始化您的矩形。
public Button(Texture2D newText, GraphicsDevice graphics)
{
texture = newText;
size = new Vector2(graphics.Viewport.Width / 20, graphics.Viewport.Height / 10);
rectangle = new Rectangle(0, 0, size.X, size.Y);
}
我正在利用业余时间制作一个 2D 太空射击游戏,以便在大学开始之前练习一些 c#。但是,我 运行 遇到了一个我似乎无法弄清楚的问题。对于菜单,我遵循了关于如何制作菜单的 Youtube 教程,但没有显示按钮。在过去的几天里,我一直在努力弄清楚,但没有运气,因此,我们将不胜感激任何帮助。如果代码令人困惑,我深表歉意,我仍在学习。当我 运行 玩游戏时,我没有遇到任何错误,我只是看到一个空白的蓝屏,没有显示任何按钮。
按钮Class
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Game1
{
class Button : Game1
{
Texture2D texture;
Vector2 position;
Rectangle rectangle;
Color colour = new Color(255, 255, 255, 255);
public Vector2 size;
bool down;
public bool isClicked;
public Button(Texture2D newText, GraphicsDevice graphics)
{
texture = newText;
size = new Vector2(graphics.Viewport.Width / 20, graphics.Viewport.Height / 10);
}
public void Update(MouseState mouse)
{
rectangle = new Rectangle((int)position.X, (int)position.Y,
(int)size.X, (int)size.Y);
Rectangle mouseRect = new Rectangle(mouse.X, mouse.Y, 1, 1);
if (mouseRect.Intersects(rectangle))
{
if (colour.A == 255) // if only one thing is in an if statement curly braces aren't needed
down = false;
if (colour.A == 0)
down = true;
if (down) colour.A += 3;
else colour.A -= 3;
if (mouse.LeftButton == ButtonState.Pressed)
isClicked = true;
}
// Once the mouse cursor has moved away from the button, the opacity will begin to increase.
else if (colour.A < 255)
{
colour.A += 3;
isClicked = false;
}
}
public void setPosition(Vector2 newPosition)
{
position = newPosition;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, rectangle, colour);
}
}`
}
相关Game1.CS代码:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System;
using System.Collections.Generic;
namespace Game1
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Texture2D mTexture;
public Vector2 mPosition = new Vector2(400, 0);
Sprite enemyShip;
public Vector2 Position;
player mPlayer;
player p;
MouseState previousMouseState;
public List<enemies> enemies = new List<enemies>();
Random random = new Random();
float spawn = 0; // delcaring the spawn variable as 0;
SpriteFont livesFont;
public float lives = 5;
SpriteFont scoreFont;
SpriteFont ammoFont;
public int ammoCount = 30;
Button playButton;
KeyboardState previousKeyboardState;
enum GameState
{
startMenu,
Loading,
Playing,
Paused,
}
// GameState gameState = new GameState();
GameState gameState = GameState.startMenu; // this is the state that it will load first.
enum MusicState
{
musicPause,
musicPlay,
}
MusicState musicState = MusicState.musicPlay;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
previousKeyboardState = Keyboard.GetState();
mPlayer = new player();
enemyShip = new Sprite();
p = new player();
IsMouseVisible = true; // this allows the mouse to be visible within the game.
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
playButton = new Button(Content.Load<Texture2D>("startButton"), graphics.GraphicsDevice);
playButton.setPosition(new Vector2(300, 200)); // setting position of the playButton.
mPlayer.LoadContent(this.Content);
mPlayer.bulletText = Content.Load<Texture2D>("bullet1"); // can load the texture this way, similiar to the line of code above.
mPlayer.LoadContent(this.Content, "spaceship");
enemyShip.LoadContent(this.Content, "enemyShip");
// loading SpriteFonts
scoreFont = Content.Load<SpriteFont>("NewSpriteFont");
livesFont = Content.Load<SpriteFont>("NewSpriteFont");
ammoFont = Content.Load<SpriteFont>("NewSpriteFont");
}
protected override void UnloadContent()
{
}
/// <param name="theGameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime theGameTime)
{
// keyboard and mouseStates
KeyboardState currentKeyboardState = Keyboard.GetState();
MouseState mouseState = Mouse.GetState();
previousMouseState = mouseState;
// switch case for gameStates
switch (gameState)
{
case GameState.startMenu:
if (playButton.isClicked == true)
{
gameState = GameState.Playing;
playButton.Update(mouseState);
}
break;
case GameState.Playing:
break;
}
// Pausing and playing the game and music
if (gameState == GameState.Playing)
{
if (currentKeyboardState.IsKeyDown(Keys.P) && previousKeyboardState.IsKeyUp(Keys.P))
{
gameState = GameState.Paused;
}
}
else if (gameState == GameState.Paused)
{
if (currentKeyboardState.IsKeyDown(Keys.P) && previousKeyboardState.IsKeyUp(Keys.P))
{
gameState = GameState.Playing;
}
}
if (musicState == MusicState.musicPlay)
{
if (currentKeyboardState.IsKeyDown(Keys.M) && previousKeyboardState.IsKeyUp(Keys.M))
{
MediaPlayer.Pause();
musicState = MusicState.musicPause;
}
}
else if (musicState == MusicState.musicPause)
{
if (currentKeyboardState.IsKeyDown(Keys.M)&& previousKeyboardState.IsKeyUp(Keys.M))
{
MediaPlayer.Play(song);
musicState = MusicState.musicPlay;
}
}
previousKeyboardState = currentKeyboardState;
colliitionDetection();
respawn();
loadEnemies(); // loads enemies through update.
spawn += (float)theGameTime.ElapsedGameTime.TotalSeconds;
foreach (enemies enemy in enemies) // foreach loop
enemy.Update(graphics.GraphicsDevice); // updating the enemy.
foreach (enemies enemy in enemies)
{
for (int b = 0; b < p.mbullets.Count; b++)
{
if (enemy.boundingBoxEnemy.Intersects(p.mbullets[b].boundingBoxBullet))
{
Console.WriteLine("please fucking work");
enemy.isVisible = false;
}
}
}
Vector2 aDirection = new Vector2(-1, 0); //aDirection - as the sprites are moving to the left we are decreasing the X direction.
Vector2 aSpeed = new Vector2(100, 0);
mPlayer.Update(theGameTime); // updating the player through here.
}
public void colliitionDetection()
{
for (int i = 0; i < enemies.Count; i++)
{
if (mPlayer.boundingBox.Intersects(enemies[i].boundingBoxEnemy))
{
mPlayer.Position.X = mPlayer.Position2.X;
/* Basicallyy what is happening, if the player hits the enemy it will stpawn him at position2, this is beacuase the player will always start the game at the start position, it is a 'const' unchangeable, but by saying that
* 'mPlayer.Position.X = mPlayer.Position2.X;' you are saying stating new coordinates for the player to spawn too, basically simple as that can't make it easier to understand you understand? ... needs to be a vector 2 aswell to use the xy
* */
if (mPlayer.Position.X == mPlayer.Position2.X)
{
//p.playerHealth -
lives = lives - 1;
respawn();
}
}
}
}
protected override void Draw(GameTime theGameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(); // sprite batch objects are what is used to draw the 2D images on the screen
// switch statement
// a switch statement that will draw sprites and fonts depending on the gameState
switch (gameState)
{
case GameState.startMenu: // the line below isn't being displayed on the screen...
playButton.Draw(this.spriteBatch); // SOMETHING WRONG WITH THIS.
// spriteBatch.Draw(Content.Load<Texture2D>("menuScreen"), new Rectangle (0,0 , 800, 600), Color.White);
break;
case GameState.Playing:
// spriteBatch.Draw(mTexture, mPosition, Color.White);
mBackground1.Draw(this.spriteBatch);
mBackground2.Draw(this.spriteBatch);
mBackground3.Draw(this.spriteBatch);
mBackground4.Draw(this.spriteBatch);
mBackground5.Draw(this.spriteBatch);
foreach (enemies enemy in enemies)
enemy.Draw(spriteBatch);
mPlayer.Draw(this.spriteBatch);
enemyShip.Draw(this.spriteBatch);
spriteBatch.DrawString(livesFont, lives.ToString(+lives + " lives remaining: "), new Vector2(600, 20), Color.Red);
// spriteBatch.DrawString(scoreFont, score.ToString("score " + score + " :"), new Vector2(100, 20), Color.Red);
spriteBatch.DrawString(ammoFont, ammoCount.ToString("ammo " + ammoCount + ":"), new Vector2(600, 100), Color.Green);
break;
case GameState.Loading:
break;
case GameState.Paused:
break;
}
spriteBatch.End(); // the end is used so once you've added everything you wanted to add to the screen the end will draw them all for you.
base.Draw(theGameTime);
}
}
我删除了一些与当前问题无关的代码。如果您更容易解决问题,请告诉我,我将 post 游戏 1 class.
的所有代码非常感谢。
虽然这段代码还有很多改进的余地(举个例子,为什么你的按钮继承自 Game1 class)我相信你的问题是因为你的按钮从未初始化矩形 属性。您在构造函数中设置了大小,但在调用更新方法之前,您的矩形永远不会 initialized/set 。意思是在 button.Update 方法被调用之前,您的矩形的宽度和高度为 0, 0.. 在单击按钮之前,您的 button.Update 方法不会被调用。这就是按钮不可见的原因。因此,要解决您眼前的问题,请在构造函数中初始化您的矩形。
public Button(Texture2D newText, GraphicsDevice graphics)
{
texture = newText;
size = new Vector2(graphics.Viewport.Width / 20, graphics.Viewport.Height / 10);
rectangle = new Rectangle(0, 0, size.X, size.Y);
}