Pygame 窗口闪烁 [英] Pygame window flickering

查看:132
本文介绍了Pygame 窗口闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的文字冒险代码,碎片.当我开始我的代码时,我的按钮和窗口总是闪烁,我不知道如何解决这个问题.我试过 pygame.window.update(),但它没有带来任何东西.可能是 FPS 的问题,我不知道.:)..........................................................................

This is my text adventure code, pieces. When i start my code, my buttons and windows always flickering, and i don't know how to fix that problem. I tried pygame.window.update(), but it did not bring anything. Maybe it's problem with FPS, i don't know. :) ...............................................................................

import pygame,sys


pygame.init()
#############
pygame.mixer.music.load('Invincible.mp3')
pygame.mixer.music.play()

#############

display_width = 800
display_height = 600

black = (0,0,0)
white = (255,255,255)

red = (200,0,0)
green = (0,200,0)

bright_red = (255,0,0)
bright_green = (0,255,0)

block_color = (53,115,255)


gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('One Day After')
clock = pygame.time.Clock()

gameIcon = pygame.image.load('gameicon.jpg')
pygame.display.set_icon(gameIcon)

pause = False

def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()


def GameOver():
    ####################################
    pygame.mixer.Sound.play("smb_gameover.wav")
    pygame.mixer.music.stop()
    ####################################
    largeText = pygame.font.SysFont("comicsansms",115)
    TextSurf, TextRect = text_objects("Game Over", largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)


    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()



        button("Play Again",150,450,100,50,green,bright_green,game_loop)
        button("Quit",550,450,100,50,red,bright_red,quitgame)

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

def button(msg,x,y,w,h,ic,ac,action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if x+w > mouse[0] > x and y+h > mouse[1] > y:
        pygame.draw.rect(gameDisplay, ac,(x,y,w,h))
        if click[0] == 1 and action != None:
            pygame.mixer.music.stop()
            action()

    else:
        pygame.draw.rect(gameDisplay, ic,(x,y,w,h))
    smallText = pygame.font.SysFont("comicsansms",20)
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ( (x+(w/2)), (y+(h/2)) )
    gameDisplay.blit(textSurf, textRect)


def quitgame():
    pygame.quit()
    sys.exit()
    quit()

def unpause():
    global pause
    pygame.mixer.music.unpause()
    pause = False


def paused():
    ############
    pygame.mixer.music.pause()
    #############
    largeText = pygame.font.SysFont("comicsansms",115)
    TextSurf, TextRect = text_objects("Paused", largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)


    while pause:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()


        button("Continue",150,450,100,50,green,bright_green,unpause)
        button("Quit",550,450,100,50,red,bright_red,quitgame)

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


def game_intro():

    intro = True

    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        pilt1 = pygame.image.load('apoc2.jpg').convert()
        gameDisplay.blit(pilt1, [0,0])
        pygame.display.flip()


        button("Start",150,450,100,50,green,bright_green,game_loop)
        button("Quit",550,450,100,50,red,bright_red,quitgame)

        pygame.display.update()

def game_loop():
    global pause

    gameExit = False

    while not gameExit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
                quit()
        gameDisplay.fill(white)
        pygame.display.flip()
        Backgroundpic = pygame.image.load('back.jpg').convert()
        gameDisplay.blit(Backgroundpic, [0,0])
        pygame.display.flip()
        tekst = "This game will go as far as you choose!"
        meie_font = pygame.font.SysFont("Arial", 36)
        teksti_pilt = meie_font.render(tekst, False, (50,50,155))
        gameDisplay.blit(teksti_pilt, (100, 250))
        tekst2 = "You are the smith of your destiny"
        meie_font = pygame.font.SysFont("Arial", 36)
        teksti_pilt = meie_font.render(tekst2, False, (50,50,155))
        gameDisplay.blit(teksti_pilt, (100, 400))

        pygame.display.flip()









game_intro()
game_loop()
pygame.quit()
quit()

推荐答案

你似乎翻了两次.在绘制完所有内容后,您只能翻转一次.删除对 flip() 的第一次调用,您的闪烁应该会减少.

You seem to be flipping twice. You must flip only once, after everything is drawn. Remove the first call to flip() and your flicker should reduce.

这篇关于Pygame 窗口闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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