C.Marks
C.Marks

Reputation: 79

PyGame/Python: Placing a circle onto an ellipse

I am trying to place multiple circles onto an eclipse and be able to move that circle around the eclipse. From looking into PyGames examples I have seen that you can rotate a line around an eclipse however cannot figure out how to do with with a circle.

This is the error message I recieve upon trying:

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

Upvotes: 0

Views: 408

Answers (2)

furas
furas

Reputation: 142711

It is not answer for your main question - because you already got answer.

To put more circles use list with angles and for loop to get angle from list (one-by-one) and draw circle.

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

Upvotes: 0

Tim Mullen
Tim Mullen

Reputation: 52

The math.sin and math.cos functions return floats, and the pos keyword argument to pygame.draw.circle expects integer positions, so you'll want to actually cast your coordinates. You have a few options for doing this:

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

Each comes with slightly different behaviours so you might want to figure out which fits your program best. (specifically: int and floor work differently for negative numbers -- int rounds towards 0 and floor rounds down, as expected)

Upvotes: 0

Related Questions