Disclaimer: It is not easy to be original, and may not be reproduced without permission
0. Preface
Hello, everyone, I'm the cat lady who took you to write the game together.
Today we are going to make a little game for children to eat mushrooms! Based on the code in the first episode, let's implement the display of a small square, and the effect is like this.
pygame
Basic framework:
Sister cat to emphasize again: although the above code is 只有10行
, it is pygame
the essence of this framework, whether your game is simple or complex , is based on this code , so everyone must carefully understand the meaning of this 10行
code.
1. Define a block class
Let's first realize the display of the square. Here we need a little knowledge of object-oriented programming. We use class
Player
define a class. Enter pygame.sprite.Sprite
this class, let Player
inherit the sprite. Then in the class, implement our own constructor def __init__(self)
, and use the super
function to call the constructor of the elf parent class.
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
In the constructor, we use the pygame
Surface
function of ---0cc2a1ebc85e84da2251c32907174a2e self.image
to generate a small square with a width of 60 pixels and a height of 30 pixels. self.image
. Because the color of the screen is black, we need to fill the small square with a different color, and use the self.image.fill
function to fill the small square with white. Through the self.image
get_rect
function of ---15ae657d245684b0d540dcd88b71ec62---, you can get the rectangular area of the small square. Inside the brackets, you need to set the display position of the rectangular area, that is, after the final program runs, where is the small square initially show. Here, we let the small square be displayed in the center of the screen, so its left vertex i.e. x coordinate is left=WIDTH//2
, and the left vertex y coordinate is top=HEIGHT//2
.
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
self.image = pygame.Surface((60, 30))
self.image.fill("white")
self.rect = self.image.get_rect(left=WIDTH//2, top=HEIGHT//2)
Now the program runs without any effect because the picture is not drawn to the game window. Next, we define a update
function to draw the small square. We call the screen.blit
function, and the name of the image to be displayed is passed in the parentheses self.image
, and where the image is to be displayed.
def update(self):
screen.blit(self.image, self.rect)
1.1 Display of blocks
We now display the small square in the game window, we are outside the while
loop, generate a player
object, and then inside the while
loop, call player对象的update
Function, draw small squares! Let's run the program to see the effect!
# 实例化Player对象
player = Player()
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
player.update()
pygame.display.update()
Well, we see that the little square has been displayed! In the next episode we'll make the little cubes move!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。