new to programming but if anyone has any clue let me know

i have tried key detection backtracking seeing where it works using online tools to try help checking if all code is functioning as it should all the detection and keyboard state seems correct i am not 100 percent sure what could be causing the issue for it not to draw on screen

namespace BlobSurvival
{

    enum Dir
    {
        Down,
        Up,
        Left,
        Right
    }
    //enum  used for direction


    public class Game1 : Game
    {
        private GraphicsDeviceManager _graphics;
        private SpriteBatch _spriteBatch;

        Texture2D playerSprite;
        Texture2D walkDown;
        Texture2D walkUp;
        Texture2D walkRight;
        Texture2D walkLeft;

        Texture2D background;
        Texture2D bullet;
        Texture2D enemy;

        Player player = new Player();
        
        Camera camera;

        //intialises the camera to follow player from the actual package

        public Game1()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
        }

        protected override void Initialize()
        {
            _graphics.PreferredBackBufferWidth = 1280;
            _graphics.PreferredBackBufferHeight = 720;
            _graphics.ApplyChanges();
            //changes the screen size to 1280 x 720

            this.camera = new Camera(_graphics.GraphicsDevice);
            //camera initalised in game

            base.Initialize();
        }

        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            playerSprite = Content.Load<Texture2D>("player/player");
            walkDown = Content.Load<Texture2D>("player/walkDown");
            walkLeft = Content.Load<Texture2D>("player/walkLeft");
            walkRight = Content.Load<Texture2D>("player/walkRight");
            walkUp = Content.Load<Texture2D>("player/walkUp");
            //dash is used to specify that asset was in folder
            background = Content.Load<Texture2D>("background");
            bullet = Content.Load<Texture2D>("bullet");
            enemy = Content.Load<Texture2D>("enemy");
            Enemy.Enemies.Add(new Enemy(new Vector2(100, 100), enemy));
            Enemy.Enemies.Add(new Enemy(new Vector2(700, 200), enemy));
            //spawns instance of enemy from the enemy class from list
            player.animations[0] = new SpriteAnimation(walkDown, 4, 12);
            player.animations[1] = new SpriteAnimation(walkUp, 4, 12);
            player.animations[2] = new SpriteAnimation(walkLeft, 4, 12);
            player.animations[3] = new SpriteAnimation(walkRight, 4, 12);
            // array of the different animations
            //stored in specific order so it matches with the DIR enum
            player.animation = player.animations[0];
            //initalises so the animation actually starts

        }

        

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            player.Update(gameTime);
            //Updates player method every frame

            this.camera.Position = player.Position;
            // camera is equal to player position, so camera follows player

            this.camera.Update(gameTime);
            //updates camera

            foreach (Bullets bullets in Bullets.bullets)
            {
                bullets.Update(gameTime);
            }
            // loop that goes through each projectile from list 

            foreach (Enemy enemY in Enemy.Enemies)
            {
                enemY.Update(gameTime, player.Position);
            }
            // player position passes through , so the enemy knows where the player is 
            base.Update(gameTime);

            foreach(Bullets bullets in Bullets.bullets)
            {
                foreach (Enemy enemy in Enemy.Enemies) 
                { 
                 int sum = bullets.radius + enemy.radius;
                    if (Vector2.Distance(bullets.Position, enemy.Position) > sum )
                    {
                        bullets.Collided = true;
                    }
                }
            }
            Bullets.bullets.RemoveAll(p => p.Collided);

            //bullets equal Collided
            // Remove all bullets from the collection where the 'Collided' property is true

            //iterates though each bullet in this bullet list
            // for every bullet it goes through the entire list of enemies
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            _spriteBatch.Begin(this.camera);
            _spriteBatch.Draw(background,new Vector2(-500, -500), Color.White);
            foreach (Enemy enemy in Enemy.Enemies)
            {
                enemy.anim.Draw(_spriteBatch);
            }
            //Draws the enemy animations in multiple instances
            foreach (Bullets bullets in Bullets.bullets)
            {
                _spriteBatch.Draw(bullet, new Vector2(bullets.Position.X - 48, bullets.Position.Y - 48), Color.White);
                //CENTERS THE BULLET SO IT COMES FROM the player not the side of it
            }
            player.animation.Draw(_spriteBatch);

            //draws bullet on screen, each instance of it

            //instead of drawing the player instead it draws the animation, the parameters can be changed such as "color" in spriteAnimation
            //player.position grabs the position value from player class
            _spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace BlobSurvival
{
    internal class Bullets
    {
        public static List<Bullets> bullets = new List<Bullets>();
        //list of type bullets , since static can be accessed outside class by Bullets.bullets
        private Vector2 position;
        private int speed = 900;
        public int radius = 18;
        //bullet circle size
        private Dir direction;
        //uses the dir enum from game1
        private bool collided = false;
        //keeps track if bullet has made collision with enemy

        public Bullets(Vector2 newPos, Dir newDir)
        {
            position = newPos;
            direction = newDir;
        }

        public Vector2 Position
        {
            get
            {
                return position;
            }
        }

        public bool Collided
        {
            get { return collided; }
            set { collided = value; }
        }

        // Property representing the collision state with getter (read) and setter (write) methods
        // Getter retrieves the current state of the collision, and setter allows code in the other places update it

        //makes the position public in other classess

        public void Update(GameTime gameTime)
        { 
            float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

            switch (direction)
            {
                case Dir.Right:
                    position.X += speed * dt;
                    break;

                case Dir.Left:
                    position.X -= speed * dt;
                    break;

                case Dir.Up:
                    position.Y -= speed * dt;
                    break;

                case Dir.Down:
                    position.Y += speed * dt;
                    break;
                    //projectile has its own speed postion utilising previous code 
            }
        }
    }
}


1

There are 1 best solutions below

2
Steven On

Have you tried debugging the code/placing breakpoints? You wouldn't need online tools to check for errors as long as you have Visual Studio Intellisense and been able to debug.

From the looks of it, the code seems fine. The thing that may be off is that the bullet position is a negative value. if the description states that it's for centering the bullet with the player, then I think that value should be positive, or at least different. As a default position is often at top-left. So now it feels possible that it's drawing correctly, but out of bounds.