马里奥跨屏幕运行过快的pygame的 [英] Mario running across screen too fast in pygame

查看:367
本文介绍了马里奥跨屏幕运行过快的pygame的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以首先在code的:

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()

如果你想运行code,精灵的形象可以在这里找到:的http:/ /imgur.com/a/FgfAd

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

马里奥雪碧在屏幕上以非常快的速度运行。即使我说,我要通过一,任何想法,为什么发生这种情况,增加的centerX的价值。

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,我真正的新pygame的,很抱歉,如果我可以俯瞰一个显而易见的事实或东西。

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

推荐答案

您计划将用不同的计算机上不同速度下运行 - 这取决于计算机的速度。您可以使用 pygame.time.Clock()让所有的计算机上相同的速度(除非很慢计算机),也减慢游戏和Mario速度

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)

现在比赛将在所有计算机上画出每秒(FPS)30帧和马里奥将被吸引每秒30次。 25 FPS是足够多的人眼看到漂亮的动画。如果你需要可以设置更多的FPS - 例如60

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.

这code( get_fps()打勾()无aguments)告诉您如何快游戏在您的计算机上。在我的电脑我得到的大多是500 FPS(但有时甚至1400 FPS)。

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()            

编辑:如果我minimalize窗口,我得到10 000 FPS:)

if I minimalize window I get 10 000 FPS :)

编辑:

如果你仍然需要慢下来马里奥和至少有30 FPS你有移动马里奥前检查时间。

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

我用 get_ticks()来获得毫秒(ms)当前时间(1000毫秒= 1秒),并计算下一步行动的时间(100毫秒= 0.1秒) 。现在,马里奥将使得每秒10步。这样,它总会让每秒10步,即使我改FPS 10 000 :)。

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 :).

这篇关于马里奥跨屏幕运行过快的pygame的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