In this article, we share the childhood handheld game, based on pygame to achieve the specific code for playing bricks, for your reference, the details are as follows
Project Background:
When I was a child, I had a handheld game console, which had games such as Tetris and Tetris, and I was so sleepy and tired of playing them, but then I broke them and didn't play them again. Today, I want to learn the following python, from the game to start, write a game of tetris, by the way, recall the following childhood fun.
This work is entirely my learning python and game production practice works, using python syntax and pygame plug-in api will not write a very detailed, mainly to record the learning of the problems encountered and ideas.
1. Environmental preparation
1.1. install python, here I installed 3.6.3. (please search for the installation tutorial)
1.2. install pygame, here I installed 1.9.3. (Please search for the installation tutorial)
1.3. Installing pycharm
1.4. Prepare the material, open the drawing tool and make a brick picture, a small ball picture and a board picture.
2、Organization of ideas
Recalling the flow of the game, there are roughly the following steps throughout the game
2.1. The game is initialized and loaded with three images.
2.2. Click on the start button and the ball moves, you can also add a pause function.
2.3. Collisions of the ball with bricks and boards, left and right, and the wall above it
2.4. board collision with left and right walls (not beyond walls)
2.5. The ball lands and the game is over!
2.6. Bricks are knocked out, game over
2.7. Scoring functions
2.8. Music ~~
3. Open the whole
Let's start with loading the image. Without further ado, let's go straight to the code
import pygame from import * from sys import exit # pygame initialization, required () # Create windows # Is accessing the display device # The set_mode method returns a Surface object, which is the window screen that appears when this program runs screen = .set_mode((360, 480)) # Set the name of the window .set_caption('Hit the bricks') # Load image # Balls ball = ('img/') # Bricks block = ('img/') # Plates board = ('img/') # Game programs generally require a dead loop that passes certain conditions before they can exit # while True: for event in (): # Exit the program after accepting the exit time if == QUIT: exit() # Set the background of the screen to white ((255, 255, 255)) #Put the image loaded above into the screen # Put the ball in and set the coordinates # (ball, (180, 430)) # Put the bricks in and set the coordinates # # There's only one brick here, you can make a loop for many bricks later # (block, (20, 20)) # Put the board in and set the coordinates # (board, (150, 450)) # Refresh the screen ()
With the above code you can simply load the image into the screen
I have to get the ball and the board moving next, so I'll write about that next time.
This is the whole content of this article.