Mario running across screen too fast in pygame

2019-05-26 00:42发布

问题:

So first of all the code:

import pygame, sys
from pygame.locals import *

class Person(pygame.sprite.Sprite):      

    def __init__(self, screen):
        self.screen = screen
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("images/mariowalking0.png")
        self.rect = self.image.get_rect()
        self.rect.center = (320, 220)
        self.poseNumber = 1
    def update(self):
        self.rect.centerx += 1
        if self.rect.centerx > self.screen.get_width():
            self.rect.centerx = 0
        self.poseNumber = (self.poseNumber + 1)
        if self.poseNumber == 2:
            self.poseNumber = 0
        self.image = pygame.image.load("images/mariowalking" + str(self.poseNumber) +".png")
    def main():
        screen = pygame.display.set_mode((640, 480))
        background = pygame.Surface(screen.get_size())
        background.fill((255, 255, 255))
        screen.blit(background, (0, 0))
        boy = Person(screen)
        allSprites = pygame.sprite.Group(boy)
        keepGoing = True
        while keepGoing:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    keepGoing = False
                    pygame.quit()
            allSprites.clear(screen, background)
            allSprites.update()
            allSprites.draw(screen)
            pygame.display.flip()
    if __name__ == "__main__":
        main()

If you want to run the code, the images of the sprites can be found here: http://imgur.com/a/FgfAd

The Mario Sprite runs across the screen at a very fast pace. Even though I said that I am going to increase the value of centerx by 1. Any ideas why this is happening.

BTW I am really new to pygame, so sorry if I am overlooking an obvious fact or something.

回答1:

Your program will run with different speed on different computer - it depends on computer speed. You can use pygame.time.Clock() to get the same speed on all computers (except very slow computers) and also to slow down game and Mario speed.

clock = pygame.time.Clock()

while keepGoing:

    # rest of the code

    pygame.display.flip()

    clock.tick(30)

Now game will draw 30 frames per second (FPS) on all computers and Mario will be drawn 30 times per second. 25 FPS is enought for human eye to see nice animation. If you need you can set more FPS - for example 60.


This code (get_fps() and tick() without aguments) show you how fast is game on your computer. On my computer I get mostly 500 FPS (but sometimes even 1400 FPS).

clock = pygame.time.Clock()

while keepGoing:

    # rest of the code

    pygame.display.flip()

    clock.tick()
    print clock.get_fps()            

EDIT: if I minimalize window I get 10 000 FPS :)


EDIT:

If you still need to slow down Mario and have at least 30 FPS you have to check time before you move Mario.

class Person(pygame.sprite.Sprite):      

    def __init__(self, screen):
        # rest of code
        self.next_move = pygame.time.get_ticks() + 100 # 100ms = 0.1s

    def update(self):
        if pygame.time.get_ticks() >= self.next_move: 
            self.next_move = pygame.time.get_ticks() + 100 # 100ms = 0.1s
            # rest of code

I use get_ticks() to get current time in milliseconds(ms) (1000 milliseconds = 1 second) and compute time of next move (100 milliseconds = 0.1 second). Now Mario will make 10 steps per second. This way it will always make 10 steps per second even if I change FPS to 10 000 :).