view atc_game.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 1757b7e8d09f
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 sys

import atc_colors
import atc_utils
import atc_plane
import atc_message
import atc_weather

from atc_maps import *

class Game:
	def __init__(self, screen):
		# background music
		#atc_utils.playmusic("some.mp3");

		self.displays = {
			"map": 1,
			"weather": 0,
			"navaids": 0,
			"menus": 0,
			"planes": 1}
	
		self.map = MAPS[0] # FIXME: add param to chose map
		self.map.bootstrap()
		
		# set up the display layers as much as possible
		self.screen		= screen
		self.screen_void	= pygame.Surface(self.screen.get_size()).convert_alpha()
		self.screen_map		= pygame.Surface(self.screen.get_size()).convert_alpha()
		self.screen_weather	= pygame.Surface(self.screen.get_size()).convert_alpha()
		self.screen_navaids	= pygame.Surface(self.screen.get_size()).convert_alpha()
		self.screen_menus	= pygame.Surface(self.screen.get_size()).convert_alpha()
		self.screen_planes	= pygame.Surface(self.screen.get_size()).convert_alpha()
		self.screen_cmd		= pygame.Surface(self.screen.get_size()).convert_alpha()
	
		self.screen_void.fill   (atc_colors.black)
		self.screen_map.fill    (atc_colors.transparent)
		self.screen_weather.fill(atc_colors.transparent)
		self.screen_navaids.fill(atc_colors.transparent)
		self.screen_menus.fill  (atc_colors.transparent)
		self.screen_cmd.fill    (atc_colors.transparent)

		(back_image, back_rect) = atc_utils.load_png(self.map.image)
		self.screen_map.blit(back_image, (0, 0))

		#blit!
		self.screen.blit(self.screen_map, (0, 0))
		pygame.display.flip()

		self.planes = []
		self.planes.append(self.__random_plane())
		
		self.weather = atc_weather.Weather()
	
		self.mess = atc_message.Message()

		self.cmd = ""
	
	def __del__(self):
		self.map.cleanup()
	
	def __proc_cmd(self):
		flno = cmd_plane(self.cmd)
		com  = cmd_cmd(self.cmd)
		
		flag = None
		if (com!=None) and (flno!=None):
			print "plane = " + flno + ", cmd = " + com
			for plane in self.planes:
				if plane.flightno.upper() == flno.upper():
					print "Found match!"
					plane.process(com)
					flag = 1
					break
	
		if (flag == None):
			# either not the right syntax, or we
			# didn't find the right plane
			print "Error, could not process command"
	
		self.cmd = ""
	
	def __proc_events(self):
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				self.mess.pickup_kill = 1
				sys.exit()
			if event.type == pygame.KEYDOWN:
				if event.key == pygame.K_a:
					self.cmd += 'A'
				if event.key == pygame.K_b:
					self.cmd += 'B'
				if event.key == pygame.K_c:
					self.cmd += 'C'
				if event.key == pygame.K_d:
					self.cmd += 'D'
				if event.key == pygame.K_e:
					self.cmd += 'E'
				if event.key == pygame.K_f:
					self.cmd += 'F'
				if event.key == pygame.K_g:
					self.cmd += 'G'
				if event.key == pygame.K_h:
					self.cmd += 'H'
				if event.key == pygame.K_i:
					self.cmd += 'I'
				if event.key == pygame.K_j:
					self.cmd += 'J'
				if event.key == pygame.K_k:
					self.cmd += 'K'
				if event.key == pygame.K_l:
					self.cmd += 'L'
				if event.key == pygame.K_m:
					self.cmd += 'M'
				if event.key == pygame.K_n:
					self.cmd += 'N'
				if event.key == pygame.K_o:
					self.cmd += 'O'
				if event.key == pygame.K_p:
					self.cmd += 'P'
				if event.key == pygame.K_q:
					self.cmd += 'Q'
				if event.key == pygame.K_r:
					self.cmd += 'R'
				if event.key == pygame.K_s:
					self.cmd += 'S'
				if event.key == pygame.K_t:
					self.cmd += 'T'
				if event.key == pygame.K_u:
					self.cmd += 'U'
				if event.key == pygame.K_v:
					self.cmd += 'V'
				if event.key == pygame.K_w:
					self.cmd += 'W'
				if event.key == pygame.K_x:
					self.cmd += 'X'
				if event.key == pygame.K_y:
					self.cmd += 'Y'
				if event.key == pygame.K_z:
					self.cmd += 'Z'
				
				if (event.key == pygame.K_0) or (event.key == pygame.K_KP0):
					self.cmd += '0'
				if (event.key == pygame.K_1) or (event.key == pygame.K_KP1):
					self.cmd += '1'
				if (event.key == pygame.K_2) or (event.key == pygame.K_KP2):
					self.cmd += '2'
				if (event.key == pygame.K_3) or (event.key == pygame.K_KP3):
					self.cmd += '3'
				if (event.key == pygame.K_4) or (event.key == pygame.K_KP4):
					self.cmd += '4'
				if (event.key == pygame.K_5) or (event.key == pygame.K_KP5):
					self.cmd += '5'
				if (event.key == pygame.K_6) or (event.key == pygame.K_KP6):
					self.cmd += '6'
				if (event.key == pygame.K_7) or (event.key == pygame.K_KP7):
					self.cmd += '7'
				if (event.key == pygame.K_8) or (event.key == pygame.K_KP8):
					self.cmd += '8'
				if (event.key == pygame.K_9) or (event.key == pygame.K_KP9):
					self.cmd += '9'
				
				if (event.key == pygame.K_COLON) or (event.key == pygame.K_SEMICOLON):
					self.cmd += ': '
				
				if event.key == pygame.K_SPACE:
					self.cmd += " "
								
				if event.key == pygame.K_ESCAPE:
					self.cmd = ""
				if event.key == pygame.K_BACKSPACE:
					self.cmd = self.cmd[:-1]
				
				if event.key == pygame.K_TAB:
					pass	# FIXME, tab complete call signs/flight numbers, commands, etc.
			
				if event.key == pygame.K_F1:
					self.displays["map"] = 1 - self.displays["map"]
				if event.key == pygame.K_F2:
					self.displays["weather"] = 1 - self.displays["weather"]
				if event.key == pygame.K_F3:
					self.displays["navaids"] = 1 - self.displays["navaids"]
				if event.key == pygame.K_F4:
					self.displays["menus"] = 1 - self.displays["menus"]
				if event.key == pygame.K_F5:
					self.displays["planes"] = 1 - self.displays["planes"]
			
				if (event.key == pygame.K_KP_ENTER) or (event.key == pygame.K_RETURN):
					if (self.cmd == "QUIT"):
						self.mess.pickup_kill = 1
						return False
			
					if (self.cmd.__len__()):
						self.__proc_cmd()
		
		return True

	def __random_plane(self):
		callsign	= "N1499H"
		flightno	= "Delta 79"
		squawk	= "1200"
		type		= "B747"
		vel		= (20, 20, 20)	# FIXME: use the specs

		return atc_plane.Plane(type=type, callsign=callsign, flightno=flightno, squawk=squawk, vel=vel)

	def run(self):
		if not self.__proc_events(): return False

		# planes		
		self.screen_planes.fill(atc_colors.transparent)
		for plane in self.planes:
			plane.display(self.screen_planes)
			plane.update()
			if (plane.status == atc_plane.plane_CRASHED):
				mess.write(self.screen_planes, plane.callsign + ": PLANE CRASHED")
				plane.status = atc_plane.plane_DEAD
		
		# weather
		self.weather.display(self.screen_weather)
		self.weather.update()
		
		# command
		self.screen_cmd.fill(atc_colors.transparent)
		if (self.cmd):
			font = pygame.font.Font(None, 18)
			text = font.render(self.cmd, 1, atc_colors.white)
			self.screen_cmd.blit(text, (25, 735))
		
		# blit what's needed
		self.screen.blit(self.screen_void, (0, 0))
		if (self.displays["map"]):
			self.screen.blit(self.screen_map, (0, 0))
		if (self.displays["weather"]):
			self.screen.blit(self.screen_weather, (0, 0))
		if (self.displays["navaids"]):
			self.screen.blit(self.screen_navaids, (0, 0))
		if (self.displays["menus"]):
			self.screen.blit(self.screen_menus, (0, 0))
		if (self.displays["planes"]):
			self.screen.blit(self.screen_planes, (0, 0))
		self.screen.blit(self.screen_cmd, (0, 0))
		
		pygame.display.flip()
		
		return True

def cmd_plane(cmd):
	parts = cmd.split(': ')
	
	if (parts.__len__()>1):
		return parts[0]
	return None

def cmd_cmd(cmd):
	parts = cmd.split(': ')
	
	if (parts.__len__()>1):
		return parts[1]
	return None

if (__name__ == '__main__'):
	print "Don't do it!"