You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description: For CS50P's final project, I decided to build a Classic Snake Game utilising Python. My project is a very classic game that involves a snake represented by green squares which grows longer as it "eats" food, which is the red square on the board. The grid is 40x30 squares, resized from a resolution of 800x600. The snake spawns at [(20, 14), (20, 15), (20, 16)], in the middle of the board. The user can control the snake by using the direction keys. The game continues until the snake hits the edge or collides with itself, else the game goes on. Room for improvement: Possibly a timer to show the elapsed time in the game, different difficulties where maybe the speed of the snake varies etc. A pop-up to notify the user of events.
Files used:
project.py
The project.py python file firstly uses the pygames and random modules. It consists of 5 custom functions, namely initialize_game(), calculate_new_head_position(), update_game(), draw_game(window) and generate_new_food_position(). It also consists of a few global variables, direction, food_position and running. calculate_new_head_position() uses pygame's event handler to log the direction key input of the user during the game and returns the corresponding new coordinate of the snake's head. (top left of the board is the origin (0,0)) draw_game(window) takes the parameter window. In the function, window.fill((255, 255, 255)) fills the entire screen with white color. The for loop iterates over each segment in the snake list, drawing a green rectangle for each segment at the specified position. The pygame.draw.rect function takes four parameters: the surface to draw on, the color of the rectangle, and the rectangle itself, which is defined by its top-left corner coordinates and its width and height. Finally, pygame.display.update() is called to make all the changes done since the last call visible. generate_new_food_position() generates the food in a square not occupied by the snake's body. update_game() checks if the snake is still within the boundaries of the game and updates its length. In initialize_game(), in the game loop, clock.tick(30) is used to ensure that no matter how fast the computer running the game is, the game will at most update 30 times in one second, making the game run at the same speed on all machines.
test_project.py
The test_project.py python file consists of 3 tests, 1 for a different custom function. test_update_game(): This is a unit test for the update_game function. It's using the patch function from Python's unittest.mock module to replace the calculate_new_head_position function with a mock that always returns (10, 10). The test sets up a scenario (the "Arrange" part) where the food is at position (10, 10), which is the same as the new head position of the snake. It then calls update_game (the "Act" part). After update_game is called, the test checks (the "Assert" part) that food_position is not (10, 10). This is because, in the game, when the snake's head reaches the food, the food should move to a new position. So, if update_game is working correctly, food_position should change when the snake's head and the food are at the same position. test_generate_new_food_position() asserts that the coordinates of the food generated are valid. Lastly, test_calculate_new_head_position() asserts that the snake's coordinates changes correctly.