簡體   English   中英

用pygame繪制對角橢圓

[英]drawing a diagonal ellipse with pygame

有誰知道繪制不與 x & y 軸對齊的橢圓的簡單方法。 我對 pygame 很陌生,所以請原諒我的無知,但我找不到任何與之相關的東西。

如果不存在簡單的方法,除了在橢圓上生成許多點並繪制所有點之外,有人可以幫助我如何繪制它嗎?

你可以這樣做。 創建一個曲面,將橢圓繪制到該曲面上,然后旋轉整個曲面(橢圓在其上)。 這是我的測試代碼:

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

希望有幫助!

如果有人好奇如何使用旋轉橢圓繪制“花”,以下內容可能會有所幫助。

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()

如果這對某人有幫助,我編寫了一個函數,可以讓您在任意兩點 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()

不幸的是,沒有直接的方法可以繪制旋轉的形狀。 pygame.transform.rotate()可以旋轉pygame.Surface對象,但不能直接旋轉形狀。 您需要在Surface上繪制形狀並旋轉該Surface

  1. 創建一個具有每像素 alpha 格式和形狀大小的pygame.Surface對象。
  2. 在 _Surface 上繪制形狀。
  3. 圍繞其中心旋轉具有形狀的曲面 請參閱如何使用 PyGame 圍繞其中心旋轉圖像?
  4. blit與shapeonto目標表面表面

編寫一個繪制旋轉形狀的函數:

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()

我不知道 python 或 pygame,但根據您正在構建的內容,使用諸如適用於 pc 和 mac 的inkscape 或適用於iPad 的inkpad 之類的程序制作圖像可能更容易。 這兩者都可以讓您制作對角橢圓,然后將其導出為 .png 並在您的代碼中使用它。 同樣,這是否可能真的取決於您對橢圓的處理方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM