Fundamentals 4 min read

Creating Expanding Random Colored Circles with Pygame in Python

This tutorial demonstrates how to use Python's Pygame library to create an interactive effect where clicking the mouse generates expanding concentric circles with random colors and sizes, stopping once a random maximum radius is reached, and includes the full source code.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Creating Expanding Random Colored Circles with Pygame in Python

This example shows how to implement an interactive visual effect using Python and the Pygame library: each mouse click creates a new circle centered at the cursor, which expands outward with a random color until it reaches a randomly chosen maximum radius, after which the circle disappears.

Below is the complete source code that creates the window, handles mouse events, manages multiple circles, assigns random colors, and updates the display.

import pygame, random, sys, time
pygame.init()
screen = pygame.display.set_mode([600, 400])
screen.fill((255, 255, 255))
radiusr = 0
arrradiusr = [0] * 10  # 圆的半径
arraddradiusr = [0] * 10  # 圆的半径增量
arrradiusbool = [False] * 10  # 圆是否存在   False代表该索引值下的圆不存在,True代表存在
arrradiusx = [0] * 10  # 圆的坐标x轴
arrradiusy = [0] * 10  # 圆的坐标y轴
RGBx = [0] * 10  # 颜色RGB值第一个值
RGBy = [0] * 10  # 颜色RGB值第二个值
RGBz = [0] * 10  # 颜色RGB值第三个值

while True:
    time.sleep(0.1)  # 0.1秒
    for event in pygame.event.get():  # 监听器
        if event.type == pygame.MOUSEBUTTONDOWN:  # 鼠标按下
            num = arrradiusbool.index(False)   #获取圆不存在的索引值
            arrradiusbool[num] = True          #将该索引值的圆设置为存在
            arrradiusr[num] = 0                #该圆的半径设置为0
            arrradiusx[num], arrradiusy[num] = pygame.mouse.get_pos()        #获取鼠标坐标
            RGBx[num] = random.randint(0, 255)       #获取颜色值
            RGBy[num] = random.randint(0, 255)       #获取颜色值
            RGBz[num] = random.randint(0, 255)       #获取颜色值
            pygame.draw.circle(screen, pygame.Color(RGBx[num], RGBy[num], RGBz[num]),
                               (arrradiusx[num], arrradiusy[num]), arrradiusr[num], 1)
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    for i in range(10):
        if arrradiusbool[i] == False:     #如果圆不存在则跳过循环
            pass
        else:
            if (arrradiusr[i] < random.randint(10, 50)):       #随机圆的大小
                arraddradiusr[i] = random.randint(0, 5)        #圆的随机半径增量
                arrradiusr[i] += arraddradiusr[i]
                pygame.draw.circle(screen, pygame.Color(RGBx[i], RGBy[i], RGBz[i]),
                                   (arrradiusx[i], arrradiusy[i]), arrradiusr[i], 1)
            else:
                arrradiusbool[i] = False    #若圆已达到最大,这将该索引值的圆设置为不存在
    pygame.display.update()
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GraphicsPygamecirclesrandom colors
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.