Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#,在屏幕上显示一个变量_C#_Variables_Xna - Fatal编程技术网

C#,在屏幕上显示一个变量

C#,在屏幕上显示一个变量,c#,variables,xna,C#,Variables,Xna,我用C#制作的游戏是方形追逐。在块移动之前,将鼠标悬停在块上并单击它。计时器将在后台倒计时,分数将在区块上的每个“标签”上增加1。您可以通过示例在XNA4.0游戏开发上完成该项目。这是2010年的C#,项目类型是Windows游戏(4.0) 现在我想给它添加额外的东西。我希望每次他们点击块时都能抽出5秒的时间,但我不知道它是否有效,所以我需要让剩余的时间可见。我想每次你到广场的时候把它弄小一点 代码:(在更新和if语句下,我已经对时间做了减法,但希望通过使其可见来确保其工作)谢谢 我需要知道如何

我用C#制作的游戏是方形追逐。在块移动之前,将鼠标悬停在块上并单击它。计时器将在后台倒计时,分数将在区块上的每个“标签”上增加1。您可以通过示例在XNA4.0游戏开发上完成该项目。这是2010年的C#,项目类型是Windows游戏(4.0)

现在我想给它添加额外的东西。我希望每次他们点击块时都能抽出5秒的时间,但我不知道它是否有效,所以我需要让剩余的时间可见。我想每次你到广场的时候把它弄小一点

代码:(在更新和if语句下,我已经对时间做了减法,但希望通过使其可见来确保其工作)谢谢

我需要知道如何下沉,在大图像中,我需要知道如何在屏幕上显示浮动。

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 RFSquareChase
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Random rand = new Random ();
        Texture2D squareTexture;
        Rectangle currentSquare;
        int playerScore = 0;
        float timeRemaining = 0.0f;
        const float TimePerSquare = 0.75f;
        Color [] colors = new Color[3] { Color.Red, Color.Green, Color.Blue };

        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();

            this.IsMouseVisible = true;
        }

        /// <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);

            squareTexture = Content.Load<Texture2D>(@"SQUARE");

            // 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

            if (timeRemaining == 0.0f)
            {
                currentSquare = new Rectangle(
                    rand.Next(0, this.Window.ClientBounds.Width - 25),
                    rand.Next(0, this.Window.ClientBounds.Height - 25),
                    25, 25);
                timeRemaining = TimePerSquare;
            }

            MouseState mouse = Mouse.GetState();

            if ((mouse.LeftButton == ButtonState.Pressed) && 
                (currentSquare.Contains(mouse.X, mouse.Y)))
            {
                playerScore++;
                timeRemaining = 0.0f;
                timeRemaining = timeRemaining - 5;
            }
            timeRemaining = MathHelper.Max(0, timeRemaining - (float)gameTime.ElapsedGameTime.TotalSeconds);

            this.Window.Title = "Score : " + playerScore.ToString();

            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.Gray);

            spriteBatch.Begin();
            spriteBatch.Draw(
                squareTexture,
                currentSquare,
                colors[playerScore % 3]);
            spriteBatch.End();

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Audio;
使用Microsoft.Xna.Framework.Content;
使用Microsoft.Xna.Framework.GamerServices;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Input;
使用Microsoft.Xna.Framework.Media;
命名空间RFSquareChase
{
/// 
///这是游戏的主要类型
/// 
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
Random rand=新的Random();
纹理2D方形纹理;
矩形和正方形;
int playerScore=0;
浮动剩余时间=0.0f;
常数浮点时间平方=0.75f;
Color[]colors=新颜色[3]{Color.Red,Color.Green,Color.Blue};
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
}
/// 
///允许游戏在开始运行之前执行任何需要的初始化。
///在这里,它可以查询任何必需的服务,并加载任何非图形化的服务
///相关内容。调用base.Initialize将枚举所有组件
///并对它们进行初始化。
/// 
受保护的覆盖无效初始化()
{
//TODO:在此处添加初始化逻辑
base.Initialize();
this.IsMouseVisible=true;
}
/// 
///LoadContent将在每个游戏中调用一次,并且是加载的地方
///你所有的内容。
/// 
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
squareTexture=内容加载(@“SQUARE”);
//TODO:使用此.Content在此处加载游戏内容
}
/// 
///UnloadContent将在每个游戏中调用一次,并且是卸载的地方
///所有内容。
/// 
受保护的覆盖无效UnloadContent()
{
//TODO:在此卸载任何非ContentManager内容
}
/// 
///允许游戏运行逻辑,例如更新世界,
///检查碰撞、收集输入和播放音频。
/// 
///提供计时值的快照。
受保护覆盖无效更新(游戏时间游戏时间)
{
//允许游戏退出
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)
这是Exit();
//TODO:在此处添加更新逻辑
如果(剩余时间==0.0f)
{
currentSquare=新矩形(
rand.Next(0,this.Window.ClientBounds.Width-25),
rand.Next(0,this.Window.ClientBounds.Height-25),
25, 25);
剩余时间=TimePerSquare;
}
MouseState mouse=mouse.GetState();
如果((mouse.LeftButton==ButtonState.Pressed)和
(currentSquare.Contains(mouse.X,mouse.Y)))
{
playerScore++;
剩余时间=0.0f;
剩余时间=剩余时间-5;
}
timeRemaining=MathHelper.Max(0,timeRemaining-(float)gameTime.ElapsedGameTime.TotalSeconds);
this.Window.Title=“Score:+playerScore.ToString();
更新(游戏时间);
}
/// 
///这就是所谓的比赛应该平局的时候。
/// 
///提供计时值的快照。
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色。灰色);
spriteBatch.Begin();
spriteBatch.画图(
方形纹理,
当前广场,
颜色[播放器核心%3]);
spriteBatch.End();
//TODO:在此处添加图形代码
基础。抽签(游戏时间);
}
}
}

要在屏幕上显示浮点值,只需使用
spriteBatch.DrawString
,并且可以使用
Math.Round
仅显示所需的小数。如你所见


要更改精灵的尺寸,可以在
spriteBatch.Draw
中设置
scale
参数。正如您所看到的,为什么不能在表单上添加一个标签,显示每次
更新(\*…*\)
?查看Spritebatch。DrawString@iabbott:XNA项目没有表单控件。我已经能够看到剩余时间受TimePerSquare的影响,我需要知道如何将分数转换为小数。我应该能够做到这一点,但我仍然不知道如何在屏幕上显示我必须执行的时间:this.Window.Title=“time:”+timeRemaining.ToString();那么你的问题是什么?您希望我们为您做这件事(这就是为什么,我假设,您发布了您的整个代码库、相关代码或