Hướng dẫn triangle rotation python - trăn xoay tam giác

Đầu tiên, chúng ta cần một hàm để xoay một điểm xung quanh gốc.

Nội phân chính

  • Bạn có thể xoay một hình dạng trong Python?
  • Làm thế nào để bạn xoay một hình vuông trong Python?
  • Làm thế nào để bạn xoay một hình rùa trong Python?
  • Làm thế nào để bạn xoay hình dạng?

Khi chúng ta xoay một điểm (x, y) xung quanh nguồn gốc theo độ theta, chúng ta sẽ nhận được tọa độ:

(x*cos (theta) -y*sin (theta), x*sin (theta)+y*cos (theta))

Nếu chúng ta muốn xoay nó xung quanh một điểm khác với nguồn gốc, chúng ta chỉ cần thay đổi nó để điểm trung tâm trở thành nguồn gốc. Bây giờ, chúng ta có thể viết chức năng sau:

from math import sin, cos, radians

def rotate_point(point, angle, center_point=(0, 0)):
    """Rotates a point around center_point(origin by default)
    Angle is in degrees.
    Rotation is counter-clockwise
    """
    angle_rad = radians(angle % 360)
    # Shift the point so that center_point becomes the origin
    new_point = (point[0] - center_point[0], point[1] - center_point[1])
    new_point = (new_point[0] * cos(angle_rad) - new_point[1] * sin(angle_rad),
                 new_point[0] * sin(angle_rad) + new_point[1] * cos(angle_rad))
    # Reverse the shifting we have done
    new_point = (new_point[0] + center_point[0], new_point[1] + center_point[1])
    return new_point

Một số đầu ra:

print(rotate_point((1, 1), 90, (2, 1)))
# This prints (2.0, 0.0)
print(rotate_point((1, 1), -90, (2, 1)))
# This prints (2.0, 2.0)
print(rotate_point((2, 2), 45, (1, 1)))
# This prints (1.0, 2.4142) which is equal to (1,1+sqrt(2))

Bây giờ, chúng ta chỉ cần xoay mọi góc của đa giác bằng chức năng trước đây của chúng ta:

def rotate_polygon(polygon, angle, center_point=(0, 0)):
    """Rotates the given polygon which consists of corners represented as (x,y)
    around center_point (origin by default)
    Rotation is counter-clockwise
    Angle is in degrees
    """
    rotated_polygon = []
    for corner in polygon:
        rotated_corner = rotate_point(corner, angle, center_point)
        rotated_polygon.append(rotated_corner)
    return rotated_polygon

Ví dụ đầu ra:

my_polygon = [(0, 0), (1, 0), (0, 1)]
print(rotate_polygon(my_polygon, 90))
# This gives [(0.0, 0.0), (0.0, 1.0), (-1.0, 0.0)]

Tham gia với chúng tôi và có quyền truy cập vào hàng ngàn hướng dẫn và một cộng đồng chuyên gia Pythonistas.

Mở khóa bài học này

Bài học này chỉ dành cho các thành viên. Tham gia với chúng tôi và có quyền truy cập vào hàng ngàn hướng dẫn và một cộng đồng chuyên gia Pythonistas.Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Mở khóa bài học này

Bài học này chỉ dành cho các thành viên. Tham gia với chúng tôi và có quyền truy cập vào hàng ngàn hướng dẫn và một cộng đồng chuyên gia Pythonistas. Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

  • Xin lỗi! Có vẻ như có một vấn đề với phát lại video 🙁 Điều này có thể là do sự cố ngừng hoạt động tạm thời hoặc do sự cố cấu hình với trình duyệt của bạn. Vui lòng xem Hướng dẫn khắc phục sự cố trình phát video của chúng tôi để giải quyết vấn đề.
  • Bảng điểm

Nhận xét & Thảo luận (2) In the previous lesson, I went on a quick tangent to show you how frame rates interact with the animations on your screen. In this lesson, I’ll show you how to move and rotate your spaceship.

00:00 Trong bài học trước, tôi đã tiếp tục nhanh chóng để cho bạn thấy tốc độ khung hình tương tác với hình ảnh động trên màn hình của bạn như thế nào. Trong bài học này, tôi sẽ chỉ cho bạn cách di chuyển và xoay tàu vũ trụ của bạn. So far, you have a generic game object called, funnily enough, GameObject. There are three types of objects in the game: rocks, bullets, and ships.

00:11 Cho đến nay, bạn có một đối tượng trò chơi chung được gọi là, đủ vui, GameObject. Có ba loại đối tượng trong trò chơi: đá, đạn và tàu. Each of these is going to need a bit of code specific to it, so you’ll want to create objects more specific than GameObject. GameObject is still a good base class for the common code of all your sprites, but in this lesson, you’re going to add a child of GameObject that is the ship itself. To fly your ship, you’ll need the game to respond to keyboard events.

00:21 Mỗi trong số này sẽ cần một chút mã cụ thể cho nó, vì vậy bạn sẽ muốn tạo các đối tượng cụ thể hơn GameObject. GameObject vẫn là một lớp cơ sở tốt cho mã chung của tất cả các sprites của bạn, nhưng trong bài học này, bạn sẽ thêm một đứa con của GameObject là con tàu. Để bay trên tàu của bạn, bạn sẽ cần trò chơi để trả lời các sự kiện bàn phím. Once your new ship class is in place, you’ll add some code for controlling its direction. The movement model for the ship is a single rocket booster and the ability to spin. There is no linear deceleration. If you want to slow down, turn the ship around 180 degrees and fire the rocket in the other direction.

