Pygame没有显示网格或fps计数器

时间:2017-10-23 11:03:56

标签: python-3.x pygame

我正在尝试制作一个RPG,但我总是得到问题,当pygame剂量不显示我使用youtube视频来帮助我制作这个RPG但它只是没有工作创作者做了一个颜色模块帮助但它只是剂量不工作我知道有更好的方法让fps计数器随时改进它

import pygame, sys, time
from Scripts.UltraColor import *

pygame.init()


cSec = 0
cFrame = 0
FPS = 0

tile_size = 32

fps_font = pygame.font.Font("C:\\Windows\\Fonts\\Verdana.ttf", 20)

def show_fps():
    fps_overlay = fps_font.render(str(FPS), True, Color.Goldenrod)
    window.blit(fps_overlay, (0,0))

def create_window():
    global window, window_height, window_width, window_title
    window_width, window_hight = 800, 600
    window_title = "RPG"
    pygame.display.set_caption(window_title)
    window = pygame.display.set_mode((window_width, window_hight), pygame.HWSURFACE|pygame.DOUBLEBUF)


def count_fps():
    global cSec, cFrame, FPS

    if cSec == time.strftime("%S"):
        cFrame += 1
    else:
        FPS = cFrame
        cFrame = 0
        cSec = time.strftime("%S")

create_window()

isRunning = True

while isRunning:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            isRunning = False

    # LOGIC
    count_fps()

    # Render Graphics
    window.fill(Color.Black)


    # - Render Sinple Terrain Grid
    for x in range(0, 640, tile_size):
        for y in range(0, 480, tile_size):
            pygame.draw.rect(window, Color.White, (x, y, tile_size + 1, tile_size + 1), 1)

    show_fps()

    pygame.display.update


pygame.quit()
sys.exit()

1 个答案:

答案 0 :(得分:0)

您需要在pygame.display.update后面添加括号来调用此函数并更新显示:pygame.display.update()

我还建议使用pygame.time.Clock来限制帧速率并获取fps。调用clock.tick(FPS)可确保游戏的运行速度不会超过此帧速率。在show_fps函数中,您只需调用clock.get_fps()即可获得当前帧速率。

import pygame


def show_fps(window, clock):
    fps_overlay = FPS_FONT.render(str(clock.get_fps()), True, GOLDENROD)
    window.blit(fps_overlay, (0, 0))


pygame.init()

FPS_FONT = pygame.font.SysFont("Verdana", 20)
GOLDENROD = pygame.Color("goldenrod")

tile_size = 32
window = pygame.display.set_mode((800, 600), pygame.HWSURFACE|pygame.DOUBLEBUF)
clock = pygame.time.Clock()
FPS = 60

isRunning = True

while isRunning:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            isRunning = False

    # Render Graphics
    window.fill((50, 50, 50))

    # Render Simple Terrain Grid
    for x in range(0, 640, tile_size):
        for y in range(0, 480, tile_size):
            pygame.draw.rect(
                window, (255, 255, 255),
                (x, y, tile_size+1, tile_size+1), 1)

    show_fps(window, clock)

    clock.tick(FPS)
    pygame.display.update()


pygame.quit()