dir.by  
  Поиск  
Компьютер, программы
MonoGame - develop games on C# for computers (Windows, Linux), phones (Android, iOS), game consoles (Xbox, PlayStation, Nintendo Switch)
 Рисуем прямоугольник в новом MonoGame приложении 
посмотрели 9891 раз
обновлено: 16 Augusta 2023
В Visual Studio в файл Game1.cs добавим C# код
1) в класс Game1 добавим переменную:
Texture2D rectangleBlock;

2) создаем
protected override void LoadContent()
{
...
    rectangleBlock = new Texture2D(GraphicsDevice, 1, 1);
    Microsoft.Xna.Framework.Color xnaColorBorder = new Microsoft.Xna.Framework.Color(128, 128, 128); // default color gray
    rectangleBlock.SetData(new[] { xnaColorBorder });
...
}

3) внутри функции рисуем прямоугольник:
protected override void Draw(GameTime gameTime)
{
...
    spriteBatch.Begin();
    Microsoft.Xna.Framework.Point position = new Microsoft.Xna.Framework.Point(100, 50); // position
    Microsoft.Xna.Framework.Point size = new Microsoft.Xna.Framework.Point(30, 20); // size
    Rectangle rectangle = new Microsoft.Xna.Framework.Rectangle(position, size);
    Microsoft.Xna.Framework.Color color = new Microsoft.Xna.Framework.Color(255, 255, 0);// color yellow
    spriteBatch.Draw(rectangleBlock, rectangle, color);
    spriteBatch.End();

...
}
Весь код
  C#  
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Game1
{
     /// <summary>
     /// This is the main type for your game.
     /// </summary>
     public class Game1 : Game
     {
          GraphicsDeviceManager graphics;
          SpriteBatch spriteBatch;

          // переменная
          Texture2D rectangleBlock;

          public Game1()
          {
               graphics = new GraphicsDeviceManager(this);
               Content.RootDirectory = "Content";

               graphics.IsFullScreen = true;
               graphics.PreferredBackBufferWidth = 800;
               graphics.PreferredBackBufferHeight = 480;
               graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;
          }

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

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

               // создаем элемент
               rectangleBlock = new Texture2D(GraphicsDevice, 1, 1);
               Microsoft.Xna.Framework.Color xnaColorBorder = new Microsoft.Xna.Framework.Color(128, 128, 128); // default color gray
               rectangleBlock.SetData(new[] { xnaColorBorder });
          }

          /// <summary>
          /// UnloadContent will be called once per game and is the place to unload
          /// game-specific 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)
          {
               if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                    Exit();

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

               // рисуем текст
               spriteBatch.Begin();
               Microsoft.Xna.Framework.Point position = new Microsoft.Xna.Framework.Point(100, 50); // position
               Microsoft.Xna.Framework.Point size = new Microsoft.Xna.Framework.Point(30, 20); // size
               Rectangle rectangle = new Microsoft.Xna.Framework.Rectangle(position, size);
               Microsoft.Xna.Framework.Color color = new Microsoft.Xna.Framework.Color(255, 255, 0); // color yellow
               spriteBatch.Draw(rectangleBlock, rectangle, color);
               spriteBatch.End();

               base.Draw(gameTime);
          }
     }
}
Запуск приложения
1) Компилирем приложение: Menu → Build → Build Solution (F7)
нет ошибок
2) Запускаем приложение: Menu → Debug → Start Without Debugging (Ctrl + F5)
Работает!
 
← Previous topic
Installing, running MonoGame application on a computer with the operating system iOS (MacOSX) using Visual Studio
 
Next topic →
Add a png file and draw a picture in the new MonoGame application
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

  Объявления  
  Объявления  
 
MonoGame
What is MonoGame?
What is Mono (Virtual Machine for C# on Unix) ?
What are the advantages of Monogame?
How to write a game in MonoGame and compile to different platforms: Windows computer, macOS computer, Android phone, iOS apple phone, Nintendo Switch game console
Download and install MonoGame
Download and install Microsoft Visual Studio to write MonoGame games on a computer with the Windows system
Open Visual Studio (to write a game using MonoGame)
Installing MonoGame (download and install) for Windows inside Visual Studio
Create and run a MonoGame application on a computer (Windows) in Android emulator mode
Create a new MonoGame application on the computer (Windows) in emulator mode Android
Running and debugging the MonoGame app on your computer (Windows) in Android emulator mode
Runs the old version of the MonoGame app in debug in Android emulator mode
Running and debugging the MonoGame application on your phone via USB
Running the MonoGame application on an Android phone (creating an apk file)
Android (install)
Installing the Android Platform (SDK 7.1.1 API 25) in Visual Studio ...
Create a Android virtual device in Visual Studio
Installing, running the MonoGame application on a computer with the MacOSX system
Installing, running MonoGame application on a computer with the operating system iOS (MacOSX) using Xamarin Studio
Installing, running MonoGame application on a computer with the operating system iOS (MacOSX) using Visual Studio
MonoGame Functionality
How to Draw a Rectangle in the New MonoGame App
Add a png file and draw a picture in the new MonoGame application
How to draw text in the new MonoGame app
Books
Books to learn MonoGame

  Ваши вопросы присылайте по почте: info@dir.by