用pygame绘制对角椭圆

4 投票
5 回答
10433 浏览
提问于 2025-04-18 04:09

有没有人知道怎么简单地画出不跟x轴和y轴对齐的椭圆?我刚开始学习pygame,所以请原谅我不太懂,但我找不到相关的信息。

如果没有简单的方法,有人能帮我想想除了生成很多很多椭圆上的点并把它们都画出来之外,还有什么其他方法吗?

5 个回答

-2

我不太懂Python或者Pygame,不过根据你要做的东西,可能用像Inkscape这样的程序(适用于电脑和Mac)或者Inkpad(适用于iPad)来制作一张图片会更简单。这两个程序都可以让你画出斜着的椭圆,然后把它导出为.png格式,这样你就可以在你的代码里使用了。不过,能不能这样做其实还得看你用这个椭圆是干什么的。

0

如果这对某些人有帮助,我写了一个函数,可以让你在任意两个点A和B之间画一个椭圆。

虽然这个数学方法不是最优雅的,但它能正常工作!下面是一个例子:

import pygame
import math


def draw_ellipse(A, B, width, color, line):
    """
    draws ellipse between two points
    A = start point (x,y)
    B = end point (x,y)
    width in pixel
    color (r,g,b)
    line thickness int, if line=0 fill ellipse
    """
    # point coordinates
    xA, yA = A[0], A[1]
    xB, yB = B[0], B[1]
    # calculate ellipse height, distance between A and B
    AB = math.sqrt((xB - xA)**2 + (yB - yA)**2)

    # difference between corner point coord and ellipse endpoint
    def sp(theta):
        return abs((width / 2 * math.sin(math.radians(theta))))

    def cp(theta):
        return abs((width / 2 * math.cos(math.radians(theta))))

    if xB >= xA and yB < yA:
        # NE quadrant
        theta = math.degrees(math.asin((yA - yB) / AB))
        xP = int(xA - sp(theta))
        yP = int(yB - cp(theta))
    elif xB < xA and yB <= yA:
        # NW
        theta = math.degrees(math.asin((yB - yA) / AB))
        xP = int(xB - sp(theta))
        yP = int(yB - cp(theta))
    elif xB <= xA and yB > yA:
        # SW
        theta = math.degrees(math.asin((yB - yA) / AB))
        xP = int(xB - sp(theta))
        yP = int(yA - cp(theta))
    else:
        # SE
        theta = math.degrees(math.asin((yA - yB) / AB))
        xP = int(xA - sp(theta))
        yP = int(yA - cp(theta))

    # create surface for ellipse
    ellipse_surface = pygame.Surface((AB, width), pygame.SRCALPHA)
    # draw surface onto ellipse
    pygame.draw.ellipse(ellipse_surface, color, (0, 0, AB, width), line)
    # rotate ellipse
    ellipse = pygame.transform.rotate(ellipse_surface, theta)
    # blit ellipse onto screen
    screen.blit(ellipse, (xP, yP))


screen = pygame.display.set_mode((1000, 1000))

running = True
while running:
    screen.fill((255, 250, 200))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

    draw_ellipse((500, 500), (420, 350), 100, (0, 255, 0), 5)
    draw_ellipse((400, 600), (700, 280), 80, (255, 0, 0), 5)
    draw_ellipse((260, 190), (670, 440), 50, (0, 0, 255), 5)

    pygame.display.update()
0

如果有人想知道怎么用旋转的椭圆画一个“花”,下面的内容可能会对你有帮助。