00:43 Khi lớp tàu mới của bạn được đặt ra, bạn sẽ thêm một số mã để kiểm soát hướng của nó. Mô hình chuyển động cho con tàu là một bộ tăng cường tên lửa duy nhất và khả năng quay. Không có giảm tốc tuyến tính. Nếu bạn muốn chậm lại, hãy quay con tàu khoảng 180 độ và bắn tên lửa theo hướng khác. This makes flying the ship a bit challenging, but that’s part of the fun of the game. One quick thing to remember from the coordinate system: It’s a bit counterintuitive, but up is a negative number.

01:03 Điều này làm cho việc bay con tàu hơi khó khăn, nhưng đó là một phần của trò chơi vui vẻ. Một điều nhanh chóng cần nhớ từ hệ tọa độ: nó có một chút phản trực giác, nhưng UP là một số âm. Remember that y gets bigger as you go down the screen, so if you want to go up, you have to subtract.

01:16 Hãy nhớ rằng y trở nên lớn hơn khi bạn đi xuống màn hình, vì vậy nếu bạn muốn đi lên, bạn phải trừ đi. First off, let’s prepare for writing ship-specific code by creating a ship class. This is inside of models.py. I’ve started the new class called Spaceship that inherits from GameObject.

01:24 Trước tiên, hãy để chuẩn bị viết mã cụ thể của tàu bằng cách tạo một lớp tàu. Đây là bên trong models.py. Tôi đã bắt đầu lớp mới có tên Spaceship kế thừa từ GameObject. The

print(rotate_point((1, 1), 90, (2, 1)))
# This prints (2.0, 0.0)
print(rotate_point((1, 1), -90, (2, 1)))
# This prints (2.0, 2.0)
print(rotate_point((2, 2), 45, (1, 1)))
# This prints (1.0, 2.4142) which is equal to (1,1+sqrt(2))
1 method here takes a position to create the ship at, then calls the parent
print(rotate_point((1, 1), 90, (2, 1)))
# This prints (2.0, 0.0)
print(rotate_point((1, 1), -90, (2, 1)))
# This prints (2.0, 2.0)
print(rotate_point((2, 2), 45, (1, 1)))
# This prints (1.0, 2.4142) which is equal to (1,1+sqrt(2))
1 using the spaceship sprite image and an initial velocity of zero.

01:38 Phương thức

print(rotate_point((1, 1), 90, (2, 1)))
# This prints (2.0, 0.0)
print(rotate_point((1, 1), -90, (2, 1)))
# This prints (2.0, 2.0)
print(rotate_point((2, 2), 45, (1, 1)))
# This prints (1.0, 2.4142) which is equal to (1,1+sqrt(2))
1 ở đây có một vị trí để tạo con tàu tại, sau đó gọi cha mẹ
print(rotate_point((1, 1), 90, (2, 1)))
# This prints (2.0, 0.0)
print(rotate_point((1, 1), -90, (2, 1)))
# This prints (2.0, 2.0)
print(rotate_point((2, 2), 45, (1, 1)))
# This prints (1.0, 2.4142) which is equal to (1,1+sqrt(2))
1 bằng hình ảnh Spaceship Sprite và vận tốc ban đầu bằng không.
Let’s make the corresponding changes in
print(rotate_point((1, 1), 90, (2, 1)))
# This prints (2.0, 0.0)
print(rotate_point((1, 1), -90, (2, 1)))
# This prints (2.0, 2.0)
print(rotate_point((2, 2), 45, (1, 1)))
# This prints (1.0, 2.4142) which is equal to (1,1+sqrt(2))
3.

01:49 Hãy để Lừa thực hiện các thay đổi tương ứng trong

print(rotate_point((1, 1), 90, (2, 1)))
# This prints (2.0, 0.0)
print(rotate_point((1, 1), -90, (2, 1)))
# This prints (2.0, 2.0)
print(rotate_point((2, 2), 45, (1, 1)))
# This prints (1.0, 2.4142) which is equal to (1,1+sqrt(2))
3. The new Spaceship class replaces the GameObject, both here in the import and here inside of the
print(rotate_point((1, 1), 90, (2, 1)))
# This prints (2.0, 0.0)
print(rotate_point((1, 1), -90, (2, 1)))
# This prints (2.0, 2.0)
print(rotate_point((2, 2), 45, (1, 1)))
# This prints (1.0, 2.4142) which is equal to (1,1+sqrt(2))
6 code.

01:57 Lớp Spaceship mới thay thế GameObject, cả ở đây trong việc nhập và ở đây bên trong mã

print(rotate_point((1, 1), 90, (2, 1)))
# This prints (2.0, 0.0)
print(rotate_point((1, 1), -90, (2, 1)))
# This prints (2.0, 2.0)
print(rotate_point((2, 2), 45, (1, 1)))
# This prints (1.0, 2.4142) which is equal to (1,1+sqrt(2))
6. Because I’m concentrating on the ship for now, I’ve also removed the rock from the the game. I’ll put it back in here later.

02:06 Vì bây giờ tôi tập trung vào con tàu, tôi cũng đã loại bỏ tảng đá khỏi trò chơi. Tôi sẽ đặt nó trở lại đây sau. With the new ship class in place, you can now start to think about how it moves around. I’m now back inside of models.py with more changes for the spaceship.

