vendredi 17 septembre 2021

How to stop pygame circles from overlapping?

I am making a game in pygame, and I am trying to display 7 balloons(circles for now), with random colors, and a random x-axis. The colors are working, however, when trying to display the circles at a random x-axis, the circles usually overlap. How can I fix this issue? Here's my code:

import pygame as pg
import random as r
import sys

pg.init()

bg = pg.image.load('bg.jpg')# Background color #
bg = pg.transform.scale(bg, (688,387))

bullets_colors_ls = []
iterator = -1
def bullets_colors():
    for i in range(1,8):
        bulletsvar = (r.choice([0,255]), r.randint(0,255), r.choice([0,255])) # bullets color auto generated using random #
        bullets_colors_ls.append(bulletsvar)
        
def make_bullets():
    global iterator
    global bullets_colors_ls
    for i in range(7):
        iterator += 1
        
        pg.draw.circle(screen, bullets_colors_ls[iterator],(x, y), radius=15)

def draw_balloons():
    global balloon_list
    global colors
    for i in range(7):
        balloon_x = r.randint(0, 500)
        balloon_color = (r.choice([0,255]), r.randint(0,255), r.choice([0,255]))
        balloon_list.append(balloon_x)
        colors.append(balloon_color)
        pg.draw.circle(screen, colors[i], (balloon_list[i], y), radius=30)

    
        
    
# Vars #
balloon_list = []
colors = []
x = 0
y = 250
velocity = 5
clock = pg.time.Clock()

screen = pg.display.set_mode((688 ,387)) # Size of the screen #
caption = pg.display.set_caption("Remember") # Title of the window #


gun = pg.image.load('game-gun.png')
gun = pg.transform.scale(gun, (150,150))
             



pg.display.flip() # Updating #

running = True # Game loop bool #

while running: # Game loop #
    clock.tick(60)
    screen.blit(bg, (0, 0))
    for event in pg.event.get():
        if event.type == pg.QUIT:
            pg.quit()
            sys.exit()
        if event.type == pg.KEYDOWN:
            if event.key == pg.K_ESCAPE:
                pg.quit()
                sys.exit()

    keys = pg.key.get_pressed()

    x += keys[pg.K_RIGHT] - keys[pg.K_LEFT] * velocity
    x -= keys[pg.K_LEFT] - keys[pg.K_RIGHT] * velocity
    draw_balloons()
    if keys[pg.K_SPACE]:
        bullets_colors()
        make_bullets()
    
     
    screen.blit(gun, (x, y))
    pg.display.update()

enter image description here

Appreciate any help, thanks!

P.S: How can I make the circles in more of a balloon shape without using an image?




Aucun commentaire:

Enregistrer un commentaire