A2: Drawing & Events
You will implement Snake, a classic 2D video game where the objective is to control a line as it grows in length while preventing it from hitting the edge of the screen or itself (Wikipedia).
There are various versions of Snake playable on the web such as this one. The general rules of the game are:
- The game screen displays a snake, which is always in motion, and fruit randomly positioned around the screen.
- The direction of the snake can be controlled by the arrows keys. The snake will move forward unless LEFT or RIGHT arrow keys are pressed, which will cause the snake to turn in that direction (relative to it's current path).
- The objective of the snake is to eat the fruit. When the snake eats a piece of the fruit disappears and is immediately replaced by another piece of fruit randomly positioned. Every time the snake eats a piece of fruit, it gets one block longer.
- A timer ticks down on each level. When the timer runs out, the next level is loaded, with increasingly more fruit (see below). The snake's position and size is not changed when the level changes (i.e. it starts a level in the same position/same size as was in the previous level).
- The snake can die by eating itself (when it collides with itself) or by hitting the edge of the screen.
- The game should run in a window on the desktop. It should includes multiple screens:
- The game starts with a splash screen that includes your name, userid, and a description of how to play the game. The game should wait here until the user presses the appropriate key to start the game, and then it should transition to the second screen.
- The second screen represents "level 1" of the game, with a snake and 5 pieces of fruit on the screen in a pattern of your choice (not random, it should be the same every time you run the game). As the snake eats one fruit, another fruit instantly appears at a random point on the screen to replace the eaten fruit. A timer at the top of the screen counts-down from 30. When it reaches zero, the player "wins" this level, and moves to the next level.
- The third screen represents "level 2" of the game, with a snake and 10 pieces of fruit on the screen in a pattern of your choice (not random, it should be the same every time you run the game). The snake should be faster than the previous level. As the snake eats one fruit, another fruit instantly appears at a random point on the screen to replace the eaten fruit. A timer at the top of the screen counts-down from 30. When it reaches zero, the player "wins" this level, and moves to the next level.
- The fourth screen represents "level 3" of the game, with a snake and 15 pieces of fruit on the screen in a pattern of your choice (not random, it should be the same every time you run the game). The snake should be faster than the previous level (i.e. fast). As the snake eats one fruit, another fruit instantly appears at a random point on the screen to replace the eaten fruit. This level does not have a timer, and runs until the player dies.
- When the player dies, the game is "over". Display a final screen informing them that the game is over, with their high score.
- The game must play smoothly with proper collision detection. To keep the movement smooth, you should support an animation rate of at least 25 frames-per-second (FPS).
- The game window should be 1280x800 pixels and does not need to support resizing (i.e. it can be a fixed size window). If your screen resolution on your computer is less than this, you can support your maximum resolution instead, but please make a note to this effect in the README.
- The game should display a count of how many pieces of fruit have been eaten in this level, as well as a score that updates over the course of the game. It is up to you to decide on a strategy for assigning the score, but the score should reset when the game is restarted.
- The game must use the arrow keys (left/right) to control the snake's movements. The snake always moves forward, and only turns when the arrow keys are pressed.
- The game should have the ability to play, pause, and skip levels, using keyboard shortcuts:
- Arrow keys: turn the snake in the direction indicated
- P: pause and un-pause the game
- R: reset to the splash screen
- 1: start level 1
- 2: start level 2
- 3: start level 3
- Q: quit and display the high score screen
- You should play sound effects for game events such as when the snake eats or when the game is over.
- You should use texture graphics for the background, the snake, and other game objects. You can create these, or use any assets that you find on the web whose license supports this (e.g. Creative Commons License).
- Implementation must be done in Java 11 or higher, and run on a desktop OS (Windows, OS X, or Linux). You are allowed to use the Java platform libraries, but you are restricted to JavaFX toolkits for the GUI. You are not allowed to use any other libraries or source code.
- Your project should use Gradle to manage builds: "gradle build" should run your program, and "gradle run" should run it.
- Your project should be in a folder named "a2" and the directory structure should look something like this. (It doesn't need to match exactly, and will vary a little based on how it was created). Make sure that gradle can build and run it from within your "a2" directory.
- Include a
readme file(readme.txt or readme.md) with a description of the game, the enhancements, and the development environment (e.g. Operating System, JDK version, etc). Include anything else that TAs should know when grading.
a2/ ├── a2.iml/ ├── build/ ├── build.gradle ├── gradle/ ├── gradlew ├── gradlew.bat ├── settings.gradle ├── readme.md └── src └── Main.java
Submit your assignment to your personal Git repository, under the
- Include all of the source code and assets that are required to build your project.
- A Gradle project structure, that allows building and running your program (see above).
- A 'README' file (e.g. readme.txt or readme.md) that documents your application, including the platform (Mac, Windows, Linux) and version of Java that you used. Include any other information that the TA will need to grade this.
Markers will test your application by building and running your code using Grade. Your submission will be assessed, roughly, as follows:
- Code compiles and runs.
- Includes a
README.txtdescribing the development environment, and enhancements that you implemented.
- Functional requirements: gameplay plays and progresses as described.
- Technical requirements: event-handling, repainting properly, support for a reasonable framerate (25-50 FPS).
1.0. Aug 18, 2020. Initial draft.
1.1. Sept 14, 2020. Clarified movement, rules for creating new fruit. Added high score display.