0

I'm attempting to put interval indicators on a circle, much like what you would find on a timer or clock.

import pygame
from pygame.locals import *
import math

SCREENWIDTH = 400
SCREENHEIGHT = 400
BACKGROUND = (0, 0, 0)
FPS = 60
clock = pygame.time.Clock()
screen = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))

lineStart = (200, 200)
lineLen = 100
color = (255, 255, 255)

pygame.init()

def drawLines(width, delta, amt):
    for i in range(amt):
        deltaAmt = 0
        deltaRadian = delta*2*math.pi #converts degree amt to radians

        x = lineStart[0] + math.cos(deltaAmt) * lineLen 
        y = lineStart[1] + math.sin(deltaAmt) * lineLen
        pygame.draw.line(screen, color, lineStart, (x, y), width)
        deltaAmt += deltaRadian


    pygame.display.flip()

drawLines(4, 0, 80)
clock.tick(FPS)

The trouble I'm having is that the code, as it is, only draws one line. I need it to draw multiple lines at a consistent degree difference. For example I need to draw four lines 90 degrees from one another, or 5 lines 72 degrees from one another. I thought I made provision for this by incrementing deltaAmt by deltaRadian's current value but looking at the code again I don't think this actually accomplishes that.

Also, you'll see that for the delta parameter I have currently passed 0 as the argument. Ideally this would mean the line is drawn at the 12 o'clock position but it's instead drawn at the 3 o'clock. When I input 90 as an argument the line is positioned at the 6 o'clock position, or 180 degrees, when it should be drawn at the 3 o'clock, or 90 degree, position.

Any help would be appreciated.

Khari Thompson
  • 99
  • 1
  • 10
  • it is normal that some systems use 3 o'clock as 0 degrees - simple use `math.sin(math.radians(angle-90))` – furas Jan 05 '17 at 07:11

1 Answers1

1

Your problem is deltaAmt = 0 inside for - you reset angle before you draw every line. deltaAmt = 0 has to be before for

If you need 0 degrees at 12 o'clock then use angle-90 in all calculations

radiants = math.radians(angle-90)

I had to use round() to convert x,y to integers because pygame uses int() which rounds values in incorrect way and after 360 degrees line is not exactly vertical/horizontal.

Version with animation - it uses start_angle to rotate lines.

import pygame
import math

# --- constants --- (UPPER_CASE names)

SCREEN_WIDTH = 400
SCREEN_HEIGHT = 400

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

FPS = 60

# --- functions --- (lower_case names)

def draw_lines(screen, number, delta_angle, start_angle, width=1):

    angle = start_angle

    for i in range(number):

        radiants = math.radians(angle-90)

        x = round(line_start[0] + math.cos(radians) * line_len)
        y = round(line_start[1] + math.sin(radians) * line_len)

        pygame.draw.line(screen, line_color, line_start, (x, y), width)

        angle += delta_angle


# --- main --- (lower_case names)

# - init -

pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
screen_rect = screen.get_rect()

# - objects-

line_start = screen_rect.center
line_len = 200
line_color = WHITE

start_angle = 0

# - mainloop -

clock = pygame.time.Clock()

running = True

while running:

    # - events -

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

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False

    # - updates (without draws) -

    start_angle += 0.5

    # - draws (without updates) -

    screen.fill(BLACK)

    draw_lines(screen, 4, 90, start_angle)

    pygame.display.flip()

    # - constant speed - FPS -

    clock.tick(FPS)

# - end -

pygame.quit()

BTW: I changed names because: PEP 8 -- Style Guide for Python Code

enter image description here


BTW: see example with clocks Clock in Python

enter image description here

Community
  • 1
  • 1
furas
  • 134,197
  • 12
  • 106
  • 148