【问题标题】:How to find x,y coordinates of a circle in Pygame如何在 Pygame 中找到圆的 x,y 坐标
【发布时间】:2015-07-21 23:45:35
【问题描述】:

我目前正在尝试创建一个数字速度计。由于尝试控制表盘时遇到的问题,该项目已戛然而止。我的变量 radial_pos 旨在计算速度计每个刻度的精确 x,y 坐标,如果用户按下 K_UPradial_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()

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

【问题讨论】:

标签: math pygame coordinates trigonometry


【解决方案1】:

看来我终于找到了一个行之有效的解决方案。这是最新的代码:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-28
    • 2016-09-17
    • 1970-01-01
    相关资源
    最近更新 更多