1

So below there is my code and Whenever I try to run it and the balloons spawn, I always get glitching which increase as we go down the line. Please see if there is anything I can do to fix this. The only part that I really would like to know is hot to make it smooth. Also I am Beginner so there are many things I would not understand. If you have any improvements for me, please feel free to tell me.

Balloon Pic('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()
JITTU VARGHESE
  • 435
  • 1
  • 4
  • 6

1 Answers1

1

Only perform a display update at the end of the application loop, not multiple updates during the frame. Multiple updates of the display cause flickering. One single pygame.display.update() or pygame.display.flip() at the end of the frame is sufficient.

Just remove pygame.display.update() from 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
Rabbid76
  • 202,892
  • 27
  • 131
  • 174