The project is a 2D game in which you are becoming a Chinchilla and you have to eat as many almonds as possible in a certain period of time.
It is an attempt of rewriting the first project from the course made in Scratch:
https://scratch.mit.edu/projects/54065950/
Inspired by seminar Writing 2D Games in C using SDL:
http://cs50.tv/2015/fall/#seminars
The project consists of 5 main folders (modules):
- board - responsible for tracking game elements position,
- core - main module with core game logic; depends on the all other modules,
- graphics - encapsulates creation of SDL-related elements, e.g. SDL_Window, SDL_Texture,
- resources - contains fonts and picutres used in game,
- ui - consists of game elements like Window, Sprite; depends on graphics module.
. ├── board │ ├── Board.c │ ├── Board.h │ └── Position.h ├── core │ ├── game-animation.c │ ├── game-animation.h │ ├── game-board-functions.c │ ├── game-board-functions.h │ ├── game.c │ └── game-parameters.h ├── graphics │ ├── graphics.c │ └── graphics.h ├── Makefile ├── README.md ├── resources │ ├── fonts │ │ └── DroidSans.ttf │ └── pics │ ├── almond.png │ └── chinchilla.png └── ui ├── Footer.c ├── Footer.h ├── Input.c ├── Input.h ├── Sprite.c ├── Sprite.h ├── Velocity.h ├── Window.c └── Window.h
Files which names start with upper case letter could be interpreted as a pseudo classes. Chinchilla and each almond are a type of Sprite, Board contains information which Field is free or not in the Window, ect.
The core part of the game is placed in a core module. It contains different functions, among others, functions responsible for moving the Chinchilla in the Window or detecting Sprites collision. The main game loop is a part of the animate function, placed in the game-animation.c file.
The idea of the Board "class" was to simplify tracking the Sprites positions.
The Board contains information about game Fields (two dimensional array of integers). As a Field we understand the part of a Window with assigned number of row (the array row number) and column (the array column number) and information whether the part is taken (element of the array) by the game Sprite.
In the game-parameters.h file, global variables are defined including:
- Window dimension (equal width and height)
- Field dimension (equal width and height)
- Board parameters:
- width and height
- rows and columns (fields) count
- Sprites parameters:
- width and height
- rows and columns (fields) count
An Almond Sprite takes one field (one row, one column) and Chinchilla Sprite takes 20 fields (5 rows and 4 columns). Below is presented example of Board state - Chinchilla's top left field is I12 and some Almonds are located at D16, R1, O14.
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | R | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
17 | |||||||||||||||||
16 | * | ||||||||||||||||
15 | |||||||||||||||||
14 | * | ||||||||||||||||
13 | * | ||||||||||||||||
12 | # | # | # | # | |||||||||||||
11 | # | # | # | # | |||||||||||||
10 | # | # | # | # | * | ||||||||||||
9 | # | # | # | # | |||||||||||||
8 | # | # | # | # | |||||||||||||
7 | |||||||||||||||||
6 | |||||||||||||||||
5 | * | * | * | ||||||||||||||
4 | |||||||||||||||||
3 | * | ||||||||||||||||
2 | * | ||||||||||||||||
1 | * |
- Initialize game objects - Window, Board, Sprites, ect.
- Draw Sprites positions.
- Begin animation loop - while CLOSED_REQUEST is not detected and TIME value is less than TIME_LIMIT
- Detect pressed key by player
- Determine Chinchilla velocity
- Determine new Chinchilla position
- Check if Chinchilla collided with specified Window (Board) bounds
- Check if Chinchilla collided with Almond - if true
- Increment value of eaten Almonds (score)
- Clear Board
- Reset all Sprites positions
- Else
- Set only new Chinchilla position
- Clear Window
- Add Chinchilla to Window with determined before position
- Add Almonds to Window with determined before position
- Update Footer's message: update score and time
- Update Window's content.
- Free allocated memory - cleanup.
- Close program.
- Rules from Makefile: exec and .o files could be generated in separate directories, for example /objs and /bin.
- Relative paths of header files included in the source files could be removed - "../include/game-functions.h".
- Drawing Almonds positions algorithm - at this moment the positions are too close to the Chinchilla position.
- Exception handling.
- Game menu.
- Tests.