如何解决多个图像在绘制时会引起闪烁
因此,在下面是我的代码,每当我尝试运行它并生成气球时,我总会出现小故障,当我们沿线前进时,小故障会增加。请查看是否有什么我可以解决的方法。我真正想知道的唯一部分是使它变得平滑的热度。我也是初学者,所以有很多我不理解的事情。如果您对我有任何改善,请随时告诉我。
import pygame
import sys
import os
import random
import time
pygame.init()
WIDTH,HEIGHT = 800,600
win = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("My Game")
clock = pygame.time.Clock()
bg_music = []
for r,d,f in os.walk("Music"):
bg_music.append(f)
i = 0
balloon_sprite = pygame.image.load("logo.png")
round_num = 1
bloons = []
def music_play():
global i
if not(pygame.mixer.music.get_busy()):
pygame.mixer.music.load('Music/'+bg_music[0][i])
pygame.mixer.music.play()
i = random.randint(0,len(bg_music)-1)
class Button:
def __init__(self,x_center,y_center,text,size,text_col,bg_col):
self.x = x_center
self.y = y_center
self.size = size
self.text_col = text_col
self.bg_col = bg_col
self.hit = False
font = pygame.font.Font('freesansbold.ttf',self.size)
self.text = font.render(text,True,self.text_col,self.bg_col)
self.textRect = self.text.get_rect()
self.textRect.center = (self.x,self.y)
win.blit(self.text,self.textRect)
def Check_Collision(self,event):
if event.type == pygame.MOUSEBUTTONDOWN:
pos_mouse = event.pos
if self.textRect.collidepoint(pos_mouse):
self.hit = True
def Start(self):
global run1
if (self.hit):
run1 = False
def Quit(self):
if (self.hit):
sys.exit()
class Balloon:
def __init__(self,x,y,health,dests):
self.health = health
self.dests = dests
self.x = x
self.y = y
self.count = 0
def draw_Balloon(self):
global balloon_sprite
win.blit(balloon_sprite,pygame.Rect((self.x,self.y),(balloon_sprite.get_width(),balloon_sprite.get_height())))
pygame.display.update()
if not(self.count==len(self.dests)):
if self.x == self.dests[self.count][0]:
if self.y== self.dests[self.count][1]:
self.count+=1
else:
if self.y< self.dests[self.count][1]:
self.y+=1
else:
self.y-=1
else:
if self.x< self.dests[self.count][0]:
self.x+=1
else:
self.x-=1
def Main():
def Check_Close(event):
if event.type == pygame.QUIT:
sys.exit()
global run1
running = True
run1 = run2 = True
while running:
while run1:
start_bt = Button(WIDTH//2,HEIGHT//2,"Start",32,(255,255,0),(0,0))
quit_bt = Button(WIDTH-25,HEIGHT-25,"QUIT",16,0))
clock.tick(60)
for event in pygame.event.get():
Check_Close(event)
start_bt.Check_Collision(event)
start_bt.Start()
quit_bt.Check_Collision(event)
quit_bt.Quit()
pygame.display.update()
music_play()
win.fill((255,255))
for i in range(10):
bloons.append('balloon'+str(i))
bloons[i] = Balloon(0,(-30*i),5,([0,50],[528,500],[150,300],[700,-100]))
while run2:
win.fill((0,0))
clock.tick(60)
for i in range(10):
bloons[i].draw_Balloon()
for event in pygame.event.get():
Check_Close(event)
music_play()
pygame.display.update()
Main()
解决方法
仅在应用程序循环结束时执行显示更新,而不在框架中执行多次更新。多次更新显示会导致闪烁。在帧末尾一个pygame.display.update()
或pygame.display.flip()
就足够了。
只需从 pygame.display.update()
中删除Balloon.draw_Balloon
:
class Balloon:
# [...]
def draw_Balloon(self):
global balloon_sprite
win.blit(balloon_sprite,pygame.Rect((self.x,self.y),(balloon_sprite.get_width(),balloon_sprite.get_height())))
# pygame.display.update() <--- DELETE
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。