02:16 Với lớp tàu mới tại chỗ, giờ đây bạn có thể bắt đầu suy nghĩ về cách nó di chuyển xung quanh. Bây giờ tôi đã trở lại bên trong models.py với nhiều thay đổi hơn cho tàu vũ trụ. As the ship moves and rotates, you’re going to need to do some vector math. To make the code clearer, I’ve created a constant here called

print(rotate_point((1, 1), 90, (2, 1)))
# This prints (2.0, 0.0)
print(rotate_point((1, 1), -90, (2, 1)))
# This prints (2.0, 2.0)
print(rotate_point((2, 2), 45, (1, 1)))
# This prints (1.0, 2.4142) which is equal to (1,1+sqrt(2))
8 that represents the up direction on the screen.

02:26 Khi con tàu di chuyển và quay, bạn sẽ cần phải thực hiện một số toán học vector. Để làm cho mã rõ ràng hơn, tôi đã tạo ra một hằng số ở đây gọi là

print(rotate_point((1, 1), 90, (2, 1)))
# This prints (2.0, 0.0)
print(rotate_point((1, 1), -90, (2, 1)))
# This prints (2.0, 2.0)
print(rotate_point((2, 2), 45, (1, 1)))
# This prints (1.0, 2.4142) which is equal to (1,1+sqrt(2))
8 đại diện cho hướng lên trên màn hình. This is to compensate for the “negative numbers are moving upwards” problem that I discussed earlier. Let me just scroll down.

02:38 Điều này là để bù đắp cho các số âm của người Viking đang di chuyển lên trên vấn đề mà tôi đã thảo luận trước đó. Hãy để tôi cuộn xuống. Our good little ship needs to turn. To do that, you’ll have to rotate the sprite and track which direction it is pointing in. Just like when moving the little red dot, the rotation movement also has a speed.

02:49 Con tàu nhỏ tốt của chúng tôi cần phải quay. Để làm điều đó, bạn sẽ phải xoay sprite và theo dõi hướng nào nó đang chỉ vào. Giống như khi di chuyển chấm đỏ nhỏ, chuyển động xoay cũng có tốc độ. This class constant represents the number of degrees to rotate each time you do a rotation movement.

03:01 Hằng số lớp này đại diện cho số độ để xoay mỗi lần bạn thực hiện chuyển động xoay. A bit of math is needed to rotate the sprite correctly, so I’ve grouped that together inside of this

print(rotate_point((1, 1), 90, (2, 1)))
# This prints (2.0, 0.0)
print(rotate_point((1, 1), -90, (2, 1)))
# This prints (2.0, 2.0)
print(rotate_point((2, 2), 45, (1, 1)))
# This prints (1.0, 2.4142) which is equal to (1,1+sqrt(2))
9 method. This first part figures out which direction to rotate. The default for the method is clockwise. To rotate clockwise, you’ll multiply the rotation by
def rotate_polygon(polygon, angle, center_point=(0, 0)):
    """Rotates the given polygon which consists of corners represented as (x,y)
    around center_point (origin by default)
    Rotation is counter-clockwise
    Angle is in degrees
    """
    rotated_polygon = []
    for corner in polygon:
        rotated_corner = rotate_point(corner, angle, center_point)
        rotated_polygon.append(rotated_corner)
    return rotated_polygon
0. To go counter-clockwise, you’ll multiply by
def rotate_polygon(polygon, angle, center_point=(0, 0)):
    """Rotates the given polygon which consists of corners represented as (x,y)
    around center_point (origin by default)
    Rotation is counter-clockwise
    Angle is in degrees
    """
    rotated_polygon = []
    for corner in polygon:
        rotated_corner = rotate_point(corner, angle, center_point)
        rotated_polygon.append(rotated_corner)
    return rotated_polygon
1.

03:08 Một chút toán học là cần thiết để xoay sprite một cách chính xác, vì vậy tôi đã nhóm lại với nhau bên trong phương pháp

print(rotate_point((1, 1), 90, (2, 1)))
# This prints (2.0, 0.0)
print(rotate_point((1, 1), -90, (2, 1)))
# This prints (2.0, 2.0)
print(rotate_point((2, 2), 45, (1, 1)))
# This prints (1.0, 2.4142) which is equal to (1,1+sqrt(2))
9 này. Phần đầu tiên này tìm ra hướng nào để xoay. Mặc định cho phương thức là theo chiều kim đồng hồ. Để xoay theo chiều kim đồng hồ, bạn sẽ nhân số vòng quay với
def rotate_polygon(polygon, angle, center_point=(0, 0)):
    """Rotates the given polygon which consists of corners represented as (x,y)
    around center_point (origin by default)
    Rotation is counter-clockwise
    Angle is in degrees
    """
    rotated_polygon = []
    for corner in polygon:
        rotated_corner = rotate_point(corner, angle, center_point)
        rotated_polygon.append(rotated_corner)
    return rotated_polygon
0. Để đi ngược chiều kim đồng hồ, bạn sẽ nhân với
def rotate_polygon(polygon, angle, center_point=(0, 0)):
    """Rotates the given polygon which consists of corners represented as (x,y)
    around center_point (origin by default)
    Rotation is counter-clockwise
    Angle is in degrees
    """
    rotated_polygon = []
    for corner in polygon:
        rotated_corner = rotate_point(corner, angle, center_point)
        rotated_polygon.append(rotated_corner)
    return rotated_polygon
