diff options
| author | BoredGuy <osome3717@gmail.com> | 2026-01-15 03:27:19 -0800 |
|---|---|---|
| committer | BoredGuy <osome3717@gmail.com> | 2026-01-15 03:27:19 -0800 |
| commit | 1ee13bd3d05d5a35f251b7d47a94cab7fbc8f5c7 (patch) | |
| tree | b1b4530c8d7b31b40cee000c6a6f7190196718e8 | |
| parent | 3149303926e7c56fb9508f0b60d26fe8c675d6aa (diff) | |
Simple Game For Practice
| -rw-r--r-- | DungeonSlime/Content/Content.mgcb | 11 | ||||
| -rw-r--r-- | DungeonSlime/Content/Fonts/Ariel.spritefont | 60 | ||||
| -rw-r--r-- | DungeonSlime/Content/Fonts/File.spritefont | 60 | ||||
| -rw-r--r-- | DungeonSlime/Game1.cs | 134 | ||||
| -rw-r--r-- | MonoGameLibrary/Core.cs | 1 |
5 files changed, 217 insertions, 49 deletions
diff --git a/DungeonSlime/Content/Content.mgcb b/DungeonSlime/Content/Content.mgcb index 0c3469d..15b8a51 100644 --- a/DungeonSlime/Content/Content.mgcb +++ b/DungeonSlime/Content/Content.mgcb @@ -13,11 +13,12 @@ #---------------------------------- Content ---------------------------------# -#begin ../../../../Music/output/learning.wav -/importer:WavImporter -/processor:SongProcessor -/processorParam:Quality=Best -/build:../../../../Music/output/learning.wav;Music/learning.wav +#begin Fonts/Ariel.spritefont +/importer:FontDescriptionImporter +/processor:FontDescriptionProcessor +/processorParam:PremultiplyAlpha=True +/processorParam:TextureFormat=Compressed +/build:Fonts/Ariel.spritefont #begin Images/logo.png /importer:TextureImporter diff --git a/DungeonSlime/Content/Fonts/Ariel.spritefont b/DungeonSlime/Content/Fonts/Ariel.spritefont new file mode 100644 index 0000000..bd33ecf --- /dev/null +++ b/DungeonSlime/Content/Fonts/Ariel.spritefont @@ -0,0 +1,60 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +This file contains an xml description of a font, and will be read by the XNA +Framework Content Pipeline. Follow the comments to customize the appearance +of the font in your game, and to change the characters which are available to draw +with. +--> +<XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics"> + <Asset Type="Graphics:FontDescription"> + + <!-- + Modify this string to change the font that will be imported. + --> + <FontName>Arial</FontName> + + <!-- + Size is a float value, measured in points. Modify this value to change + the size of the font. + --> + <Size>12</Size> + + <!-- + Spacing is a float value, measured in pixels. Modify this value to change + the amount of spacing in between characters. + --> + <Spacing>0</Spacing> + + <!-- + UseKerning controls the layout of the font. If this value is true, kerning information + will be used when placing characters. + --> + <UseKerning>true</UseKerning> + + <!-- + Style controls the style of the font. Valid entries are "Regular", "Bold", "Italic", + and "Bold, Italic", and are case sensitive. + --> + <Style>Regular</Style> + + <!-- + If you uncomment this line, the default character will be substituted if you draw + or measure text that contains characters which were not included in the font. + --> + <!-- <DefaultCharacter>*</DefaultCharacter> --> + + <!-- + CharacterRegions control what letters are available in the font. Every + character from Start to End will be built and made available for drawing. The + default range is from 32, (ASCII space), to 126, ('~'), covering the basic Latin + character set. The characters are ordered according to the Unicode standard. + See the documentation for more information. + --> + <CharacterRegions> + <CharacterRegion> + <Start> </Start> + <End>~</End> + </CharacterRegion> + </CharacterRegions> + </Asset> +</XnaContent> diff --git a/DungeonSlime/Content/Fonts/File.spritefont b/DungeonSlime/Content/Fonts/File.spritefont new file mode 100644 index 0000000..bd33ecf --- /dev/null +++ b/DungeonSlime/Content/Fonts/File.spritefont @@ -0,0 +1,60 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +This file contains an xml description of a font, and will be read by the XNA +Framework Content Pipeline. Follow the comments to customize the appearance +of the font in your game, and to change the characters which are available to draw +with. +--> +<XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics"> + <Asset Type="Graphics:FontDescription"> + + <!-- + Modify this string to change the font that will be imported. + --> + <FontName>Arial</FontName> + + <!-- + Size is a float value, measured in points. Modify this value to change + the size of the font. + --> + <Size>12</Size> + + <!-- + Spacing is a float value, measured in pixels. Modify this value to change + the amount of spacing in between characters. + --> + <Spacing>0</Spacing> + + <!-- + UseKerning controls the layout of the font. If this value is true, kerning information + will be used when placing characters. + --> + <UseKerning>true</UseKerning> + + <!-- + Style controls the style of the font. Valid entries are "Regular", "Bold", "Italic", + and "Bold, Italic", and are case sensitive. + --> + <Style>Regular</Style> + + <!-- + If you uncomment this line, the default character will be substituted if you draw + or measure text that contains characters which were not included in the font. + --> + <!-- <DefaultCharacter>*</DefaultCharacter> --> + + <!-- + CharacterRegions control what letters are available in the font. Every + character from Start to End will be built and made available for drawing. The + default range is from 32, (ASCII space), to 126, ('~'), covering the basic Latin + character set. The characters are ordered according to the Unicode standard. + See the documentation for more information. + --> + <CharacterRegions> + <CharacterRegion> + <Start> </Start> + <End>~</End> + </CharacterRegion> + </CharacterRegions> + </Asset> +</XnaContent> diff --git a/DungeonSlime/Game1.cs b/DungeonSlime/Game1.cs index 879b96a..d84fe25 100644 --- a/DungeonSlime/Game1.cs +++ b/DungeonSlime/Game1.cs @@ -1,4 +1,7 @@ -using Microsoft.Xna.Framework; +using System; +using System.Collections.Generic; +using System.Formats.Tar; +using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; @@ -6,23 +9,91 @@ using MonoGameLibrary; namespace DungeonSlime; +internal class Target +{ + //Constants + private static readonly Rectangle SourceRect = new Rectangle(0, 0, 128, 128); + private static readonly float BaseScale = 0.5f; + private static readonly float MaxScale = 1.5f; + + //Fields + private Vector2 _position = Vector2.Zero; + private float _scale = 1f; + private readonly Texture2D _logoTexture; + + public Target(Texture2D logoTexture, Random random, int windowWidth, int windowHeight) + { + _logoTexture = logoTexture; + Randomize(random, windowWidth, windowHeight); + } + + public void Randomize(Random random, int windowWidth, int windowHeight) + { + _position = + new Vector2((float)random.NextDouble(), (float)random.NextDouble()) * new Vector2(windowWidth, windowHeight); + _scale = BaseScale + (MaxScale - BaseScale) * random.NextSingle(); + } + + public virtual bool IsHit(MouseState mouseState) + { + float mouseX = mouseState.Position.X; + float mouseY = mouseState.Position.Y; + + return mouseState.LeftButton == ButtonState.Pressed && + _position.X <= mouseX && mouseX <= _position.X + SourceRect.Width * _scale && + _position.Y <= mouseY && mouseY <= _position.Y + SourceRect.Height * _scale; + } + + public void Draw(SpriteBatch _spriteBatch) + { + _spriteBatch.Draw( + _logoTexture, + _position, + SourceRect, + Color.White, + 0f, + Vector2.Zero, + _scale, + SpriteEffects.None, + 0f + ); + } +} + public class Game1 : Core { + private static readonly int NumTargets = 10; + private Texture2D _logo; + private List<Target> targets = []; + private int _score = 0; + private Random _random = new Random(); + private SpriteFont _arielFont; public Game1(): base("Dungeon Slime", 1280, 720, false) { - + } protected override void Initialize() { base.Initialize(); + + for (var i = 0; i < NumTargets; i++) + { + targets.Add(new Target( + _logo, + _random, + Window.ClientBounds.Width, + Window.ClientBounds.Height + )); + } } protected override void LoadContent() { _logo = Content.Load<Texture2D>("Images/logo"); + _arielFont = Content.Load<SpriteFont>("Fonts/Ariel"); } protected override void Update(GameTime gameTime) @@ -30,7 +101,17 @@ public class Game1 : Core if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) Exit(); - // TODO: Add your update logic here + foreach (var target in targets) + { + if (target.IsHit(Mouse.GetState())) + { + target.Randomize(_random, Window.ClientBounds.Width, Window.ClientBounds.Height); + _score++; + + //Can't hit multiple targets at once + break; + } + } base.Update(gameTime); } @@ -39,51 +120,16 @@ public class Game1 : Core { GraphicsDevice.Clear(Color.CornflowerBlue); - Rectangle iconSourceRect = new Rectangle(0, 0,128, 128); - Rectangle wordmarkSourceRect = new Rectangle(150, 34, 458, 58); - SpriteBatch.Begin( sortMode: SpriteSortMode.BackToFront, - samplerState: SamplerState.LinearWrap + samplerState: SamplerState.PointClamp ); - //Draw only icon portion - SpriteBatch.Draw( - _logo, - new Vector2( - Window.ClientBounds.Width, - Window.ClientBounds.Height - ) * 0.5f, - iconSourceRect, - Color.White, - 0.0f, - new Vector2( - iconSourceRect.Width, - iconSourceRect.Height - ) * 0.5f, - 1.0f, - SpriteEffects.None, - 1.0f - ); - - //Draw only rectangle portion - SpriteBatch.Draw( - _logo, - new Vector2( - Window.ClientBounds.Width, - Window.ClientBounds.Height - ) * 0.5f, - wordmarkSourceRect, - Color.White, - 0.0f, - new Vector2( - wordmarkSourceRect.Width, - wordmarkSourceRect.Height - ) * 0.5f, - 1.0f, - SpriteEffects.None, - 0.0f - ); + SpriteBatch.DrawString(_arielFont, $"{_score}", Vector2.Zero, Color.Green); + foreach (var target in targets) + { + target.Draw(SpriteBatch); + } SpriteBatch.End(); diff --git a/MonoGameLibrary/Core.cs b/MonoGameLibrary/Core.cs index a746030..fddcc0c 100644 --- a/MonoGameLibrary/Core.cs +++ b/MonoGameLibrary/Core.cs @@ -29,6 +29,7 @@ public class Core : Game { Graphics.PreferredBackBufferWidth = width; Graphics.PreferredBackBufferHeight = height; Graphics.IsFullScreen = fullscreen; + Graphics.SynchronizeWithVerticalRetrace = true; Graphics.ApplyChanges(); |
