Day One - January 10, 2024
//T.Diep
//January 10, 2024
//Snake Game, my version of the classic game where the user can change
the difficulty of the game. Use the arrow keys to move and eat apples
and grow larger.
import hsa_ufa.Console;
import java.awt.Color;
public class SnakeGame1 {
public static void main(String[] args) {
Console c = new Console(600, 600, "Snake Game");
c.setBackgroundColor(Color.BLACK);
c.clear();
int rows = 24; int cols = 24; int tileSize = 25;
while (true) {
drawGrid(c, rows, cols, tileSize); // draw the grid
}
}
private static void drawGrid(Console c, int rows, int cols, int
tileSize) {
for (int row = 0; row <= rows; row++) {
int y = row * tileSize;
c.setColor(Color.GRAY);
c.drawLine(0, y, cols * tileSize, y);
}
for (int col = 0; col <= cols; col++) {
int x = col * tileSize;
c.setColor(Color.GRAY);
c.drawLine(x, 0, x, rows * tileSize);
}
}
}
Today, i made a gray grid with a black background which will be used
as the interface for my snake game. I made every square 25 units by 25
units and the console size 600 by 600. This created 24 rows and
columns.
Day Two - January 11, 2024
int headX = 5; // Initial X-coordinate of the snake head
int headY = 5; // Initial Y-coordinate of the snake head
int directionX = 1; // Initial X-direction of the snake (1 for
right)
int directionY = 0; // Initial Y-direction of the snake (0 for no
vertical movement)
long lastUpdateTime = System.currentTimeMillis();
long updateInterval = 100; // Time in milliseconds between updates
// Draw the initial snake head and grid lines
drawSnakeHead(c, headX, headY, tileSize);
// Game loop
do {
// Check if it's time to update the game
long currentTime = System.currentTimeMillis();
if (currentTime - lastUpdateTime >= updateInterval) {
// Update the snake's position
headX += directionX;
headY += directionY;
// Draw the entire scene, including the snake and grid lines
c.clear();
drawSnakeHead(c, headX, headY, tileSize);
// Update the last update time
lastUpdateTime = currentTime;
} // Check for arrow key input
if (c.isKeyDown(Console.VK_UP) && directionY == 0) {
directionX = 0;
directionY = -1;
} else if (c.isKeyDown(Console.VK_DOWN) && directionY == 0) {
directionX = 0;
directionY = 1;
} else if (c.isKeyDown(Console.VK_LEFT) && directionX == 0) {
directionX = -1;
directionY = 0;
} else if (c.isKeyDown(Console.VK_RIGHT) && directionX == 0) {
directionX = 1;
directionY = 0;
}
} while (true);
}
private static void drawSnakeHead(Console c, int x, int y, int size)
{
c.setColor(Color.GREEN);
c.fillRect(x * size, y * size, size, size);
}
}
Today, I made my snake, which can be moved using the arrow keys. It
cannot die yet and no borders have been set.
Day Three - January 12, 2024
String choice;
String name;
c.setColor(Color.WHITE); // Set text color to white
c.print("Welcome to my Snake Game, press enter to start: ");
choice = c.readLine();
c.print("What is your player name? ");
name = c.readLine();
if (choice.equals("")) {
c.clear();
}
// Initial apple creation
createApple(c, tileSize, rows, cols);
// Check if the snake head touches the apple
if (headX == appleX && headY == appleY) {
// Snake head touched the apple, move the apple to a new location
moveApple(c, tileSize, rows, cols);
}
drawApple(c, tileSize);
private static void createApple(Console c, int size, int rows, int
cols) {
c.setColor(Color.RED);
int maxRow = rows - 1;
int maxCol = cols - 1;
appleX = (int) (Math.random() * maxCol);
appleY = (int) (Math.random() * maxRow);
c.fillRect(appleX * size, appleY * size, size, size);
}
private static void moveApple(Console c, int size, int rows, int cols)
{
// Move the apple to a new location
createApple(c, size, rows, cols);
}
private static void drawApple(Console c, int size) {
c.setColor(Color.RED);
c.fillRect(appleX * size, appleY * size, size, size);
}
Today, I made a menu page where you can enter your username, and i
also made apples that spawn one at a time and the snake can eat them
when they touch.
Day Four - January 15, 2024
// Check if the snake head touches the apple
if (headX == appleX && headY == appleY) {
// Snake head touched the apple, move the apple to a new location
moveApple(c, tileSize, rows, cols);
}
drawApple(c, tileSize);
private static void createApple(Console c, int size, int rows, int
cols) {
c.setColor(Color.RED);
int maxRow = rows - 1;
int maxCol = cols - 1;
appleX = (int) (Math.random() * maxCol);
appleY = (int) (Math.random() * maxRow);
c.fillRect(appleX * size, appleY * size, size, size);
}
private static void moveApple(Console c, int size, int rows, int cols)
{
// Move the apple to a new location
createApple(c, size, rows, cols);
}
private static void drawApple(Console c, int size) {
c.setColor(Color.RED);
c.fillRect(appleX * size, appleY * size, size, size);
}
Today, i made it so that the snake grows everytime it eats and made
scores and high scores. you can now die when you hit the walls.
Day Four - January 15, 2024
if (headX < 0 || headX >= cols || headY < 0 || headY >= rows || checkSelfCollision(snakeBody))
private static boolean checkSelfCollision(ArrayList snakeBody) {
Tile head = snakeBody.get(0);
int headX = head.x;
int headY = head.y;
for (int i = 1; i < snakeBody.size(); i++) {
Tile segment = snakeBody.get(i);
if (headX == segment.x && headY == segment.y) {
return true; // Collision with the body
}
}
return false;
}
}
Today, i made it so that the snake dies when it hits itself
Day Five - January 16, 2024
import java.io.*;
c.print("Welcome to my Snake Game, press anything to start: ");
choice = c.readLine(); // choice to play the game
c.clear(); // clear console
String name; // collects the name
do {
try {
c.print("What is your player name? ");
name = c.readLine(); // get name
if (name.equals("")) {
c.clear();
throw new Exception(); // Prompt user to press Enter again if input is
not empty
}
} catch (Exception e) {
c.println("Invalid input. Please enter a name. "); // catch inputs
where name is empty
continue; // Continue to the next iteration of the loop
}
break; // Break out of the loop when a valid name is provided
} while (true);
c.clear(); // clear console
// Speed selection
while (true) {
try {
c.print("Choose a speed (50 (Fast), 100 (Normal), 200 (Slow)): "); //
let user choose speed, time it
// takes for snake to move is
// determined by the value inputted
speed = c.readInt();
if (speed == 50 || speed == 100 || speed == 200) {
break; // Break out of the loop when a valid speed is provided
} else {
c.clear();
throw new Exception(); // Prompt user to press Enter again if input is
not, 50, 100, or 200
}
} catch (Exception e) {
c.println("Invalid input. Please enter a speed of 200, 100, or 50.");
// catch invaild inputs
}
}
Today, I incorporated try and catch statements to make sure users
input the right information.
Day Six - January 17, 2024
if (headX < 0 || headX >= cols || headY < 0 || headY >= rows ||
checkSelfCollision(snakeBody)) { // if
// snake // dies c.setColor(Color.RED);
c.print("\t\nGame Over! Press 'enter' to play again (press anything
else to stop): ");
choice = c.readLine();
if (score >= yourHighScore) {
yourHighScore = score; // get highest score
}
if (yourHighScore >= highScore) {
highScore = yourHighScore;
}
if (choice.equals("")) {
break;
} else {
String end;
output.println("Highscore: " + yourHighScore + " " + name + " Speed: "
+ speed + " Time: " + time/1000+"s");
c.clear();
String results = outputResults(name, yourHighScore, speed, time);
c.println(results);
c.println("Enter anything to exit the program: ");
end = c.readLine();
output.close();
input.close();
c.close();
}
}
Today, I continued to make the function that would find the best score
in a txt file called leaderBoard.txt and also display it in the
console. It can also print the results in the leaderboard txt file.
Day Seven - January 18, 2024
private static int getHighScore(String fileName) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
int highestScore = 0;
String line;
// Loop through all lines in the file to find the highest score
while ((line = reader.readLine()) != null) {
String[] parts = line.split("\\s+");
if (parts.length >= 2) {
int fileScore = Integer.parseInt(parts[1].trim());
if (fileScore > highestScore) {
highestScore = fileScore;
}
}
}
// Close the reader
reader.close();
return highestScore;
}
private static String outputResults(String name, int yourHighScore, int speed, double time) {
String results = (name + ": Score: " + yourHighScore + ", " + speed + "ms, " + time/1000 + "s");
return results;
}
}
Today, I finished making methods to find the highest value in the file. This was especially difficult since I had to find the values and loop through all the lines and compare every value to find the higest value.
Day Eight-Ten - January 19-24, 2024
These three days were dedicated to building my site using html, css, and javascript.