Skip to content

Commit

Permalink
no commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
SrinSS01 committed Mar 26, 2022
1 parent ea22c95 commit ad42159
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 13 deletions.
82 changes: 82 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Snake game
A simple snake game made with ncurses (Tested on windows only for now).

## How to play?
* Download and unzip the zip file from [releases]()
* Then run the bat file

<img src="./images/zip.png"></img>

This will open up the game window

<img height="350" src="./images/difficulty.png" width="350"/></img>

After that you'll be asked to select the difficulty `which determines how fast the snake will move`
Press any key to start the game.

<img height="350" src="./images/start.png" width="350"/></img>

While playing if you press `ESCAPE` or `Q` it'll open the main menu

<img height="350" src="./images/main_menu.png" width="350"/></img>

You can :
* `continue` playing
* `restart` the game (`it'll reset all your points and levels`)
* `quit`

and if you hit a `wall` or `snake body` then it'll be game over

<img height="350" src="./images/game_over.png" width="350"/></img>

## How to add custom levels?

In the levels folder create a `<level-name>.snake` file
Inside that file you need to add some commands to draw walls
### To draw horizontal line use
```
hline(x, y, '<character>' or ASCII, length)
```
### To draw vertical line use
```
vline(x, y, '<character>' or ASCII, length)
```
* x - `x position of the line`
* y - `y position of the line`
* character - `character to draw`
* ASCII - `ascii value of the character`
* length - `length of the line`
### To draw box use
`box`
### To draw border use
```
border(ls, rs, ts, bs, tl, tr, bl, br)
```
* `ls` - left side,
* `rs` - right side,
* `ts` - top side,
* `bs` - bottom side,
* `tl` - top left-hand corner,
* `tr` - top right-hand corner,
* `bl` - bottom left-hand corner, and
* `br` - bottom right-hand corner.
#### default values :
`ls`, `rs`, `ts`, `bs` &rarr; `default_hline` or `default_vline`
`tl` &rarr; `default_ul`
`tr` &rarr; `default_ur`
`bl` &rarr; `default_ll`
`br` &rarr; `default_lr`

## And finally you must return the starting position of the snake
```
return(x, y, <increment or decrement>, lock_x, lock_y)
```
* `x` - x co-ordinate of the snake
* `y` - y co-ordinate of the snake
* `increment or decrement` - the values will be `+1` or `-1` to determine whether to draw the snake left, right, up or down
* `lock_x` - the values will be `true` or `false` to determine whether to start moving in the x-axis
* `lock_y` - the values will be `true` or `false` to determine whether to start moving in the y-axis

