Search code examples
pythonimagepygamemousedraw

An arc that follows my mouse but stays on the circle


I am trying to make the arc follow my mouse while staying on the circle path.

I do not know why it is not working.

When I run it, it just create a pygame white screen with no error.

Here is my code:

from __future__ import division 
from math import atan2, degrees, pi
import math
import pygame

pygame.init()
screen = pygame.display.set_mode((800, 800))
pygame.display.set_caption("globe")
CENTER = (400, 400)
RADIUS = 350

running = True

def drawCircleArc(screen,color,center,radius,startDeg,endDeg,thickness):

    (x,y) = center
    rect = (x-radius,y-radius,radius*2,radius*2)
    startRad = math.radians(startDeg)
    endRad = math.radians(endDeg)
    pygame.draw.arc(screen,color,rect,startRad,endRad,thickness)


while running:

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

  mouse = pygame.mouse.get_pos()

  relx = mouse[0] - CENTER[0]
  rely = mouse[1] - CENTER[1]
  rad = atan2(-rely,relx)
  rad %= 2*pi
  degs = degrees(rad)


  screen.fill((152,206,231))

  drawCircleArc(screen,(243,79,79),CENTER,RADIUS, degs + 90,degs + 100 ,10)


  pygame.draw.circle(screen, (71,153,192), CENTER, RADIUS)
  pygame.display.update()

pygame.quit()

Picture Picture2

What I really want is the following picture Thank you


Solution

  • I think the follow will do what you want. I fixed two problems:

    1. You were drawing the graphics in the wrong order and covering up the short reddish arc (they need to be drawn from back to front), and

    2. The two literal values you were adding to the calcuated degs angle were too large.

    I also made a several other changes that weren't strictly needed, including reformatting the code follow the PEP 8 - Style Guide for Python Code guidelines and adding a pygame.time.Clock to slow down the refresh rate to something I though was more reasonable.

    from __future__ import division
    from math import atan2, degrees, pi
    import math
    import pygame
    
    pygame.init()
    screen = pygame.display.set_mode((800, 800))
    pygame.display.set_caption("globe")
    clock = pygame.time.Clock()
    FPS = 60  # Frames per sec
    CENTER = (400, 400)
    RADIUS = 350
    
    running = True
    
    def draw_circle_arc(screen, color, center, radius, start_deg, end_deg, thickness):
        x, y = center
        rect = (x-radius, y-radius, radius*2, radius*2)
        start_rad = math.radians(start_deg)
        end_rad = math.radians(end_deg)
        pygame.draw.arc(screen, color, rect, start_rad, end_rad, thickness)
    
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
    
        mouse = pygame.mouse.get_pos()
        relx = mouse[0] - CENTER[0]
        rely = mouse[1] - CENTER[1]
        rad = atan2(-rely, relx)
        degs = degrees(rad)
    
        screen.fill((152,206,231))
        pygame.draw.circle(screen, (71,153,192), CENTER, RADIUS)
        draw_circle_arc(screen, (243,79,79), CENTER, RADIUS, degs-10, degs+10, 10)
        pygame.display.update()
        clock.tick(FPS)
    
    pygame.quit()
    

    Here's what it looks like running

    screenshot of script running showing arc and mouse position