简体   繁体   中英

C# Monogame snake game

Well first i must say i am a complete noob, I just started with 2d graphics and i experiment on a snake game, in the Update method i call snakeUpdate which updates the position according the keystate the thing is it never updates the position, someone explain a bit please.

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;


    }
}

}

You're not assigning oldState and newState correctly: you're initializing those variables (incorrectly) only one time when Game1 is instantiated. You'll need something like this:

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

    // ...

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

You are not updating your keyboard state. Write something like this inside your Update method (before listener):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM