Python - Meteor Shower (concise version)

  1. Import the necessary libraries, including pygame, random and sys;

Regarding pygame, if you are interested, you can read this article: Python - A brief talk about Pygame

import pygame
import random
import sys
  1. Initialize the pygame library;

pygame.init()
  1. Define constants, including screen size, number of meteors, maximum speed of meteors, maximum length of meteors, minimum length of meteors, etc.;

Here we define the screen size as 800x600, the number of meteors as 10, the maximum speed as 10, the maximum length as 80, and the minimum length as 40.

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
NUM_METEORS = 10
MAX_SPEED = 10
MAX_LENGTH = 80
MIN_LENGTH = 40
  1. Define the meteor class, including attributes such as position, speed, length, and methods for drawing and updating meteors;

class Meteor:
    def __init__(self):
        self.x = random.randint(0, SCREEN_WIDTH)
        self.y = random.randint(-SCREEN_HEIGHT, 0)
        self.speed = random.randint(1, MAX_SPEED)
        self.length = random.randint(MIN_LENGTH, MAX_LENGTH)

    def draw(self, surface):
        pygame.draw.line(surface, (255, 255, 255), (self.x, self.y), (self.x - self.length, self.y + self.length))

    def update(self):
        self.x -= self.speed
        self.y += self.speed

        if self.x < -self.length or self.y > SCREEN_HEIGHT + self.length:
            self.__init__()  # 如果流星超出屏幕,重新初始化流星属性

In the above code, we define a Meteor class, including the meteor's position, speed, length and other attributes, as well as methods for drawing and updating meteors. In the initialization method __init__, we randomly generate the position, speed and length of the meteor. In the drawing method draw, we use the draw.line method of the pygame library to draw the meteor. In the update method update, we update the meteor's position based on its speed and position. If the meteor goes off screen, we reinitialize the meteor's properties.

  1. Define the main function, including functions such as creating meteors, updating meteors, and drawing meteors;

# 创建流星
meteors = []
for i in range(NUM_METEORS):
    meteors.append(Meteor())

# 创建屏幕
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

# 主函数
def main():
    # 循环调用更新和绘制流星的方法
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        screen.fill((0, 0, 0))  # 填充屏幕颜色

        for meteor in meteors:
            meteor.update()
            meteor.draw(screen)

        pygame.display.update()  # 更新屏幕显示

# 调用主函数
if __name__ == '__main__':
    main()

In the code above, we first create NUM_METEORS meteors and add them to the meteors list. Then we created a screen object and looped through the main function to call the methods for updating and drawing the meteor. In each loop, we first fill the screen with black, then update the position of each meteor and draw the meteor. Finally, call the pygame.display.update() method to update the screen display.

  1. Exit the pygame library.

pygame.quit()
sys.exit()

Finally, we need to exit the pygame library when the program ends.

The complete code is as follows:

import pygame
import random
import sys

# 初始化pygame
pygame.init()

# 定义常量
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
NUM_METEORS = 10
MAX_SPEED = 10
MAX_LENGTH = 80
MIN_LENGTH = 40

# 定义流星类
class Meteor:
    def __init__(self):
        self.x = random.randint(0, SCREEN_WIDTH)
        self.y = random.randint(-SCREEN_HEIGHT, 0)
        self.speed = random.randint(1, MAX_SPEED)
        self.length = random.randint(MIN_LENGTH, MAX_LENGTH)

    def draw(self, surface):
        pygame.draw.line(surface, (255, 255, 255), (self.x, self.y), (self.x - self.length, self.y + self.length))

    def update(self):
        self.x -= self.speed
        self.y += self.speed

        if self.x < -self.length or self.y > SCREEN_HEIGHT + self.length:
            self.__init__()

# 创建流星
meteors = []
for i in range(NUM_METEORS):
    meteors.append(Meteor())

# 创建屏幕
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

# 主函数
def main():
    # 循环调用更新和绘制流星的方法
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

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

        for meteor in meteors:
            meteor.update()
            meteor.draw(screen)

        pygame.display.update()

# 调用主函数
if __name__ == '__main__':
    main()

# 退出pygame
pygame.quit()
sys.exit()

Guess you like

Origin blog.csdn.net/qq_48892708/article/details/129740680