NOTE!! This program is one off my university projects made with C++. I havent coded the whole program because we had these Point and GameBoard classes implemented as a template of this project. So I have only implemented the graphical interface to this project and also this Data class which handles the player highscores. NOTE!!
PROJECT INSTRUCTIONS THAT I MADE TO COURSE:
This program implements a snake game. At the beginning user can give username in games homescreen. If player gives username she/he score will be saved to high scores table. At the home screen user can also change game area width and heigth with spinboxes. Minimum width and heigth are set to be 200 px and maximum 500 px. This limiting is done because doing this the gameboard doesn’t go too small or too big. Then the player can play the game more easily for example when she/he sees the whole gameboard. Also the game buttons are therefore easier to place. At the homescreen player can also give a seed value, wich generates random position to snake’s food. The seed value can vary from 0 to 500.
When player clicks “Play” button on the homescreen the game starts and the start button gets disabled. The gameboard, snake’s head and food are showed. Also stopwatch, reset and pause buttons are appearing on the screen. Stopwatch starts immediately after player has clicked “Play”- button and shows to player the time wich she/he has spent on the game. At the beginning the snake is in place and when user hits moving keys the snake starts moving. User can move snake with “wasd”-keyboard keys (“w”=up, “s”=down, “a”=left, “d”=rigth). Snakes aim is to eat food (bananas) from the screen. The snake’s food is done on the screen with png-image of bananas. The bananas image is taken from course materials section 12.4. When snake eats food, the food goes to random location on the screen, snake body grows, score is updated and the snakes movement speed increases. Snakes head and tail are carry out also with png-images, which are drawn with paint. When snakes body grows the rectangle is added to snakes body and showed on the screen. Snakes body parts colors will vary green to light blue and from ligth blue to green when the parts are added. When snake’s length is 3 the tail is added to snake. Player score is calculated with formula:
score=score+(1000*(STEP/(gameboardwidth+gameboardheigth)/2))/usedSecondsFromlastfood
So the score depends how big is the play area. Default added points per food is 1000 points. This is multiplied with factor <1. This factor is defined with snake STEP (wich tells how many pixels snake moves on the screen in one step) and with calculating screen specs average. So when the screen is smaller the player gets more points. The added score is also divided with spent seconds from last food. So when it tooks longer to eat food the added score is smaller. This score calculating method gives the player options to affect his/her score, when she/he wants to do his/her score big as possible. Snakes movement speed increases with formula:
duration=duration-((gameboardheigth+gameboardwidth)/2)/(STEP*4)
So the snake movement speed inceases more (duration is smaller) when the screen is bigger. That how much duration is changed is implemented with screen specs average divided by 4*STEP (see definition above). That factor 4 is used because with that factor the snake movement speed doesn’t increase too much at once. Minimum duration is set to be 120 ms which is the duration when user can still play the game.
Player will win the game when the snake’s size is as big as the play area size (snake fills the whole screen). When player wins the screen around the play are will turn green. Also the “YOU WON + score” label is shown. High scores table is also shown left side of the screen. On the other hand player will lose the game when she/he collides to the wall or when the head collides to other snake part. When player loses the game the screen around play area turns red and the “GAME OVER + score” label is shown. Also in this case the high scores table is also shown. High scores table takes informations from data.txt file, where the high scores are stored. Player scores are stored to table when she/he is given username. High scores table label shows only top 5 players on the list. If the player’s score is not in top five the players position and score are shown at the bottom of the table. If the highscores file data.txt is not found the error message to user is given.
Player can pause the game by using pausebutton wich pauses the game and also enables start button again. She/he can continue the game by pressing start button. Player can reset the game with reset button whenever she/he wants. When the reset button is pressed the games homescreen appears and user can modify the game parameters or the username and play the game again. User can close the game by clicking cross on the rigth-upper corner of the window.
The programs backend is in classes Data, GameBoard and Point. Gameboard and Point classes take care of snake and food moving. It also checks is player lost or won the game. Data class updates high scores table and gives that table of results to frontend. Mainwindow class is program’s frontend. It shows the game area, buttons, timer, snake parts, etc.. to user. There are some labels and user inputs wich are initialized with QT designer. For example seed and screen’s width and height spinboxes and their labels. Also high scores table and stopwatch labels are initialized with QT designer.
NOTE! When trying to run this program make sure that your build directory contains files data.txt, bananas.png, headVol2.png and tailVol2.png.