1.
The amount of rotation is the
def rotate_polygon(polygon, angle, center_point=(0, 0)):
    """Rotates the given polygon which consists of corners represented as (x,y)
    around center_point (origin by default)
    Rotation is counter-clockwise
    Angle is in degrees
    """
    rotated_polygon = []
    for corner in polygon:
        rotated_corner = rotate_point(corner, angle, center_point)
        rotated_polygon.append(rotated_corner)
    return rotated_polygon
2 times the
def rotate_polygon(polygon, angle, center_point=(0, 0)):
    """Rotates the given polygon which consists of corners represented as (x,y)
    around center_point (origin by default)
    Rotation is counter-clockwise
    Angle is in degrees
    """
    rotated_polygon = []
    for corner in polygon:
        rotated_corner = rotate_point(corner, angle, center_point)
        rotated_polygon.append(rotated_corner)
    return rotated_polygon
3, just calculated. And then the ship’s
def rotate_polygon(polygon, angle, center_point=(0, 0)):
    """Rotates the given polygon which consists of corners represented as (x,y)
    around center_point (origin by default)
    Rotation is counter-clockwise
    Angle is in degrees
    """
    rotated_polygon = []
    for corner in polygon:
        rotated_corner = rotate_point(corner, angle, center_point)
        rotated_polygon.append(rotated_corner)
    return rotated_polygon
4 vector needs to be updated by the rotation angle.

03:31 Lượng xoay là

def rotate_polygon(polygon, angle, center_point=(0, 0)):
    """Rotates the given polygon which consists of corners represented as (x,y)
    around center_point (origin by default)
    Rotation is counter-clockwise
    Angle is in degrees
    """
    rotated_polygon = []
    for corner in polygon:
        rotated_corner = rotate_point(corner, angle, center_point)
        rotated_polygon.append(rotated_corner)
    return rotated_polygon
2 lần
def rotate_polygon(polygon, angle, center_point=(0, 0)):
    """Rotates the given polygon which consists of corners represented as (x,y)
    around center_point (origin by default)
    Rotation is counter-clockwise
    Angle is in degrees
    """
    rotated_polygon = []
    for corner in polygon:
        rotated_corner = rotate_point(corner, angle, center_point)
        rotated_polygon.append(rotated_corner)
    return rotated_polygon
3, vừa được tính toán. Và sau đó, vectơ tàu ____ ____24 cần được cập nhật bằng góc quay.
The
def rotate_polygon(polygon, angle, center_point=(0, 0)):
    """Rotates the given polygon which consists of corners represented as (x,y)
    around center_point (origin by default)
    Rotation is counter-clockwise
    Angle is in degrees
    """
    rotated_polygon = []
    for corner in polygon:
        rotated_corner = rotate_point(corner, angle, center_point)
        rotated_polygon.append(rotated_corner)
    return rotated_polygon
5 method on the
def rotate_polygon(polygon, angle, center_point=(0, 0)):
    """Rotates the given polygon which consists of corners represented as (x,y)
    around center_point (origin by default)
    Rotation is counter-clockwise
    Angle is in degrees
    """
    rotated_polygon = []
    for corner in polygon:
        rotated_corner = rotate_point(corner, angle, center_point)
        rotated_polygon.append(rotated_corner)
    return rotated_polygon
4 vector does all the funky trig math you need to do to do the rotation. With the
print(rotate_point((1, 1), 90, (2, 1)))
# This prints (2.0, 0.0)
print(rotate_point((1, 1), -90, (2, 1)))
# This prints (2.0, 2.0)
print(rotate_point((2, 2), 45, (1, 1)))
# This prints (1.0, 2.4142) which is equal to (1,1+sqrt(2))
9 method all set to go, you now just need to update the
def rotate_polygon(polygon, angle, center_point=(0, 0)):
    """Rotates the given polygon which consists of corners represented as (x,y)
    around center_point (origin by default)
    Rotation is counter-clockwise
    Angle is in degrees
    """
    rotated_polygon = []
    for corner in polygon:
        rotated_corner = rotate_point(corner, angle, center_point)
        rotated_polygon.append(rotated_corner)
    return rotated_polygon
8 method to appropriately rotate the sprite based on the current direction vector. The Pygame
def rotate_polygon(polygon, angle, center_point=(0, 0)):
    """Rotates the given polygon which consists of corners represented as (x,y)
    around center_point (origin by default)
    Rotation is counter-clockwise
    Angle is in degrees
    """
    rotated_polygon = []
    for corner in polygon:
        rotated_corner = rotate_point(corner, angle, center_point)
        rotated_polygon.append(rotated_corner)
    return rotated_polygon
9 method does both a rotation and scaling of a sprite at the same time. It takes three parameters: the sprite image being rotated, the angle of rotation, and a scale factor.

04:14 Góc ở đây được tính toán bằng phương pháp

my_polygon = [(0, 0), (1, 0), (0, 1)]
print(rotate_polygon(my_polygon, 90))
# This gives [(0.0, 0.0), (0.0, 1.0), (-1.0, 0.0)]
0 trên vectơ
def rotate_polygon(polygon, angle, center_point=(0, 0)):
    """Rotates the given polygon which consists of corners represented as (x,y)
    around center_point (origin by default)
    Rotation is counter-clockwise
    Angle is in degrees
    """
    rotated_polygon = []
    for corner in polygon:
        rotated_corner = rotate_point(corner, angle, center_point)
        rotated_polygon.append(rotated_corner)
    return rotated_polygon
4. Điều này cho thấy sự khác biệt về độ giữa hướng hiện tại và thẳng lên. Vì bạn chỉ quan tâm đến việc xoay và không chia tỷ lệ, tham số cuối cùng thành
def rotate_polygon(polygon, angle, center_point=(0, 0)):
    """Rotates the given polygon which consists of corners represented as (x,y)
    around center_point (origin by default)
    Rotation is counter-clockwise
    Angle is in degrees
    """
    rotated_polygon = []
    for corner in polygon:
        rotated_corner = rotate_point(corner, angle, center_point)
        rotated_polygon.append(rotated_corner)
    return rotated_polygon
9 là yếu tố tỷ lệ là
my_polygon = [(0, 0), (1, 0), (0, 1)]
print(rotate_polygon(my_polygon, 90))
# This gives [(0.0, 0.0), (0.0, 1.0), (-1.0, 0.0)]
3.
The angle here is calculated using the
my_polygon = [(0, 0), (1, 0), (0, 1)]
print(rotate_polygon(my_polygon, 90))
# This gives [(0.0, 0.0), (0.0, 1.0), (-1.0, 0.0)]
0 method on the
def rotate_polygon(polygon, angle, center_point=(0, 0)):
    """Rotates the given polygon which consists of corners represented as (x,y)
    around center_point (origin by default)
    Rotation is counter-clockwise
    Angle is in degrees
    """
    rotated_polygon = []
    for corner in polygon:
        rotated_corner = rotate_point(corner, angle, center_point)
        rotated_polygon.append(rotated_corner)
    return rotated_polygon
4 vector. This figures out the difference in degrees between the current direction and straight up. As you’re only interested in rotating and not scaling, the final parameter to
def rotate_polygon(polygon, angle, center_point=(0, 0)):
    """Rotates the given polygon which consists of corners represented as (x,y)
    around center_point (origin by default)
    Rotation is counter-clockwise
    Angle is in degrees
    """
    rotated_polygon = []
    for corner in polygon:
        rotated_corner = rotate_point(corner, angle, center_point)
        rotated_polygon.append(rotated_corner)
    return rotated_polygon
9 is a scale factor of
my_polygon = [(0, 0), (1, 0), (0, 1)]
print(rotate_polygon(my_polygon, 90))
# This gives [(0.0, 0.0), (0.0, 1.0), (-1.0, 0.0)]
3.

04:34 Hãy nhớ rằng khi bạn blit một cái gì đó, bạn đã luôn sao chép hình chữ nhật. Không chỉ vậy, nó luôn luôn là hình chữ nhật có các cạnh song song với các cạnh của màn hình. Remember that when you blit something, you’re always copying rectangles. Not only that, but it is always rectangles whose sides are parallel to the sides of the screen.

04:44 Các sprite mới được xoay không còn trong tình huống này nữa. Điều này làm cho hộp giới hạn của blit không chính xác. Nếu bạn chỉ sử dụng tọa độ của sprite xoay, bạn có thể tách một phần của hình ảnh. May mắn thay, phương pháp

my_polygon = [(0, 0), (1, 0), (0, 1)]
print(rotate_polygon(my_polygon, 90))
# This gives [(0.0, 0.0), (0.0, 1.0), (-1.0, 0.0)]
4 biết cách giải quyết vấn đề này. The newly-rotated sprite isn’t in this situation anymore. This makes the bounding box of the blit incorrect. If you just use the coordinates of the rotated sprite, you might chop part of the image off. Fortunately, the
my_polygon = [(0, 0), (1, 0), (0, 1)]
print(rotate_polygon(my_polygon, 90))
# This gives [(0.0, 0.0), (0.0, 1.0), (-1.0, 0.0)]
4 method knows how to deal with this problem.

05:02 Nó trả về một hộp giới hạn mới mà sprite xoay phù hợp bên trong mà không bị cắt. It returns a new bounding box that the rotated sprite fits inside of without it getting clipped.

05:09 Bạn cũng sẽ nhớ lại rằng vectơ

my_polygon = [(0, 0), (1, 0), (0, 1)]
print(rotate_polygon(my_polygon, 90))
# This gives [(0.0, 0.0), (0.0, 1.0), (-1.0, 0.0)]
5 dựa trên trung tâm của đối tượng trò chơi. Trước đây, bạn chỉ sử dụng bán kính của đối tượng để dịch vị trí cục bộ của con tàu thành một vị trí có thể bị xóa. Bây giờ bạn làm một cái gì đó tương tự, nhưng sử dụng hộp giới hạn được tính toán gần đây thay vì bán kính. You’ll also recall that the
my_polygon = [(0, 0), (1, 0), (0, 1)]
print(rotate_polygon(my_polygon, 90))
# This gives [(0.0, 0.0), (0.0, 1.0), (-1.0, 0.0)]
5 vector is based on the center of the game object. Previously, you just used the radius of the object to translate the ship’s local position into a blittable one. Now you do something similar, but use the recently calculated bounding box instead of the radius.

05:29 Tất cả các phép toán dịch và phối hợp hệ thống đó đang được thực hiện, bạn đã sẵn sàng để thực sự làm mờ sprite lên bề mặt. Tàu được vẽ ngay bây giờ. All that rotation and coordinate system translation math being done, you’re ready to actually blit the sprite onto the surface. Ships be drawn now.

05:43 Được rồi, tôi đã trở lại bên trong

print(rotate_point((1, 1), 90, (2, 1)))
# This prints (2.0, 0.0)
print(rotate_point((1, 1), -90, (2, 1)))
# This prints (2.0, 2.0)
print(rotate_point((2, 2), 45, (1, 1)))
# This prints (1.0, 2.4142) which is equal to (1,1+sqrt(2))
3. Hãy để tôi cuộn xuống phương pháp xử lý đầu vào. Trên dòng 29, tôi đã chèn một số mã xử lý các máy nhấn chính. Okay, I’m back inside of
print(rotate_point((1, 1), 90, (2, 1)))
# This prints (2.0, 0.0)
print(rotate_point((1, 1), -90, (2, 1)))
# This prints (2.0, 2.0)
print(rotate_point((2, 2), 45, (1, 1)))
# This prints (1.0, 2.4142) which is equal to (1,1+sqrt(2))
3. Let me scroll down to the input handling method. On line 29, I’m inserting some code that handles key presses.

05:54 Bạn có thể nhớ lại từ tập lệnh

my_polygon = [(0, 0), (1, 0), (0, 1)]
print(rotate_polygon(my_polygon, 90))
# This gives [(0.0, 0.0), (0.0, 1.0), (-1.0, 0.0)]
7 Việc sử dụng sự kiện
my_polygon = [(0, 0), (1, 0), (0, 1)]
print(rotate_polygon(my_polygon, 90))
# This gives [(0.0, 0.0), (0.0, 1.0), (-1.0, 0.0)]
8. Mỗi lần bạn nhấn phím, bạn sẽ nhận được một và chỉ một sự kiện
my_polygon = [(0, 0), (1, 0), (0, 1)]
print(rotate_polygon(my_polygon, 90))
# This gives [(0.0, 0.0), (0.0, 1.0), (-1.0, 0.0)]
8.
You may recall from the
my_polygon = [(0, 0), (1, 0), (0, 1)]
print(rotate_polygon(my_polygon, 90))
# This gives [(0.0, 0.0), (0.0, 1.0), (-1.0, 0.0)]
7 script the use of the
my_polygon = [(0, 0), (1, 0), (0, 1)]
print(rotate_polygon(my_polygon, 90))
# This gives [(0.0, 0.0), (0.0, 1.0), (-1.0, 0.0)]
8 event. Each time you press a key, you get one and only one
my_polygon = [(0, 0), (1, 0), (0, 1)]
print(rotate_polygon(my_polygon, 90))
# This gives [(0.0, 0.0), (0.0, 1.0), (-1.0, 0.0)]
8 event.

06:03 Trong trường hợp xoay con tàu, bạn muốn có thể giữ chìa khóa và tiếp tục xoay. Trong trường hợp này, thay vì sử dụng sự kiện

my_polygon = [(0, 0), (1, 0), (0, 1)]
print(rotate_polygon(my_polygon, 90))
# This gives [(0.0, 0.0), (0.0, 1.0), (-1.0, 0.0)]
8, bạn sử dụng hàm GameObject1. In the case of rotating the ship, you want to be able to hold the key and have rotation continue. In this case, instead of using the
my_polygon = [(0, 0), (1, 0), (0, 1)]
print(rotate_polygon(my_polygon, 90))
# This gives [(0.0, 0.0), (0.0, 1.0), (-1.0, 0.0)]
8 event, you use the GameObject1 function.

06:15 Điều này trả về một dict có một cặp giá trị khóa cho mọi hỗ trợ pygame chính. Nếu khóa được nhấn tại thời điểm cuộc gọi đến GameObject2, thì giá trị trong từ điển sẽ là GameObject3. Các dòng 30 đến 31 đang thêm khả năng thoát khỏi trò chơi bằng cách nhấn các phím Escape hoặc Q. Các dòng 32 và 33 gọi GameObject4 theo chiều kim đồng hồ khi nhấn mũi tên phải. Và các dòng 34 và 35 xoay con tàu theo hướng khác khi nhấn lại. Với tất cả những gì tại chỗ, con tàu nhỏ của bạn có thể quay. Hãy để kiểm tra điều đó. This returns a dict that has a key-value pair for every key Pygame supports. If the key is pressed at the time of the call to GameObject2, then the value in the dictionary will be GameObject3. Lines 30 through 31 are adding the ability to quit the game by pressing the Escape or q keys. Lines 32 and 33 call GameObject4 in the clockwise direction when the right arrow is being pressed. And lines 34 and 35 rotate the ship in the other direction when left is pressed. With all that in place, your little ship can spin. Let’s check that out.

06:59 Yay! Quay! Theo cả hai hướng, thậm chí! Nếu bạn thấy điều này hơi chậm, bạn luôn có thể điều chỉnh hằng số tốc độ xoay bên trong đối tượng Spaceship. Yay! Spinning! In both directions, even! If you find this a bit slow, you can always adjust the rotate speed constant inside of the Spaceship object.

07:12 Xoay là mát mẻ và tất cả, nhưng tôi muốn bay. Con tàu này cần một bộ tăng áp tên lửa. Để giữ cho nó đơn giản, hãy để Lôi không làm động bất kỳ ngọn lửa hay bất cứ điều gì, nhưng nó chắc chắn có thể làm với một số chuyển động. Như tôi đã đề cập trước đây, con tàu này sẽ có một động cơ tên lửa duy nhất ra phía sau và do đó chỉ có thể tăng tốc theo hướng nó đang chỉ. Để làm chậm con tàu của bạn, bạn phải xoay và tăng tốc theo hướng ngược lại. Rotating is cool and all, but I want to fly. This ship needs a rocket booster. To keep it simple, let’s not animate any flames or anything, but it can definitely do with some motion. As I mentioned before, this ship will have a single rocket engine out the back and therefore can only accelerate in the direction it is pointing. To slow your ship down, you have to rotate and accelerate in the opposite direction.

