I have learned part of pygame before. Some time ago, I thought I could use this thing to help others make a graduation project. Things can be left alone and learning will never stop.
Because Bishi's content is a Parkour game, I found a github code to take a look directly, and I was lucky to find the author's log, so I quoted the content of the old man throughout( https://www.cnblogs.com/msxh/tag/python/ ), it's a note.
sprite class
Common classes in game development are translated into elves in China. Some game engines will also program for elves (various inherited classes) to reflect this.
At first, I thought it was this class that did these operations for us to divide the picture into animation frames. We just need to pass in the picture and set a frame size. But the code tells me to cut the frame and then select the animation frame to be drawn. These operations are all done by ourselves.. (I don't like to operate pictures. It's really troublesome.)
def __init__(self, target): pygame.sprite.Sprite.__init__(self) self.target_surface = target self.image = None self.master_image = None self.rect = None self.topleft = 0,0 self.frame = 0 self.old_frame = -1 self.frame_width = 1 self.frame_height = 1 self.first_frame = 0 self.last_frame = 0 self.columns = 1 self.last_time = 0
-Initialization settings
member | significance |
---|---|
target_surface | python. display. surface object returned by setmode() |
image | Currently displayed animation frame |
master_image | Read sequence diagram containing all animation frames |
frame | Sequence number of the current frame |
old_frame | Sequence number of previous frame |
frame_width | One animation frame width |
frame_height | The height of an animation frame |
first_frame | First animation frame number, 0 |
last_frame | The number of the last animation frame is - 1 |
columns | How many animation frames are there in the sequence diagram |
last_time | Update animation frame time that will be used later |
def load(self, filename, width, height, columns): self.master_image = pygame.image.load(filename).convert_alpha() self.frame_width = width self.frame_height = height self.rect = 0,0,width,height self.columns = columns rect = self.master_image.get_rect() self.last_frame = (rect.width // width) * (rect.height // height) - 1
From the loading picture, we can see the functions of some variables in initialization. What makes sense is last_frame, this quantity is actually calculated by us... By calculating the incoming sequence diagram and the size of a frame.. (it feels reasonable, but I always think it should be easier to use...)
def update(self, current_time, rate=50): # . . . The number of frames explains the content. if self.frame != self.old_frame: frame_x = (self.frame % self.columns) * self.frame_width frame_y = (self.frame // self.columns) * self.frame_height rect = ( frame_x, frame_y, self.frame_width, self.frame_height ) self.image = self.master_image.subsurface(rect) self.old_frame = self.frame
The content of this section is to cut the sequence diagram to obtain the animation frame ready for reality, give the image variable, and then the next step is to transfer the serial number of the current frame.
- But in fact, I still don't know why I use image to update the animation frame every time I update. I also tried. As long as I read the image in update, another image will display the read other images. The drawing frame of each wizard should be the draw() function, but this function cannot be overridden, so it can only be partially processed in update.
- In the final analysis, this animation frame basically depends on the programmer. What is displayed in each frame, how many frames are set by the program, and cut the wizard sequence diagram into animation frames. These operations are written into the class by themselves. High degree of freedom.. But it's really not intelligent...
Frame number control
Each sprite should have an update function to update itself. When I read it, I was wondering what mechanism pygame provides to control the frame rate. In the article, I just mentioned to configure it in this way and that way. I had some understanding after I tried to change something.
def update(self, current_time, rate=50): if current_time > self.last_time + rate: self.frame += 1 if self.frame > self.last_frame: self.frame = self.first_frame self.last_time = current_time # . . .
- current_time is used as the input parameter. In the loop, use the update of group to call the update in our sprite, and the content of this parameter is a number. From pyGame Init() is called to start timing. How many milliseconds have passed. And this number is by calling pyGame time. get_ Ticks() gets.
- In this update function, a default parameter of 50 is the point that determines the refresh frequency. By looking at the content of the function, you can know that every update takes at least 50 milliseconds. When set to 1000, it can be observed that the animation frame is indeed updated every second.
In addition, there is a more display method to control the frame rate.
framerate = pygame.time.Clock() # . . . while True: framerate.tick(4)
- pygame. time. The function of clock() is described as create an object to help track time.
- There is a sentence fragment in the loop tick(4). This function is in file The description in is that each frame should be called once, and the return value is the number of milliseconds from the previous frame. However, if a parameter is passed in, it is used to limit the frame rate of the program to not exceed the given value. For example, I filled in the value 4, and the picture is indeed updated with 4 animation frames every second.
- At the same time, he will limit the loop to only run four times in one second.