有没有人知道一种简单的方法来绘制与x& x不对齐的椭圆。 y轴。我对pygame很新,所以请原谅我的无知,但我找不到任何相关内容。
如果不存在简单的方法,除了在椭圆上生成许多点并绘制所有点之外,有人可以帮助我绘制这个吗?
答案 0 :(得分: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()
结果将是旋转的椭圆。有可能使'#34持有者"椭圆的透明。检查模块上的文档:
http://www.pygame.org/docs/ref/transform.html#pygame.transform.rotate
http://www.pygame.org/docs/ref/surface.html
希望有所帮助!
答案 1 :(得分: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()
答案 2 :(得分: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()
答案 3 :(得分:0)
不幸的是,没有直接的方法可以绘制旋转的形状。 pygame.transform.rotate()
可以旋转 pygame.Surface
对象,但不能直接旋转形状。您需要在 Surface 上绘制形状并旋转该 Surface:
pygame.Surface
对象。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()
答案 4 :(得分:-2)
我不知道python或pygame,但是根据你正在构建的内容,使用像pc和mac的inkscape或iPad的inkpad这样的程序来制作图像可能更容易。这两个都可以让你创建一个对角椭圆,然后将它导出为.png并在你的代码中使用它。同样,如果可能的话,这取决于你对椭圆做了什么。