Hướng dẫn how do i run a python game file? - làm cách nào để chạy một tệp trò chơi python?

Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem nó cùng với hướng dẫn bằng văn bản để hiểu sâu hơn về sự hiểu biết của bạn: Tạo một trò chơi cuộn bên 2D với pygame This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Make a 2D Side-Scroller Game With PyGame

Show

Khi tôi bắt đầu học lập trình máy tính vào cuối thiên niên kỷ qua, nó được thúc đẩy bởi mong muốn của tôi để viết các trò chơi máy tính. Tôi đã cố gắng tìm ra cách viết các trò chơi bằng mọi ngôn ngữ và trên mọi nền tảng tôi đã học, bao gồm cả Python. Đó là cách mà tôi phát hiện ra

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 và học cách sử dụng nó để viết các trò chơi và các chương trình đồ họa khác. Vào thời điểm đó, tôi thực sự muốn có một mồi trên
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6.

Đến cuối bài viết này, bạn sẽ có thể:

  • Vẽ các mục trên màn hình của bạn
  • Chơi hiệu ứng âm thanh và âm nhạc
  • Xử lý đầu vào của người dùng
  • Thực hiện các vòng lặp sự kiện
  • Mô tả cách lập trình trò chơi khác với lập trình Python thủ tục tiêu chuẩn

Primer này giả định rằng bạn có một sự hiểu biết cơ bản về việc viết các chương trình Python, bao gồm các chức năng do người dùng xác định, nhập khẩu, vòng lặp và điều kiện. Bạn cũng nên quen thuộc với cách mở các tệp trên nền tảng của mình. Một sự hiểu biết cơ bản về Python hướng đối tượng cũng hữu ích.

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 hoạt động với hầu hết các phiên bản Python, nhưng Python 3.6 được khuyến nghị và sử dụng trong suốt bài viết này.

Bạn có thể lấy tất cả các mã trong bài viết này để theo dõi:

Nền và thiết lập

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 là một trình bao bọc Python cho thư viện SDL, viết tắt của lớp DirectMedia đơn giản. SDL cung cấp quyền truy cập đa nền tảng cho hệ thống của bạn, các thành phần phần cứng đa phương tiện cơ bản của bạn, chẳng hạn như âm thanh, video, chuột, bàn phím và cần điều khiển.
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 bắt đầu cuộc sống như một sự thay thế cho dự án PYSDL bị đình trệ. Bản chất đa nền tảng của cả SDL và
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 có nghĩa là bạn có thể viết các trò chơi và các chương trình Python đa phương tiện phong phú cho mọi nền tảng hỗ trợ chúng!Simple DirectMedia Layer. SDL provides cross-platform access to your system’s underlying multimedia hardware components, such as sound, video, mouse, keyboard, and joystick.
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 started life as a replacement for the stalled PySDL project. The cross-platform nature of both SDL and
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 means you can write games and rich multimedia Python programs for every platform that supports them!

Để cài đặt

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 trên nền tảng của bạn, hãy sử dụng lệnh
27# Variable to keep the main loop running
28running = True
29
30# Main loop
31while running:
32    # Look at every event in the queue
33    for event in pygame.event.get():
34        # Did the user hit a key?
35        if event.type == KEYDOWN:
36            # Was it the Escape key? If so, stop the loop.
37            if event.key == K_ESCAPE:
38                running = False
39
40        # Did the user click the window close button? If so, stop the loop.
41        elif event.type == QUIT:
42            running = False
3 thích hợp:

Bạn có thể xác minh cài đặt bằng cách tải một trong các ví dụ đi kèm với thư viện:

$ python3 -m pygame.examples.aliens

Nếu một cửa sổ trò chơi xuất hiện, thì

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 sẽ được cài đặt đúng cách! Nếu bạn gặp vấn đề, thì Hướng dẫn bắt đầu phác thảo một số vấn đề và cảnh báo cho tất cả các nền tảng.

Chương trình pygame cơ bản

Trước khi xuống các chi tiết cụ thể, hãy để Lôi xem một chương trình

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 cơ bản. Chương trình này tạo ra một cửa sổ, lấp đầy nền bằng màu trắng và vẽ một vòng tròn màu xanh ở giữa nó:

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()

Khi bạn chạy chương trình này, bạn sẽ thấy một cửa sổ trông như thế này:

Hướng dẫn how do i run a python game file? - làm cách nào để chạy một tệp trò chơi python?

Hãy để chia nhỏ mã này, từng phần:

  • Dòng 4 và 5 nhập và khởi tạo thư viện

     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    18
    19# Define constants for the screen width and height
    20SCREEN_WIDTH = 800
    21SCREEN_HEIGHT = 600
    22
    23# Create the screen object
    24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
    25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    
    6. Không có những dòng này, không có
     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    18
    19# Define constants for the screen width and height
    20SCREEN_WIDTH = 800
    21SCREEN_HEIGHT = 600
    22
    23# Create the screen object
    24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
    25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    
    6.
    import and initialize the
     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    18
    19# Define constants for the screen width and height
    20SCREEN_WIDTH = 800
    21SCREEN_HEIGHT = 600
    22
    23# Create the screen object
    24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
    25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    
    6 library. Without these lines, there is no
     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    18
    19# Define constants for the screen width and height
    20SCREEN_WIDTH = 800
    21SCREEN_HEIGHT = 600
    22
    23# Create the screen object
    24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
    25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    
    6.

  • Dòng 8 Thiết lập cửa sổ hiển thị chương trình của bạn. Bạn cung cấp một danh sách hoặc một tuple chỉ định chiều rộng và chiều cao của cửa sổ để tạo. Chương trình này sử dụng một danh sách để tạo một cửa sổ vuông với 500 pixel ở mỗi bên. sets up your program’s display window. You provide either a list or a tuple that specifies the width and height of the window to create. This program uses a list to create a square window with 500 pixels on each side.

  • Dòng 11 và 12 thiết lập một vòng lặp trò chơi để kiểm soát khi chương trình kết thúc. Bạn sẽ bao gồm các vòng lặp trò chơi sau này trong hướng dẫn này. set up a game loop to control when the program ends. You’ll cover game loops later on in this tutorial.

  • Các dòng 15 đến 17 Quét và xử lý các sự kiện trong vòng lặp trò chơi. Bạn cũng sẽ tham gia các sự kiện một chút. Trong trường hợp này, sự kiện duy nhất được xử lý là

    27# Variable to keep the main loop running
    28running = True
    29
    30# Main loop
    31while running:
    32    # Look at every event in the queue
    33    for event in pygame.event.get():
    34        # Did the user hit a key?
    35        if event.type == KEYDOWN:
    36            # Was it the Escape key? If so, stop the loop.
    37            if event.key == K_ESCAPE:
    38                running = False
    39
    40        # Did the user click the window close button? If so, stop the loop.
    41        elif event.type == QUIT:
    42            running = False
    
    8, xảy ra khi người dùng nhấp vào nút đóng cửa sổ. scan and handle events within the game loop. You’ll get to events a bit later as well. In this case, the only event handled is
    27# Variable to keep the main loop running
    28running = True
    29
    30# Main loop
    31while running:
    32    # Look at every event in the queue
    33    for event in pygame.event.get():
    34        # Did the user hit a key?
    35        if event.type == KEYDOWN:
    36            # Was it the Escape key? If so, stop the loop.
    37            if event.key == K_ESCAPE:
    38                running = False
    39
    40        # Did the user click the window close button? If so, stop the loop.
    41        elif event.type == QUIT:
    42            running = False
    
    8, which occurs when the user clicks the window close button.

  • Dòng 20 lấp đầy cửa sổ với một màu rắn.

    27# Variable to keep the main loop running
    28running = True
    29
    30# Main loop
    31while running:
    32    # Look at every event in the queue
    33    for event in pygame.event.get():
    34        # Did the user hit a key?
    35        if event.type == KEYDOWN:
    36            # Was it the Escape key? If so, stop the loop.
    37            if event.key == K_ESCAPE:
    38                running = False
    39
    40        # Did the user click the window close button? If so, stop the loop.
    41        elif event.type == QUIT:
    42            running = False
    
    9 chấp nhận danh sách hoặc tuple chỉ định các giá trị RGB cho màu. Vì
    44# Fill the screen with white
    45screen.fill((255, 255, 255))
    46
    47# Create a surface and pass in a tuple containing its length and width
    48surf = pygame.Surface((50, 50))
    49
    50# Give the surface a color to separate it from the background
    51surf.fill((0, 0, 0))
    52rect = surf.get_rect()
    
    0 đã được cung cấp, cửa sổ chứa đầy màu trắng.
    fills the window with a solid color.
    27# Variable to keep the main loop running
    28running = True
    29
    30# Main loop
    31while running:
    32    # Look at every event in the queue
    33    for event in pygame.event.get():
    34        # Did the user hit a key?
    35        if event.type == KEYDOWN:
    36            # Was it the Escape key? If so, stop the loop.
    37            if event.key == K_ESCAPE:
    38                running = False
    39
    40        # Did the user click the window close button? If so, stop the loop.
    41        elif event.type == QUIT:
    42            running = False
    
    9 accepts either a list or tuple specifying the RGB values for the color. Since
    44# Fill the screen with white
    45screen.fill((255, 255, 255))
    46
    47# Create a surface and pass in a tuple containing its length and width
    48surf = pygame.Surface((50, 50))
    49
    50# Give the surface a color to separate it from the background
    51surf.fill((0, 0, 0))
    52rect = surf.get_rect()
    
    0 was provided, the window is filled with white.

  • Dòng 23 vẽ một vòng tròn trong cửa sổ, sử dụng các tham số sau: draws a circle in the window, using the following parameters:

    • 44# Fill the screen with white
      45screen.fill((255, 255, 255))
      46
      47# Create a surface and pass in a tuple containing its length and width
      48surf = pygame.Surface((50, 50))
      49
      50# Give the surface a color to separate it from the background
      51surf.fill((0, 0, 0))
      52rect = surf.get_rect()
      
      1: cửa sổ để vẽ
      the window on which to draw
    • 44# Fill the screen with white
      45screen.fill((255, 255, 255))
      46
      47# Create a surface and pass in a tuple containing its length and width
      48surf = pygame.Surface((50, 50))
      49
      50# Give the surface a color to separate it from the background
      51surf.fill((0, 0, 0))
      52rect = surf.get_rect()
      
      2: Một tuple chứa các giá trị màu RGB
      a tuple containing RGB color values
    • 44# Fill the screen with white
      45screen.fill((255, 255, 255))
      46
      47# Create a surface and pass in a tuple containing its length and width
      48surf = pygame.Surface((50, 50))
      49
      50# Give the surface a color to separate it from the background
      51surf.fill((0, 0, 0))
      52rect = surf.get_rect()
      
      3: Một tuple chỉ định tọa độ trung tâm của vòng tròn
      a tuple specifying the center coordinates of the circle
    • 44# Fill the screen with white
      45screen.fill((255, 255, 255))
      46
      47# Create a surface and pass in a tuple containing its length and width
      48surf = pygame.Surface((50, 50))
      49
      50# Give the surface a color to separate it from the background
      51surf.fill((0, 0, 0))
      52rect = surf.get_rect()
      
      4: Bán kính của vòng tròn để vẽ trong pixel
      the radius of the circle to draw in pixels
  • Dòng 26 Cập nhật nội dung của màn hình lên màn hình. Không có cuộc gọi này, không có gì xuất hiện trong cửa sổ! updates the contents of the display to the screen. Without this call, nothing appears in the window!

  • Dòng 29 thoát

     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    18
    19# Define constants for the screen width and height
    20SCREEN_WIDTH = 800
    21SCREEN_HEIGHT = 600
    22
    23# Create the screen object
    24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
    25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    
    6. Điều này chỉ xảy ra khi vòng lặp kết thúc. exits
     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    18
    19# Define constants for the screen width and height
    20SCREEN_WIDTH = 800
    21SCREEN_HEIGHT = 600
    22
    23# Create the screen object
    24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
    25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    
    6. This only happens once the loop finishes.

