簡體   English   中英

C#Monogame蛇游戲

[英]C# Monogame snake game

好吧,首先我必須說我是一個完整的菜鳥,我只是從2D圖形開始,然后我嘗試了一個蛇游戲,在Update方法中我調用了蛇更新,它根據關鍵狀態更新位置,這是它從不更新位置,有人解釋說請一點。

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;


    }
}

}

您沒有正確分配oldStatenewState :僅在實例化Game1時一次(不正確)初始化這些變量。 您將需要以下內容:

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

    // ...

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

您沒有更新鍵盤狀態。 在您的Update方法內(在偵聽器之前)編寫如下代碼:

oldState = newState;
newState = Keyboard.GetState();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM