如何在Pygame中找到圆的x,y坐标

时间:2015-07-21 23:45:35

标签: math pygame coordinates sin cos

我目前正在尝试制作数字车速表。由于试图控制表盘时遇到问题,该项目已暂停。我的变量 radial_pos 用于计算车速表每个刻度的精确x,y坐标,如果用户按 K_UP radial_pos 将增加按1显示车速表的下一个刻度。但是,自从我学会了找到圆的坐标的概念以来,我担心它已经太多年了,更不用说理解它足以能够相应地增加它。到目前为止,这是我的代码:

import pygame, sys
from pygame.locals import *

pygame.init()

#Screen Resolution
width = 1080 
height = 720

#Instrument Characteristics
color = (255,255,255)
background = (0,0,0)

radius_speedometer = 100
weight_speedometer = radius_speedometer-5

#Window Measurements
screen= pygame.display.set_mode((width,height),0,32)
pos_center_x = width/2
pos_center_y = height/2

#Speedometer Dial Position
pos_speedometer_dial_x = pos_center_x - 60
pos_speedometer_dial_y = pos_center_y +60

#Radial Position (hint: ticks on the clock)
'''
radial_pos = ...
'''

while True:
    for event in pygame.event.get():
        if event.type==QUIT:
            pygame.quit()
            sys.exit()

        if event.type==KEYDOWN:
            if event.key==K_DOWN:
                radial_pos=-1
            if event.key==K_UP:
                radial_pos=+1

        if event.type==KEYUP:
            if event.key==K_DOWN:
                radial_pos=0
            if event.key==K_UP:
                radial_pos=0

    screen.lock()

    #Speedometer Ring
    pygame.draw.circle(screen, color, (pos_center_x,pos_center_y),      radius_speedometer)
    pygame.draw.circle(screen, background, (pos_center_x,pos_center_y), weight_speedometer)

    #Speedometer Dial
    pygame.draw.line(screen, color, (pos_center_x,pos_center_y), (pos_speedometer_dial_x, pos_speedometer_dial_y),5)

    screen.unlock()

    pygame.display.update()

如果有人对此特定问题有任何建议,我们将不胜感激。谢谢你的时间。

1 个答案:

答案 0 :(得分:0)

似乎我终于找到了一个效果很好的解决方案。这是最新的代码:

import pygame, sys
from pygame.locals import *
from math import *

pygame.init()

#Screen Resolution
width = 1080 
height = 720

#Instrument Characteristics
color = (255,255,255)
background = (0,0,0)

radius_speedometer = 100
weight_speedometer = radius_speedometer-5

#Window Measurements
screen= pygame.display.set_mode((width,height),0,32)
pos_center_x = width/2
pos_center_y = height/2

#Speedometer Dial Position
pos_speedometer_dial_x = pos_center_x - 60
pos_speedometer_dial_y = pos_center_y +60

#Radial Position (hint: ticks on the clock)
radial_pos = 70
move_rad = 0

while True:
    for event in pygame.event.get():
        if event.type==QUIT:
            pygame.quit()
            sys.exit()


        if event.type==KEYDOWN:
            if event.key==K_DOWN:
                move_rad=-0.01
            if event.key==K_UP:
                move_rad=+0.01

        if event.type==KEYUP:
            if event.key==K_DOWN:
                move_rad=-0.05
            if event.key==K_UP:
                move_rad=-0.005

    angle = 2.0*pi*radial_pos/120.0

    radial_pos+=move_rad

    pos_speedometer_dial_x = pos_center_x + (radius_speedometer-10)*sin(angle)
    pos_speedometer_dial_y = pos_center_y - (radius_speedometer-10)*cos(angle)

    screen.lock()

    #Speedometer Ring
    pygame.draw.circle(screen, color, (pos_center_x,pos_center_y), radius_speedometer)
    pygame.draw.circle(screen, background, (pos_center_x,pos_center_y), weight_speedometer)

    #Speedometer Dial
    pygame.draw.line(screen, color, (pos_center_x,pos_center_y), (pos_speedometer_dial_x, pos_speedometer_dial_y),5)

    screen.unlock()

    pygame.display.update()

它并不完美(例如还没有零),但是我们现在更接近完成第一个仪表盘组合仪器了!