Support
Quality
Security
License
Reuse
Coming Soon for all Libraries!
Currently covering the most popular Java, JavaScript and Python libraries. See a SAMPLE HERE.
kandi's functional review helps you automatically verify the functionalities of the libraries and avoid rework.
Build Status
Supported Platforms
Support and Contributions
Source Code
Helpful Links
License
Monogame-uwp package can´t be published to the Windows Store because it still targets Windows.Mobile and can´t be enrypted because of that
<TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.10049.0" MaxVersionTested="10.0.10049.0" />
How to draw on the fly 2D pixel-by-pixel in MonoGame/XNA?
Texture2D pixel = new Texture2D(GraphicsDevice, 1, 1);
pixel.SetData(new Color[] {Color.White});
-----------------------
Texture2D Tex, Tex1, Tex2;
const int WIDTH = 32;
const int HEIGHT = 32;
Color[] TexSource = new Color[WIDTH * HEIGHT];
int radius = 5;
point center = new Point(WIDTH >> 2, HEIGHT >> 2);
Tex = new Texture2D(GraphicsDevice, WIDTH, HEIGHT);
for(int x = 0 ; x < WIDTH; x++)
for(int y = 0 ; y < HEIGHT; y++)
if(Math.Sqrt((x - center.x)* (x - center.x)) < radius && Math.Sqrt((y - center.y) * (y - center.y) < radius))
TexSource[x + y * WIDTH] = Color.Red;
else
TexSource[x + y * WIDTH] = Color.White;
Tex = SetData<Color>(TexSource);
//The resulting texture is a red circle on a white background.
//If you want to draw on top of an existing texture Tex1 as Tex2:
Tex1 = Content.Load<Texture2D>(Tex1Name);
Tex2 = new Texture2D(GraphicsDevice, Tex1.Width, Tex1.Height);
TexSource = Tex1.GetData<Color>();
// Draw a white \ on the image, we will assume tex1 is square for this task
for(int x = 0 ; x < Tex1.Width; x++)
for(int y = 0 ; y < Tex1.Width; y++)
if(x==y) TexSource[x + y * Tex1.Width] = Color.White;
-----------------------
Texture2D Tex, Tex1, Tex2;
const int WIDTH = 32;
const int HEIGHT = 32;
Color[] TexSource = new Color[WIDTH * HEIGHT];
int radius = 5;
point center = new Point(WIDTH >> 2, HEIGHT >> 2);
Tex = new Texture2D(GraphicsDevice, WIDTH, HEIGHT);
for(int x = 0 ; x < WIDTH; x++)
for(int y = 0 ; y < HEIGHT; y++)
if(Math.Sqrt((x - center.x)* (x - center.x)) < radius && Math.Sqrt((y - center.y) * (y - center.y) < radius))
TexSource[x + y * WIDTH] = Color.Red;
else
TexSource[x + y * WIDTH] = Color.White;
Tex = SetData<Color>(TexSource);
//The resulting texture is a red circle on a white background.
//If you want to draw on top of an existing texture Tex1 as Tex2:
Tex1 = Content.Load<Texture2D>(Tex1Name);
Tex2 = new Texture2D(GraphicsDevice, Tex1.Width, Tex1.Height);
TexSource = Tex1.GetData<Color>();
// Draw a white \ on the image, we will assume tex1 is square for this task
for(int x = 0 ; x < Tex1.Width; x++)
for(int y = 0 ; y < Tex1.Width; y++)
if(x==y) TexSource[x + y * Tex1.Width] = Color.White;
Where did these borders come from in Monogame Android?
#if ANDROID
graphics.IsFullScreen = true;
graphics.PreferredBackBufferWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
graphics.PreferredBackBufferHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
graphics.SupportedOrientations = DisplayOrientation.Portrait;//if your game does NOT support for anything else but portrait mode
graphics.ApplyChanges();
#endif
C# cannot convert from 'float' to 'Microsoft.Xna.Framework.Point'
int scaledSize = (int)(sizeOfSprites * scale);
Microsoft.Xna.Framework.Rectangle position = new Microsoft.Xna.Framework.Rectangle(
cell.X * scaledSize,
cell.Y * scaledSize,
scaledSize,
scaledSize
);
-----------------------
Microsoft.Xna.Framework.Rectangle position = new Microsoft.Xna.Framework.Rectangle(
(int)(cell.X * SIZE), (int)(cell.Y * SIZE), SIZE, SIZE);
);
My ball and my rectangle wont collide properly (Monogame)
public void enemyCollision(Rectangle enemyRectangle)
{
if (ballRectangle.Intersects(enemyRectangle))
ballDirectionY = false;
}
public void enemyCollision(Rectangle enemyRectangle)
{
if (ballRectangle.Intersects(enemyRectangle))
{
if (ballRectangle.x > enemyRectangle.x &&
ballRectangle.x < enemyRectangle.x + enemyRectangle.Width &&
ballRectangle.y > enemyRectangle.y)
ballDirectionY = false;
}
}
-----------------------
public void enemyCollision(Rectangle enemyRectangle)
{
if (ballRectangle.Intersects(enemyRectangle))
ballDirectionY = false;
}
public void enemyCollision(Rectangle enemyRectangle)
{
if (ballRectangle.Intersects(enemyRectangle))
{
if (ballRectangle.x > enemyRectangle.x &&
ballRectangle.x < enemyRectangle.x + enemyRectangle.Width &&
ballRectangle.y > enemyRectangle.y)
ballDirectionY = false;
}
}
Monogame not drawing png images
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
realisticSturgeonSprite = Texture2D.FromFile(GraphicsDevice,@"Lake_Sturgeon.png");
// you can add path if needed (copy to output option above doesn't need it)
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
_spriteBatch.Begin();
_spriteBatch.Draw(realisticSturgeonSprite, new Vector2(50, 60), Color.White);
_spriteBatch.End();
base.Draw(gameTime);
}
Monogame buttons
_spriteBatch.Draw(ButtonTexture,ButtonRectangle,Color.White);
ButtonRectangle = new Rectangle(ScreenX * 0.4,ScreenY*0.5,ScreenX * 0.1,ScreenY*0.1);
MouseState mouse = Mouse.GetState();
if(ButtonRectangle.Contains(mouse.Position) && mouse.LeftButton == ButtonState.Pressed)
{
//do stuff
}
-----------------------
_spriteBatch.Draw(ButtonTexture,ButtonRectangle,Color.White);
ButtonRectangle = new Rectangle(ScreenX * 0.4,ScreenY*0.5,ScreenX * 0.1,ScreenY*0.1);
MouseState mouse = Mouse.GetState();
if(ButtonRectangle.Contains(mouse.Position) && mouse.LeftButton == ButtonState.Pressed)
{
//do stuff
}
-----------------------
_spriteBatch.Draw(ButtonTexture,ButtonRectangle,Color.White);
ButtonRectangle = new Rectangle(ScreenX * 0.4,ScreenY*0.5,ScreenX * 0.1,ScreenY*0.1);
MouseState mouse = Mouse.GetState();
if(ButtonRectangle.Contains(mouse.Position) && mouse.LeftButton == ButtonState.Pressed)
{
//do stuff
}
c# monogame gradually rotate camera behind a player
float newRotation = targetY;
if (targetY < MathHelper.ToRadians(90) && currentY > MathHelper.ToRadians(270))
{
newRotation = targetY + MathHelper.ToRadians(360);
}
if (targetY > MathHelper.ToRadians(270) && currentY < MathHelper.ToRadians(90))
{
newRotation = targetY - MathHelper.ToRadians(360);
}
if (currentY < newRotation)
{
currentY += (newRotation - currentY) / rotateSpeed;
}
if (currentY > newRotation)
{
currentY -= Math.Abs(newRotation - currentY) / rotateSpeed;
}
if (currentY >= MathHelper.Pi * 2) currentY = 0.0f;
if (currentY < 0) currentY = MathHelper.Pi * 2;
-----------------------
float newRotation = targetY;
if (targetY < MathHelper.ToRadians(90) && currentY > MathHelper.ToRadians(270))
{
newRotation = targetY + MathHelper.ToRadians(360);
}
if (targetY > MathHelper.ToRadians(270) && currentY < MathHelper.ToRadians(90))
{
newRotation = targetY - MathHelper.ToRadians(360);
}
if (currentY < newRotation)
{
currentY += (newRotation - currentY) / rotateSpeed;
}
if (currentY > newRotation)
{
currentY -= Math.Abs(newRotation - currentY) / rotateSpeed;
}
if (currentY >= MathHelper.Pi * 2) currentY = 0.0f;
if (currentY < 0) currentY = MathHelper.Pi * 2;
Monogame VsCode extension isn't working properly when I try to use it
dotnet tool install --global dotnet-mgcb-editor
mgcb-editor --register
"mgcb.pipelineToolPath": "mgcb-editor"
-----------------------
dotnet tool install --global dotnet-mgcb-editor
mgcb-editor --register
"mgcb.pipelineToolPath": "mgcb-editor"
Physics Simulation Ball falling into floor
if (particle.Top < 0)
{
velocity.Y = -velocity.Y;
particle.Y = -particle.Y;
}
else if (particle.Bottom >= GraphicsDevice.ViewPort.Height)
{
velocity.Y = -velocity.Y;
particle.Y = GraphicsDevice.ViewPort.Height - (particle.Bottom - GraphicsDevice.ViewPort.Height + particle.Height);
}
QUESTION
How to structure a project in Monogame
Asked 2022-Mar-14 at 17:18I'm trying to create a PC version of a cord-driven turn-based boardgame. I went for the Monogame framework in c#. I'm still learning C# and Monogame and I'm reading all about how it is important to "split" the code in the data/game engine part and the graphics/GUI part. So trying to pursue this, I have the following in mind.
Graphics/GUI
Here I define all the classes relative to the screens (game - menus), GUI components like labels and buttons and possibly even mouse and keyboard inputs (the logic being the user has to click on something in the gui in order to perform an action).
Data or Game logic/Engine
Still not even sure on how I should call this part of the project, here I would put all the logic of the game. The classes of player, cards, "IA", rules etc... would be defined here.
My difficulty is now to understand how to connect this two parts but still maintain the "loosely coupled" approach. I was thinking that maybe the objects or better the instances of the game logic part would have to subscribe to events (such as button_click) defined in the GUI part...but can I even subscribe to events of another class? Is this the correct way?
I admit I'm still trying to fully understand the event driven concept, but is this the right way of doing stuff in c# and Monogame? I apologize for the vague topic/question but thanks in advance.
ANSWER
Answered 2022-Mar-14 at 17:18I don't know anything about Monogame, but generally it's a good idea to organise any kind of application into layers like this, as long as you don't make the code overly complicated by doing so. Your idea to separate the UI code from the game logic sounds like a good start.
First, think of your application as an onion, with a series of concentric layers. Your proposed structure has only two layers, but that's OK. A larger application may benefit from more layers, e.g. a data access layer if you were to store the state of the game in a database, but there's no need to add extra layers just for the sake of having more layers.
Your outermost layer would contain the UI code, typically this is known as the presentation layer.
Your innermost layer would contain the game logic, typically this is known as the domain layer. A good rule of thumb for whether something belongs in the domain layer is to ask, does this represent something that a user of your application (or a player of your game) who's not a developer would recognise?
Second, remember that each layer can make calls to code in the same layer, or to code in in other layers inside it, but not to code in layers outside it. So projects in the presentation layer can have references to projects in the domain layer, but projects in the domain layer can't have references to projects in the presentation layer. If you've got circular references between the two layers then it's a sign that something in one of the layers actually belongs in the other layer.
Here's another answer to a similar question about layering an application, albeit not in the context of a game: https://stackoverflow.com/a/70315655/16563198
On the second part of the question, if you want to use events then it's probably best to handle any events in the presentation layer and have the event handlers make calls into the domain layer (but disclaimer, I don't know Monogame).
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
No vulnerabilities reported
Save this library and start creating your kit
Explore Related Topics
Save this library and start creating your kit