Python code rain

Series of articles

serial number Article directory direct link
1 Romantic 520 Confession Code https://want595.blog.csdn.net/article/details/130666881
2 Full screen confession code https://want595.blog.csdn.net/article/details/129794518
3 beating heart https://want595.blog.csdn.net/article/details/129503123
4 floating hearts https://want595.blog.csdn.net/article/details/128808630
5 love light wave https://want595.blog.csdn.net/article/details/132311588
6 meteor shower https://want595.blog.csdn.net/article/details/129395465
7 starry sky https://want595.blog.csdn.net/article/details/129572082
8 Fireworks show https://want595.blog.csdn.net/article/details/128746664
9 christmas tree https://want595.blog.csdn.net/article/details/128213770
10 snowflake code https://want595.blog.csdn.net/article/details/129038108
11 Simulated starry sky https://want595.blog.csdn.net/article/details/129948882
12 Birthday Cake https://want595.blog.csdn.net/article/details/129694998
13 Cherry tree https://want595.blog.csdn.net/article/details/130350743
14 colorful balloons https://want595.blog.csdn.net/article/details/130950744
15 colorful flowers https://want595.blog.csdn.net/article/details/130897838
16 spoof code https://want595.blog.csdn.net/article/details/131274862

Preface

Today the blogger brings you interesting code rain, let’s take a look!

Pygame graphics library

Getting Started with Pygame

Pygame is a Python library and toolkit for developing 2D graphics and simple games. It provides a range of features to handle development needs in graphics, sound, input, and more. Here are the basic steps to get started with Pygame:

1. Install Pygame: First, make sure you have Python installed. Pygame can then be installed from the command line using the pip command. Enter the following command on the command line: `pip install pygame`

2. Import the Pygame module: In your Python program, use the `import pygame` statement to import the Pygame module.

3. Initialize Pygame: At the beginning of the program, use `pygame.init()` to initialize Pygame. This will initialize various modules and functions of Pygame.

4. Create a game window: Use the `pygame.display.set_mode()` function to create a game window. It takes a tuple or list as parameters representing the width and height of the window. For example, `screen = pygame.display.set_mode((800, 600))` will create a game window that is 800 pixels wide and 600 pixels high.

5. Game loop: Use a while loop to perform the main loop of the game. In each loop, user input is processed, game state is updated, graphics are drawn, and other operations. This loop will continue to run until the player closes the game window.

6. Handle events: In the game loop, use the `pygame.event.get()` function to get the events that occurred. You can use a for loop to iterate through these events, such as key presses, mouse clicks, etc.

7. Drawing graphics: Use various Pygame functions and methods to draw graphics, sprites, text, etc. Use `pygame.display.flip()` to update the screen display.

8. Game exit: When the player closes the game window, the loop will terminate and you can use `pygame.quit()` to clean up Pygame resources.

The above is just a simple introduction to Pygame. You can learn and understand more functions and usage of Pygame in depth by reading official documents, finding tutorials and sample codes. I wish you use Pygame to develop interesting games!

Pygame basic functions

The following are some pygame introductory functions and sample code to help you get started using the pygame library:

1. `pygame.init()`: Initialize the pygame library.
 

import pygame

pygame.init()

2. `pygame.display.set_mode()`: Create a window display interface.

import pygame

# 设置窗口大小为800x600像素
screen = pygame.display.set_mode((800, 600))

3. `pygame.display.set_caption()`: Set the title of the window.

import pygame

# 设置窗口标题为"游戏窗口"
pygame.display.set_caption("游戏窗口")

4. `pygame.event.get()`: Get all events in the event queue.
 

import pygame

# 获取所有的事件
for event in pygame.event.get():
    # 处理事件
    if event.type == pygame.QUIT:
        # 如果是窗口关闭事件,退出程序
        pygame.quit()

5. `pygame.image.load()`: Load image file.

import pygame

# 加载图片
image = pygame.image.load("image.png")

6. `pygame.draw.rect()`: Draw a rectangle.

import pygame

# 绘制一个红色的矩形,左上角坐标为(100, 100),宽度为200,高度为100
pygame.draw.rect(screen, (255, 0, 0), (100, 100, 200, 100))

7. `pygame.font.Font()`: Create a font object.

import pygame

# 创建一个字体对象,字体为宋体,大小为25
font = pygame.font.Font("simsun.ttf", 25)

8. `pygame.Surface.blit()`: Draw another Surface object on a Surface object.

import pygame

# 绘制image图片到screen上,位置为(0, 0)
screen.blit(image, (0, 0))

The above are some commonly used pygame functions and simple example codes. You can extend and adapt it to your needs and start using pygame to create games or graphics applications.

Python code rain

programming

import pygame
import random

# 初始化
pygame.init()
font = pygame.font.SysFont('宋体', 25)
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
screenwidth = screen.get_width()
screenheight = screen.get_height()
surface = pygame.Surface((screenwidth, screenheight), pygame.SRCALPHA)
pygame.Surface.convert(surface)
surface.fill((0, 0, 0, 10))
screen.fill((0, 0, 0, 10))
# 内容
str = [chr(i) for i in range(48, 58)] + [chr(i) for i in range(97, 123)] 
texts = [font.render(i, True, (0, 255, 0)) for i in str]
lst = list(range(99))
……
完整代码文末公众号免费获取哦

program analysis

- Import pygame and random libraries.

import pygame
import random

- Call pygame.init() to initialize pygame.

pygame.init()

- Create font objects and set font style and size.

font = pygame.font.SysFont('宋体', 25)

- Create a full screen window.

screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)

- Get the width and height of the screen.

screenwidth = screen.get_width()
screenheight = screen.get_height()

- Create a Surface object for drawing transparency effects on the screen.

surface = pygame.Surface((screenwidth, screenheight), pygame.SRCALPHA)

- Convert and fill color to Surface objects.

pygame.Surface.convert(surface)
surface.fill((0, 0, 0, 10))

- Fill the screen with a black background.

screen.fill((0, 0, 0, 10))

- Define a list containing numbers and lowercase letters used to generate character objects.

str = [chr(i) for i in range(48, 58)] + [chr(i) for i in range(97, 123)] 

- Render each character in the character list as a font object and put it into a new list.

texts = [font.render(i, True, (0, 255, 0)) for i in str]

- Create a list containing integers from 0 to 98.

lst = list(range(99))

- Enter the game loop and continuously handle events.

- Set the delay time to 50 milliseconds to control the falling speed of characters.

- Draw transparent background on screen.

- Traverse the list of integers, selecting a random character each time and drawing it to the screen at the corresponding position.

- Add 1 to the elements in the integer list to achieve the character falling effect.

- Resets the elements in the integer list to 0 with a 5% probability to achieve the effect of characters starting to fall again.

- Call pygame.display.flip() to update the screen display.

end

Thanks for the support! !

Guess you like

Origin blog.csdn.net/m0_68111267/article/details/132574687