Article | carefree huan \

Source: Python Technology “ID: pythonall”

I have nothing to do at home this weekend. My brother and sister-in-law want to go out happy, so they threw his son to my house and asked me to play with him as a dad. In order to let this grinding little goblin quiet, I good play disc king, I brainwave, ready to write a simple small game for him to play for a while.

Train of thought

For three-year-olds, they don’t need a complex game, they want a dumb-ass game with a colorful interface. And write this small game can not take up too much of my time, otherwise the gain outweighs the loss!

Based on this idea, I remembered where I had seen a small game ———— colorful concentric circles. Its gameplay is to click the mouse every time, will draw a circle with the mouse as the center of the circle, and then on the basis of this circle continuously expand the circle (similar to the diffusion of water waves), so as to form a concentric circle, and reach a random size, stop expanding, where each concentric circle color is random.

This small game just meets the current scene, so I roll up my sleeves ready to achieve it in three and five ways, for my king of the road to win time!

implementation

First, I need to initialize various variables:


pygame.init()
screen = pygame.display.set_mode([600.400])
screen.fill((255.255.255Radius = [0] * 10CircleDelt = [0] * 10CircleExists circleExists = [False] *10CircleX = [0] * 10CircleY = [0] * 10# RGBx = [0] * 10
RGBy = [0] * 10
RGBz = [0] * 10
Copy the code

Then I need to listen for the mouse event and draw an initial circle based on the mouse position:

# Mouse downif event.type== pygame.MOUSEBUTTONDOWN: CircleExists [num] = True circleExists[num] = True0
            radius[num] = 0CircleX [num] = pygame.mouse. Get_pos () circleY[num] = pygame.mouse.0.255)
            RGBy[num] = random.randint(0.255)
            RGBz[num] = random.randint(0.255Circle (screen, pygame.color (RGBx[num], RGBy[num], RGBz[num]), circleX[num], circleY[num]), radius[num],1)
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
            
Copy the code

Once I’ve drawn the circle, I need it to randomly expand into concentric circles, and this concentric circle needs to be drawn round and round:


for i in range(10): # Skip loop if the circle does not existif not circleExists[i]:
            pass
        else: # Random circle sizeif radius[i] < random.randint(10.50CircleDelt [I] = random. Randint (0.5Circle (RGBx[I], RGBy[I], RGBz[I]), (circleX[I], (RGBx[I], RGBz[I], circleY[i]), radius[i],1)
            elseCircleExists [I] = False: # If the circle has reached the maximum value, this sets the circle of the index value to non-existentCopy the code

The end result looks like this:

conclusion

Although I am not a dad yet, I feel that I need to think more about PyGame and reserve some interesting mini-games for my future son to play, so as to show the advantages of being a tech person. Cheers!

PS: Reply “Python” within the public number to enter the Python novice learning exchange group, together with the 100-day plan!

Old rules, brothers still remember, the lower right corner of the “watching” click, if you feel the content of the article is good, remember to share moments to let more people know!

[Code access ****]

Identify the qr code at the end of the article, reply: 201216