07:38 Hãy để viết mã. Tôi đã trở lại bên trong models.py. Hãy để tôi cuộn xuống. Ở đây, đối tượng Spaceship. Và một hằng số mới được sinh ra: lần này, một số gia tốc để đi theo vòng quay. Let’s write the code. I’m back inside models.py. Let me scroll down. Here’s the Spaceship object. And a new constant is born: this time, some acceleration to go with the rotation.

07:55 Giống như với vòng quay, toán học gia tốc sẽ được gói gọn trong một cuộc gọi phương thức. Toán học này đơn giản hơn xoay vòng. Tất cả những gì bạn phải làm là nhân hướng trong di chuyển với hằng số gia tốc, làm cho nó đi nhanh hơn, và sau đó thêm kết quả mới vào vận tốc. Just like with rotation, the acceleration math will be encapsulated in a method call. This math is simpler than rotation. All you have to do is multiply the direction in travel by the acceleration constant, making it go faster, and then add the new result to the velocity.

08:15 Với phương thức GameObject8 tại chỗ, tất cả những gì còn lại để làm là thêm một điều khiển trò chơi. Tôi đã trở lại bên trong

print(rotate_point((1, 1), 90, (2, 1)))
# This prints (2.0, 0.0)
print(rotate_point((1, 1), -90, (2, 1)))
# This prints (2.0, 2.0)
print(rotate_point((2, 2), 45, (1, 1)))
# This prints (1.0, 2.4142) which is equal to (1,1+sqrt(2))
3. Hãy để tôi chuyển xuống phương pháp GameObject0. With the GameObject8 method in place, all that’s left to do is add a game control. I’m back inside of
print(rotate_point((1, 1), 90, (2, 1)))
# This prints (2.0, 0.0)
print(rotate_point((1, 1), -90, (2, 1)))
# This prints (2.0, 2.0)
print(rotate_point((2, 2), 45, (1, 1)))
# This prints (1.0, 2.4142) which is equal to (1,1+sqrt(2))
3. Let me move down to the GameObject0 method.

08:25 Tôi đã thêm một tấm séc cho mũi tên lên. Nếu phím đó được nhấn, phương thức tàu GameObject8 được gọi và bạn đi nhanh hơn. Bay tôi lên mặt trăng, em yêu! I’ve added a check for the up arrow. If that key is pressed, the ship’s GameObject8 method is called and you go faster. Fly me to the moon, baby!

08:41 uh oh, tôi đã đi đâu? Giống như đá không bao giờ kết thúc của chúng tôi từ các bài học trở lại, con tàu của tôi vẫn đang vận tải, nhưng cố gắng tìm ra vòng quay và gia tốc phù hợp để đưa nó trở lại trên màn hình có thể là một thách thức mà không thể nhìn thấy con tàu. Uh oh, where did I go? Like our never-ending rock from lessons back, my ship is still trucking along, but trying to figure out the right rotation and acceleration to get it back on the screen could be a bit of a challenge without being able to see the ship.

08:56 Vì đây là một bản sao tiểu hành tinh, hãy để Lừa làm những gì các tiểu hành tinh đã làm: bọc màn hình. As this is an Asteroids clone, let’s do what Asteroids did: wrap the screen.

09:03 Bạn sẽ nhớ lại rằng chuyển động của con tàu được tính bên trong phương pháp GameObject2 của lớp cơ sở GameObject trong models.py. You will recall that the motion of the ship is calculated inside of the GameObject2 method of the base GameObject class in models.py.

09:12 Nếu con tàu ở trong ranh giới của màn hình, đây là nơi để thực hiện điều đó. Dòng 20 là dòng GameObject2 cũ. Dòng 21 là thay đổi mới đối với phương thức đảm bảo đối tượng luôn luôn trên màn hình. If the ship is to stay in the boundaries of the screen, this is the place to make that happen. Line 20 is the old GameObject2 line. Line 21 is the new change to the method that ensures the object’s always on the screen.

09:26 Hàm GameObject6 trả về một vị trí mới dựa trên giá trị được truyền vào và bề mặt GameObject được chứa bên trong. The GameObject6 function returns a new position based on the value passed in and the surface the GameObject is contained within.

09:34 Nếu GameObject nằm ngoài giới hạn bề mặt, GameObject6 sẽ trả về một vị trí mới vẫn còn trong giới hạn. Lưu ý rằng phương pháp GameObject2 bây giờ cần bề mặt được di chuyển trong để gọi để làm việc. Kịch bản