Some useful macros: `$w` & `$h`
* `$w` - contains the game window width
* `$h` - contains the game window height
Binary file added images/difficulty.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/game_over.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/main_menu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/start.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/zip.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed jar/Snake.jar
Binary file not shown.
2 changes: 2 additions & 0 deletions src/me/srin/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class Main {
private static final int A_ALTCHARSET = 0x00010000;
public static final int A_REVERSE = 0x00200000;
public static final int A_CHARTEXT = 0x0000ffff;
public static final long ACS_VLINE = PDC_ACS('x');
public static final long ACS_HLINE = PDC_ACS('q');
public static final long ACS_ULCORNER = PDC_ACS('l');
public static final long ACS_URCORNER = PDC_ACS('k');
public static final long ACS_LLCORNER = PDC_ACS('m');
Expand Down
34 changes: 21 additions & 13 deletions src/me/srin/util/engine/LevelParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
public class LevelParser {
private static final File DIRECTORY = new File(System.getProperty("user.dir") + "/levels/");
public static final Pattern ARITHMETIC_PATTERN = Pattern.compile("(\\d+) *([+\\-*/]) *(\\d+)");
public static final Pattern CHARACTER_OR_ASCII_SCAN_PATTERN = Pattern.compile("^(\\d+)$|^'(.)'$");
static {
if (!DIRECTORY.exists() && DIRECTORY.mkdirs()) {
out.println("Created directory " + DIRECTORY.getAbsolutePath());
Expand Down Expand Up @@ -49,7 +50,7 @@ public static List<Supplier<Coord>> parse() throws IOException {
actions.add(() -> gameWindow.hline(
toInt(argsFinal[0].trim()),
toInt(argsFinal[1].trim()),
toInt(argsFinal[2].trim()),
(int) toCharacter(argsFinal[2].trim()),
toInt(argsFinal[3].trim())
));
}
Expand All @@ -59,7 +60,7 @@ public static List<Supplier<Coord>> parse() throws IOException {
actions.add(() -> gameWindow.vline(
toInt(argsFinal[0].trim()),
toInt(argsFinal[1].trim()),
toInt(argsFinal[2].trim()),
(int) toCharacter(argsFinal[2].trim()),
toInt(argsFinal[3].trim())
));
}
Expand All @@ -79,14 +80,14 @@ public static List<Supplier<Coord>> parse() throws IOException {
assert args != null;
final String[] argsFinal = args;
actions.add(() -> gameWindow.border(
toBorder(argsFinal[0].trim()),
toBorder(argsFinal[1].trim()),
toBorder(argsFinal[2].trim()),
toBorder(argsFinal[3].trim()),
toBorder(argsFinal[4].trim()),
toBorder(argsFinal[5].trim()),
toBorder(argsFinal[6].trim()),
toBorder(argsFinal[7].trim())
toCharacter(argsFinal[0].trim()),
toCharacter(argsFinal[1].trim()),
toCharacter(argsFinal[2].trim()),
toCharacter(argsFinal[3].trim()),
toCharacter(argsFinal[4].trim()),
toCharacter(argsFinal[5].trim()),
toCharacter(argsFinal[6].trim()),
toCharacter(argsFinal[7].trim())
));
}
}
Expand All @@ -102,16 +103,23 @@ public static List<Supplier<Coord>> parse() throws IOException {
}
return coords;
}
private static long toBorder(String expression) throws IllegalArgumentException {
private static long toCharacter(String expression) throws IllegalArgumentException {
if (expression == null) throw new IllegalArgumentException("expression cannot be null");
return switch (expression) {
case "default_vline" -> ACS_VLINE;
case "default_hline" -> ACS_HLINE;
case "default_ul" -> ACS_ULCORNER;
case "default_ur" -> ACS_URCORNER;
case "default_ll" -> ACS_LLCORNER;
case "default_lr" -> ACS_LRCORNER;
default -> {
if (!expression.matches("'.'")) throw new IllegalArgumentException("%s is an invalid argument".formatted(expression));
yield expression.charAt(1);
Matcher matcher = CHARACTER_OR_ASCII_SCAN_PATTERN.matcher(expression);
if (matcher.find()) {
String ascii = matcher.group(1);
String character = matcher.group(2);
yield ascii != null? Integer.parseInt(ascii): character.charAt(0);
}
else throw new IllegalArgumentException("%s is an invalid argument".formatted(expression));
}
};
}
Expand Down
9 changes: 9 additions & 0 deletions src/me/srin/util/engine/Snake.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ else if (start.isLock_x()) {
var _x = (start.getX() + start.getIncrement_value()) % gameWindowWidth;
if (_x < 0) _x += gameWindowWidth;
start.setX(_x);
} else {
int gameWindowHeight = gameWindow.getHeight();
var _y = (start.getY() + start.getIncrement_value()) % gameWindowHeight;
if (_y < 0) _y += gameWindowHeight;
start.setY(_y);
int gameWindowWidth = gameWindow.getWidth();
var _x = (start.getX() + start.getIncrement_value()) % gameWindowWidth;
if (_x < 0) _x += gameWindowWidth;
start.setX(_x);
}
var charachter_at_snek_mouth = gameWindow.inch(start.getX(), start.getY()) & A_CHARTEXT;
gameWindow.attron(COLOR_PAIR(WHITE_RED));
Expand Down

0 comments on commit ad42159

Please sign in to comment.