首页 > 解决方案 > 多个图像在绘制它们时会导致故障

问题描述

所以下面是我的代码,每当我尝试运行它并且气球产生时,我总是会遇到故障,随着我们走下线而增加。请看看我能做些什么来解决这个问题。我真正想知道的唯一部分是让它变得光滑。我也是初学者,所以有很多东西我不明白。如果您对我有任何改进,请随时告诉我。

气球图片('Logo.png')

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,0))
            quit_bt = Button(WIDTH-25, HEIGHT-25, "QUIT", 16, (255,255,0), (0,0,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,255))
        for i in range(10):
                bloons.append('balloon'+str(i))
                bloons[i] = Balloon(0,(-30*i) ,5, ([0,50], [528,50], [528,500], [150,500], [150,300], [700,300], [700,-100]))
        while run2:
            win.fill((0,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()

标签: pythonpygame

解决方案


仅在应用程序循环结束时执行显示更新,而不是在帧期间执行多次更新。多次更新显示会导致闪烁。一个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

推荐阅读