在雷达上播放声音

时间:2017-05-20 19:27:05

标签: python audio pygame playback

我有一个圆形雷达。 " arm"雷达以恒定速度旋转。我需要雷达上的光点才能在它们通过时发出声音。

我如何解决这个问题,以便每次革命都能保证一击?

(使用pygame)

我已经尝试使用if语句来捕捉" arm"的(x,y)坐标。和blips是等价的。但是,我不能取得一致的成功。我也试过进行斜率测量和余弦测量,并检查它们是否在手臂和光点之间相等;我没有实时获得一致的成功,并且当有击中时,通常有两个在圆圈上cos相同的位置进行。因为手臂的精确到小数位多于小数位,所以我很难看到if语句的好处。我不知道怎么回事。任何想法将不胜感激!

janky radar

代码段:

import pygame
from pygame.locals import *
import math

SIZE = 800, 800

screen = pygame.display.set_mode(SIZE, pygame.HWSURFACE | pygame.DOUBLEBUF)
done = False
bpm = 180 # based on a metronome to easily modulate speed
bbpm = 4
spm = bbpm*60/bpm
turnsPerMs = 1/(1000*spm) 
startTime = pygame.time.get_ticks()
color = Color("red")
CENTER = (400,400)
RADIUS = 200
lineLen = 100
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 200)
BACKGROUND = (218, 226, 235)


pygame.init()

class Blip:
    def __init__(self):
        self.mouse = (0, 0)
        self.vector = (self.mouse[0] - CENTER[0], self.mouse[1] - CENTER[1])
        self.distance = math.sqrt(self.vector[0] ** 2 + self.vector[1] ** 2)
        self.scalar = RADIUS / self.distance
        self.lineend = (int(round(CENTER[0] + self.vector[0] * self.scalar)), int(round(CENTER[1] + self.vector[1] * self.scalar)))
        self.radius = 20
        self.position = (self.lineend[0] - self.radius, self.lineend[0] + self.radius, self.lineend[1] - self.radius,
                        self.lineend[1] + self.radius)
        self.o = 255
        self.s = 200
        self.t = 150

"""def load_sound(self, filename):
        print(filename)
        self.sound = pygame.mixer.music.load(filename)"""

   def play_sound(self):
        print("Blip!")

   def draw_blip(self):
        pygame.draw.circle(screen, (100, 150, 200), self.lineend, self.radius)

class TickArm:
    def __init__(self):
    self.timeDelta = 0
    self.revDelta = 0
    self.deltaRadian = 0
    self.x = SIZE[0]/2
    self.y = SIZE[1]/2

    def draw_tickarm(self):
        self.timeDelta = pygame.time.get_ticks() - startTime
        self.revDelta = self.timeDelta*turnsPerMs;
        self.deltaRadian = self.revDelta*2*math.pi;
        self.x = CENTER[0] + math.cos(self.deltaRadian) * RADIUS
        self.y = CENTER[1] + math.sin(self.deltaRadian) * RADIUS

        # then render the line ->(x,y)
        pygame.draw.line(screen, color, CENTER, (self.x,self.y), 1)

tickarm = TickArm()
blip = Blip()

while not done:
    for e in pygame.event.get():
        if e.type == QUIT or (e.type == KEYDOWN and e.key == K_ESCAPE):
            done = True
            break

    screen.fill((BACKGROUND))
    pygame.draw.circle(screen, color, CENTER, RADIUS, 2)
    pygame.draw.circle(screen, color, CENTER, RADIUS-25, 2)
    blip.draw_blip()
    tickarm.draw_tickarm()
    pygame.display.flip()

1 个答案:

答案 0 :(得分:1)

没有足够的评论来评论。抱歉! 但你可以试试这个:

screen.get_at(x,y)

怎么去: 将所有blip的中心存储在元组center_lists中

for i in center_lists:
    if screen.get_at(i) == green:
        #play the sound

您要做的是检查每个帧上所有光点的中心颜色。如果它们是绿色的,你可以播放声音。您甚至可以进一步为不同类型的blip创建具有不同声音的另一个元组,并使用迭代器计数i来播放相应的一个。