view atc_message.py @ 45:c93c3c910c85

Updated headers/copyrights ATC is now licensed under GPLv2 only
author Josef "Jeff" Sipek <jeffpc@josefsipek.net>
date Fri, 01 Jun 2007 02:04:37 -0400
parents f346ce878117
children
line wrap: on
line source

#/*
# * ATC - Air Traffic Controller simulation game
# *
# * Copyright (C) 2004-2007 Josef "Jeff" Sipek <jeffpc@josefsipek.net>
# *
# * This program is free software; you can redistribute it and/or modify
# * it under the terms of the GNU General Public License version 2 as
# * published by the Free Software Foundation.
# *
# * This program is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# * GNU General Public License for more details.
# */

import pygame
import threading

class Message:
	""" Interface to display a message and beep it off in morse code """
	def __init__(self):
		""" Set up everything """
		self.snd = {}
		self.snd["."] = pygame.mixer.Sound('data/sound/sbeep.wav')
		self.snd["."].set_volume(0.4)
		self.snd["-"] = pygame.mixer.Sound('data/sound/lbeep.wav')
		self.snd["-"].set_volume(0.4)
		self.snd[" "] = pygame.mixer.Sound('data/sound/nosnd.wav')
		self.snd[" "].set_volume(0.4)
		
		self.delay = {}
		self.delay["."] = 250
		self.delay["-"] = 400
		self.delay[" "] = 330
		
		self.morse = {}
		self.morse["A"] = ".-"
		self.morse["B"] = "-..."
		self.morse["C"] = "-.-."
		self.morse["D"] = "-.."
		self.morse["E"] = "."
		self.morse["F"] = "..-."
		self.morse["G"] = "--."
		self.morse["H"] = "...."
		self.morse["I"] = ".."
		self.morse["J"] = ".---"
		self.morse["K"] = "-.-"
		self.morse["L"] = ".-.."
		self.morse["M"] = "--"
		self.morse["N"] = "-."
		self.morse["O"] = "---"
		self.morse["P"] = ".--."
		self.morse["Q"] = "--.-"
		self.morse["R"] = ".-."
		self.morse["S"] = "..."
		self.morse["T"] = "-"
		self.morse["U"] = "..-"
		self.morse["V"] = "...-"
		self.morse["W"] = ".--"
		self.morse["X"] = "-..-"
		self.morse["Y"] = "-.--"
		self.morse["Z"] = "--.."
		self.morse["1"] = ".----"
		self.morse["2"] = "..---"
		self.morse["3"] = "...--"
		self.morse["4"] = "....-"
		self.morse["5"] = "....."
		self.morse["6"] = "-...."
		self.morse["7"] = "--..."
		self.morse["8"] = "---.."
		self.morse["9"] = "----."
		self.morse["0"] = "-----"
		self.morse[" "] = " "
		self.morse[":"] = ""

		self.statusmess = ""
		
		self.messages = []
		
		self.pickup_kill = 0
		self.pickup_thread = threading.Timer(0, self.__pickup)
		self.pickup_thread.start()

	def write(self,screen,mess):
		""" Add message to the queue """
		code = ""
		for letter in mess:
			code += self.morse[letter] + " "

		self.messages.append(code)

	def __beep(self,char):
		""" Beep one . or - """
		self.snd[char].play()

	def __beeper(self,mess):
		""" Beep out all . and - in a message """
		for char in mess:
			self.__beep(char)
			pygame.time.delay(self.delay[char])
			if (self.pickup_kill):
				return

	def __pickup(self):
		""" The message pickup thread code, it picks up new messages
		and calls the appropreate functions to beep them out """
		while(not self.pickup_kill):
			try:
				message = self.messages.pop()
				self.statusmess = message
				self.__beeper(message)
				pygame.time.delay(1400)
				self.statusmess = ""
			except IndexError:
				pass
			
			pygame.time.delay(100)