-
-
Save codecademydev/390c7b2de5c0c76c0af051d40973b802 to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace ConsoleGame | |
{ | |
class Game : SuperGame | |
{ | |
using System; | |
namespace ConsoleGame | |
{ | |
class Game : SuperGame | |
{ | |
public new static void UpdatePosition(string keyPressed,out int xCoord,out int yCoord) | |
{ | |
xCoord = 0; | |
yCoord = 0; | |
switch (keyPressed) | |
{ | |
case "LeftArrow": | |
xCoord--; | |
break; | |
case "RightArrow": | |
xCoord++; | |
break; | |
case "UpArrow": | |
yCoord--; | |
break; | |
case "DownArrow": | |
yCoord++; | |
break; | |
default: | |
Console.WriteLine("wrong key pressed"); | |
break; | |
} | |
} | |
public new static char UpdateCursor (string keyPressed) | |
{ | |
switch (keyPressed) | |
{ | |
case "LeftArrow": | |
return '<'; | |
break; | |
case "RightArrow": | |
return '>'; | |
break; | |
case "UpArrow": | |
return '^'; | |
break; | |
case "DownArrow": | |
return 'v'; | |
break; | |
default: | |
return 'X'; | |
break; | |
} | |
} | |
public new static int KeepInBounds(int coordinate, int maxValue) | |
{ | |
if (coordinate > maxValue) | |
{ | |
return 0; | |
} | |
else if (coordinate < 0) | |
{ | |
return maxValue; | |
} | |
else | |
{ | |
return coordinate; | |
} | |
} | |
public new static bool DidScore(int x, int y, int fx, int fy) | |
{ | |
if ((x == fx) && (y ==fy)) | |
{ | |
return true; | |
} | |
else | |
return false; | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace ConsoleGame | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Random rand = new Random(); | |
Console.CursorVisible = false; | |
// Determine bounds and set starting positions | |
int rows = Console.BufferHeight; | |
int cols = Console.BufferWidth; | |
char cursor = '<'; | |
int characterRow = rows / 2; | |
int characterCol = cols / 2; | |
char fruit = '@'; | |
int fruitRow = rand.Next(1, rows); | |
int fruitCol = rand.Next(1, cols); | |
int score = 0; | |
// Code in this loop executes infinitely | |
// unless Q or CTRL + C is pressed | |
while (true) | |
{ | |
// Draw score, character, and fruit | |
Console.Clear(); | |
Console.SetCursorPosition(0, 0); | |
Console.Write($"Score: {score}"); | |
Console.SetCursorPosition(characterCol, characterRow); | |
Console.Write(cursor); | |
Console.SetCursorPosition(fruitCol, fruitRow); | |
Console.Write(fruit); | |
// Capture user input | |
ConsoleKeyInfo cki = Console.ReadKey(false); | |
// End game if Q is pressed | |
if (cki.Key == ConsoleKey.Q) | |
{ | |
Console.Clear(); | |
Console.SetCursorPosition(0, 0); | |
Console.CursorVisible = true; | |
break; | |
} | |
// Change character position based on key | |
// Uses UpdatePosition() | |
string key = cki.Key.ToString(); | |
int colChange = 0; | |
int rowChange = 0; | |
Game.UpdatePosition(key, out colChange, out rowChange); | |
characterCol += colChange; | |
characterRow += rowChange; | |
// Update character symbol | |
// Uses UpdateCursor() | |
cursor = Game.UpdateCursor(key); | |
// Keep character in bounds | |
// Uses KeepInBounds() | |
characterCol = Game.KeepInBounds(characterCol, cols); | |
characterRow = Game.KeepInBounds(characterRow, rows); | |
// Update score and fruit if player scored | |
// Uses DidScore() | |
if (Game.DidScore(characterCol, characterRow, fruitCol, fruitRow)) | |
{ | |
score++; | |
fruitCol = rand.Next(cols); | |
fruitRow = rand.Next(rows); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment