Sprites = [] #Manages all sprites
#Red Stat Section
REDHEALTH = 1000
REDGOLD = 50
REDCOMMAND = 100
#Blue Stat Section
BLUEHEALTH = 1000
BLUEGOLD = 50
BLUECOMMAND = 100
PHASE = 1
TURN = 'Red'
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#The main function
def main():
	pygame.init()
	pygame.display.set_caption('Table Wars')
	background = pygame.Surface(screen.get_size())
	background = background.convert()
	background.fill((250, 250, 250))
	global REDGOLD
	global REDCOMMAND
	global REDHEALTH
	global BLUEGOLD
	global BLUECOMMAND
	global BLUEHEALTH
	global PHASE
	global TURN
	global mousex
	global mousey
	screen.blit(background, (0, 0))
	pygame.display.flip()
	clock = pygame.time.Clock()
#this block initialises sprites as variables
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	field = Playfield()
	rbase = Red_Base()
	bbase = Blu_Base()
	hud = HUD_main()
	cred = HUD_cred()
	cblu = HUD_cblu()
	hpred = HUD_hred()
	hpblu = HUD_hblu()
	healthred = Red_Health()
	goldred = Red_Gold()
	cmndred = Red_Command()
	healthblu = Blu_Health()
	goldblu = Blu_Gold()
	cmndblu = Blu_Command()
	turnmanage = Turn_step()
	redI = Red_Infantry()
	redA = Red_Archer()
	redC = Red_Catapult()
	redAn = Red_Angel()
	bluI = Blue_Infantry()
	bluA = Blue_Archer()
	bluC = Blue_Catapult()
	bluAn = Blue_Angel()
	turn = Turn_colour()
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#this block appends each HUD sprite to the Sprites array...
	Sprites.append(field)
	Sprites.append(rbase)
	Sprites.append(bbase)
	Sprites.append(hud)
	Sprites.append(cred)
	Sprites.append(cblu)
	Sprites.append(hpred)
	Sprites.append(hpblu)
	Sprites.append(healthred)
	Sprites.append(goldred)
	Sprites.append(cmndred)
	Sprites.append(healthblu)
	Sprites.append(goldblu)
	Sprites.append(cmndblu)
	Sprites.append(turnmanage)
	Sprites.append(turn)
