圆面上的区间指标

时间:2017-01-05 04:50:37

标签: python timer pygame 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)

我遇到的麻烦是,代码实际上只绘制了一行。我需要它以一致的程度差异绘制多条线。例如,我需要绘制彼此相差90度的四条线,或彼此相距72度的5条线。我认为我通过将deltaAmt增加deltaRadian的当前值来为此做好准备但是再次查看代码我不认为这实际上完成了。

另外,你会看到delta参数我当前传递了0作为参数。理想情况下,这意味着线条在12点钟位置绘制,而是在3点钟位置绘制。当我输入90作为参数时,线应位于6点钟位置,或180度,当它应在3点钟或90度位置绘制时。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

你的问题是deltaAmt = 0 for - 你在绘制每一行之前重置角度。 deltaAmt = 0必须在for

之前

如果您在12点钟时需要0度,则在所有计算中使用angle-90

radiants = math.radians(angle-90)

我必须使用round()x,y转换为整数,因为pygame使用int()以不正确的方式舍入值并且在360度线不完全垂直/水平之后

带动画的版本 - 它使用start_angle来旋转线条。

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:我更改了名称,因为:PEP 8 -- Style Guide for Python Code

enter image description here

BTW:查看时钟Clock in Python

的示例

enter image description here