如何解决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 举报,一经查实,本站将立刻删除。