Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#单博弈蛇博弈_C#_.net_Xna_Monogame - Fatal编程技术网

C#单博弈蛇博弈

C#单博弈蛇博弈,c#,.net,xna,monogame,C#,.net,Xna,Monogame,首先,我必须说我是一个完全的noob,我刚开始使用2d图形,我在一个蛇游戏上做实验,在更新方法中,我称之为snakeUpdate,它根据按键状态更新位置。问题是它从不更新位置,请有人解释一下 using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphi

首先,我必须说我是一个完全的noob,我刚开始使用2d图形,我在一个蛇游戏上做实验,在更新方法中,我称之为snakeUpdate,它根据按键状态更新位置。问题是它从不更新位置,请有人解释一下

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Utilities;


namespace SnakeGame1
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    private Texture2D background;
    private Texture2D shuttle;
    private Texture2D earth;
    KeyboardState newState = new KeyboardState();
    KeyboardState oldState = new KeyboardState();
    private float angle = 0;
    public Vector2 position= new Vector2(480,240);
    public Vector2 velocity;
    public Game1()
        : base()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }


    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
        // TODO: Add your initialization logic here

        base.Initialize();
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>


    protected override void LoadContent()
    {

        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        // TODO: use this.Content to load your game content here
        background = Content.Load<Texture2D>("stars.jpg"); // change these names to the names of your images
        shuttle = Content.Load<Texture2D>("shuttle.png");  // if you are using your own images.
        earth = Content.Load<Texture2D>("earth.png");
    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();

        listener();
        snakeUp();




        // TODO: Add your update logic here
        //angle += 0.01f;

        base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        // TODO: Add your drawing code here
        spriteBatch.Begin();
        spriteBatch.Draw(background, new Rectangle(0, 0, 800, 480), Color.White);
        spriteBatch.Draw(earth, new Vector2(400, 240), Color.WhiteSmoke);
        Vector2 origin = new Vector2(0, 0);
        spriteBatch.Draw(shuttle, position, new Rectangle(0,0, shuttle.Width, shuttle.Height), Color.White, angle, origin, 0.5f, SpriteEffects.None, 1);
        spriteBatch.End();

        base.Draw(gameTime);
    }

    public void listener()
    {

        if (newState.IsKeyDown(Keys.Right) && (oldState.IsKeyUp(Keys.Right)))
            velocity = new Vector2(3, 0);
        if (newState.IsKeyDown(Keys.Left) && (oldState.IsKeyUp(Keys.Left)))
            velocity = new Vector2(-3, 0);
        if (newState.IsKeyDown(Keys.Down) && (oldState.IsKeyUp(Keys.Down)))
            velocity = new Vector2(0, 3);
        if (newState.IsKeyDown(Keys.Up) && (oldState.IsKeyUp(Keys.Up)))
            velocity = new Vector2(0, -3);

    }
    public void snakeUp() {
        position += velocity;


    }
}
使用系统;
使用System.Collections.Generic;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Content;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Input;
使用Microsoft.Xna.Framework.Storage;
使用Microsoft.Xna.Framework.Utilities;
命名空间蛇形游戏1
{
/// 
///这是游戏的主要类型
/// 
公共类游戏1:游戏
{
图形管理器图形;
SpriteBatch SpriteBatch;
私人背景;
私人飞机;
私有结构2D地球;
KeyboardState newState=新的KeyboardState();
KeyboardState oldState=新KeyboardState();
专用浮动角度=0;
公共向量2位置=新向量2(480240);
公共矢量2速度;
公共游戏1()
:base()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
}
/// 
///允许游戏在开始运行之前执行任何需要的初始化。
///在这里,它可以查询任何必需的服务,并加载任何非图形化的服务
///相关内容。调用base.Initialize将枚举所有组件
///并对它们进行初始化。
/// 
受保护的覆盖无效初始化()
{
//TODO:在此处添加初始化逻辑
base.Initialize();
}
/// 
///LoadContent将在每个游戏中调用一次,并且是加载的地方
///你所有的内容。
/// 
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
//TODO:使用此.Content在此处加载游戏内容
background=Content.Load(“stars.jpg”);//将这些名称更改为图像的名称
shuttle=Content.Load(“shuttle.png”);//如果您正在使用自己的图像。
earth=Content.Load(“earth.png”);
}
/// 
///UnloadContent将在每个游戏中调用一次,并且是卸载的地方
///所有内容。
/// 
受保护的覆盖无效UnloadContent()
{
//TODO:在此卸载任何非ContentManager内容
}
/// 
///允许游戏运行逻辑,例如更新世界,
///检查碰撞、收集输入和播放音频。
/// 
///提供计时值的快照。
受保护覆盖无效更新(游戏时间游戏时间)
{
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed | | | Keyboard.GetState().IsKeyDown(Keys.Escape))
退出();
监听器();
蛇咬();
//TODO:在此处添加更新逻辑
//角度+=0.01f;
更新(游戏时间);
}
/// 
///这就是所谓的比赛应该平局的时候。
/// 
///提供计时值的快照。
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色:矢车菊蓝);
//TODO:在此处添加图形代码
spriteBatch.Begin();
spriteBatch.Draw(背景,新矩形(0,0,800,480),彩色,白色);
spriteBatch.Draw(地球,新矢量2(400240),彩色,白烟);
向量2原点=新向量2(0,0);
spriteBatch.Draw(梭形,位置,新矩形(0,0,梭形,宽度,梭形,高度),颜色,白色,角度,原点,0.5f,SpriteBeffects.None,1);
spriteBatch.End();
基础。抽签(游戏时间);
}
公共空侦听器()
{
if(newState.IsKeyDown(Keys.Right)和&(oldState.iskeydup(Keys.Right)))
速度=新矢量2(3,0);
if(newState.IsKeyDown(Keys.Left)和&(oldState.iskeydup(Keys.Left)))
速度=新矢量2(-3,0);
if(newState.IsKeyDown(Keys.Down)和&(oldState.iskeydup(Keys.Down)))
速度=新矢量2(0,3);
if(newState.IsKeyDown(Keys.Up)和&(oldState.iskeydup(Keys.Up)))
速度=新矢量2(0,-3);
}
公共空间蛇{
位置+=速度;
}
}

}您没有更新键盘状态。在更新方法中编写如下内容(在侦听器之前):