#...and then loads it on the screen.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#main loop
	running = True
	spawned_units = 0
	while running:
		clock.tick(60)
		mouseX, mouseY = pygame.mouse.get_pos()
		for event in pygame.event.get():
			if event.type == QUIT:
				running = False
			if PHASE == 4:
				if TURN == 'Red':
					TURN = 'Blue'
					PHASE = 1
					print "It is now the Red Team's turn."
				elif TURN == 'Blue':
					TURN = 'Red'
					PHASE = 1
					print "It is now the Red Team's turn. As both players took their turn, they are awarded with 25 Gold"
					REDGOLD = REDGOLD + 25
					BLUEGOLD = BLUEGOLD + 25
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
##The phases are in reverse order
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	#this is the Attacking code block for each unit
			if PHASE == 3 and TURN == 'Red':
				if event.type == MOUSEBUTTONDOWN and event.button == 1:
					if mouseX in range(redI.rect.left, redI.rect.right) and mouseY in range(redI.rect.top, redI.rect.bottom):
						if redI.selected == 0:
							print 'Red infantry ready to fight!'
							redI.selected = 1
							redA.selected = 0
							redC.selected = 0
							redAn.selected = 0
						else:
							print 'Already selected!'
					elif mouseX in range(redA.rect.left, redA.rect.right) and mouseY in range(redA.rect.top, redA.rect.bottom):
						if redA.selected == 0:
							print 'Red Archer ready to fight!'
							redI.selected = 0
							redA.selected = 1
							redC.selected = 0
							redAn.selected = 0
						else:
							print 'Already selected!'
					elif mouseX in range(redC.rect.left, redC.rect.right) and mouseY in range(redC.rect.top, redC.rect.bottom):
						if redC.selected == 0:
							print 'Red Catapult ready to fight!'
							redI.selected = 0
							redA.selected = 0
							redC.selected = 1
							redAn.selected = 0
						else:
							print 'Already selected!'
					elif mouseX in range(redAn.rect.left, redAn.rect.right) and mouseY in range(redAn.rect.top, redAn.rect.bottom):
						if redAn.selected == 0:
							print 'Red Angel ready to fight!'
							redI.selected = 0
							redA.selected = 0
							redC.selected = 0
							redAn.selected = 1
						else:
							print 'Already selected!'
				if event.type == MOUSEBUTTONDOWN and event.button == 3:
					#check to see if the target is in range
					if mouseX in range(bluI.rect.left, bluI.rect.right) and mouseY in range(bluI.rect.top, bluI.rect.bottom):
						if redI.selected == 1:
							distance = abs(bluI.rect.center[0] - redI.rect.center[0]) + abs(bluI.rect.center[1] - redI.rect.center[1])
							if distance >= 0 and distance <= 20:
								print 'The red soldier attacks the blue soldier. he takes 25 damage'
								bluI.health = bluI.health - 25
								PHASE = 4
								redI.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
								redI.selected = 0
						if redA.selected == 1:
							distance = abs(bluI.rect.center[0] - redA.rect.center[0]) + abs(bluI.rect.center[1] - redA.rect.center[1])
							if distance >= 50 and distance <= 300:
								print 'The red archer attacks the blue soldier. he takes 35 damage'
								bluI.health = bluI.health - 35
								PHASE = 4
								redA.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
								redA.selected = 0
						if redC.selected == 1:
							distance = abs(bluC.rect.center[0] - redC.rect.center[0]) + abs(bluC.rect.center[1] - redC.rect.center[1])
							if distance >= 75 and distance <= 100:
								print 'The red catapult attacks the blue soldier. he takes 45 damage'
								bluI.health = bluI.health - 45
								PHASE = 4
								redC.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
								redC.selected = 0
						if redAn.selected == 1:
							distance = abs(bluAn.rect.center[0] - redAn.rect.center[0]) + abs(bluAn.rect.center[1] - redAn.rect.center[1])
							if distance >= 30 and distance <= 200:
								print 'The red angel attacks the blue soldier. he takes 50 damage'
								bluI.health = bluI.health - 50
								PHASE = 4
								redAn.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
								redAn.selected = 0
					if mouseX in range(bluA.rect.left, bluA.rect.right) and mouseY in range(bluA.rect.top, bluA.rect.bottom):
						if redI.selected == 1:
							distance = abs(bluA.rect.center[0] - redI.rect.center[0]) + abs(bluA.rect.center[1] - redI.rect.center[1])
							if distance >= 0 and distance <= 20:
								print 'The red soldier attacks the blue archer. He takes 25 damage'
								bluA.health = bluA.health - 25
								PHASE = 4
								redI.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
								redI.selected = 0
						if redA.selected == 1:
							distance = abs(bluA.rect.center[0] - redA.rect.center[0]) + abs(bluA.rect.center[1] - redA.rect.center[1])
							if distance >= 50 and distance <= 300:
								print 'The red archer attacks the blue archer. he takes 35 damage'
								bluA.health = bluA.health - 35
								PHASE = 4
								redA.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
								redA.selected = 0
						if redC.selected == 1:
							distance = abs(bluA.rect.center[0] - redC.rect.center[0]) + abs(bluA.rect.center[1] - redC.rect.center[1])
							if distance >= 75 and distance <= 100:
								print 'The red catapult attacks the blue archer'
								bluA.health = bluA.health - 45
								PHASE = 4
								redC.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
						if redAn.selected == 1:
							distance = abs(bluA.rect.center[0] - redAn.rect.center[0]) + abs(bluA.rect.center[1] - redAn.rect.center[1])
							if distance >= 50 and distance <= 300:
								print 'The red angel attacks the blue archer'
								bluA.health = bluA.health - 50
								PHASE = 4
								redAn.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
					if mouseX in range(bluC.rect.left, bluC.rect.right) and mouseY in range(bluC.rect.top, bluC.rect.bottom):
						if redI.selected == 1:
							distance = abs(bluC.rect.center[0] - redI.rect.center[0]) + abs(bluC.rect.center[1] - redI.rect.center[1])
							if distance >= 0 and distance <= 20:
								print 'The red soldier attacks the blue catapult'
								bluC.health = bluC.health - 25
								PHASE = 4
								redI.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
						if redA.selected == 1:
							distance = abs(bluC.rect.center[0] - redA.rect.center[0]) + abs(bluC.rect.center[1] - redA.rect.center[1])
							if distance >= 50 and distance <= 300:
								print 'The red archer attacks the blue catapult'
								bluC.health = bluC.health - 35
								PHASE = 4
								redA.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
						if redC.selected == 1:
							distance = abs(bluC.rect.center[0] - redC.rect.center[0]) + abs(bluC.rect.center[1] - redC.rect.center[1])
							if distance >= 75 and distance <= 100:
								print 'The red catapult attacks the blue catapult'
								bluC.health = bluC.health - 45
								PHASE = 4
								redC.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
						if redAn.selected == 1:
							distance = abs(bluC.rect.center[0] - redAn.rect.center[0]) + abs(bluC.rect.center[1] - redAn.rect.center[1])
							if distance >= 30 and distance <= 200:
								print 'The red angel attacks the blue catapult'
								bluC.health = bluC.health - 50
								PHASE = 4
								redAn.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
					if mouseX in range(bluAn.rect.left, bluAn.rect.right) and mouseY in range(bluAn.rect.top, bluAn.rect.bottom):
						if redI.selected == 1:
							print "The red soldier misses! A swordsman can't hit an angel!"
							redI.selected = 0
						if redA.selected == 1:
							distance = abs(bluAn.rect.center[0] - redA.rect.center[0]) + abs(bluAn.rect.center[1] - redA.rect.center[1])
							if distance >= 50 and distance <= 300:
								print 'The red archer attacks the blue angel'
								bluAn.health = bluAn.health - 35
								PHASE = 4
								redA.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
						if redC.selected == 1:
							print "The red catapult misses! A catapult can't hit an angel!"
							redC.selected = 0
						if redAn.selected == 1:
							distance = abs(bluAn.rect.center[0] - redAn.rect.center[0]) + abs(bluAn.rect.center[1] - redAn.rect.center[1])
							if distance >= 30 and distance <= 200:
								print 'The red angel attacks the blue angel'
								bluAn.health = bluAn.health - 50
								PHASE = 4
								redAn.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	#this is the Attacking code block for each Blue unit
			if PHASE == 3 and TURN == 'Blue':
				if event.type == MOUSEBUTTONDOWN and event.button == 1:
					if mouseX in range(bluI.rect.left, bluI.rect.right) and mouseY in range(bluI.rect.top, bluI.rect.bottom):
						if bluI.selected == 0:
							print 'Blue infantry ready to fight!'
							bluI.selected = 1
							bluA.selected = 0
							bluC.selected = 0
							bluAn.selected = 0
						else:
							print 'Already selected!'
					elif mouseX in range(bluA.rect.left, bluA.rect.right) and mouseY in range(bluA.rect.top, bluA.rect.bottom):
						if bluA.selected == 0:
							print 'Blue Archer ready to fight!'
							bluI.selected = 0
							bluA.selected = 1
							bluC.selected = 0
							bluAn.selected = 0
						else:
							print 'Already selected!'
					elif mouseX in range(bluC.rect.left, bluC.rect.right) and mouseY in range(bluC.rect.top, bluC.rect.bottom):
						if bluC.selected == 0:
							print 'Blue Catapult ready to fight!'
							bluI.selected = 0
							bluA.selected = 0
							bluC.selected = 1
							bluAn.selected = 0
						else:
							print 'Already selected!'
					elif mouseX in range(bluAn.rect.left, bluAn.rect.right) and mouseY in range(bluAn.rect.top, bluAn.rect.bottom):
						if bluAn.selected == 0:
							print 'Blue Angel ready to fight!'
							bluI.selected = 0
							bluA.selected = 0
							bluC.selected = 0
							bluAn.selected = 1
						else:
							print 'Already selected!'
				if event.type == MOUSEBUTTONDOWN and event.button == 3:
					#check to see if the target is in range
					if mouseX in range(redI.rect.left, redI.rect.right) and mouseY in range(redI.rect.top, redI.rect.bottom):
						if bluI.selected == 1:
							distance = abs(redI.rect.center[0] - bluI.rect.center[0]) + abs(redI.rect.center[1] - bluI.rect.center[1])
							if distance >= 0 and distance <= 20:
								print 'The blue soldier attacks the red soldier'
								redI.health = redI.health - 25
								PHASE = 4
								bluI.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
						if bluA.selected == 1:
							distance = abs(redI.rect.center[0] - bluA.rect.center[0]) + abs(redI.rect.center[1] - bluA.rect.center[1])
							if distance >= 50 and distance <= 300:
								print 'The blue archer attacks the red soldier'
								redI.health = redI.health - 35
								PHASE = 4
								bluA.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
						if bluC.selected == 1:
							distance = abs(redI.rect.center[0] - bluC.rect.center[0]) + abs(redI.rect.center[1] - bluC.rect.center[1])
							if distance >= 75 and distance <= 100:
								print 'The blue catapult attacks the red soldier'
								redI.health = redI.health - 45
								PHASE = 4
								bluC.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
						if bluAn.selected == 1:
							distance = abs(redI.rect.center[0] - bluAn.rect.center[0]) + abs(redI.rect.center[1] - bluAn.rect.center[1])
							if distance >= 30 and distance <= 200:
								print 'The blue angel attacks the red soldier'
								redI.health = redI.health - 50
								PHASE = 4
								bluAn.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
								bluAn.selected = 0
					if mouseX in range(redA.rect.left, bluA.rect.right) and mouseY in range(redA.rect.top, bluA.rect.bottom):
						if bluI.selected == 1:
							distance = abs(redA.rect.center[0] - bluI.rect.center[0]) + abs(redA.rect.center[1] - bluI.rect.center[1])
							if distance >= 0 and distance <= 20:
								print 'The blue soldier attacks the red archer'
								redA.health = redA.health - 25
								PHASE = 4
								bluI.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
								bluI.selected = 0
						if bluA.selected == 1:
							distance = abs(redA.rect.center[0] - bluA.rect.center[0]) + abs(redA.rect.center[1] - bluA.rect.center[1])
							if distance >= 50 and distance <= 300:
								print 'The blue archer attacks the red archer'
								redA.health = redA.health - 35
								PHASE = 4
								bluA.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
								bluA.selected = 0
						if bluC.selected == 1:
							distance = abs(redA.rect.center[0] - bluC.rect.center[0]) + abs(redA.rect.center[1] - bluC.rect.center[1])
							if distance >= 75 and distance <= 100:
								print 'The blue catapult attacks the red archer'
								redA.health = redA.health - 35
								PHASE = 4
								bluC.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
								bluC.selected = 0
						if bluAn.selected == 1:
							distance = abs(redA.rect.center[0] - bluAn.rect.center[0]) + abs(redA.rect.center[1] - bluAn.rect.center[1])
							if distance >= 30 and distance <= 200:
								print 'The blue angel attacks the red archer'
								redA.health = redA.health - 50
								PHASE = 4
								bluAn.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
								bluAn.selected = 0
					if mouseX in range(redC.rect.left, redC.rect.right) and mouseY in range(redC.rect.top, redC.rect.bottom):
						if bluI.selected == 1:
							distance = abs(redC.rect.center[0] - bluI.rect.center[0]) + abs(redC.rect.center[1] - bluI.rect.center[1])
							if distance >= 0 and distance <= 20:
								print 'The blue soldier attacks the red catapult'
								redC.health = redC.health - 25
								PHASE = 4
								bluI.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
								bluI.selected = 0
						if bluA.selected == 1:
							distance = abs(redC.rect.center[0] - bluA.rect.center[0]) + abs(redC.rect.center[1] - bluA.rect.center[1])
							if distance >= 50 and distance <= 300:
								print 'The blue archer attacks the red catapult'
								redC.health = redC.health - 35
								PHASE = 4
								bluA.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
								bluA.selected = 0
						if bluC.selected == 1:
							distance = abs(redC.rect.center[0] - bluC.rect.center[0]) + abs(redC.rect.center[1] - bluC.rect.center[1])
							if distance >= 75 and distance <= 100:
								print 'The blue catapult attacks the red catapult. It takes 90 damage!'
								redC.health = redC.health - 90
								PHASE = 4
								bluC.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
								bluC.selected = 0
						if bluAn.selected == 1:
							distance = abs(redC.rect.center[0] - bluAn.rect.center[0]) + abs(redC.rect.center[1] - bluAn.rect.center[1])
							if distance >= 30 and distance <= 200:
								print 'The blue angel attacks the red catapult'
								redC.health = redC.health - 50
								PHASE = 4
								bluAn.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
								bluAn.selected = 0
					if mouseX in range(redAn.rect.left, redAn.rect.right) and mouseY in range(redAn.rect.top, redAn.rect.bottom):
						if bluI.selected == 1:
							print "The blue soldier misses! A swordsman can't hit an angel!"
						if bluA.selected == 1:
							distance = abs(redAn.rect.center[0] - bluA.rect.center[0]) + abs(redAn.rect.center[1] - bluA.rect.center[1])
							if distance >= 50 and distance <= 300:
								print 'The blue archer attacks the red angel'
								redAn.health = redAn.health - 35
								PHASE = 4
								bluA.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
								bluA.selected = 0
						if bluC.selected == 1:
							print "The blue catapult misses! A catapult can't hit an angel!"
						if bluAn.selected == 1:
							distance = abs(redAn.rect.center[0] - bluAn.rect.center[0]) + abs(redAn.rect.center[1] - bluAn.rect.center[1])
							if distance >= 50 and distance <= 300:
								print 'The blue angel attacks the red angel'
								redAn.health = redAn.health - 50
								PHASE = 4
								bluAn.selected = 0
							else:
								print 'That unit is out of range. Try attacking with another unit.'
								bluAn.selected = 0
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	#This is the Red Team movement code.
			if PHASE == 2 and TURN == 'Red': #initialises the ability to move units.
				if event.type == MOUSEBUTTONDOWN and event.button == 1:
					if mouseX in range(redI.rect.left, redI.rect.right) and mouseY in range(redI.rect.top, redI.rect.bottom):
						if redI.selected == 0:
							print 'Red infantry ready to move!'
							redI.selected = 1
							redA.selected = 0
							redC.selected = 0
							redAn.selected = 0
						else:
							print 'Already selected!'
					elif mouseX in range(redA.rect.left, redA.rect.right) and mouseY in range(redA.rect.top, redA.rect.bottom):
						if redA.selected == 0:
							print 'Red Archer ready to move!'
							redI.selected = 0
							redA.selected = 1
							redC.selected = 0
							redAn.selected = 0
						else:
							print 'Already selected!'
					elif mouseX in range(redC.rect.left, redC.rect.right) and mouseY in range(redC.rect.top, redC.rect.bottom):
						if redC.selected == 0:
							print 'Red Catapult ready to move!'
							redI.selected = 0
							redA.selected = 0
							redC.selected = 1
							redAn.selected = 0
						else:
							print 'Already selected!'
					elif mouseX in range(redAn.rect.left, redAn.rect.right) and mouseY in range(redAn.rect.top, redAn.rect.bottom):
						if redAn.selected == 0:
							print 'Red Angel ready to move!'
							redI.selected = 0
							redA.selected = 0
							redC.selected = 0
							redAn.selected = 1
						else:
							print 'Already selected!'
				if event.type == MOUSEBUTTONDOWN and event.button == 3:
					if redI.selected == 1:
						if redI.rect.center != pygame.mouse.get_pos:
							if mouseX < redI.rect.x + 100 and mouseX > redI.rect.x - 100 and mouseY < redI.rect.y + 100 and mouseY > redI.rect.y - 100:
								redI.rect.center = pygame.mouse.get_pos()
								redI.selected = 0
								PHASE = 3 #this line permits only one unit at a time to move
							else:
								print "Soldiers can only move 100 pixels at a time"
					if redA.selected == 1:
						if redA.rect.center != pygame.mouse.get_pos():
							if mouseX < redA.rect.x + 120 and mouseX > redA.rect.x - 120 and mouseY < redA.rect.y + 120 and mouseY > redA.rect.y - 120:
								redA.rect.center = pygame.mouse.get_pos()
								redA.selected = 0
								PHASE = 3 #this line permits only one unit at a time to move
							else:
								print "Archers can only move 120 pixels at a time"
					if redC.selected == 1:
						if redC.rect.center != pygame.mouse.get_pos:
							if mouseX < redC.rect.x + 75 and mouseX > redC.rect.x - 75 and mouseY < redC.rect.y + 75 and mouseY > redC.rect.y - 75:
								redC.rect.center = pygame.mouse.get_pos()
								redC.selected = 0
								PHASE = 3 #this line permits only one unit at a time to move
							else:
								print "Catapults can only move 75 pixels at a time"
					if redAn.selected == 1:
						if redAn.rect.center != pygame.mouse.get_pos:
							if mouseX < redAn.rect.x + 300 and mouseX > redAn.rect.x - 300 and mouseY < redAn.rect.y + 300 and mouseY > redAn.rect.y - 300:
								redAn.rect.center = pygame.mouse.get_pos()
								redAn.selected = 0
								PHASE = 3 #this line permits only one unit at a time to move
							else:
								print "Angels can only move 300 pixels at a time"
			if PHASE == 2 and TURN == 'Blue': #initialises the ability for the Blue Team to move units.
				if event.type == MOUSEBUTTONDOWN and event.button == 1:
					if mouseX in range(bluI.rect.left, bluI.rect.right) and mouseY in range(bluI.rect.top, bluI.rect.bottom):
						if bluI.selected == 0:
							print 'Blue infantry ready to move!'
							bluI.selected = 1
							bluA.selected = 0
							bluC.selected = 0
							bluAn.selected = 0
						else:
							print 'Already selected!'
					elif mouseX in range(bluA.rect.left, bluA.rect.right) and mouseY in range(bluA.rect.top, bluA.rect.bottom):
						if bluA.selected == 0:
							print 'Blue Archer ready to move!'
							bluI.selected = 0
							bluA.selected = 1
							bluC.selected = 0
							bluAn.selected = 0
						else:
							print 'Already selected!'
					elif mouseX in range(bluC.rect.left, bluC.rect.right) and mouseY in range(bluC.rect.top, bluC.rect.bottom):
						if bluC.selected == 0:
							print 'Blue Catapult ready to move!'
							bluI.selected = 0
							bluA.selected = 0
							bluC.selected = 1
							bluAn.selected = 0
						else:
							print 'Already selected!'
					elif mouseX in range(bluAn.rect.left, bluAn.rect.right) and mouseY in range(bluAn.rect.top, bluAn.rect.bottom):
						if bluAn.selected == 0:
							print 'Blue Angel ready to move!'
							bluI.selected = 0
							bluA.selected = 0
							bluC.selected = 0
							bluAn.selected = 1
						else:
							print 'Already selected!'
				if event.type == MOUSEBUTTONDOWN and event.button == 3:
					if bluI.selected == 1:
						if bluI.rect.center != pygame.mouse.get_pos:
							if mouseX < bluI.rect.x + 100 and mouseX > bluI.rect.x - 100 and mouseY < bluI.rect.y + 100 and mouseY > bluI.rect.y - 100:
								bluI.rect.center = pygame.mouse.get_pos()
								bluI.selected = 0
								PHASE = 3 #this line permits only one unit at a time to move
							else:
								print "Soldiers can only move 100 pixels at a time"
					if bluA.selected == 1:
						if bluA.rect.center != pygame.mouse.get_pos():
							if mouseX < bluA.rect.x + 120 and mouseX > bluA.rect.x - 120 and mouseY < bluA.rect.y + 120 and mouseY > bluA.rect.y - 120:
								bluA.rect.center = pygame.mouse.get_pos()
								bluA.selected = 0
								PHASE = 3 #this line permits only one unit at a time to move
							else:
								print "Archers can only move 120 pixels at a time"
					if bluC.selected == 1:
						if bluC.rect.center != pygame.mouse.get_pos:
							if mouseX < bluC.rect.x + 75 and mouseX > bluC.rect.x - 75 and mouseY < bluC.rect.y + 75 and mouseY > bluC.rect.y - 75:
								bluC.rect.center = pygame.mouse.get_pos()
								bluC.selected = 0
								PHASE = 3 #this line permits only one unit at a time to move
							else:
								print "Catapults can only move 75 pixels at a time"
					if bluAn.selected == 1:
						if bluAn.rect.center != pygame.mouse.get_pos:
							if mouseX < bluAn.rect.x + 300 and mouseX > bluAn.rect.x - 300 and mouseY < bluAn.rect.y + 300 and mouseY > bluAn.rect.y - 300:
								bluAn.rect.center = pygame.mouse.get_pos()
								bluAn.selected = 0
								PHASE = 3 #this line permits only one unit at a time to move
							else:
								print "Angels can only move 300 pixels at a time"
			if not hasattr(event, 'key'): continue
			#The next four blocks give the player the ability to spawn units
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			if spawned_units < 3 and PHASE == 1 and TURN == 'Red':
				if REDGOLD < 10:
					print "Out of money! Moving to Phase 2!"
					PHASE = 2
					spawned_units = 0
				elif event.type == KEYDOWN and event.key == K_1:
					if REDGOLD >= 10 and REDCOMMAND >= 5:
						Sprites.append(redI)
						REDGOLD -= 10
						REDCOMMAND -= 5
						spawned_units = spawned_units + 1
					else:
						print "Not enough gold!"
				elif event.type == KEYDOWN and event.key == K_2:
					if REDGOLD >= 20 and REDCOMMAND >= 10:
						Sprites.append(redA)
						REDGOLD -= 20
						REDCOMMAND -= 10
						spawned_units = spawned_units + 1
					else:
						print "Not enough gold!"
				elif event.type == KEYDOWN and event.key == K_3:
					if REDGOLD >= 50 and REDCOMMAND >= 25:
						Sprites.append(redC)
						REDGOLD -= 50
						REDCOMMAND -= 25
						spawned_units = spawned_units + 1
					else:
						print "Not enough gold!"
				elif event.type == KEYDOWN and event.key == K_4:
					if REDGOLD >= 100 and REDCOMMAND >= 50:
						Sprites.append(redAn)
						REDGOLD -= 100
						REDCOMMAND -= 50
						spawned_units = spawned_units + 1
					else:
						print "Not enough gold!"
