C# 子弹不是';不要像游戏一样出现在太空入侵者身上

C# 子弹不是';不要像游戏一样出现在太空入侵者身上,c#,xna,xna-4.0,C#,Xna,Xna 4.0,我刚刚开始制作一个类似太空入侵者的游戏 在编程方面,我是一个完全的新手,经验很少(我只是尝试一下,因为我需要在下周末完成一个软件设计项目) 不管怎样,我一直在学习如何发射子弹。而且它似乎不起作用。除了“子弹”类中的“速度”变量外,我几乎复制了本教程的所有方面(我认为我不需要这个变量,因为我只使用侧向移动,而不是向前/向后移动) 这是下面的代码。提前感谢。:) Main using System; using System.Collections.Generic; using System.Li

我刚刚开始制作一个类似太空入侵者的游戏

在编程方面,我是一个完全的新手,经验很少(我只是尝试一下,因为我需要在下周末完成一个软件设计项目)

不管怎样,我一直在学习如何发射子弹。而且它似乎不起作用。除了“子弹”类中的“速度”变量外,我几乎复制了本教程的所有方面(我认为我不需要这个变量,因为我只使用侧向移动,而不是向前/向后移动)

这是下面的代码。提前感谢。:)


Main

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;

namespace Software_Design_Major_Project
{

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D ship; // Declaring the sprite
        Vector2 shipposition = Vector2.Zero; 

        List<bullets> bullets = new List<bullets>();
        KeyboardState pastkey;

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

            graphics.PreferredBackBufferWidth = 600; 


        }



        protected override void Initialize()
        {

            base.Initialize();
        }


        protected override void LoadContent()
        {

            spriteBatch = new SpriteBatch(GraphicsDevice);

            ship = Content.Load<Texture2D>("ship"); // Loads the ship into the memory.
            shipposition = new Vector2((graphics.GraphicsDevice.Viewport.Width / 2) -
            (ship.Width / 2), 420);


        }


        protected override void UnloadContent()
        {

        }


        protected override void Update(GameTime gameTime)
        {

            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Left) && shipposition.X
            >= 0) 
            {
                shipposition.X -= 6;
            }
            if(Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Right) && shipposition.X <
            ((graphics.GraphicsDevice.Viewport.Width) - (ship.Width))) 
            {
                shipposition.X += 6;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Space) && pastkey.IsKeyUp(Keys.Space)) 
            {
                shoot();                
            }
            pastkey = Keyboard.GetState();
            updatebullets();
            base.Update(gameTime);
        }

        public void updatebullets() 
        {
            foreach(bullets bullet in bullets)
            {

                if (Vector2.Distance(bullet.position, shipposition) < 0)
                    bullet.isvisible = false;
            }

            for (int i = 0; i < bullets.Count; i++) 
            {
                if (!bullets[i].isvisible)
                    bullets.RemoveAt(i);
                i--;
            }
        }

        public void shoot() 
        {
            bullets newbullet = new bullets(Content.Load<Texture2D>("bullet"));
            newbullet.position = shipposition;
            newbullet.isvisible = true;

            if (bullets.Count < 20)
                bullets.Add(newbullet);
        }

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

            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend); 
            spriteBatch.Draw(ship, shipposition, Color.White);
            spriteBatch.End();

            foreach (bullets bullet in bullets)
                bullet.Draw(spriteBatch);

            base.Draw(gameTime);
        }



    }
}

顺便说一句,很抱歉发了这么长的邮件

我没有看到任何代码会改变子弹的位置,你需要一个速度变量,并根据每帧的速度更新位置

你可以从a=Math.Atan(Y,X)得到角度


看起来你可能删除了计算位置的部分,因为你认为你不需要速度的东西。我建议您尝试将其添加回去,看看是否有效,然后删除不需要的部件。

您可以在此处查看此示例。我很久以前就编写过这个东西了,我想在XNA的v2.0中,现在记不得了

其中的逻辑很简单:)

开始制作“经典”总是一个好的开始

编辑-您也可以在此处获得它: Edit2-我现在刚刚检查了代码,其中有一些注释记录了大部分内容,可能你会得到一些用葡萄牙语编写的东西。
PS-不要复制超过代码!试着学习样本的基本知识。

没人会很快发现这一点。 子弹还没抽出来,你就要结束你的SpriteBatch了。 调用SpriteBatch.Draw必须在Begin和End方法之间进行,因为所有的工作都是在幕后进行的,以使绘图尽可能快。 您只需将spriteBatch.End()进一步向下移动,如下所示:

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

        spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
        spriteBatch.Draw(ship, shipposition, Color.White);

        foreach (bullets bullet in bullets)
            bullet.Draw(spriteBatch);

        spriteBatch.End(); //Move to here instead

        base.Draw(gameTime);
    }
额外琐事:
基本上,幕后的情况是,sprite批处理实际上使用begin(是的,这些可以更改)描述的设置对纹理进行批处理,然后在调用end时,它计算出哪些位落在屏幕之外,并注意不要绘制它们,如果要求,它会对纹理进行深度排序,并在将纹理绘制到后缓冲区之前进行各种其他优化,最后在所有绘制完成后,后缓冲区会显示在屏幕上,旧的后缓冲区会被提取并重新用于下一次绘制调用。这听起来需要做很多工作,但随着现代计算机的工作方式,这些优化往往会带来很大的改进。

嗨,欢迎光临!考虑你的问题更具描述性的标题。大多数来这里的人都需要某种方式的帮助。让人们了解你的问题所在。=)也要考虑清楚你的实际问题,并且很容易找到。在这种情况下,“似乎不起作用”是我能找到的唯一提示,表明您有问题。它怎么不起作用?什么不起作用?你认为会发生什么?你看过可用的XNA样本了吗?告诉子弹如何移动不是很重要吗?无论你是做2D还是3D运动,速度都是一段时间内位置的变化。如果没有这些,子弹就会停在那里。哈哈,有没有办法编辑标题?对不起,这里有点晚了,没有校对我的问题。嗯,从我在教程中看到的情况来看,在这家伙完成教程后,他能够毫无问题地拍摄。我已经设法让我的飞船精灵用箭头键移动,但是当我按空格键射击时,没有子弹出现,我无法移动飞船。我把速度的东西放回去了,但我仍然遇到同样的问题D:
protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
        spriteBatch.Draw(ship, shipposition, Color.White);

        foreach (bullets bullet in bullets)
            bullet.Draw(spriteBatch);

        spriteBatch.End(); //Move to here instead

        base.Draw(gameTime);
    }