PyGame/Python: 将圆放在椭圆上

PyGame/Python: Placing a circle onto an ellipse

我正在尝试将多个圆圈放在日食上并能够围绕日食移动该圆圈。通过查看 PyGames 示例,我发现您可以围绕日食旋转一条线,但无法弄清楚如何使用圆圈。

这是我在尝试时收到的错误消息:

Traceback (most recent call last):
File "C:/Python32/Attempts/simple_graphics_demo.py", line 66, in <module>
pygame.draw.circle(screen, BLUE, [x, y], 15, 3)
TypeError: integer argument expected, got float

.

import pygame
import math

# Initialize the game engine
pygame.init()

# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)

PI = 3.141592653

# Set the height and width of the screen
size = [400, 400]
screen = pygame.display.set_mode(size)

my_clock = pygame.time.Clock()

# Loop until the user clicks the close button.
done = False

angle = 0

while not done:
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        done = True

# Set the screen background
screen.fill(WHITE)

# Dimensions of radar sweep
# Start with the top left at 20,20
# Width/height of 250
box_dimensions = [20, 20, 250, 250]

# Draw the outline of a circle to 'sweep' the line around
pygame.draw.ellipse(screen, GREEN, box_dimensions, 2)

# Draw a black box around the circle
pygame.draw.rect(screen, BLACK, box_dimensions, 2)

# Calculate the x,y for the end point of our 'sweep' based on
# the current angle
x = 125 * math.sin(angle) + 145
y = 125 * math.cos(angle) + 145

# Draw the line from the center at 145, 145 to the calculated
# end spot
pygame.draw.line(screen, GREEN, [145, 145], [x, y], 2)

# Attempt to draw a circle on the radar
pygame.draw.circle(screen, BLUE, [x, y], 15, 3)

# Increase the angle by 0.03 radians
angle = angle + .03

# If we have done a full sweep, reset the angle to 0
if angle > 2 * PI:
    angle = angle - 2 * PI

# Flip the display, wait out the clock tick
pygame.display.flip()
my_clock.tick(60)

# on exit.
pygame.quit()

math.sinmath.cos 函数 return 浮动,pygame.draw.circle 的 pos 关键字参数需要整数位置,因此您需要实际转换坐标.您有几种选择:

  • [int(x), int(y)]
  • [math.floor(x), math.floor(y)]
  • [math.ceil(x), math.ceil(y)]

每个都有略微不同的行为,因此您可能想找出最适合您的程序的。 (具体来说:intfloor 对负数的处理方式不同——int 向 0 舍入,floor 向下舍入,如预期的那样)

这不是您主要问题的答案 - 因为您已经有了答案。

要放置更多圆,请使用带角度的列表和 for 循环从列表中获取角度(一个接一个)并绘制圆。

import pygame
import math

# === CONSTANTS ===

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

SIZE = (400, 400)

TWO_PI = 2 * math.pi # you don't have to calculate it in loop

# === MAIN ===

# --- init ---

pygame.init()

screen = pygame.display.set_mode(SIZE)

# --- objects ---

angles = [0, 1, math.pi] # angles for many circles

box_dimensions = [20, 20, 250, 250] # create only once

# --- mainloop ---

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

while not done:

    # --- events ---

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

    # --- draws (without updates) ---

    screen.fill(WHITE)

    pygame.draw.ellipse(screen, GREEN, box_dimensions, 2)

    pygame.draw.rect(screen, BLACK, box_dimensions, 2)

    # draw many circles 
    for a in angles: 

        x = int(125 * math.sin(a)) + 145
        y = int(125 * math.cos(a)) + 145

        pygame.draw.line(screen, GREEN, [145, 145], [x, y], 2)
        pygame.draw.circle(screen, BLUE, [x, y], 15, 3)

    pygame.display.flip()
    clock.tick(60)

    # --- updates (without draws) ---

    # new values for many angles
    for i, a in enumerate(angles):
        a += .03

        if a > TWO_PI:
            a -= TWO_PI

        angles[i] = a

# --- the end ---
pygame.quit()