2013-11-22 1 views
2

에 너무 빨리 화면에서 실행 : 코드를 실행하려면마리오 그래서 먼저 모든 코드의 파이 게임

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

, 스프라이트의 이미지가 여기에서 찾을 수 있습니다 : http://imgur.com/a/FgfAd

마리오 Sprite는 매우 빠른 속도로 화면에서 실행됩니다. 나는 centerx의 가치를 1 씩 늘릴 것이라고 말했지만, 왜 이런 일이 일어나고 있는지 알 수 있습니다.

BTW 저는 파이 게임에 새로운 것이므로 명백한 사실이나 뭔가를 간과한다면 미안합니다.

+0

업데이트가 매우 자주 호출됩니다. 각 flip() 후 몇 밀리 초의 지연 시간을 추가하십시오. 또한 flip이 update()보다 비쌉니다. –

답변

10

프로그램이 다른 컴퓨터에서 다른 속도로 실행됩니다. 컴퓨터 속도에 따라 다릅니다. pygame.time.Clock()을 사용하면 모든 컴퓨터 (매우 느린 컴퓨터 제외)에서 동일한 속도를 얻고 게임 및 마리오 속도를 느리게 할 수 있습니다.

clock = pygame.time.Clock() 

while keepGoing: 

    # rest of the code 

    pygame.display.flip() 

    clock.tick(30) 

지금 게임은 모든 컴퓨터에서 두 번째 (FPS) 당 30 개 프레임을 그릴 것입니다 마리오는 초당 30 번 그려집니다. 25 FPS는 인간의 눈으로 멋진 애니메이션을 볼 수있는 충분한 공간입니다. 당신은 당신이 더 많은 FPS를 설정할 수 있습니다 필요한 경우 - 예 (60)


위해이 코드 (aguments없이 get_fps()tick())는 얼마나 빨리하는 컴퓨터에 게임을 보여줍니다. 내 컴퓨터에서 나는 대부분 500 FPS를 얻는다. (때로는 심지어 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 :)를 얻을


편집 :

여전히 마리오를 느리게 할 필요가 적어도 30가있는 경우 FPS를 이용하면 마리오를 움직이기 전에 시간을 확인해야합니다.

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 

난 (100 밀리 초 = 0.1) 밀리 초 (MS)의 현재 시간 (1000 밀리 초 = 1)를 얻고, 다음의 이동 시간을 계산할 get_ticks()를 사용한다. 이제 Mario는 초당 10 단계를 수행합니다. 이렇게하면 FPS를 10 000으로 변경하더라도 항상 초당 10 단계가됩니다. :)

+0

하지만 창을 최소화하면 그래픽, 하하 – justhalf

+1

예, 그래픽이 보이지 않습니다. 그러나 컴퓨터가 얼마나 빠르며 자랑스러워 할 수 있습니다. – furas

+1

파이썬이'print' 문을 화면에 매우 효과적으로 버퍼링하지 않는다는 사실을 고려할 때, print 문은 실제로 당신을 감속시킬 수 있습니다. 또 다른 가능성은'theGameClock.get_fps()'의 값을 직접 화면에 표시하거나'pygame.display.set_caption (gameClock.get_fps()) '를 사용하는 것입니다. – SimonT