2016-07-22 6 views
1

背景が絶え間なく動いているPyGame(Geometry Dashなど)で 'Runner'スタイルのゲームを作ろうとしています。これまでのところすべて正常に動作しますが、背景画像のレンダリングでは、フレームレートが1秒あたり35フレームを超えないように制限されています。無限/繰り返しの背景要素を追加する前に、60 fpsで簡単に実行できます。これらの2行のコードは、(削除された場合、ゲームは60 + fpsで実行できます):PyGameの大きな画像を低フレームレートでレンダリングする

screen.blit(bg、(bg_x、0))| screen.blit(bg、(bg_x2、0))

ゲームを高速化するためにできることはありますか?前もって感謝します!

簡体ソースコード:

import pygame 
pygame.init() 

screen = pygame.display.set_mode((1000,650), 0, 32) 
clock = pygame.time.Clock() 

def text(text, x, y, color=(0,0,0), size=30, font='Calibri'): # blits text to the screen 
    text = str(text) 

    font = pygame.font.SysFont(font, size) 
    text = font.render(text, True, color) 

    screen.blit(text, (x, y)) 

def game(): 
    bg = pygame.image.load('background.png') 
    bg_x = 0 # stored positions for the background images 
    bg_x2 = 1000 

    pygame.time.set_timer(pygame.USEREVENT, 1000) 
    frames = 0 # counts number of frames for every second 
    fps = 0 

    while True: 
     frames += 1 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 
      if event.type == pygame.USEREVENT: # updates fps every second 
       fps = frames 
       frames = 0 # reset frame count 

     bg_x -= 10 # move the background images 
     bg_x2 -= 10 

     if bg_x == -1000: # if the images go off the screen, move them to the other end to be 'reused' 
      bg_x = 1000 
     elif bg_x2 == -1000: 
      bg_x2 = 1000 

     screen.fill((0,0,0)) 
     screen.blit(bg, (bg_x, 0)) 
     screen.blit(bg, (bg_x2, 0)) 

     text(fps, 0, 0) 

     pygame.display.update() 
     #clock.tick(60) 

game() 

ここで背景画像がある:

enter image description here

答えて

1

あなたはconvert()を使用してみましたか? documentationから

bg = pygame.image.load('background.png').convert() 

あなたは、多くの場合、画面上でより迅速に描画されますコピーを作成するには、引数なしでSurface.convert()を呼び出すことになるでしょう。

.pngのように、画像を透明にするには、画像に透明度があるように、読み込んだ後にconvert_alpha()メソッドを使用します。

+0

ありがとうございました!それは今の風のように実行されます! –

関連する問題