# Programming 2 - Spel i Pygame ###### tags: `python` `pygame` ## Objective: Create a 2D game using the Pygame library in Python that includes a player character, enemies, collectibles, and a scoring system. ## Essential requirements: + The game should have a resolution of minimum 640x480, maximum 1080x1024. + The game should have a caption that says "2D-Game". + The game should include a background image that is displayed on the screen. + The game should include a character that can be controlled by the player using the arrow keys. The character should have at least two different images for left and right movement. + Include at least two types of enemies that move around on the screen. + Include at least one types of collectibles that the player can collect to increase their score. + Include a scoring system that displays the player's current score on the screen. + Include collision detection between the player character and the enemies. If the player character collides with an enemy, the player loses a life. If the player loses all their lives, the game should display a "Game Over" messege. + Include sound effects for when the player character moves, when it collects a collectible, and when it collides with an enemy. + The game should also have a start screen that allows the player to start a new game or exit the game. + The game should have a game over screen, that allows the player to play again or exit the game. *Note that this is the basic requirements, you will have a chance for better grade if you use: patterns, add physics or network play.* ## Intro 0. Create a github repository and make sure you can commit to github through it. 1. Create a new python file, and name it as "game.py" 2. Import the necessary modules by adding the following lines at the top of your script: ```python import pygame from pygame.locals import * ``` 3. Initialize pygame by adding the following line: ```python pygame.init() ``` 4. Set the screen size and caption by adding the following lines: ```python size = (width, height) = (640, 480) screen = pygame.display.set_mode(size) pygame.display.set_caption("My Game") ``` 5. Create a game loop by adding the following code: ```python running = True while running: for event in pygame.event.get(): if event.type == QUIT: running = False # update and draw runs here ``` Within the game loop, you can add code to update the game state and draw the game on the screen. For example, you can add a background image to the screen by adding the following lines: ```python background_image = pygame.image.load("background.png") screen.blit(background_image, (0, 0)) pygame.display.update() ``` 7. Create a class for the player character, enemies and collectibles, as described above and add them to the game loop. 8. Implement collision detection and scoring system. 9. Implement sound effects. 10. Create start screen and game over screen as described above. 11. Test your game, and make sure it runs without any errors or crashes. 12. Submit your "game.py" file and all the assets used(images, sound effects) and upload it to github. ## Requirements + For E + The game should have a resolution of minimum 640x480, maximum 1080x1024. + The game should have a caption that says “2D-Game”. + The game should include a background image that is displayed on the screen. + The game should include a character that can be controlled by the player using the arrowkeys. + The character should have at least two different images for left and right movement. + Include at least two types of enemies that move around on the screen. + Include collision detection between the player character and the enemies. If the player character collides with an enemy, the player loses a life. If the player loses all their lives, the game should display a “Game Over” messege. + For C + Include sound effects for when the player character moves, when it collects a collectible, and when it collides with an enemy. + A start screen that allows the player to start a new game or exit the game. + A game over screen, that allows the player to play again or exit the game. + Include at least one types of collectibles that the player can collect to increase their score. + For A + **Use a patter for the following below** + Include a way to fire bullets to kill enemies + Include a scoring system that displays the player’s current score on the screen. + Choose one: + Make a upgrade system where weapons can be used together + Make a way to generate new levels