Salut,
Je présente aujourd'hui une classe que j'ai fait pour pouvoir afficher une image avec du texte au dessus facilement. Ce n'est pas très dur à faire, mon but est donc de le publier ainsi aider d'autres.
Voici le code de la classe qui s'appelle ImageWithTexte:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 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; using Microsoft.Xna.Framework.Net; using Microsoft.Xna.Framework.Storage; namespace WindowsGame10 { class ImageWithTexte { Vector2 positionImage; Texture2D texture; SpriteFont Font; float textePosX; float textePosY; Vector2 textePosition; string Text; string ImageName; float ImagePositionX; float ImagePositionY; public ImageWithTexte(string Text, float ImagePositionX, float ImagePositionY) { positionImage = new Vector2(this.ImagePositionX = ImagePositionX, this.ImagePositionY = ImagePositionY); textePosition = new Vector2(textePosX = ImagePositionX, textePosY = ImagePositionY - 25); ImagePositionX = positionImage.X; ImagePositionY = positionImage.Y; this.Text = Text; } public void Loads(ContentManager content, string FontName, string ImageName) { this.ImageName = ImageName; Font = content.Load<SpriteFont>(FontName); texture = content.Load <Texture2D>(ImageName); } public void Draw(SpriteBatch spriteBatch) { spriteBatch.Begin(); spriteBatch.Draw(texture, positionImage, Color.White); spriteBatch.DrawString(Font, Text, textePosition, Color.White); spriteBatch.End(); } } }
Ensuite, voilà le code qui permet de l'utiliser:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 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; using Microsoft.Xna.Framework.Net; using Microsoft.Xna.Framework.Storage; using WindowsGame10; namespace WindowsGame11 { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; ImageWithTexte image; //On déclare la classe. public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; image = new ImageWithTexte("the text is me", 50, 100); //On assigne le texte, et les positions X et Y de l'image sur la fenêtre. this.graphics.PreferredBackBufferHeight = 300; //Taille de la fenêtre. this.graphics.PreferredBackBufferWidth = 300; //Taille de la fenêtre. } protected override void Initialize() { base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); image.Loads(Content, "Font", "Torshid"); //Le nom du ContentManager utilisé, le nom du fichier SpriteFont et le nom de l'image se situant dans le ContentManager. } protected override void UnloadContent() { } protected override void Update(GameTime gameTime) { base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); image.Draw(spriteBatch); //On dessine l'image et le texte qui se trouve au dessus. base.Draw(gameTime); } } }
La position du texte n'est modifiable ici que par la classe. Avec peu de modifications, il est possible d'afficher le texte au milieu de l'image (je ne parle que de l'axe X) et de modifier la position en dehors de la classe.
Comme ça qu'on affiche les pseudos des joueurs dans un jeu 2D.
Bon bah voilà, en espérant que ça aide certaines personnes!
P.S.: Je sais que le code est un peu foutu.
Partager