Đó là phiên bản

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 của Hello Hello, thế giới. Bây giờ, hãy để đào sâu hơn một chút vào các khái niệm đằng sau mã này.

Khái niệm pygame

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 và thư viện SDL có thể mang tính di động trên các nền tảng và thiết bị khác nhau, cả hai đều cần xác định và làm việc với sự trừu tượng cho các thực tế phần cứng khác nhau. Hiểu những khái niệm và trừu tượng đó sẽ giúp bạn thiết kế và phát triển các trò chơi của riêng bạn.

Khởi tạo và mô -đun

Thư viện

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 bao gồm một số cấu trúc Python, bao gồm một số mô -đun khác nhau. Các mô -đun này cung cấp quyền truy cập trừu tượng vào phần cứng cụ thể trên hệ thống của bạn, cũng như các phương thức thống nhất để làm việc với phần cứng đó. Ví dụ:
44# Fill the screen with white
45screen.fill((255, 255, 255))
46
47# Create a surface and pass in a tuple containing its length and width
48surf = pygame.Surface((50, 50))
49
50# Give the surface a color to separate it from the background
51surf.fill((0, 0, 0))
52rect = surf.get_rect()
9 cho phép truy cập thống nhất vào màn hình video của bạn, trong khi
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
0 cho phép điều khiển trừu tượng vào cần điều khiển của bạn.modules. These modules provide abstract access to specific hardware on your system, as well as uniform methods to work with that hardware. For example,
44# Fill the screen with white
45screen.fill((255, 255, 255))
46
47# Create a surface and pass in a tuple containing its length and width
48surf = pygame.Surface((50, 50))
49
50# Give the surface a color to separate it from the background
51surf.fill((0, 0, 0))
52rect = surf.get_rect()
9 allows uniform access to your video display, while
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
0 allows abstract control of your joystick.

Sau khi nhập thư viện

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 trong ví dụ trên, điều đầu tiên bạn làm là khởi tạo pygame bằng
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
2. Hàm này gọi các hàm
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
3 riêng biệt của tất cả các mô -đun
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 được bao gồm. Vì các mô -đun này là trừu tượng cho phần cứng cụ thể, nên bước khởi tạo này được yêu cầu để bạn có thể làm việc với cùng một mã trên Linux, Windows và Mac.

Hiển thị và bề mặt

Ngoài các mô-đun,

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 còn bao gồm một số lớp Python, gói gọn các khái niệm phụ thuộc không phải là phần mềm. Một trong số đó là
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6, ở cơ bản nhất, định nghĩa một khu vực hình chữ nhật mà bạn có thể vẽ. Các đối tượng
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6 được sử dụng trong nhiều bối cảnh trong
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6. Sau đó, bạn sẽ thấy cách tải hình ảnh vào
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6 và hiển thị nó trên màn hình.classes, which encapsulate non-hardware dependent concepts. One of these is the
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6 which, at its most basic, defines a rectangular area on which you can draw.
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6 objects are used in many contexts in
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6. Later you’ll see how to load an image into a
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6 and display it on the screen.

Trong

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6, mọi thứ được xem trên một
44# Fill the screen with white
45screen.fill((255, 255, 255))
46
47# Create a surface and pass in a tuple containing its length and width
48surf = pygame.Surface((50, 50))
49
50# Give the surface a color to separate it from the background
51surf.fill((0, 0, 0))
52rect = surf.get_rect()
9 do người dùng tạo, có thể là một cửa sổ hoặc toàn màn hình. Màn hình được tạo bằng
54# Put the center of surf at the center of the display
55surf_center = (
56    (SCREEN_WIDTH-surf.get_width())/2,
57    (SCREEN_HEIGHT-surf.get_height())/2
58)
59
60# Draw surf at the new coordinates
61screen.blit(surf, surf_center)
62pygame.display.flip()
2, trả về
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6 đại diện cho phần có thể nhìn thấy của cửa sổ. Đây là
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6 mà bạn chuyển vào các chức năng vẽ như
54# Put the center of surf at the center of the display
55surf_center = (
56    (SCREEN_WIDTH-surf.get_width())/2,
57    (SCREEN_HEIGHT-surf.get_height())/2
58)
59
60# Draw surf at the new coordinates
61screen.blit(surf, surf_center)
62pygame.display.flip()
5 và nội dung của
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6 được đẩy đến màn hình khi bạn gọi
54# Put the center of surf at the center of the display
55surf_center = (
56    (SCREEN_WIDTH-surf.get_width())/2,
57    (SCREEN_HEIGHT-surf.get_height())/2
58)
59
60# Draw surf at the new coordinates
61screen.blit(surf, surf_center)
62pygame.display.flip()
7.

Hình ảnh và RECT

Chương trình

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 cơ bản của bạn đã vẽ một hình dạng trực tiếp lên màn hình
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6, nhưng bạn cũng có thể làm việc với hình ảnh trên đĩa. Mô -đun
20# Define a Player object by extending pygame.sprite.Sprite
21# The surface drawn on the screen is now an attribute of 'player'
22class Player(pygame.sprite.Sprite):
23    def __init__(self):
24        super(Player, self).__init__()
25        self.surf = pygame.Surface((75, 25))
26        self.surf.fill((255, 255, 255))
27        self.rect = self.surf.get_rect()
0 cho phép bạn tải và lưu hình ảnh theo nhiều định dạng phổ biến. Hình ảnh được tải vào các đối tượng
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6, sau đó có thể được thao tác và hiển thị theo nhiều cách.

Như đã đề cập ở trên, các đối tượng

54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6 được biểu diễn bằng hình chữ nhật, cũng như nhiều đối tượng khác trong
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6, chẳng hạn như hình ảnh và cửa sổ. Hình chữ nhật được sử dụng rất nhiều đến nỗi có một lớp
20# Define a Player object by extending pygame.sprite.Sprite
21# The surface drawn on the screen is now an attribute of 'player'
22class Player(pygame.sprite.Sprite):
23    def __init__(self):
24        super(Player, self).__init__()
25        self.surf = pygame.Surface((75, 25))
26        self.surf.fill((255, 255, 255))
27        self.rect = self.surf.get_rect()
4 đặc biệt chỉ để xử lý chúng. Bạn sẽ sử dụng các đối tượng và hình ảnh
20# Define a Player object by extending pygame.sprite.Sprite
21# The surface drawn on the screen is now an attribute of 'player'
22class Player(pygame.sprite.Sprite):
23    def __init__(self):
24        super(Player, self).__init__()
25        self.surf = pygame.Surface((75, 25))
26        self.surf.fill((255, 255, 255))
27        self.rect = self.surf.get_rect()
4 trong trò chơi của mình để vẽ người chơi và kẻ thù và để quản lý sự va chạm giữa chúng.

Được rồi, đó là lý thuyết đủ. Hãy để thiết kế và viết một trò chơi!

Thiết kế trò chơi cơ bản

Trước khi bạn bắt đầu viết bất kỳ mã nào, nó luôn luôn là một ý tưởng tốt để có một số thiết kế. Vì đây là một trò chơi hướng dẫn, hãy để thiết kế một số trò chơi cơ bản cho nó:

  • Mục tiêu của trò chơi là tránh những trở ngại đến:
    • Người chơi bắt đầu ở phía bên trái của màn hình.
    • Các chướng ngại vật bước vào ngẫu nhiên từ bên phải và di chuyển bên trái theo một đường thẳng.
  • Người chơi có thể di chuyển sang trái, phải, lên hoặc xuống để tránh các chướng ngại vật.
  • Người chơi không thể di chuyển khỏi màn hình.
  • Trò chơi kết thúc khi người chơi bị một chướng ngại vật tấn công hoặc khi người dùng đóng cửa sổ.

Khi anh ấy mô tả các dự án phần mềm, một đồng nghiệp cũ của tôi đã từng nói, bạn không biết những gì bạn làm cho đến khi bạn biết những gì bạn không làm. Với ý nghĩ đó, đây là một số điều mà won đã được đề cập trong hướng dẫn này:

  • Không có nhiều cuộc sống
  • Không ghi điểm
  • Không có khả năng tấn công của người chơi
  • Không có mức độ tiến bộ
  • Không có nhân vật ông chủ

Bạn có thể tự do thử sức mình để thêm các tính năng này và các tính năng khác vào chương trình của riêng bạn.

Bắt đầu nào!

Nhập và khởi tạo pygame

Sau khi bạn nhập

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6, bạn cũng sẽ cần phải khởi tạo nó. Điều này cho phép
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 kết nối các bản tóm tắt của nó với phần cứng cụ thể của bạn:

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()

