Pygame运行速度超慢(〜2 fps)

时间:2018-12-12 21:58:53

标签: python pygame

我的pygame游戏超级慢,大约2 fps。您应该可以使用WASD来回移动,并且始终位于屏幕中央。我知道这与我的tilemap和我的数学方式有关,但我无法确定。另外,如果有一种更好的方法可以在将地图移动到后面的同时将玩家保持在中心,我很想知道如何做到这一点。

import pygame, sys, numpy
#create fps clock
clock = pygame.time.Clock()
#
MAPHEIGHT = 80
MAPWIDTH = 80
TILESIZE =  40
TILESONSCREENW = 13
TILESONSCREENH = 13
#set screen size
SCREENH = TILESONSCREENH*TILESIZE
SCREENW = TILESONSCREENW*TILESIZE
#create character vars
circleRad = 40
circleSpeed = 4
#create circle pos vars
circleX = 250
circleY = 250
#create keyboard button vars
rightP = False
leftP = False
upP = False
downP = False
#
playerOnTileS = pygame.Surface((MAPWIDTH*TILESIZE, MAPHEIGHT*TILESIZE))
#constants for the tilemap
GRASS = pygame.image.load("grass.png")
#tilemap
tilemap = [[GRASS for i in range(MAPHEIGHT)] for j in range(MAPWIDTH)]
#create window
DISPLAYSURF = pygame.display.set_mode((SCREENW, SCREENH))
#set window name
pygame.display.set_caption("Snowball Fight!")
#---------------------------------------------------
class Player:
    def __init__(self, playX, playY, size):
        self.playerX = playX
        self.playerY = playY
        self.size = size
        self.playerSurface = pygame.Surface((size, size))
        pygame.draw.rect(self.playerSurface, (19,135,67), (0,0,size, size))
#------------------------------------------------
    def update(self):
        playerOnTileS.blit(self.playerSurface, (self.playerX, self.playerY))
        DISPLAYSURF.blit(playerOnTileS, (SCREENW/2-self.playerX-self.size ,SCREENH/2-self.playerY-self.size))
#game loop
myPlayer = Player(0,0,circleRad)
while True:
    DISPLAYSURF.fill((0,0,0))
    for event in pygame.event.get():
        #if the user closed the window
        if event.type == pygame.QUIT:
            #close pygame
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                leftP = True
            if event.key == pygame.K_d:
                rightP = True
            if event.key == pygame.K_w:
                upP = True
            if event.key == pygame.K_s:
                downP = True
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_a:
                leftP = False
            if event.key == pygame.K_d:
                rightP = False
            if event.key == pygame.K_w:
                upP = False
            if event.key == pygame.K_s:
                downP = False
    if leftP:
        myPlayer.move(-circleSpeed,0,True)
    if rightP:
        myPlayer.move(circleSpeed,0,True)
    if downP:
        myPlayer.move(0,circleSpeed,True)
    if upP:
        myPlayer.move(0,-circleSpeed,True)
    for row in range(len(tilemap)):
        for column in range(len(tilemap[row])):
            playerOnTileS.blit(tilemap[row][column],(column*TILESIZE,row*TILESIZE))
    myPlayer.update()
    pygame.display.update()
    clock.tick(30)

1 个答案:

答案 0 :(得分:1)

Images / pygame。通常应使用pygame.Surface.convertconvert_alpha方法转换表面。未转换表面的性能令人讨厌。转换草图像时,我得到了近30 FPS。

下一个问题是嵌套的for循环。 Python的函数调用开销相当大,因此,最好在主循环开始之前将所有图块都绘制到一个大的背景表面上,然后每帧将该背景表面绘制到DISPLAYSURF上以清除它,这是个好主意。进行此更改后,我得到pygame的最大FPS值,这意味着clock.get_fps()在2000至1666.666 FPS之间跳跃。

# Ahead of the while loop.
playerOnTileS = pygame.Surface((MAPWIDTH*TILESIZE, MAPHEIGHT*TILESIZE))
for row in range(len(tilemap)):
    for column in range(len(tilemap[row])):
        playerOnTileS.blit(tilemap[row][column],(column*TILESIZE,row*TILESIZE))

您必须更改update方法(顺便说一下,最好将其命名为draw),然后将播放器表面涂在DISPLAYSURF上。