您没有更新键盘状态。在更新方法中编写如下内容(在侦听器之前):


您没有更新键盘状态。在更新方法中编写如下内容(在侦听器之前):


您没有更新键盘状态。在更新方法中编写如下内容(在侦听器之前):


您没有正确分配
oldState
newState
:在Game1实例化时,您只初始化了一次这些变量(错误)。您将需要以下内容:

public class Game1 : Game {
    KeyboardState oldState = Keyboard.GetState();
    KeyboardState newState = Keyboard.GetState();

    // ...

    protected override void Update(GameTime gameTime) {
        oldState = newState;
        newState = Keyboard.GetState();
        // ...
    }
}

您没有正确分配
oldState
newState
:在Game1实例化时,您只初始化了一次这些变量(错误)。您将需要以下内容:

public class Game1 : Game {
    KeyboardState oldState = Keyboard.GetState();
    KeyboardState newState = Keyboard.GetState();

    // ...

    protected override void Update(GameTime gameTime) {
        oldState = newState;
        newState = Keyboard.GetState();
        // ...
    }
}

您没有正确分配
oldState
newState
:在Game1实例化时,您只初始化了一次这些变量(错误)。您将需要以下内容:

public class Game1 : Game {
    KeyboardState oldState = Keyboard.GetState();
    KeyboardState newState = Keyboard.GetState();

    // ...

    protected override void Update(GameTime gameTime) {
        oldState = newState;
        newState = Keyboard.GetState();
        // ...
    }
}

您没有正确分配
oldState
newState
:在Game1实例化时,您只初始化了一次这些变量(错误)。您将需要以下内容:

public class Game1 : Game {
    KeyboardState oldState = Keyboard.GetState();
    KeyboardState newState = Keyboard.GetState();

    // ...

    protected override void Update(GameTime gameTime) {
        oldState = newState;
        newState = Keyboard.GetState();
        // ...
    }
}

答案是正确的,但是我可以建议将不同的对象重构为它们自己的类吗?答案是正确的,但是我可以建议将不同的对象重构为它们自己的类吗?答案是正确的,但是我可以建议将不同的对象重构为它们自己的类吗?答案是正确的,但是