print(rotate_point((1, 1), 90, (2, 1)))
# This prints (2.0, 0.0)
print(rotate_point((1, 1), -90, (2, 1)))
# This prints (2.0, 2.0)
print(rotate_point((2, 2), 45, (1, 1)))
# This prints (1.0, 2.4142) which is equal to (1,1+sqrt(2))
3 sẽ cần cập nhật, nhưng tôi sẽ quay lại điều đó trong một phút. Bạn đã thấy cách GameObject6 được sử dụng. If the GameObject is out of the surface’s bounds, GameObject6 will return a new position that is still in the bounds. Note that the GameObject2 method now needs the surface being moved within for the call to work. The
print(rotate_point((1, 1), 90, (2, 1)))
# This prints (2.0, 0.0)
print(rotate_point((1, 1), -90, (2, 1)))
# This prints (2.0, 2.0)
print(rotate_point((2, 2), 45, (1, 1)))
# This prints (1.0, 2.4142) which is equal to (1,1+sqrt(2))
3 script will need updating, but I’ll get back to that in a minute. You’ve seen how GameObject6 is used.

09:56 Tôi đã thực hiện nó bên trong các công ty GameObject3. Don Tiết quên nhập nó, và bây giờ hãy để tôi chỉ cho bạn chức năng GameObject6 trông như thế nào. I’ve implemented it inside of the utils GameObject3. Don’t forget to import it, and now let me show you what the GameObject6 function itself looks like.

10:09 Đây là bên trong GameObject5 và dòng 15 là khởi đầu của hàm GameObject6 mới. Phần đầu tiên nhận được các giá trị GameObject7 và GameObject8 trong vectơ GameObject9 được truyền vào. This is inside of GameObject5, and line 15 is the beginning of the new GameObject6 function. The first part gets the GameObject7 and GameObject8 values out of the GameObject9 vector passed in.

10:23 Phần thứ hai tìm thấy chiều rộng và chiều cao của bề mặt được vẽ trên. Phần cuối cùng trả về một đối tượng GameObject0 mới, chứa các tham số GameObject7 và GameObject8 được sửa đổi. The second part finds the width and height of the surface being drawn upon. The last part returns a new GameObject0 object, which contains modified GameObject7 and GameObject8 parameters.

10:35 Giá trị GameObject7 được sửa đổi theo chiều rộng và giá trị GameObject8 được sửa đổi theo chiều cao. Nếu GameObject7 của vị trí đối tượng vượt quá chiều rộng, hoạt động mod sẽ bao bọc nó xung quanh. Tương tự đối với GameObject8 và chiều cao. The GameObject7 value is modded against the width and the GameObject8 value modded against the height. If the GameObject7 of the object’s position exceeds the width, the mod operation will wrap it around. Same goes for the GameObject8 and the height.

10:54 Bây giờ, bên trong

print(rotate_point((1, 1), 90, (2, 1)))
# This prints (2.0, 0.0)
print(rotate_point((1, 1), -90, (2, 1)))
# This prints (2.0, 2.0)
print(rotate_point((2, 2), 45, (1, 1)))
# This prints (1.0, 2.4142) which is equal to (1,1+sqrt(2))
3, phương pháp GameObject8 cần được cập nhật. Điều này phản ánh sự thay đổi đối với phương pháp GameObject2. Now, inside of
print(rotate_point((1, 1), 90, (2, 1)))
# This prints (2.0, 0.0)
print(rotate_point((1, 1), -90, (2, 1)))
# This prints (2.0, 2.0)
print(rotate_point((2, 2), 45, (1, 1)))
# This prints (1.0, 2.4142) which is equal to (1,1+sqrt(2))
3, the GameObject8 method needs to be updated. This reflects the change to the GameObject2 method.

11:02 Bây giờ nó lấy màn hình làm tham số bề mặt tạo thành ranh giới của hàm GameObject6. Với mã này tại chỗ, hãy để đi chơi với con tàu. It now takes the screen as the surface parameter that forms the boundaries of the GameObject6 function. With this code in place, let’s go play with the ship.

11:22 và nó ở đó. Con tàu của bạn bây giờ sẽ luôn ở trên màn hình. And there it is. Your ship will now always be on the screen.

11:28 Con tàu của bạn đã sẵn sàng đi! Bây giờ tôi muốn đá. Gì? Được rồi, uh, những người trẻ tuổi đi google google tisted sister và sau đó chèn trò đùa của cha bạn ở đây. Your ship is all set to go! Now I want to rock. What? Okay, uh, you young ones go Google “Twisted Sister” and then insert your own dad joke here.

Bạn có thể xoay một hình dạng trong Python?

Hàm iMutils.Rotate () được sử dụng để xoay hình ảnh bằng một góc trong Python. rotate() function is used to rotate an image by an angle in Python.

Làm thế nào để bạn xoay một hình vuông trong Python?

Để xoay, hãy gọi hàm ROTATEX (), ROTATEY () hoặc ROTATEZ () để xoay xung quanh mỗi trục.call the rotateX() , rotateY() , or rotateZ() function to rotate around each of the axes.

Làm thế nào để bạn xoay một hình rùa trong Python?

Phải (độ): Xoay hướng rùa phải đối mặt phải (theo chiều kim đồng hồ) theo số lượng được chỉ định (tính bằng độ). rùa. Bên trái (độ): Xoay hướng rùa hướng sang trái (ngược chiều kim đồng hồ) theo số lượng được chỉ định (tính bằng độ).

Làm thế nào để bạn xoay hình dạng?

Xoay 90 độ..

Chọn đối tượng mà bạn muốn xoay ..

Chuyển đến định dạng hình dạng, công cụ vẽ hoặc công cụ hình ảnh> Định dạng ..

Chọn Xoay, và sau đó: Để xoay đối tượng 90 độ sang phải, chọn xoay phải 90 °. Để xoay đối tượng 90 độ sang trái, chọn xoay trái 90 ° ..