using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ConsoleSnakeGameAssignment
{
class Program
{
public enum TileType { NONE = 0, PLAYER, FOOD, WALL, DEADPLAYER };
public class Vector2
{
public int x = 0;
public int y = 0;
}
public class Snake
{
public int xpos;
public int ypos;
public TileType type;
public Vector2 vec = new Vector2();
public List<SnakeSegment> body = new List<SnakeSegment>();
}
public class SnakeSegment
{
public int x;
public int y;
}
static void Main(string[] args)
{
TileType[,] levelOne = new TileType[25, 80];
Snake player = new Snake();
// The player is stopped when we start the game
// so the vector is <0,0>
player.vec.x = 0;
player.vec.y = 0;
Array.Clear(levelOne, 0, levelOne.Length); // Set's all tiles to zero
ConsoleKey key = ConsoleKey.Z; // Unused bogus key
// Clear the screen
Console.Clear();
Console.CursorVisible = false;
// You could create an array of characters e.g bad guys
/*
GameCharacter[] levelCharacters = new GameCharacter[1];
for (int index = 0; index < levelCharacters.Length; index++)
{
levelCharacters[index] = new GameCharacter();
levelCharacters[index].movement = new Vector2();
}
CreateLevel(levelOne, levelCharacters);
*/
// Create levelOne by setting the tiles to the
// correct values including the walls, player
// and randomly placed food.
CreateLevel(levelOne, player);
// Draw the level
DrawLevel(levelOne);
// The snake starts a single segment
// which we add to the head of the
// list to form the snake's head.
SnakeSegment seg = new SnakeSegment();
seg.x = player.xpos;
seg.y = player.ypos;
player.body.Add(seg);
// GAME LOOP
// Keep reading the keys until the
// player quits the game.
while (key != ConsoleKey.Q)
{
if (Console.KeyAvailable == true)
{
key = Console.ReadKey(true).Key;
// Update the players vector to make
// the snake move in a new direction
player.vec = UpdatePlayer(key);
}
// Update the position of player(i.e. snake) and draw
// the it.
// If you want bad guys, then move them around in here
// as well. Perhaps move them K-steps in one direction
// then reverse them, or randomly select a new
// direction after a random number of steps, etc.
// This method also checks for walls, etc, and contains
// the game logic.
// Note: Because this is quite a simple game we also
// redraw the tiles in here. In a 'proper' game, we
// would have a seperate draw method and just use the
// this to update the positions.
//
UpdateLevel(levelOne, player);
// Set the game speed
Thread.Sleep(100);
}
Console.ReadLine(); // Pause
}
// Fill the tiles with the correct values.
// Use FOR loops to add the walls along the sides of the
// window.
// Position the player in the level.
// Randomly place the food.
// You may add your own characters, mines, pits, etc.
static void CreateLevel(TileType[,] levelOne, Snake player)
{
int tilePositionX = 0;
int tilePositionY = 0;
int index = 0;
for (tilePositionY = 0; tilePositionX < 70; tilePositionX++)
{
levelOne[tilePositionY, tilePositionX] = TileType.WALL;
}
for (tilePositionX = 69; tilePositionY < 25; tilePositionY++)
{
levelOne[tilePositionY, tilePositionX] = TileType.WALL;
}
for (tilePositionY = 24; tilePositionX > 0; tilePositionX--)
{
levelOne[tilePositionY, tilePositionX] = TileType.WALL;
}
for (tilePositionX = 0; tilePositionY > 0; tilePositionY--)
{
levelOne[tilePositionY, tilePositionX] = TileType.WALL;
}
#region GameCode_DO_NOT_CHANGE
// Randomly place the food in the level.
// The WHILE loop below is used to make sure that we
// don't add food where the wall or the player is.
Random randObj = new Random();
for (index = 1; index < 50; index++)
{
tilePositionX = 0;
tilePositionY = 0;
if (levelOne[tilePositionY, tilePositionX] != TileType.PLAYER)
{
while (levelOne[tilePositionY, tilePositionX] != TileType.NONE)
{
tilePositionX = (int)(randObj.NextDouble() * (levelOne.GetUpperBound(1)));
if (tilePositionX > 69)
{
tilePositionX = tilePositionX - 11;
}
tilePositionY = (int)(randObj.NextDouble() * (levelOne.GetUpperBound(0)));
}
levelOne[tilePositionY, tilePositionX] = TileType.FOOD;
DrawTile(levelOne, tilePositionX, tilePositionY);
}
#endregion
}
player.xpos = 10;
player.ypos = 10;
levelOne[player.xpos, player.ypos] = TileType.PLAYER;
}
// Using a nested FOR loop, this method draws each row and
// draw each character along each row by calling DrawTile.
static void DrawLevel(TileType[,] level)
{
int tilePositionX = 1;
int tilePositionY = 1;
for (tilePositionY = 0; tilePositionY < 25; tilePositionY++)
{
for (tilePositionX = 0; tilePositionX < 80; tilePositionX++)
{
DrawTile(level, tilePositionX, tilePositionY);
}
}
#region GameCode_DO_NOT_CHANGE
// Added at end of method to avoid leaving cursor in
// the wrong place and causing the text to shift up
// an unwanted extra line.
Console.CursorLeft = 0;
Console.CursorTop = 0;
#endregion
}
#region GameCode_DO_NOT_CHANGE
// Draw the required at the provided tile position.
// You may change the graphics here if you wish.
static void DrawTile(TileType[,] level, int tilePositionX, int tilePositionY)
{
Console.CursorLeft = tilePositionX;
Console.CursorTop = tilePositionY;
if (level[tilePositionY, tilePositionX] == TileType.WALL)
{
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("#");
}
else if (level[tilePositionY, tilePositionX] == TileType.PLAYER)
{
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("*");
}
else if (level[tilePositionY, tilePositionX] == TileType.FOOD)
{
Console.BackgroundColor = ConsoleColor.DarkMagenta;
Console.Write("F");
}
else if (level[tilePositionY, tilePositionX] == TileType.DEADPLAYER)
{
Console.Write("D");
}
else
{
Console.BackgroundColor = ConsoleColor.Black;
Console.Write(" ");
}
}
#endregion
// Generate a new 2 Dimensional normalized vector indicating
// the direction the player should be moving.
static Vector2 UpdatePlayer(ConsoleKey key)
{
Vector2 vec = new Vector2();
if (key == ConsoleKey.W)
{
vec.x = 0;
vec.y = -1;
}
else
{
if (key == ConsoleKey.D)
{
vec.x = 1;
vec.y = 0;
}
else
{
if (key == ConsoleKey.S)
{
vec.x = 0;
vec.y = 1;
}
else
{
if (key == ConsoleKey.A)
{
vec.x = -1;
vec.y = 0;
}
}
}
}
return vec;
}
// Update the players position and check that the snake
// hasn't hit a wall, itself, etc.
//
// Remove ths segement of the list tail as the snake
// moves around, but grow the snake by one segment if it eats food.
//
static public void UpdateLevel(TileType[,] level, Snake player)
{
if (level[player.ypos + player.vec.y, player.xpos + player.vec.x] != TileType.WALL)
{
// ADD CODE HERE
level[player.ypos, player.xpos] = TileType.PLAYER;
DrawTile(level, player.xpos, player.ypos);
}
else
{
}
// ADD CODE HERE
}
}
}
any help i need to create some loops in the updateLevel. these loops much make sure the snake isnt touching any walls or itself and it much grow to every apple that is eaten. any help would be great thanks