first commit
This commit is contained in:
93
Game1.cs
Normal file
93
Game1.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using System;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
|
||||
namespace SnakeMonoGame;
|
||||
|
||||
public class Game1 : Game
|
||||
{
|
||||
private GraphicsDeviceManager _graphics;
|
||||
private SpriteBatch _spriteBatch;
|
||||
private KeyboardState _previousKeyboardState;
|
||||
private int w = 15;
|
||||
private int h = 15;
|
||||
private int scale;
|
||||
private int xoff;
|
||||
private bool paused = false;
|
||||
|
||||
public Snake Snake;
|
||||
|
||||
public Game1()
|
||||
{
|
||||
_graphics = new GraphicsDeviceManager(this);
|
||||
|
||||
Content.RootDirectory = "Content";
|
||||
IsMouseVisible = true;
|
||||
}
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
Window.Title = "SnakeMonoGame - Press Space to Pause - Esc to Quit";
|
||||
Snake = new Snake(w, h);
|
||||
|
||||
scale = (int)Window.ClientBounds.Height / h;
|
||||
xoff = (int)(Window.ClientBounds.Width - (w * scale)) / 2;
|
||||
|
||||
base.Initialize();
|
||||
}
|
||||
|
||||
protected override void LoadContent()
|
||||
{
|
||||
_spriteBatch = new SpriteBatch(GraphicsDevice);
|
||||
}
|
||||
|
||||
protected override void Update(GameTime gameTime)
|
||||
{
|
||||
Input();
|
||||
if (!paused) { Snake.Update(); }
|
||||
|
||||
base.Update(gameTime);
|
||||
}
|
||||
|
||||
protected override void Draw(GameTime gameTime)
|
||||
{
|
||||
Color DarkGray = new Color(0.2f, 0.2f, 0.2f);
|
||||
GraphicsDevice.Clear(Color.Black);
|
||||
|
||||
_spriteBatch.Begin();
|
||||
Primitives2D.FillRectangle(_spriteBatch, new Rectangle(xoff + 0, 0, scale * w, scale * h), DarkGray);
|
||||
|
||||
Primitives2D.FillRectangle(_spriteBatch, new Rectangle(xoff + Calc(Snake.Food.X), Calc(Snake.Food.Y), scale, scale), Color.DarkRed);
|
||||
Primitives2D.DrawRectangle(_spriteBatch, new Rectangle(xoff + Calc(Snake.Food.X), Calc(Snake.Food.Y), scale, scale), DarkGray, 2f);
|
||||
foreach (Point p in Snake.Segments)
|
||||
{
|
||||
Primitives2D.FillRectangle(_spriteBatch, new Rectangle(xoff + Calc(p.X), Calc(p.Y), scale, scale), Color.Gray);
|
||||
Primitives2D.DrawRectangle(_spriteBatch, new Rectangle(xoff + Calc(p.X), Calc(p.Y), scale, scale), DarkGray, 2f);
|
||||
}
|
||||
_spriteBatch.End();
|
||||
|
||||
base.Draw(gameTime);
|
||||
}
|
||||
|
||||
private void Input()
|
||||
{
|
||||
KeyboardState keyboardState = Keyboard.GetState();
|
||||
|
||||
if (KeyPress(keyboardState, Keys.Escape)) { Exit(); }
|
||||
if (KeyPress(keyboardState, Keys.Space)) { paused = !paused; }
|
||||
|
||||
if (!paused)
|
||||
{
|
||||
Snake.AddDirection(KeyPress(keyboardState, Keys.Up) || KeyPress(keyboardState, Keys.W) ? "UpArrow" : "");
|
||||
Snake.AddDirection(KeyPress(keyboardState, Keys.Down) || KeyPress(keyboardState, Keys.S) ? "DownArrow" : "");
|
||||
Snake.AddDirection(KeyPress(keyboardState, Keys.Left) || KeyPress(keyboardState, Keys.A) ? "LeftArrow" : "");
|
||||
Snake.AddDirection(KeyPress(keyboardState, Keys.Right) || KeyPress(keyboardState, Keys.D) ? "RightArrow" : "");
|
||||
}
|
||||
|
||||
_previousKeyboardState = keyboardState;
|
||||
}
|
||||
|
||||
private int Calc(int Value) { return Value * this.scale; }
|
||||
private bool KeyPress(KeyboardState keyboardState, Keys key) { return keyboardState.IsKeyDown(key) && _previousKeyboardState.IsKeyUp(key); }
|
||||
}
|
||||
Reference in New Issue
Block a user