#The code that allows the Blue Team to spawn units
			elif spawned_units < 3 and PHASE == 1 and TURN == 'Blue':
				if BLUEGOLD < 10:
					print "Out of money! Moving to Phase 2!"
					PHASE = 2
					spawned_units = 0
				elif event.type == KEYDOWN and event.key == K_1:
					if BLUEGOLD >= 10 and BLUECOMMAND >= 5:
						Sprites.append(bluI)
						BLUEGOLD -= 10
						BLUECOMMAND -= 5
						spawned_units = spawned_units + 1
					else:
						print "Not enough gold!"
				elif event.type == KEYDOWN and event.key == K_2:
					if BLUEGOLD >= 20 and BLUECOMMAND >= 10:
						Sprites.append(bluA)
						BLUEGOLD -= 20
						BLUECOMMAND -= 10
						spawned_units = spawned_units + 1
					else:
						print "Not enough gold!"
				elif event.type == KEYDOWN and event.key == K_3:
					if BLUEGOLD >= 50 and BLUECOMMAND >= 25:
						Sprites.append(bluC)
						BLUEGOLD -= 50
						BLUECOMMAND -= 25
						spawned_units = spawned_units + 1
					else:
						print "Not enough gold!"
				elif event.type == KEYDOWN and event.key == K_4:
					if REDGOLD >= 100 and REDCOMMAND >= 50:
						Sprites.append(bluAn)
						BLUEGOLD -= 100
						BLUECOMMAND -= 50
						spawned_units = spawned_units + 1
					else:
						print "Not enough gold!"
			if spawned_units >= 3:
				PHASE = 2
				spawned_units = 0
			if event.type == KEYDOWN and event.key == K_SPACE:
				if PHASE == 1:
					PHASE = 2
					print 'Skipping to Phase 2!'
					spawned_units = 0
				elif PHASE == 2:
					PHASE = 3
					print 'Skipping to Phase 3!'
				elif PHASE == 3:
					PHASE = 1
					if TURN == 'Red':
						TURN = 'Blue'
						print "It is now the Blue Team's turn"
					else:
						TURN = 'Red'
						print "It is now the Red Team's turn. As both players took their turn, they are awarded with 25 Gold"
						REDGOLD = REDGOLD + 25
						BLUEGOLD = BLUEGOLD + 25
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		sprites = pygame.sprite.OrderedUpdates(Sprites)
		sprites.update()
		screen.blit(background, (0, 0))
		sprites.draw(screen)
		pygame.display.flip()
	pygame.quit()