pygame中的clock.tick功能似乎在闪烁

问题描述

因此,关于我的旧帖子,我做了一个学校作业的游戏,由于某种原因,运行该程序时,窗口似乎在闪烁。我尝试了许多解决方案,但似乎没有用。我怎样才能解决这个问题?我的clock.tick可能设置为每秒60帧。方便的解决方案将非常有用。

# Program: Import Library,Pygame,for initialization of this program
import pygame
# Initialize the game engine
pygame.init()

# Define Colours

BLACK    = (   0,0)
WHITE    = ( 255,255,255)
GREEN    = (   0,0)
RED      = ( 255,0)
BLUE     = (   0,255)

display_width = 1080
display_height = 720
size = (display_width,display_height)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("MiniConomy Trivia,for Adults")

# Button Program

class Button:
    def __init__(self,size,text,pos,bgColor=(0,0),textColor=(0,0)):
        self.pos  = pos
        self.size = size
        self.text = text
        self.font = pygame.font.Font(pygame.font.get_default_font(),size[1])
        self.textSurf = self.font.render(f"{text}",True,textColor)
        self.button = pygame.Surface((size[0],size[1])).convert()
        self.button.fill(bgColor)

    def render(self,window):
        window.blit(self.button,(self.pos[0],self.pos[1]))
        window.blit(self.textSurf,(self.pos[0]+1,self.pos[1]+5))

    def clicked(self,events):
        mousePos = pygame.mouse.get_pos()#  get the mouse position
        for event in events:
            if self.button.get_rect(topleft=self.pos).collidepoint(mousePos[0],mousePos[1]):
                if event.type == pygame.MOUSEBUTTONDOWN:
                    return True
        return False

# Setting a Title Screen
def text_objects(text,font):
    textSurface = font.render(text,BLACK)
    return textSurface,textSurface.get_rect()
largeText = pygame.font.Font('freesansbold.ttf',90)
 
# Creating a Title Screen
TextSurf,TextRect = text_objects("MiniConomy",largeText)
TextRect.center = (540,150)

# Play Button

button = Button([280,50],"Let's Begin",[380,302])
button2 = Button([210,"Levels",402])
button3 = Button([215,"Settings",502])


background_image = pygame.image.load("Miniconomy.PNG").convert()

#Loop until the user clicks the close button
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()

# -------- Main Program Loop -----------
while not done:
    events = pygame.event.get()
    for event in events: # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done = True # Flag that we are done so we exit this loop

    # --- Game logic should go here
    # --- Drawing code should go here
    screen.blit(background_image,(0,0))
 
    # --- Go ahead and update the screen with what we've drawn.
    pygame.display.flip()
    # --- Limit to 60 frames per second
    clock.tick(60)
 
    # Set the screen background
    screen.blit(TextSurf,TextRect)
    
    # Button 1 Control
    button.render(screen)
    if button.clicked(events):
        print("Game logic goes here")

    button2.render(screen)
    if button2.clicked(events):
        print("Game Logic goes Here")

    button3.render(screen)
    if button3.clicked(events):
        print("Game Logic goes Here")
    pygame.display.flip()

pygame.quit()
quit()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)