Thư viện

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 xác định nhiều thứ ngoài các mô -đun và lớp học. Nó cũng xác định một số hằng số cục bộ cho những thứ như phím kẽ, chuyển động của chuột và các thuộc tính hiển thị. Bạn tham chiếu các hằng số này bằng cách sử dụng cú pháp
20# Define a Player object by extending pygame.sprite.Sprite
21# The surface drawn on the screen is now an attribute of 'player'
22class Player(pygame.sprite.Sprite):
23    def __init__(self):
24        super(Player, self).__init__()
25        self.surf = pygame.Surface((75, 25))
26        self.surf.fill((255, 255, 255))
27        self.rect = self.surf.get_rect()
9. Bằng cách nhập các hằng số cụ thể từ
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Define constants for the screen width and height
17SCREEN_WIDTH = 800
18SCREEN_HEIGHT = 600
19
20# Define a player object by extending pygame.sprite.Sprite
21# The surface drawn on the screen is now an attribute of 'player'
22class Player(pygame.sprite.Sprite):
23    def __init__(self):
24        super(Player, self).__init__()
25        self.surf = pygame.Surface((75, 25))
26        self.surf.fill((255, 255, 255))
27        self.rect = self.surf.get_rect()
28
29# Initialize pygame
30pygame.init()
31
32# Create the screen object
33# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
34screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
35
36# Instantiate player. Right now, this is just a rectangle.
37player = Player()
38
39# Variable to keep the main loop running
40running = True
41
42# Main loop
43while running:
44    # for loop through the event queue
45    for event in pygame.event.get():
46        # Check for KEYDOWN event
47        if event.type == KEYDOWN:
48            # If the Esc key is pressed, then exit the main loop
49            if event.key == K_ESCAPE:
50                running = False
51        # Check for QUIT event. If QUIT, then set running to false.
52        elif event.type == QUIT:
53            running = False
54
55    # Fill the screen with black
56    screen.fill((0, 0, 0))
57
58    # Draw the player on the screen
59    screen.blit(player.surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
60
61    # Update the display
62    pygame.display.flip()
0, bạn có thể sử dụng cú pháp
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Define constants for the screen width and height
17SCREEN_WIDTH = 800
18SCREEN_HEIGHT = 600
19
20# Define a player object by extending pygame.sprite.Sprite
21# The surface drawn on the screen is now an attribute of 'player'
22class Player(pygame.sprite.Sprite):
23    def __init__(self):
24        super(Player, self).__init__()
25        self.surf = pygame.Surface((75, 25))
26        self.surf.fill((255, 255, 255))
27        self.rect = self.surf.get_rect()
28
29# Initialize pygame
30pygame.init()
31
32# Create the screen object
33# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
34screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
35
36# Instantiate player. Right now, this is just a rectangle.
37player = Player()
38
39# Variable to keep the main loop running
40running = True
41
42# Main loop
43while running:
44    # for loop through the event queue
45    for event in pygame.event.get():
46        # Check for KEYDOWN event
47        if event.type == KEYDOWN:
48            # If the Esc key is pressed, then exit the main loop
49            if event.key == K_ESCAPE:
50                running = False
51        # Check for QUIT event. If QUIT, then set running to false.
52        elif event.type == QUIT:
53            running = False
54
55    # Fill the screen with black
56    screen.fill((0, 0, 0))
57
58    # Draw the player on the screen
59    screen.blit(player.surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
60
61    # Update the display
62    pygame.display.flip()
1 thay thế. Điều này sẽ giúp bạn tiết kiệm một số tổ hợp phím và cải thiện khả năng đọc tổng thể.

Thiết lập màn hình

Bây giờ bạn cần một cái gì đó để vẽ! Tạo một màn hình để trở thành khung vẽ tổng thể:

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

Bạn tạo màn hình để sử dụng bằng cách gọi

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Define constants for the screen width and height
17SCREEN_WIDTH = 800
18SCREEN_HEIGHT = 600
19
20# Define a player object by extending pygame.sprite.Sprite
21# The surface drawn on the screen is now an attribute of 'player'
22class Player(pygame.sprite.Sprite):
23    def __init__(self):
24        super(Player, self).__init__()
25        self.surf = pygame.Surface((75, 25))
26        self.surf.fill((255, 255, 255))
27        self.rect = self.surf.get_rect()
28
29# Initialize pygame
30pygame.init()
31
32# Create the screen object
33# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
34screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
35
36# Instantiate player. Right now, this is just a rectangle.
37player = Player()
38
39# Variable to keep the main loop running
40running = True
41
42# Main loop
43while running:
44    # for loop through the event queue
45    for event in pygame.event.get():
46        # Check for KEYDOWN event
47        if event.type == KEYDOWN:
48            # If the Esc key is pressed, then exit the main loop
49            if event.key == K_ESCAPE:
50                running = False
51        # Check for QUIT event. If QUIT, then set running to false.
52        elif event.type == QUIT:
53            running = False
54
55    # Fill the screen with black
56    screen.fill((0, 0, 0))
57
58    # Draw the player on the screen
59    screen.blit(player.surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
60
61    # Update the display
62    pygame.display.flip()
2 và truyền một bản hoặc danh sách với chiều rộng và chiều cao mong muốn. Trong trường hợp này, cửa sổ là 800x600, theo định nghĩa của các hằng số
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Define constants for the screen width and height
17SCREEN_WIDTH = 800
18SCREEN_HEIGHT = 600
19
20# Define a player object by extending pygame.sprite.Sprite
21# The surface drawn on the screen is now an attribute of 'player'
22class Player(pygame.sprite.Sprite):
23    def __init__(self):
24        super(Player, self).__init__()
25        self.surf = pygame.Surface((75, 25))
26        self.surf.fill((255, 255, 255))
27        self.rect = self.surf.get_rect()
28
29# Initialize pygame
30pygame.init()
31
32# Create the screen object
33# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
34screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
35
36# Instantiate player. Right now, this is just a rectangle.
37player = Player()
38
39# Variable to keep the main loop running
40running = True
41
42# Main loop
43while running:
44    # for loop through the event queue
45    for event in pygame.event.get():
46        # Check for KEYDOWN event
47        if event.type == KEYDOWN:
48            # If the Esc key is pressed, then exit the main loop
49            if event.key == K_ESCAPE:
50                running = False
51        # Check for QUIT event. If QUIT, then set running to false.
52        elif event.type == QUIT:
53            running = False
54
55    # Fill the screen with black
56    screen.fill((0, 0, 0))
57
58    # Draw the player on the screen
59    screen.blit(player.surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
60
61    # Update the display
62    pygame.display.flip()
3 và
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Define constants for the screen width and height
17SCREEN_WIDTH = 800
18SCREEN_HEIGHT = 600
19
20# Define a player object by extending pygame.sprite.Sprite
21# The surface drawn on the screen is now an attribute of 'player'
22class Player(pygame.sprite.Sprite):
23    def __init__(self):
24        super(Player, self).__init__()
25        self.surf = pygame.Surface((75, 25))
26        self.surf.fill((255, 255, 255))
27        self.rect = self.surf.get_rect()
28
29# Initialize pygame
30pygame.init()
31
32# Create the screen object
33# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
34screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
35
36# Instantiate player. Right now, this is just a rectangle.
37player = Player()
38
39# Variable to keep the main loop running
40running = True
41
42# Main loop
43while running:
44    # for loop through the event queue
45    for event in pygame.event.get():
46        # Check for KEYDOWN event
47        if event.type == KEYDOWN:
48            # If the Esc key is pressed, then exit the main loop
49            if event.key == K_ESCAPE:
50                running = False
51        # Check for QUIT event. If QUIT, then set running to false.
52        elif event.type == QUIT:
53            running = False
54
55    # Fill the screen with black
56    screen.fill((0, 0, 0))
57
58    # Draw the player on the screen
59    screen.blit(player.surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
60
61    # Update the display
62    pygame.display.flip()
4 trên các dòng 20 và 21. Điều này trả về một
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6 đại diện cho kích thước bên trong của cửa sổ. Đây là phần của cửa sổ bạn có thể điều khiển, trong khi HĐH điều khiển đường viền cửa sổ và thanh tiêu đề.

Nếu bạn chạy chương trình này ngay bây giờ, thì bạn sẽ thấy một cửa sổ bật lên một cách ngắn gọn và sau đó ngay lập tức biến mất khi chương trình thoát ra. Don lồng chớp mắt hoặc bạn có thể bỏ lỡ nó! Trong phần tiếp theo, bạn sẽ tập trung vào vòng lặp trò chơi chính để đảm bảo rằng chương trình của bạn chỉ thoát ra khi được cung cấp đúng.

Thiết lập vòng lặp trò chơi

Mỗi trò chơi từ Pong đến Fortnite đều sử dụng một vòng lặp trò chơi để kiểm soát lối chơi. Vòng lặp trò chơi thực hiện bốn điều rất quan trọng:

  1. Quy trình đầu vào người dùng
  2. Cập nhật trạng thái của tất cả các đối tượng trò chơi
  3. Cập nhật màn hình và đầu ra âm thanh
  4. Duy trì tốc độ của trò chơi

Mỗi chu kỳ của vòng lặp trò chơi được gọi là khung và bạn càng có thể thực hiện mọi thứ nhanh hơn mỗi chu kỳ, trò chơi của bạn sẽ chạy nhanh hơn. Các khung tiếp tục xảy ra cho đến khi một số điều kiện để thoát khỏi trò chơi được đáp ứng. Trong thiết kế của bạn, có hai điều kiện có thể kết thúc vòng lặp trò chơi:frame, and the quicker you can do things each cycle, the faster your game will run. Frames continue to occur until some condition to exit the game is met. In your design, there are two conditions that can end the game loop:

  1. Người chơi va chạm với một trở ngại. (Bạn sẽ bao gồm phát hiện va chạm sau.)
  2. Người chơi đóng cửa sổ.

Điều đầu tiên mà vòng lặp trò chơi thực hiện là quá trình đầu vào của người dùng để cho phép người chơi di chuyển xung quanh màn hình. Do đó, bạn cần một số cách để nắm bắt và xử lý một loạt các đầu vào. Bạn làm điều này bằng cách sử dụng hệ thống sự kiện

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6.

Các sự kiện xử lý

Nhấn phím, chuyển động của chuột và thậm chí các chuyển động cần điều khiển là một số cách mà người dùng có thể cung cấp đầu vào. Tất cả các kết quả đầu vào của người dùng trong một sự kiện được tạo ra. Các sự kiện có thể xảy ra bất cứ lúc nào và thường xuyên (nhưng không phải luôn luôn) bắt nguồn bên ngoài chương trình. Tất cả các sự kiện trong

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 được đặt trong hàng đợi sự kiện, sau đó có thể được truy cập và thao tác. Đối phó với các sự kiện được gọi là xử lý chúng và mã để làm như vậy được gọi là người xử lý sự kiện.handling them, and the code to do so is called an event handler.

Mỗi sự kiện trong

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 đều có một loại sự kiện được liên kết với nó. Đối với trò chơi của bạn, các loại sự kiện mà bạn tập trung vào là Keypresses và đóng cửa sổ. Các sự kiện Keypress có loại sự kiện
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Define constants for the screen width and height
17SCREEN_WIDTH = 800
18SCREEN_HEIGHT = 600
19
20# Define a player object by extending pygame.sprite.Sprite
21# The surface drawn on the screen is now an attribute of 'player'
22class Player(pygame.sprite.Sprite):
23    def __init__(self):
24        super(Player, self).__init__()
25        self.surf = pygame.Surface((75, 25))
26        self.surf.fill((255, 255, 255))
27        self.rect = self.surf.get_rect()
28
29# Initialize pygame
30pygame.init()
31
32# Create the screen object
33# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
34screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
35
36# Instantiate player. Right now, this is just a rectangle.
37player = Player()
38
39# Variable to keep the main loop running
40running = True
41
42# Main loop
43while running:
44    # for loop through the event queue
45    for event in pygame.event.get():
46        # Check for KEYDOWN event
47        if event.type == KEYDOWN:
48            # If the Esc key is pressed, then exit the main loop
49            if event.key == K_ESCAPE:
50                running = False
51        # Check for QUIT event. If QUIT, then set running to false.
52        elif event.type == QUIT:
53            running = False
54
55    # Fill the screen with black
56    screen.fill((0, 0, 0))
57
58    # Draw the player on the screen
59    screen.blit(player.surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
60
61    # Update the display
62    pygame.display.flip()
9 và sự kiện đóng cửa sổ có loại
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
00. Các loại sự kiện khác nhau cũng có thể có dữ liệu khác được liên kết với chúng. Ví dụ, loại sự kiện
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Define constants for the screen width and height
17SCREEN_WIDTH = 800
18SCREEN_HEIGHT = 600
19
20# Define a player object by extending pygame.sprite.Sprite
21# The surface drawn on the screen is now an attribute of 'player'
22class Player(pygame.sprite.Sprite):
23    def __init__(self):
24        super(Player, self).__init__()
25        self.surf = pygame.Surface((75, 25))
26        self.surf.fill((255, 255, 255))
27        self.rect = self.surf.get_rect()
28
29# Initialize pygame
30pygame.init()
31
32# Create the screen object
33# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
34screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
35
36# Instantiate player. Right now, this is just a rectangle.
37player = Player()
38
39# Variable to keep the main loop running
40running = True
41
42# Main loop
43while running:
44    # for loop through the event queue
45    for event in pygame.event.get():
46        # Check for KEYDOWN event
47        if event.type == KEYDOWN:
48            # If the Esc key is pressed, then exit the main loop
49            if event.key == K_ESCAPE:
50                running = False
51        # Check for QUIT event. If QUIT, then set running to false.
52        elif event.type == QUIT:
53            running = False
54
55    # Fill the screen with black
56    screen.fill((0, 0, 0))
57
58    # Draw the player on the screen
59    screen.blit(player.surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
60
61    # Update the display
62    pygame.display.flip()
9 cũng có một biến gọi là
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
02 để chỉ ra khóa nào được nhấn.type associated with it. For your game, the event types you’ll focus on are keypresses and window closure. Keypress events have the event type
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Define constants for the screen width and height
17SCREEN_WIDTH = 800
18SCREEN_HEIGHT = 600
19
20# Define a player object by extending pygame.sprite.Sprite
21# The surface drawn on the screen is now an attribute of 'player'
22class Player(pygame.sprite.Sprite):
23    def __init__(self):
24        super(Player, self).__init__()
25        self.surf = pygame.Surface((75, 25))
26        self.surf.fill((255, 255, 255))
27        self.rect = self.surf.get_rect()
28
29# Initialize pygame
30pygame.init()
31
32# Create the screen object
33# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
34screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
35
36# Instantiate player. Right now, this is just a rectangle.
37player = Player()
38
39# Variable to keep the main loop running
40running = True
41
42# Main loop
43while running:
44    # for loop through the event queue
45    for event in pygame.event.get():
46        # Check for KEYDOWN event
47        if event.type == KEYDOWN:
48            # If the Esc key is pressed, then exit the main loop
49            if event.key == K_ESCAPE:
50                running = False
51        # Check for QUIT event. If QUIT, then set running to false.
52        elif event.type == QUIT:
53            running = False
54
55    # Fill the screen with black
56    screen.fill((0, 0, 0))
57
58    # Draw the player on the screen
59    screen.blit(player.surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
60
61    # Update the display
62    pygame.display.flip()
9, and the window closure event has the type
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
00. Different event types may also have other data associated with them. For example, the
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Define constants for the screen width and height
17SCREEN_WIDTH = 800
18SCREEN_HEIGHT = 600
19
20# Define a player object by extending pygame.sprite.Sprite
21# The surface drawn on the screen is now an attribute of 'player'
22class Player(pygame.sprite.Sprite):
23    def __init__(self):
24        super(Player, self).__init__()
25        self.surf = pygame.Surface((75, 25))
26        self.surf.fill((255, 255, 255))
27        self.rect = self.surf.get_rect()
28
29# Initialize pygame
30pygame.init()
31
32# Create the screen object
33# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
34screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
35
36# Instantiate player. Right now, this is just a rectangle.
37player = Player()
38
39# Variable to keep the main loop running
40running = True
41
42# Main loop
43while running:
44    # for loop through the event queue
45    for event in pygame.event.get():
46        # Check for KEYDOWN event
47        if event.type == KEYDOWN:
48            # If the Esc key is pressed, then exit the main loop
49            if event.key == K_ESCAPE:
50                running = False
51        # Check for QUIT event. If QUIT, then set running to false.
52        elif event.type == QUIT:
53            running = False
54
55    # Fill the screen with black
56    screen.fill((0, 0, 0))
57
58    # Draw the player on the screen
59    screen.blit(player.surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
60
61    # Update the display
62    pygame.display.flip()
9 event type also has a variable called
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
02 to indicate which key was pressed.

Bạn truy cập danh sách tất cả các sự kiện hoạt động trong hàng đợi bằng cách gọi

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
03. Sau đó, bạn lặp qua danh sách này, kiểm tra từng loại sự kiện và trả lời phù hợp:

27# Variable to keep the main loop running
28running = True
29
30# Main loop
31while running:
32    # Look at every event in the queue
33    for event in pygame.event.get():
34        # Did the user hit a key?
35        if event.type == KEYDOWN:
36            # Was it the Escape key? If so, stop the loop.
37            if event.key == K_ESCAPE:
38                running = False
39
40        # Did the user click the window close button? If so, stop the loop.
41        elif event.type == QUIT:
42            running = False

Hãy cùng xem xét kỹ hơn về vòng lặp trò chơi này:

  • Dòng 28 Thiết lập một biến điều khiển cho vòng lặp trò chơi. Để thoát khỏi vòng lặp và trò chơi, bạn đã đặt

     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    04. Vòng lặp trò chơi bắt đầu trên dòng 29. sets up a control variable for the game loop. To exit the loop and the game, you set
     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    04. The game loop starts on line 29.

  • Dòng 31 bắt đầu người xử lý sự kiện, đi bộ qua mọi sự kiện hiện tại trong hàng đợi sự kiện. Nếu không có sự kiện, thì danh sách này trống rỗng, và người xử lý đã giành được bất cứ điều gì. starts the event handler, walking through every event currently in the event queue. If there are no events, then the list is empty, and the handler won’t do anything.

  • Dòng 35 đến 38 Kiểm tra xem

     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    05 hiện tại là một sự kiện
     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Define constants for the screen width and height
    17SCREEN_WIDTH = 800
    18SCREEN_HEIGHT = 600
    19
    20# Define a player object by extending pygame.sprite.Sprite
    21# The surface drawn on the screen is now an attribute of 'player'
    22class Player(pygame.sprite.Sprite):
    23    def __init__(self):
    24        super(Player, self).__init__()
    25        self.surf = pygame.Surface((75, 25))
    26        self.surf.fill((255, 255, 255))
    27        self.rect = self.surf.get_rect()
    28
    29# Initialize pygame
    30pygame.init()
    31
    32# Create the screen object
    33# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
    34screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    35
    36# Instantiate player. Right now, this is just a rectangle.
    37player = Player()
    38
    39# Variable to keep the main loop running
    40running = True
    41
    42# Main loop
    43while running:
    44    # for loop through the event queue
    45    for event in pygame.event.get():
    46        # Check for KEYDOWN event
    47        if event.type == KEYDOWN:
    48            # If the Esc key is pressed, then exit the main loop
    49            if event.key == K_ESCAPE:
    50                running = False
    51        # Check for QUIT event. If QUIT, then set running to false.
    52        elif event.type == QUIT:
    53            running = False
    54
    55    # Fill the screen with black
    56    screen.fill((0, 0, 0))
    57
    58    # Draw the player on the screen
    59    screen.blit(player.surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
    60
    61    # Update the display
    62    pygame.display.flip()
    
    9. Nếu có, thì chương trình kiểm tra khóa nào được nhấn bằng cách xem thuộc tính
     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    07. Nếu khóa là phím ESC, được biểu thị bằng
     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    08, thì nó thoát khỏi vòng lặp trò chơi bằng cách đặt
     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    04.
    check if the current
     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    05 is a
     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Define constants for the screen width and height
    17SCREEN_WIDTH = 800
    18SCREEN_HEIGHT = 600
    19
    20# Define a player object by extending pygame.sprite.Sprite
    21# The surface drawn on the screen is now an attribute of 'player'
    22class Player(pygame.sprite.Sprite):
    23    def __init__(self):
    24        super(Player, self).__init__()
    25        self.surf = pygame.Surface((75, 25))
    26        self.surf.fill((255, 255, 255))
    27        self.rect = self.surf.get_rect()
    28
    29# Initialize pygame
    30pygame.init()
    31
    32# Create the screen object
    33# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
    34screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    35
    36# Instantiate player. Right now, this is just a rectangle.
    37player = Player()
    38
    39# Variable to keep the main loop running
    40running = True
    41
    42# Main loop
    43while running:
    44    # for loop through the event queue
    45    for event in pygame.event.get():
    46        # Check for KEYDOWN event
    47        if event.type == KEYDOWN:
    48            # If the Esc key is pressed, then exit the main loop
    49            if event.key == K_ESCAPE:
    50                running = False
    51        # Check for QUIT event. If QUIT, then set running to false.
    52        elif event.type == QUIT:
    53            running = False
    54
    55    # Fill the screen with black
    56    screen.fill((0, 0, 0))
    57
    58    # Draw the player on the screen
    59    screen.blit(player.surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
    60
    61    # Update the display
    62    pygame.display.flip()
    
    9 event. If it is, then the program checks which key was pressed by looking at the
     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    07 attribute. If the key is the Esc key, indicated by
     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    08, then it exits the game loop by setting
     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    04.

  • Dòng 41 và 42 thực hiện kiểm tra tương tự cho loại sự kiện được gọi là

     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    00. Sự kiện này chỉ xảy ra khi người dùng nhấp vào nút đóng cửa sổ. Người dùng cũng có thể sử dụng bất kỳ hành động hệ điều hành nào khác để đóng cửa sổ. do a similar check for the event type called
     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    00. This event only occurs when the user clicks the window close button. The user may also use any other operating system action to close the window.

Khi bạn thêm các dòng này vào mã trước đó và chạy nó, bạn sẽ thấy một cửa sổ có màn hình trống hoặc đen:

Hướng dẫn how do i run a python game file? - làm cách nào để chạy một tệp trò chơi python?

Cửa sổ giành chiến thắng biến mất cho đến khi bạn nhấn phím ESC, hoặc nếu không thì kích hoạt sự kiện

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
00 bằng cách đóng cửa sổ.Esc key, or otherwise trigger a
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
00 event by closing the window.

Vẽ trên màn hình

Trong chương trình mẫu, bạn đã vẽ trên màn hình bằng hai lệnh:

  1. 27# Variable to keep the main loop running
    28running = True
    29
    30# Main loop
    31while running:
    32    # Look at every event in the queue
    33    for event in pygame.event.get():
    34        # Did the user hit a key?
    35        if event.type == KEYDOWN:
    36            # Was it the Escape key? If so, stop the loop.
    37            if event.key == K_ESCAPE:
    38                running = False
    39
    40        # Did the user click the window close button? If so, stop the loop.
    41        elif event.type == QUIT:
    42            running = False
    
    9 để lấp đầy nền
    to fill the background
  2. 54# Put the center of surf at the center of the display
    55surf_center = (
    56    (SCREEN_WIDTH-surf.get_width())/2,
    57    (SCREEN_HEIGHT-surf.get_height())/2
    58)
    59
    60# Draw surf at the new coordinates
    61screen.blit(surf, surf_center)
    62pygame.display.flip()
    
    5 để vẽ một vòng tròn
    to draw a circle

Bây giờ bạn sẽ tìm hiểu về cách thứ ba để vẽ lên màn hình: sử dụng

54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6.

Hãy nhớ lại rằng

54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6 là một đối tượng hình chữ nhật mà bạn có thể vẽ, giống như một tờ giấy trống. Đối tượng
44# Fill the screen with white
45screen.fill((255, 255, 255))
46
47# Create a surface and pass in a tuple containing its length and width
48surf = pygame.Surface((50, 50))
49
50# Give the surface a color to separate it from the background
51surf.fill((0, 0, 0))
52rect = surf.get_rect()
1 là
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6 và bạn có thể tạo các đối tượng
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6 của riêng mình tách biệt với màn hình hiển thị. Hãy để xem cách thức hoạt động của nó:

44# Fill the screen with white
45screen.fill((255, 255, 255))
46
47# Create a surface and pass in a tuple containing its length and width
48surf = pygame.Surface((50, 50))
49
50# Give the surface a color to separate it from the background
51surf.fill((0, 0, 0))
52rect = surf.get_rect()

Sau khi màn hình chứa đầy màu trắng trên dòng 45, một

54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6 mới được tạo trên dòng 48.
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6 này rộng 50 pixel, cao 50 pixel và được gán cho
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
21. Tại thời điểm này, bạn đối xử với nó giống như
44# Fill the screen with white
45screen.fill((255, 255, 255))
46
47# Create a surface and pass in a tuple containing its length and width
48surf = pygame.Surface((50, 50))
49
50# Give the surface a color to separate it from the background
51surf.fill((0, 0, 0))
52rect = surf.get_rect()
1. Vì vậy, trên đường, 51 bạn lấp đầy nó với màu đen. Bạn cũng có thể truy cập cơ bản
20# Define a Player object by extending pygame.sprite.Sprite
21# The surface drawn on the screen is now an attribute of 'player'
22class Player(pygame.sprite.Sprite):
23    def __init__(self):
24        super(Player, self).__init__()
25        self.surf = pygame.Surface((75, 25))
26        self.surf.fill((255, 255, 255))
27        self.rect = self.surf.get_rect()
4 bằng
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
24. Điều này được lưu trữ dưới dạng
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
25 để sử dụng sau.

Sử dụng 1# Simple pygame program 2 3# Import and initialize the pygame library 4import pygame 5pygame.init() 6 7# Set up the drawing window 8screen = pygame.display.set_mode([500, 500]) 9 10# Run until the user asks to quit 11running = True 12while running: 13 14 # Did the user click the window close button? 15 for event in pygame.event.get(): 16 if event.type == pygame.QUIT: 17 running = False 18 19 # Fill the background with white 20 screen.fill((255, 255, 255)) 21 22 # Draw a solid blue circle in the center 23 pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75) 24 25 # Flip the display 26 pygame.display.flip() 27 28# Done! Time to quit. 29pygame.quit() 26 và 1# Simple pygame program 2 3# Import and initialize the pygame library 4import pygame 5pygame.init() 6 7# Set up the drawing window 8screen = pygame.display.set_mode([500, 500]) 9 10# Run until the user asks to quit 11running = True 12while running: 13 14 # Did the user click the window close button? 15 for event in pygame.event.get(): 16 if event.type == pygame.QUIT: 17 running = False 18 19 # Fill the background with white 20 screen.fill((255, 255, 255)) 21 22 # Draw a solid blue circle in the center 23 pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75) 24 25 # Flip the display 26 pygame.display.flip() 27 28# Done! Time to quit. 29pygame.quit() 27

Chỉ cần tạo một

54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6 mới là đủ để xem nó trên màn hình. Để làm điều đó, bạn cần phải blit
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6 vào một
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6 khác. Thuật ngữ
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
31 là viết tắt của chuyển khối và
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
26 là cách bạn sao chép nội dung của một
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6 sang một nội dung khác. Bạn chỉ có thể
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
26 từ cái này sang cái khác, nhưng vì màn hình chỉ là một
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6 khác, nên đó không phải là vấn đề. Tại đây, cách bạn vẽ
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
21 trên màn hình:Block Transfer, and
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
26 is how you copy the contents of one
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6 to another. You can only
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
26 from one
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6 to another, but since the screen is just another
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6, that’s not a problem. Here’s how you draw
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
21 on the screen:

54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()

Cuộc gọi

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
26 trên dòng 55 có hai đối số:

  1. 54# This line says "Draw surf onto the screen at the center"
    55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
    56pygame.display.flip()
    
    6 để vẽ
  2. Vị trí để vẽ nó trên nguồn
    54# This line says "Draw surf onto the screen at the center"
    55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
    56pygame.display.flip()
    
    6

Các tọa độ

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
41 bảo chương trình của bạn đặt
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
21 vào trung tâm chính xác của màn hình, nhưng nó không trông hoàn toàn như vậy:

Hướng dẫn how do i run a python game file? - làm cách nào để chạy một tệp trò chơi python?

Lý do tại sao hình ảnh trông ngoài trung tâm là

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
26 đặt góc trên cùng bên trái của
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
21 tại vị trí được đưa ra. Nếu bạn muốn
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
21 được tập trung, thì bạn sẽ phải thực hiện một số toán học để chuyển nó lên và sang trái. Bạn có thể làm điều này bằng cách trừ đi chiều rộng và chiều cao của
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
21 từ chiều rộng và chiều cao của màn hình, chia mỗi cái cho 2 để xác định vị trí trung tâm, sau đó chuyển các số đó làm đối số đến
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
47:top-left corner of
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
21 at the location given. If you want
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
21 to be centered, then you’ll have to do some math to shift it up and to the left. You can do this by subtracting the width and height of
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
21 from the width and height of the screen, dividing each by 2 to locate the center, and then passing those numbers as arguments to
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
47:

54# Put the center of surf at the center of the display
55surf_center = (
56    (SCREEN_WIDTH-surf.get_width())/2,
57    (SCREEN_HEIGHT-surf.get_height())/2
58)
59
60# Draw surf at the new coordinates
61screen.blit(surf, surf_center)
62pygame.display.flip()

Lưu ý cuộc gọi đến

54# Put the center of surf at the center of the display
55surf_center = (
56    (SCREEN_WIDTH-surf.get_width())/2,
57    (SCREEN_HEIGHT-surf.get_height())/2
58)
59
60# Draw surf at the new coordinates
61screen.blit(surf, surf_center)
62pygame.display.flip()
7 sau cuộc gọi đến
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
49. Điều này cập nhật toàn bộ màn hình với tất cả mọi thứ mà Lừa được vẽ kể từ lần lật cuối cùng. Không có cuộc gọi đến
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
27, không có gì được hiển thị.

Sprites

Trong thiết kế trò chơi của bạn, người chơi bắt đầu ở bên trái và các chướng ngại vật đến từ bên phải. Bạn có thể đại diện cho tất cả các trở ngại với các đối tượng

54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6 để làm cho việc vẽ mọi thứ dễ dàng hơn, nhưng làm thế nào để bạn biết nơi để vẽ chúng? Làm thế nào để bạn biết nếu một trở ngại đã va chạm với người chơi? Điều gì xảy ra khi chướng ngại vật bay khỏi màn hình? Điều gì sẽ xảy ra nếu bạn muốn vẽ hình ảnh nền cũng di chuyển? Điều gì sẽ xảy ra nếu bạn muốn hình ảnh của bạn được hoạt hình? Bạn có thể xử lý tất cả các tình huống này và nhiều hơn nữa với Sprites.

Trong các thuật ngữ lập trình, một sprite là biểu diễn 2D của một cái gì đó trên màn hình. Về cơ bản, nó là một bức tranh.

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 cung cấp một lớp
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
53, được thiết kế để giữ một hoặc một số biểu diễn đồ họa của bất kỳ đối tượng trò chơi nào bạn muốn hiển thị trên màn hình. Để sử dụng nó, bạn tạo một lớp mới mở rộng
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
53. Điều này cho phép bạn sử dụng các phương pháp tích hợp của nó.sprite is a 2D representation of something on the screen. Essentially, it’s a picture.
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 provides a
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
53 class, which is designed to hold one or several graphical representations of any game object that you want to display on the screen. To use it, you create a new class that extends
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
53. This allows you to use its built-in methods.

Người chơi

Tại đây, cách bạn sử dụng các đối tượng

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
53 với trò chơi hiện tại để xác định trình phát. Chèn mã này sau dòng 18:

20# Define a Player object by extending pygame.sprite.Sprite
21# The surface drawn on the screen is now an attribute of 'player'
22class Player(pygame.sprite.Sprite):
23    def __init__(self):
24        super(Player, self).__init__()
25        self.surf = pygame.Surface((75, 25))
26        self.surf.fill((255, 255, 255))
27        self.rect = self.surf.get_rect()

Trước tiên, bạn xác định

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
56 bằng cách mở rộng
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
57 trên dòng 22. sau đó
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
58 sử dụng
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
59 để gọi phương thức
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
58 của
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
53. Để biết thêm thông tin về lý do tại sao điều này là cần thiết, bạn có thể đọc Supercharge các lớp của mình với Python Super ().

Tiếp theo, bạn xác định và khởi tạo

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
62 để giữ hình ảnh để hiển thị, hiện là hộp trắng. Bạn cũng xác định và khởi tạo
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
63, mà bạn sẽ sử dụng để vẽ người chơi sau. Để sử dụng lớp mới này, bạn cũng cần tạo một đối tượng mới và cũng thay đổi mã bản vẽ. Mở rộng khối mã bên dưới để xem tất cả cùng nhau:

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Define constants for the screen width and height
17SCREEN_WIDTH = 800
18SCREEN_HEIGHT = 600
19
20# Define a player object by extending pygame.sprite.Sprite
21# The surface drawn on the screen is now an attribute of 'player'
22class Player(pygame.sprite.Sprite):
23    def __init__(self):
24        super(Player, self).__init__()
25        self.surf = pygame.Surface((75, 25))
26        self.surf.fill((255, 255, 255))
27        self.rect = self.surf.get_rect()
28
29# Initialize pygame
30pygame.init()
31
32# Create the screen object
33# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
34screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
35
36# Instantiate player. Right now, this is just a rectangle.
37player = Player()
38
39# Variable to keep the main loop running
40running = True
41
42# Main loop
43while running:
44    # for loop through the event queue
45    for event in pygame.event.get():
46        # Check for KEYDOWN event
47        if event.type == KEYDOWN:
48            # If the Esc key is pressed, then exit the main loop
49            if event.key == K_ESCAPE:
50                running = False
51        # Check for QUIT event. If QUIT, then set running to false.
52        elif event.type == QUIT:
53            running = False
54
55    # Fill the screen with black
56    screen.fill((0, 0, 0))
57
58    # Draw the player on the screen
59    screen.blit(player.surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
60
61    # Update the display
62    pygame.display.flip()

Chạy mã này. Bạn sẽ thấy một hình chữ nhật màu trắng ở khoảng giữa màn hình:

Hướng dẫn how do i run a python game file? - làm cách nào để chạy một tệp trò chơi python?

Bạn nghĩ điều gì sẽ xảy ra nếu bạn thay đổi dòng 59 thành

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
64? Hãy thử nó và xem:

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
0

Khi bạn chuyển

20# Define a Player object by extending pygame.sprite.Sprite
21# The surface drawn on the screen is now an attribute of 'player'
22class Player(pygame.sprite.Sprite):
23    def __init__(self):
24        super(Player, self).__init__()
25        self.surf = pygame.Surface((75, 25))
26        self.surf.fill((255, 255, 255))
27        self.rect = self.surf.get_rect()
4 đến
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
26, nó sử dụng tọa độ của góc trên bên trái để vẽ bề mặt. Bạn sẽ sử dụng điều này sau để làm cho người chơi của bạn di chuyển!

Đầu vào người dùng

Cho đến nay, bạn đã học được cách thiết lập

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 và vẽ các đối tượng trên màn hình. Bây giờ, niềm vui thực sự bắt đầu! Bạn sẽ làm cho người chơi có thể điều khiển bằng bàn phím.

Trước đó, bạn đã thấy rằng

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
03 trả về một danh sách các sự kiện trong hàng đợi sự kiện mà bạn quét cho các loại sự kiện
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Define constants for the screen width and height
17SCREEN_WIDTH = 800
18SCREEN_HEIGHT = 600
19
20# Define a player object by extending pygame.sprite.Sprite
21# The surface drawn on the screen is now an attribute of 'player'
22class Player(pygame.sprite.Sprite):
23    def __init__(self):
24        super(Player, self).__init__()
25        self.surf = pygame.Surface((75, 25))
26        self.surf.fill((255, 255, 255))
27        self.rect = self.surf.get_rect()
28
29# Initialize pygame
30pygame.init()
31
32# Create the screen object
33# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
34screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
35
36# Instantiate player. Right now, this is just a rectangle.
37player = Player()
38
39# Variable to keep the main loop running
40running = True
41
42# Main loop
43while running:
44    # for loop through the event queue
45    for event in pygame.event.get():
46        # Check for KEYDOWN event
47        if event.type == KEYDOWN:
48            # If the Esc key is pressed, then exit the main loop
49            if event.key == K_ESCAPE:
50                running = False
51        # Check for QUIT event. If QUIT, then set running to false.
52        elif event.type == QUIT:
53            running = False
54
55    # Fill the screen with black
56    screen.fill((0, 0, 0))
57
58    # Draw the player on the screen
59    screen.blit(player.surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
60
61    # Update the display
62    pygame.display.flip()
9. Chà, đó không phải là cách duy nhất để đọc Keypresses.
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 cũng cung cấp
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
71, trả về một từ điển chứa tất cả các sự kiện
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Define constants for the screen width and height
17SCREEN_WIDTH = 800
18SCREEN_HEIGHT = 600
19
20# Define a player object by extending pygame.sprite.Sprite
21# The surface drawn on the screen is now an attribute of 'player'
22class Player(pygame.sprite.Sprite):
23    def __init__(self):
24        super(Player, self).__init__()
25        self.surf = pygame.Surface((75, 25))
26        self.surf.fill((255, 255, 255))
27        self.rect = self.surf.get_rect()
28
29# Initialize pygame
30pygame.init()
31
32# Create the screen object
33# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
34screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
35
36# Instantiate player. Right now, this is just a rectangle.
37player = Player()
38
39# Variable to keep the main loop running
40running = True
41
42# Main loop
43while running:
44    # for loop through the event queue
45    for event in pygame.event.get():
46        # Check for KEYDOWN event
47        if event.type == KEYDOWN:
48            # If the Esc key is pressed, then exit the main loop
49            if event.key == K_ESCAPE:
50                running = False
51        # Check for QUIT event. If QUIT, then set running to false.
52        elif event.type == QUIT:
53            running = False
54
55    # Fill the screen with black
56    screen.fill((0, 0, 0))
57
58    # Draw the player on the screen
59    screen.blit(player.surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
60
61    # Update the display
62    pygame.display.flip()
9 hiện tại trong hàng đợi.

Đặt điều này trong vòng lặp trò chơi của bạn ngay sau khi vòng xử lý sự kiện. Điều này trả về một từ điển chứa các phím được nhấn ở đầu mỗi khung hình:

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
1

Tiếp theo, bạn viết một phương thức trong

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
56 để chấp nhận từ điển đó. Điều này sẽ xác định hành vi của sprite dựa trên các khóa được nhấn. Ở đây, những gì có thể trông như thế nào:

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
2

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
74,
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
75,
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
76 và
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
77 tương ứng với các phím mũi tên trên bàn phím. Nếu mục từ điển cho khóa đó là
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
78, thì khóa đó đã giảm và bạn di chuyển trình phát
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
63 theo hướng thích hợp. Ở đây bạn sử dụng
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
80, là viết tắt của di chuyển tại chỗ, để di chuyển
20# Define a Player object by extending pygame.sprite.Sprite
21# The surface drawn on the screen is now an attribute of 'player'
22class Player(pygame.sprite.Sprite):
23    def __init__(self):
24        super(Player, self).__init__()
25        self.surf = pygame.Surface((75, 25))
26        self.surf.fill((255, 255, 255))
27        self.rect = self.surf.get_rect()
4 hiện tại.move in place, to move the current
20# Define a Player object by extending pygame.sprite.Sprite
21# The surface drawn on the screen is now an attribute of 'player'
22class Player(pygame.sprite.Sprite):
23    def __init__(self):
24        super(Player, self).__init__()
25        self.surf = pygame.Surface((75, 25))
26        self.surf.fill((255, 255, 255))
27        self.rect = self.surf.get_rect()
4.

Sau đó, bạn có thể gọi

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
82 Mỗi khung hình để di chuyển Sprite của người chơi để đáp ứng với Keypresses. Thêm cuộc gọi này ngay sau cuộc gọi đến
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
83:

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
3

Bây giờ bạn có thể di chuyển hình chữ nhật trình phát của mình xung quanh màn hình với các phím mũi tên:

Hướng dẫn how do i run a python game file? - làm cách nào để chạy một tệp trò chơi python?

Bạn có thể nhận thấy hai vấn đề nhỏ:

  1. Hình chữ nhật người chơi có thể di chuyển rất nhanh nếu một khóa được giữ xuống. Bạn sẽ làm việc về điều đó sau.
  2. Hình chữ nhật người chơi có thể di chuyển khỏi màn hình. Hãy để giải quyết vấn đề đó bây giờ.

Để giữ trình phát trên màn hình, bạn cần thêm một số logic để phát hiện nếu

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
25 sẽ di chuyển khỏi màn hình. Để làm điều đó, bạn kiểm tra xem các tọa độ
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
25 có di chuyển ra ngoài ranh giới màn hình hay không. Nếu vậy, sau đó bạn hướng dẫn chương trình di chuyển nó trở lại cạnh:

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
4

Ở đây, thay vì sử dụng

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
86, bạn chỉ cần thay đổi tọa độ tương ứng của
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
87,
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
88,
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
89 hoặc
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
90 trực tiếp. Kiểm tra điều này và bạn sẽ tìm thấy hình chữ nhật trình phát không còn có thể di chuyển khỏi màn hình.

Bây giờ hãy để thêm một số kẻ thù!

Kẻ thù

Những gì một trò chơi không có kẻ thù? Bạn sẽ sử dụng các kỹ thuật mà bạn đã học để tạo ra một lớp kẻ thù cơ bản, sau đó tạo ra rất nhiều trong số chúng cho người chơi của bạn để tránh. Đầu tiên, nhập thư viện

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
91:

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
5

Sau đó, tạo một lớp sprite mới được gọi là

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
92, theo cùng một mẫu bạn đã sử dụng cho
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
56:

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
6

Có bốn sự khác biệt đáng chú ý giữa

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
92 và
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
56:

  1. Trên các dòng 62 đến 67, bạn cập nhật

     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    25 để trở thành một vị trí ngẫu nhiên dọc theo cạnh phải của màn hình. Trung tâm của hình chữ nhật chỉ ra khỏi màn hình. Nó nằm ở một vị trí nào đó từ 20 đến 100 pixel từ cạnh phải và ở đâu đó giữa các cạnh trên và dưới., you update
     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    25 to be a random location along the right edge of the screen. The center of the rectangle is just off the screen. It’s located at some position between 20 and 100 pixels away from the right edge, and somewhere between the top and bottom edges.

  2. Trên dòng 68, bạn xác định

     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    97 là một số ngẫu nhiên trong khoảng từ 5 đến 20. Điều này chỉ định mức độ nhanh chóng của kẻ thù di chuyển về phía người chơi., you define
     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    97 as a random number between 5 and 20. This specifies how fast this enemy moves towards the player.

  3. Trên các dòng 73 đến 76, bạn xác định

     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    82. Nó không có đối số vì kẻ thù tự động di chuyển. Thay vào đó,
     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    82 di chuyển kẻ thù về phía bên trái của màn hình tại
     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    97 được xác định khi nó được tạo ra.
    , you define
     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    82. It takes no arguments since enemies move automatically. Instead,
     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    82 moves the enemy toward the left side of the screen at the
     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    97 defined when it was created.

  4. Trên dòng 74, bạn kiểm tra xem kẻ thù có di chuyển ngoài màn hình hay không. Để đảm bảo

     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    92 hoàn toàn ra khỏi màn hình và won chỉ biến mất trong khi nó vẫn còn nhìn thấy được, bạn kiểm tra xem bên phải của
     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    63 đã đi qua phía bên trái của màn hình. Khi kẻ thù ngoài màn hình, bạn gọi
     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    
    03 để ngăn chặn nó được xử lý thêm.
    , you check whether the enemy has moved off-screen. To make sure the
     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    92 is fully off the screen and won’t just disappear while it’s still visible, you check that the right side of the
     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    63 has gone past the left side of the screen. Once the enemy is off-screen, you call
     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    
    03 to prevent it from being processed further.

Vì vậy,

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
03 làm gì? Để tìm ra điều này, bạn phải biết về các nhóm Sprite.Sprite Groups.

Nhóm sprite

Một lớp siêu hữu ích khác mà

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 cung cấp là
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
06. Đây là một đối tượng chứa một nhóm các đối tượng
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
53. Vậy tại sao sử dụng nó? Thay vào đó, bạn có thể theo dõi các đối tượng
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
53 của mình trong danh sách không? Chà, bạn có thể, nhưng lợi thế của việc sử dụng
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
09 nằm trong các phương pháp mà nó phơi bày. Các phương pháp này giúp phát hiện xem bất kỳ
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
92 nào đã va chạm với
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
56, giúp cập nhật dễ dàng hơn nhiều.

Hãy cùng xem cách tạo các nhóm sprite. Bạn sẽ tạo hai đối tượng

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
09 khác nhau:

  1.  1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    
    09 đầu tiên sẽ giữ mỗi
     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    53 trong trò chơi.
  2.  1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    
    09 thứ hai sẽ chỉ giữ các đối tượng
     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    92.

Ở đây, những gì trông giống như trong mã:

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
7

Khi bạn gọi

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
03,
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
53 sẽ bị xóa khỏi mỗi
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
09 mà nó thuộc về. Điều này cũng loại bỏ các tham chiếu đến
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
53, cho phép người thu gom rác Python, lấy lại bộ nhớ khi cần thiết.

Bây giờ bạn có một nhóm

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
21, bạn có thể thay đổi cách vẽ các đối tượng. Thay vì gọi
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
26 chỉ vào
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
56, bạn có thể lặp lại mọi thứ trong
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
21:

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
8

Bây giờ, bất cứ điều gì được đưa vào

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
21 sẽ được rút ra với mọi khung hình, cho dù đó là kẻ thù hay người chơi.

Có một vấn đề chỉ có một vấn đề mà bạn không có kẻ thù! Bạn có thể tạo ra một loạt kẻ thù khi bắt đầu trò chơi, nhưng trò chơi sẽ nhanh chóng trở nên nhàm chán khi tất cả chúng rời khỏi màn hình vài giây sau đó. Thay vào đó, hãy để khám phá cách giữ nguồn cung kẻ thù ổn định khi trò chơi tiến triển.

Sự kiện tùy chỉnh

Thiết kế kêu gọi kẻ thù xuất hiện đều đặn. Điều này có nghĩa là trong các khoảng thời gian đã thiết lập, bạn cần phải làm hai điều:

  1. Tạo một
     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    92 mới.
  2. Thêm nó vào
     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    
    21 và
     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    
    28.

Bạn đã có mã xử lý các sự kiện ngẫu nhiên. Vòng lặp sự kiện được thiết kế để tìm kiếm các sự kiện ngẫu nhiên xảy ra mọi khung hình và đối phó với chúng một cách thích hợp. May mắn thay,

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 không giới hạn bạn chỉ sử dụng các loại sự kiện mà nó đã xác định. Bạn có thể xác định các sự kiện của riêng bạn để xử lý khi bạn thấy phù hợp.

Hãy cùng xem cách tạo ra một sự kiện tùy chỉnh mà LỚN tạo ra cứ sau vài giây. Bạn có thể tạo một sự kiện tùy chỉnh bằng cách đặt tên cho nó:

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
9

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 định nghĩa các sự kiện trong nội bộ là số nguyên, vì vậy bạn cần xác định một sự kiện mới với một số nguyên duy nhất. Dự trữ sự kiện cuối cùng
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 được gọi là
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
32, do đó, xác định
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
33 trên dòng 83 đảm bảo nó độc đáo.

Tiếp theo, bạn cần chèn sự kiện mới này vào hàng đợi sự kiện đều đặn trong suốt trò chơi. Đó là nơi mà mô -đun

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
34 xuất hiện. Dòng 84 đốt cháy sự kiện
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
35 mới cứ sau 250 mili giây, hoặc bốn lần mỗi giây. Bạn gọi
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
36 bên ngoài vòng lặp trò chơi vì bạn chỉ cần một bộ đếm thời gian, nhưng nó sẽ bắn trong toàn bộ trò chơi.

Thêm mã để xử lý sự kiện mới của bạn:

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
0

Bất cứ khi nào trình xử lý sự kiện nhìn thấy sự kiện

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
35 mới trên dòng 115, nó sẽ tạo ra một
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
92 và thêm nó vào
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
28 và
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
21. Vì
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
92 nằm trong
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
21, nó sẽ được vẽ mọi khung hình. Bạn cũng cần gọi
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
43 trên dòng 126, cập nhật mọi thứ trong
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
28, để đảm bảo chúng di chuyển đúng cách:

Hướng dẫn how do i run a python game file? - làm cách nào để chạy một tệp trò chơi python?

Tuy nhiên, đó không phải là lý do duy nhất có một nhóm chỉ với

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
28.

Phát hiện va chạm

Thiết kế trò chơi của bạn kêu gọi trò chơi kết thúc bất cứ khi nào kẻ thù va chạm với người chơi. Kiểm tra va chạm là một kỹ thuật cơ bản của lập trình trò chơi và thường yêu cầu một số toán học không tầm thường để xác định xem hai sprites có trùng nhau hay không.

Đây là nơi một khung như

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 có ích! Viết mã phát hiện va chạm là tẻ nhạt, nhưng
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 có rất nhiều phương pháp phát hiện va chạm có sẵn để bạn sử dụng.

Đối với hướng dẫn này, bạn sẽ sử dụng một phương thức gọi là

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
48, được đọc là Sprite Sprite va chạm bất kỳ. Phương pháp này chấp nhận
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
53 và
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
09 làm tham số. Nó nhìn vào mọi đối tượng trong
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
09 và kiểm tra xem
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
63 của nó có giao nhau với
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
63 của
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
53 không. Nếu vậy, thì nó trả về
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
78. Nếu không, nó trả về
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
56. Điều này là hoàn hảo cho trò chơi này vì bạn cần kiểm tra xem
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
57 có va chạm với một trong số
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
09 của
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
28 không.

Ở đây, những gì trông giống như trong mã:

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
1

Dòng 135 kiểm tra xem

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
57 có va chạm với bất kỳ đối tượng nào trong
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
28 hay không. Nếu vậy,
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
62 được gọi để loại bỏ nó khỏi mọi nhóm mà nó thuộc về. Vì các đối tượng duy nhất được hiển thị là trong
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
21,
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
57 sẽ không còn được hiển thị nữa. Khi người chơi đã bị giết, bạn cũng cần thoát khỏi trò chơi, vì vậy bạn đã đặt
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
04 để thoát khỏi vòng lặp trò chơi trên dòng 138.

Tại thời điểm này, bạn đã có các yếu tố cơ bản của một trò chơi tại chỗ:

Hướng dẫn how do i run a python game file? - làm cách nào để chạy một tệp trò chơi python?

Bây giờ, hãy để Lôi ăn mặc nó một chút, làm cho nó dễ chơi hơn và thêm một số khả năng nâng cao để giúp nó nổi bật.

Hình ảnh sprite

Được rồi, bạn có một trò chơi, nhưng hãy để thành thật mà nói, đó là loại xấu xí. Người chơi và kẻ thù chỉ là những khối trắng trên nền đen. Đó là hiện đại khi Pong mới, nhưng nó không cắt nó nữa. Hãy để thay thế tất cả những hình chữ nhật màu trắng nhàm chán bằng một số hình ảnh mát hơn sẽ làm cho trò chơi cảm thấy như một trò chơi thực tế.

Trước đó, bạn đã học được rằng hình ảnh trên đĩa có thể được tải vào

54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6 với một số trợ giúp từ mô -đun
20# Define a Player object by extending pygame.sprite.Sprite
21# The surface drawn on the screen is now an attribute of 'player'
22class Player(pygame.sprite.Sprite):
23    def __init__(self):
24        super(Player, self).__init__()
25        self.surf = pygame.Surface((75, 25))
26        self.surf.fill((255, 255, 255))
27        self.rect = self.surf.get_rect()
0. Đối với hướng dẫn này, chúng tôi đã tạo ra một máy bay phản lực nhỏ cho người chơi và một số tên lửa cho kẻ thù. Bạn có thể sử dụng nghệ thuật này, vẽ của riêng bạn hoặc tải xuống một số tài sản nghệ thuật trò chơi miễn phí để sử dụng. Bạn có thể nhấp vào liên kết bên dưới để tải xuống nghệ thuật được sử dụng trong hướng dẫn này:

Thay đổi các hàm tạo đối tượng

Trước khi bạn sử dụng hình ảnh để đại diện cho người chơi và kẻ thù, bạn cần thực hiện một số thay đổi cho các hàm tạo của chúng. Mã bên dưới thay thế mã được sử dụng trước đó:

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
2

Hãy để dòng giải nén dòng 31 một chút.

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
68 tải một hình ảnh từ đĩa. Bạn chuyển nó một đường dẫn đến tệp. Nó trả về một
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6 và cuộc gọi
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
70 tối ưu hóa
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6, làm cho các cuộc gọi
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
26 trong tương lai nhanh hơn.

Dòng 32 sử dụng

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
73 để chỉ màu
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 sẽ hiển thị trong suốt. Trong trường hợp này, bạn chọn màu trắng, bởi vì đó là màu nền của hình ảnh máy bay phản lực. Hằng số RLEAccel là một tham số tùy chọn giúp
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 hiển thị nhanh hơn trên các màn hình không tăng tốc. Điều này được thêm vào câu lệnh nhập
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Define constants for the screen width and height
17SCREEN_WIDTH = 800
18SCREEN_HEIGHT = 600
19
20# Define a player object by extending pygame.sprite.Sprite
21# The surface drawn on the screen is now an attribute of 'player'
22class Player(pygame.sprite.Sprite):
23    def __init__(self):
24        super(Player, self).__init__()
25        self.surf = pygame.Surface((75, 25))
26        self.surf.fill((255, 255, 255))
27        self.rect = self.surf.get_rect()
28
29# Initialize pygame
30pygame.init()
31
32# Create the screen object
33# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
34screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
35
36# Instantiate player. Right now, this is just a rectangle.
37player = Player()
38
39# Variable to keep the main loop running
40running = True
41
42# Main loop
43while running:
44    # for loop through the event queue
45    for event in pygame.event.get():
46        # Check for KEYDOWN event
47        if event.type == KEYDOWN:
48            # If the Esc key is pressed, then exit the main loop
49            if event.key == K_ESCAPE:
50                running = False
51        # Check for QUIT event. If QUIT, then set running to false.
52        elif event.type == QUIT:
53            running = False
54
55    # Fill the screen with black
56    screen.fill((0, 0, 0))
57
58    # Draw the player on the screen
59    screen.blit(player.surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
60
61    # Update the display
62    pygame.display.flip()
0 trên dòng 11.

Không có gì khác cần phải thay đổi. Hình ảnh vẫn là một

54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6, ngoại trừ bây giờ nó có một hình ảnh được vẽ trên đó. Bạn vẫn sử dụng nó theo cùng một cách.

Ở đây, những thay đổi tương tự như

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
92 trông giống như:

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
3

Chạy chương trình bây giờ sẽ cho thấy đây là cùng một trò chơi bạn có trước đây, ngoại trừ bây giờ bạn đã thêm một số skin đồ họa đẹp với hình ảnh. Nhưng tại sao dừng lại chỉ làm cho người chơi và kẻ thù trông đẹp? Hãy để thêm một vài đám mây đi qua để tạo ấn tượng về một chiếc máy bay phản lực bay trên bầu trời.skins with images. But why stop at just making the player and enemy sprites look nice? Let’s add a few clouds going past to give the impression of a jet flying through the sky.

Thêm hình ảnh nền

Đối với các đám mây nền, bạn sử dụng các nguyên tắc tương tự như bạn đã làm cho

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
56 và
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
92:

  1. Tạo lớp
     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    
    81.
  2. Thêm một hình ảnh của một đám mây vào nó.
  3. Tạo một phương thức
     1# Simple pygame program
     2
     3# Import and initialize the pygame library
     4import pygame
     5pygame.init()
     6
     7# Set up the drawing window
     8screen = pygame.display.set_mode([500, 500])
     9
    10# Run until the user asks to quit
    11running = True
    12while running:
    13
    14    # Did the user click the window close button?
    15    for event in pygame.event.get():
    16        if event.type == pygame.QUIT:
    17            running = False
    18
    19    # Fill the background with white
    20    screen.fill((255, 255, 255))
    21
    22    # Draw a solid blue circle in the center
    23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
    24
    25    # Flip the display
    26    pygame.display.flip()
    27
    28# Done! Time to quit.
    29pygame.quit()
    
    82 di chuyển
     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    
    83 về phía bên trái của màn hình.
  4. Tạo một sự kiện tùy chỉnh và trình xử lý để tạo các đối tượng
     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    
    83 mới trong một khoảng thời gian đã đặt.
  5. Thêm các đối tượng
     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    
    83 mới được tạo vào một ____209 mới được gọi là
     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    
    87.
  6. Cập nhật và vẽ
     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    
    87 trong vòng lặp trò chơi của bạn.

Ở đây, những gì

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
81 trông giống như:

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
4

Tất cả sẽ trông rất quen thuộc. Nó rất giống với

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
92.

Để có các đám mây xuất hiện trong các khoảng thời gian nhất định, bạn sẽ sử dụng mã tạo sự kiện tương tự như những gì bạn đã sử dụng để tạo kẻ thù mới. Đặt nó ngay bên dưới sự kiện sáng tạo của kẻ thù:

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
5

Điều này nói rằng chờ 1000 mili giây, hoặc một giây, trước khi tạo ra

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
83 tiếp theo.

Tiếp theo, hãy tạo một

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
09 mới để giữ mỗi
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
83 mới được tạo:

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
6

Tiếp theo, thêm một trình xử lý cho sự kiện

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
94 mới trong trình xử lý sự kiện:

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
7

Cuối cùng, hãy đảm bảo

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
87 được cập nhật mọi khung hình:

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
8

Dòng 172 cập nhật

27# Variable to keep the main loop running
28running = True
29
30# Main loop
31while running:
32    # Look at every event in the queue
33    for event in pygame.event.get():
34        # Did the user hit a key?
35        if event.type == KEYDOWN:
36            # Was it the Escape key? If so, stop the loop.
37            if event.key == K_ESCAPE:
38                running = False
39
40        # Did the user click the window close button? If so, stop the loop.
41        elif event.type == QUIT:
42            running = False
9 ban đầu để lấp đầy màn hình với màu xanh da trời dễ chịu. Bạn có thể thay đổi màu này thành một thứ khác. Có thể bạn muốn một thế giới ngoài hành tinh với bầu trời tím, một vùng đất hoang độc màu xanh neon hoặc bề mặt của sao Hỏa màu đỏ!

Lưu ý rằng mỗi

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
81 và
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
92 mới được thêm vào
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
21 cũng như
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
87 và
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
28. Điều này được thực hiện bởi vì mỗi nhóm được sử dụng cho một mục đích riêng biệt:

  • Kết xuất được thực hiện bằng cách sử dụng
     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    
    21.
    is done using
     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    
    21.
  • Cập nhật vị trí được thực hiện bằng cách sử dụng
     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    
    87 và
     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    
    28.
    are done using
     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    
    87 and
     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    
    28.
  • Phát hiện va chạm được thực hiện bằng cách sử dụng
     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    
    28.
    is done using
     1# Import the pygame module
     2import pygame
     3
     4# Import pygame.locals for easier access to key coordinates
     5# Updated to conform to flake8 and black standards
     6from pygame.locals import (
     7    K_UP,
     8    K_DOWN,
     9    K_LEFT,
    10    K_RIGHT,
    11    K_ESCAPE,
    12    KEYDOWN,
    13    QUIT,
    14)
    15
    16# Initialize pygame
    17pygame.init()
    
    28.

Bạn tạo ra nhiều nhóm để bạn có thể thay đổi cách các Sprites di chuyển hoặc hành xử mà không ảnh hưởng đến chuyển động hoặc hành vi của các Sprites khác.

Tốc độ trò chơi

Trong khi thử nghiệm trò chơi, bạn có thể nhận thấy rằng kẻ thù di chuyển nhanh một chút. Nếu không, thì điều đó ổn, vì các máy khác nhau sẽ thấy kết quả khác nhau tại thời điểm này.

Lý do cho điều này là các vòng lặp trò chơi xử lý các khung nhanh như bộ xử lý và môi trường sẽ cho phép. Vì tất cả các sprites di chuyển một lần mỗi khung hình, chúng có thể di chuyển hàng trăm lần mỗi giây. Số lượng khung được xử lý mỗi giây được gọi là tốc độ khung hình và việc có quyền này là sự khác biệt giữa một trò chơi có thể chơi được và một trò chơi đáng quên.frame rate, and getting this right is the difference between a playable game and a forgettable one.

Thông thường, bạn muốn tốc độ khung hình cao nhất có thể, nhưng đối với trò chơi này, bạn cần làm chậm nó một chút để trò chơi có thể chơi được. May mắn thay, mô -đun

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
34 chứa
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
07 được thiết kế chính xác cho mục đích này.

Sử dụng

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
07 để thiết lập tốc độ khung hình có thể chơi chỉ cần hai dòng mã. Lần đầu tiên tạo ra một
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
07 mới trước khi vòng lặp trò chơi bắt đầu:

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
9

Cuộc gọi thứ hai

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
10 để thông báo cho
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 rằng chương trình đã kết thúc khung hình:

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
0

Đối số được chuyển cho

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
10 thiết lập tốc độ khung hình mong muốn. Để làm điều này,
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
10 tính toán số lượng mili giây mà mỗi khung hình nên thực hiện, dựa trên tốc độ khung hình mong muốn. Sau đó, nó so sánh con số đó với số mili giây đã trôi qua kể từ lần cuối
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
10 được gọi. Nếu không đủ thời gian đã trôi qua, thì
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
10 chậm trễ xử lý để đảm bảo rằng nó không bao giờ vượt quá tốc độ khung hình được chỉ định.

Vượt qua tốc độ khung hình nhỏ hơn sẽ dẫn đến nhiều thời gian hơn trong mỗi khung để tính toán, trong khi tốc độ khung hình lớn hơn cung cấp lối chơi mượt mà hơn (và có thể nhanh hơn):

Hướng dẫn how do i run a python game file? - làm cách nào để chạy một tệp trò chơi python?

Chơi xung quanh với con số này để xem những gì cảm thấy tốt nhất cho bạn!

Hiệu ứng âm thanh

Cho đến nay, bạn đã tập trung vào lối chơi và các khía cạnh trực quan của trò chơi của bạn. Bây giờ, hãy để khám phá cho trò chơi của bạn một số hương vị thính giác là tốt.

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 cung cấp
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
17 để xử lý tất cả các hoạt động liên quan đến âm thanh. Bạn sẽ sử dụng các lớp và phương pháp mô -đun này để cung cấp các hiệu ứng âm nhạc và âm thanh nền cho các hành động khác nhau.

Tên

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
17 đề cập đến thực tế là mô -đun trộn các âm thanh khác nhau thành một tổng thể gắn kết. Sử dụng mô hình phụ
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
19, bạn có thể truyền phát các tệp âm thanh riêng lẻ theo nhiều định dạng khác nhau, chẳng hạn như MP3, OGG và MOD. Bạn cũng có thể sử dụng
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
20 để giữ một hiệu ứng âm thanh duy nhất được phát, ở các định dạng WAV OGG hoặc không nén. Tất cả việc phát lại xảy ra trong nền, vì vậy khi bạn chơi
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
20, phương thức quay lại ngay lập tức khi âm thanh phát.

Như với hầu hết mọi thứ

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6, sử dụng
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
17 bắt đầu với bước khởi tạo. May mắn thay, điều này đã được xử lý bởi
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
2. Bạn chỉ cần gọi
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
25 nếu bạn muốn thay đổi mặc định:

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
1

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
25 chấp nhận một số đối số, nhưng mặc định hoạt động tốt trong hầu hết các trường hợp. Lưu ý rằng nếu bạn muốn thay đổi mặc định, bạn cần gọi
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
25 trước khi gọi
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
2. Nếu không, mặc định sẽ có hiệu lực bất kể thay đổi của bạn.

Sau khi hệ thống được khởi tạo, bạn có thể thiết lập âm thanh và âm nhạc nền:

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
2

Dòng 138 và 139 Tải một clip âm thanh nền và bắt đầu chơi nó. Bạn có thể nói với clip âm thanh để lặp và không bao giờ kết thúc bằng cách đặt tham số được đặt tên

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
29. load a background sound clip and begin playing it. You can tell the sound clip to loop and never end by setting the named parameter
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
29.

Các dòng 143 đến 145 Tải ba âm thanh mà bạn sẽ sử dụng cho các hiệu ứng âm thanh khác nhau. Hai cái đầu tiên đang tăng và âm thanh, được chơi khi người chơi di chuyển lên hoặc xuống. Cuối cùng là âm thanh được sử dụng bất cứ khi nào có va chạm. Bạn cũng có thể thêm các âm thanh khác, chẳng hạn như âm thanh cho bất cứ khi nào

 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
92 được tạo hoặc âm thanh cuối cùng khi trò chơi kết thúc. load three sounds you’ll use for various sound effects. The first two are rising and falling sounds, which are played when the player moves up or down. The last is the sound used whenever there is a collision. You can add other sounds as well, such as a sound for whenever an
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
92 is created, or a final sound for when the game ends.

Vì vậy, làm thế nào để bạn sử dụng các hiệu ứng âm thanh? Bạn muốn phát mỗi âm thanh khi một sự kiện nhất định xảy ra. Ví dụ: khi con tàu di chuyển lên, bạn muốn chơi

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
31. Do đó, bạn thêm một cuộc gọi đến
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
32 bất cứ khi nào bạn xử lý sự kiện đó. Trong thiết kế, điều đó có nghĩa là thêm các cuộc gọi sau vào
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
82 cho
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
56:

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
3

Để va chạm giữa người chơi và kẻ thù, bạn phát âm thanh khi phát hiện va chạm:

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
4

Ở đây, bạn dừng bất kỳ hiệu ứng âm thanh nào khác trước tiên, bởi vì trong một vụ va chạm, người chơi không còn di chuyển nữa. Sau đó, bạn chơi âm thanh va chạm và tiếp tục thực hiện từ đó.

Cuối cùng, khi trò chơi kết thúc, tất cả các âm thanh sẽ dừng lại. Điều này đúng cho dù trò chơi kết thúc do va chạm hay người dùng thoát thủ công. Để làm điều này, hãy thêm các dòng sau ở cuối chương trình sau vòng lặp:

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
5

Về mặt kỹ thuật, một vài dòng cuối cùng này không bắt buộc, vì chương trình kết thúc ngay sau đó. Tuy nhiên, nếu bạn quyết định sau này để thêm một màn hình giới thiệu hoặc màn hình thoát vào trò chơi của bạn, thì có thể có nhiều mã chạy hơn sau khi trò chơi kết thúc.

Đó là nó! Kiểm tra lại và bạn sẽ thấy một cái gì đó như thế này:

Hướng dẫn how do i run a python game file? - làm cách nào để chạy một tệp trò chơi python?

Một ghi chú về các nguồn

Bạn có thể nhận thấy nhận xét trên các dòng 136-137 khi nhạc nền được tải, liệt kê nguồn âm nhạc và liên kết đến giấy phép Creative Commons. Điều này đã được thực hiện bởi vì người tạo ra âm thanh đó yêu cầu nó. Các yêu cầu giấy phép cho rằng để sử dụng âm thanh, cả phân bổ thích hợp và liên kết đến giấy phép phải được cung cấp.

Dưới đây là một số nguồn cho âm nhạc, âm thanh và nghệ thuật mà bạn có thể tìm kiếm nội dung hữu ích:

  • OpenGameart.org: Âm thanh, hiệu ứng âm thanh, Sprites và các tác phẩm nghệ thuật khác sounds, sound effects, sprites, and other artwork
  • Kenney.nl: Âm thanh, hiệu ứng âm thanh, Sprites và các tác phẩm nghệ thuật khác sounds, sound effects, sprites, and other artwork
  • Gamer Art 2D: Sprites và các tác phẩm nghệ thuật khác sprites and other artwork
  • CC Mixter: Âm thanh và hiệu ứng âm thanh sounds and sound effects
  • Freesound: Âm thanh và hiệu ứng âm thanh sounds and sound effects

Khi bạn thực hiện các trò chơi của mình và sử dụng nội dung được tải xuống như nghệ thuật, âm nhạc hoặc mã từ các nguồn khác, vui lòng chắc chắn rằng bạn đang tuân thủ các điều khoản cấp phép của các nguồn đó.

Sự kết luận

Trong suốt hướng dẫn này, bạn đã học được cách lập trình trò chơi với

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 khác với lập trình thủ tục tiêu chuẩn. Bạn cũng đã học được cách:

  • Thực hiện các vòng lặp sự kiện
  • Vẽ các mục trên màn hình
  • Chơi hiệu ứng âm thanh và âm nhạc
  • Xử lý đầu vào của người dùng

Để làm điều này, bạn đã sử dụng một tập hợp con của các mô -đun

 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6, bao gồm
44# Fill the screen with white
45screen.fill((255, 255, 255))
46
47# Create a surface and pass in a tuple containing its length and width
48surf = pygame.Surface((50, 50))
49
50# Give the surface a color to separate it from the background
51surf.fill((0, 0, 0))
52rect = surf.get_rect()
9,
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
17 và
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
19,
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
34,
20# Define a Player object by extending pygame.sprite.Sprite
21# The surface drawn on the screen is now an attribute of 'player'
22class Player(pygame.sprite.Sprite):
23    def __init__(self):
24        super(Player, self).__init__()
25        self.surf = pygame.Surface((75, 25))
26        self.surf.fill((255, 255, 255))
27        self.rect = self.surf.get_rect()
0,
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
42 và
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
02. Bạn cũng đã sử dụng một số lớp
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6, bao gồm
20# Define a Player object by extending pygame.sprite.Sprite
21# The surface drawn on the screen is now an attribute of 'player'
22class Player(pygame.sprite.Sprite):
23    def __init__(self):
24        super(Player, self).__init__()
25        self.surf = pygame.Surface((75, 25))
26        self.surf.fill((255, 255, 255))
27        self.rect = self.surf.get_rect()
4,
54# This line says "Draw surf onto the screen at the center"
55screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
6,
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
20 và
 1# Simple pygame program
 2
 3# Import and initialize the pygame library
 4import pygame
 5pygame.init()
 6
 7# Set up the drawing window
 8screen = pygame.display.set_mode([500, 500])
 9
10# Run until the user asks to quit
11running = True
12while running:
13
14    # Did the user click the window close button?
15    for event in pygame.event.get():
16        if event.type == pygame.QUIT:
17            running = False
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28# Done! Time to quit.
29pygame.quit()
53. Nhưng những thứ này chỉ làm trầy xước bề mặt của những gì
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 có thể làm! Kiểm tra tài liệu chính thức
 1# Import the pygame module
 2import pygame
 3
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
6 để biết danh sách đầy đủ các mô -đun và lớp học có sẵn.

Bạn có thể tìm thấy tất cả các tệp mã, đồ họa và âm thanh cho bài viết này bằng cách nhấp vào liên kết bên dưới:

Hãy thoải mái để lại ý kiến ​​dưới đây là tốt. Happy Pythoning!

Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem nó cùng với hướng dẫn bằng văn bản để hiểu sâu hơn về sự hiểu biết của bạn: Tạo một trò chơi cuộn bên 2D với pygame This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Make a 2D Side-Scroller Game With PyGame

Làm cách nào để chạy một ứng dụng .py?

Tệp PY! Nhập một số mã vào đây, sau đó chuyển đến: Chạy> Chạy mô -đun. Bạn sẽ được nhắc lưu tệp của mình dưới dạng mô -đun (một đoạn mã python nhỏ gọn có thể được chạy từ các tập lệnh khác) và từ đó, ứng dụng của bạn sẽ chạy! Đó là cách chạy các tập tin Python.Enter some code here, then go to: Run > Run Module. You'll be prompted to save your file as a module (a compact piece of Python code that can be run from other scripts) and from there, your app will run! That is how to run Python files.

Làm cách nào để bắt đầu các trò chơi lập trình trong Python?

Bước 1: Xin chào Bunny..
Nhập thư viện pygame.....
Khởi tạo pygame và thiết lập cửa sổ hiển thị ..
Tải hình ảnh mà bạn sẽ sử dụng cho chú thỏ ..
Tiếp tục lặp qua mã thụt lề sau.....
Đổ đầy màn hình với màu đen trước khi bạn vẽ bất cứ thứ gì ..
Thêm hình ảnh thỏ mà bạn đã tải vào màn hình tại x = 100 và y = 100 ..

Làm cách nào để chạy trò chơi rắn ở Python?

Làm thế nào để thực hiện trò chơi rắn trong Python ?..
Cài đặt pygame ..
Tạo màn hình ..
Tạo con rắn ..
Di chuyển con rắn ..
Trò chơi kết thúc khi rắn chạm vào ranh giới ..
Thêm thức ăn ..
Tăng chiều dài của con rắn ..
Hiển thị điểm số ..

Làm thế nào để bạn chạy một tệp .py trên windows?

Loại CD Pythonprogram và nhấn Enter.Nó sẽ đưa bạn đến thư mục PythonPrograms.Nhập Dir và bạn sẽ thấy tệp Hello.py.Để chạy chương trình, hãy nhập python hello.py và nhấn enter.