def flower(width: int, color: tuple, edges: bool=False):
    W, H = width, width
    # create a flower surface with an alpha channel on which to draw 
    # the petals
    flower = pygame.Surface((W, H), pygame.SRCALPHA, 32).convert_alpha()
    R = flower.get_rect()
    cx, cy = R.center
    # assuming petal height should be half their width
    petal_size = (width//2, width//4)
    pw, ph = petal_size
    radius = pw/2
    center_radius = width//10
    center_color = (255-color[0], 255-color[1], 255-color[2])

    def draw_petal(S, x, y, w, h, angle):
        # Create surface for drawing an individual petal
        surface = pygame.Surface((w, h), pygame.SRCALPHA, 32).convert_alpha()
        # Draw the un-rotated petal
        pygame.draw.ellipse(surface, color, (0, 0, w, h), 0)
        if edges:
            pygame.draw.ellipse(surface, BLACK, (0, 0, w, h), 1)

        # Create a new surface with the petal rotated by angle
        rot_surface = pygame.transform.rotate(surface, angle)
        # Need center of rotated surface to blit (draw) the rotated
        # petal at the given (x, y) coordinate
        rcx, rcy = rot_surface.get_rect().center
        # Draw the center of the rotated petal at (x, y)
        S.blit(rot_surface, (x - rcx, y - rcy))

    # Petals are drawn at diagonals first, then the horizontal petals,
    # then the vertical petals
    angles = [
        45, 135, 225, 315,      # diagonals
        0, 180,                 # horizontal
        90, 270                 # vertical
    ]
    for a in angles:
        # placing petal centers onto circle of radius (petal_width/2)
        x, y = map(int, (
            radius*math.cos(math.radians(a)), radius*math.sin(math.radians(a))
        ))
        draw_petal(flower, cx+x, cy+y, pw, ph, -a)
    # draw flower center (don't remember what it's called)
    pygame.draw.circle(flower, center_color, (cx, cx), center_radius)
    if edges:
        pygame.draw.circle(flower, BLACK, (cx, cx), center_radius, 1)

    def draw_flower(S, x, y, flower=flower):
        S.blit(flower, (x - cx, y - cy))
    return draw_flower

要使用这段代码:

import math
import pygame

BLACK    = (   0,   0,   0)
GREEN    = (   0, 255,   0)
RED      = ( 255,   0,   0)

pygame.init()

size = (800, 800)
SW, SH = size
screen = pygame.display.set_mode(size)

pygame.display.set_caption("Flower Demo")

done = False
clock = pygame.time.Clock()

# insert above flower code

draw_green_flower = flower(100, GREEN)
draw_red_flower = flower(100, RED)
try:
    while not done:

        for event in pygame.event.get(): 
            if event.type == pygame.QUIT:
                done = True 
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    done = True 

        screen.fill(WHITE)

        draw_green_flower(screen, SW/2,SH/2)
        draw_red_flower(screen, SW/2-100,SH/2-100)

        pygame.display.flip()

        clock.tick(60)

finally:
    pygame.quit()
1

很遗憾,直接绘制一个旋转的形状并没有简单的方法。虽然pygame.transform.rotate()可以旋转一个pygame.Surface对象,但你不能直接旋转一个形状。你需要先在一个Surface上绘制这个形状,然后再旋转这个Surface

  1. 创建一个带有逐像素透明度格式pygame.Surface对象,大小要和形状一致。
  2. 在这个Surface上绘制形状。
  3. 围绕形状的中心旋转这个Surface。可以参考如何使用PyGame围绕中心旋转图像?
  4. 使用blit将带有形状的Surface绘制到目标Surface上。

编写一个函数来绘制旋转的形状:

def draw_ellipse_angle(surface, color, rect, angle, width=0):
    target_rect = pygame.Rect(rect)
    shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
    pygame.draw.ellipse(shape_surf, color, (0, 0, *target_rect.size), width)
    rotated_surf = pygame.transform.rotate(shape_surf, angle)
    surface.blit(rotated_surf, rotated_surf.get_rect(center = target_rect.center))

最简单的例子:

import pygame

def draw_ellipse_angle(surface, color, rect, angle, width=0):
    target_rect = pygame.Rect(rect)
    shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
    pygame.draw.ellipse(shape_surf, color, (0, 0, *target_rect.size), width)
    rotated_surf = pygame.transform.rotate(shape_surf, angle)
    surface.blit(rotated_surf, rotated_surf.get_rect(center = target_rect.center))

pygame.init()
window = pygame.display.set_mode((250, 250))
clock = pygame.time.Clock()

background = pygame.Surface(window.get_size())
ts, w, h, c1, c2 = 50, *window.get_size(), (160, 160, 160), (192, 192, 192)
tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
for rect, color in tiles:
    pygame.draw.rect(background, color, rect)

angle = 0
run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    window.blit(background, (0, 0))
    draw_ellipse_angle(window, (0, 0, 255), (25, 75, 200, 100), angle, 5)
    angle += 1
    pygame.display.flip()

pygame.quit()
exit()
3

你可以这样做。先创建一个表面,然后在这个表面上画一个椭圆,接着把整个表面(上面有椭圆)旋转一下。下面是我的测试代码:

import pygame, sys

screen = pygame.display.set_mode((1024, 640))

running = True

#let's create a surface to hold our ellipse:
surface = pygame.Surface((320, 240))

red = (180, 50, 50)
size = (0, 0, 300, 200)

#drawing an ellipse onto the 
ellipse = pygame.draw.ellipse(surface, red, size)

#new surface variable for clarity (could use our existing though)
#we use the pygame.transform module to rotate the original surface by 45°
surface2 = pygame.transform.rotate(surface, 45)

while running:
    screen.fill((255, 250, 200))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    screen.blit(surface2, (100, 100))
    pygame.display.update()

这样得到的结果就是一个旋转过的椭圆。你还可以让椭圆的“容器”变得透明。可以查看这些模块的文档了解更多信息:

http://www.pygame.org/docs/ref/transform.html#pygame.transform.rotate

http://www.pygame.org/docs/ref/surface.html

希望这些对你有帮助!

撰写回答