# The loop will carry on until the user exits the game (e.g. # Add the paddle and the ball to the list of sprites #This will be a list that will contain all the sprites we intend to use in our game. #Let's import the Paddle Class & the Ball Class #Import the pygame library and initialise the game engine If so we remove the brick (using the kill() method) and increment the score by one. On lines 114 to 129 we detect if the ball hits a brick.If the number of lives reaches zero, we display a “Game Over” message. On lines 93 to 103 we take a life away when the ball hit the bottom edge of the screen.On lines 39 to 57 we create three rows of bricks and add them to a group called all_bricks.We made several changes to the code as follows: The final code for the main.py is provided below. A “Game Over” message will be displayed if the number of lives reaches zero.A “Level Complete” message will be displayed if all bricks have been removed. Both the score and number of lives will be displayed at the top of the screen.The player will lose a life if the ball bounces against the bottom edge of the screen.The player will score a point if the ball bounces against a brick.The final stage of our tutorial focuses on adding a brick wall and a scoring system to our Breakout game: Breakout Tutorial 5: Adding a Brick Wall.Breakout Tutorial 4: Adding a Bouncing Ball.Breakout Tutorial 3: Controlling the Paddle.Pygame.quit() 3) Place that bounce sound file in same directory where you created the python file.This tutorial is the second tutorial in a series of five Pygame tutorials: If event.type = pygame.MOUSEBUTTONDOWN and live_ball = False:īall.reset(player_paddle.x + (player_paddle.width//2), player_paddle.y - player_paddle.height)ĭraw_text(f"", font2, score_col, 50, screen_height - 50) Self.rect = Rect(self.x, self.y, self.ball_rad * 2, self.ball_rad * 2)īall = game_ball(player_paddle.x + (player_paddle.width//2), player_paddle.y - player_paddle.height)ĭraw_text('CLICK ANYWHERE TO START', font, text_col, 100, screen_height // 2 + 100)ĭraw_text('YOU WON', font, text_col, 240, screen_height // 2 + 50)ĭraw_text('YOU LOST', font, text_col, 240, screen_height // 2 + 50) (screen, paddle_outline, ( + self.ball_rad, + self.ball_rad ), self.ball_rad, 1) (screen, paddle_col, ( + self.ball_rad, + self.ball_rad ), self.ball_rad) #After Iterating through all the walls, check if wall is destroyed #Check if block still exists, in which case wall is not destroyed Rect = pygame.Rect(block_x, block_y, self.width, self.height) #generate x and y position for each block and create rectangle #Defining an empty list for a individual block #Ball Bounce Sound - Present in the same directory where this python file is presentīounce_fx = ("./bounce.wav")įont = ('Constantia', 30)įont2 = ('Algerian', 30)ĭef draw_text(text, font, text_col, x,y): Screen = _mode((screen_width, screen_height)) py file and paste the following code in that. pip install pygameĢ) After that create a. If Pygame is not installed on your computer install it using the below command. Place this sound in the same directory where you place this python file. Note - Find a nice Brick - ball collision sound from the internet and download it in. Let's create our Brick Breaker Game with Pygame. We have to control the paddle with left right arrow keys and prevent the ball to hit the bottom of the game window. There are three types of bricks one that vanishes with one collision of ball, one that requires two collision and the last one requires three collision to break. In this game, there is a ball and a paddle and a wall of bricks.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |