Help: Unit Spawning bugs


oventoaster

Still Fresh
Joined
Apr 17, 2012
Messages
7
Hello again OpenPandora. I am almost finished my game, Table Wars. Right now, there are two bugs I am having trouble with in my game. Both of which relate to the spawning of the units in the game.


How I am spawning units currently is appending the sprite to a global group via a keystroke, like so:



Code:
if spawned_units < 3 and PHASE == 1 and TURN == 'Red':

...

	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!"

...

this snippet shows that, in the game, whenever it is Phase 1 and it is the Red Team's turn, pressing 1 will spawn a Red soldier (redI being a vairiable for the Red Soldier class), spend 10 gold, and subtracting 5 Command. The spawned_units variable makes sure a player does not spawn more than three units; the game goes to Phase 2 immediately after the third unit spawns.



I found a problem with this however. Whenever a player attempts to spawn two soldiers, the Sprites.append call calls correctly, but the soldier appears right on top of the other soldier, regardless if I have moved the other soldier previously. How do I spawn two of the same unit, yet have one class? I have seen the Pygame examples able to display 100 of the same sprite moving on their own path and bouncing off of walls, and yet, there was only one class for the sprite itself. Furthermore, How do I make the units spawn in random places around the base?



The second problem is whenever a unit dies. The unit dies after a self.update checks on the unit's health:





Code:
class Red_Infantry(pygame.sprite.Sprite):

	def __init__(self):

		pygame.sprite.Sprite.__init__(self)

		self.image, self.rect = load_image('Soldier_red.png', -1)

		self.selected = 0

		self.area = screen.get_rect()

		self.rect.topleft = (100, 300)

		self.health = 100 #Soldiers are have mediocre toughness.

		self.attack_damage = 25

		self.range_maximum = 20 #in pixels, this is melee range

		self.range_minimum = 0

		self.update()

	def update(self):

		if self.health <= 0:

			self.kill()


This is the class for the Red Soldier. His health is 100. The unit "dies" through a self.kill call after his self.health reaches 0. However, whenever the player attempts to respawn the Red soldier, he remains invisible, yet Gold and Command are subtracted. How do I prevent this from happening?


I hope for a reply soon. I would love to finish this project by next week and get started on my next project.
 
I think that what's happening is that you're appending the same instance of the class each time. I assume that, somewhere in your code, you have redI=Red_Infantry(), and that this line is only executed once (ie. it's not in a loop).


You can think of classes as being sort of like an object's template, or a description on how to create an object (since this is Python, classes are actually objects too, but that's not important right now). Whenever you call a class (ie. put parentheses after the class name), you create an instance of the class. Since this is the Red_Infantry class, every instance should be a distinct infantry unit. But if you've only created one instance (redI), you've really only created one unit, and that one unit will stay in its spot and not come back to life no matter how many times you append it to the sprite list.


To fix this, you simply need to create a new instance of Red_Infantry every time you buy a new infantry unit, and append that to your sprites list. I suggest changing your Sprites.append line to read:



Code:
Sprites.append(Red_Infantry())
Note all the parentheses. I would also recommend that you think about whether you should even have that redI variable, but I'm not really sure what it's for.


Hope that helps you!
 
That would help, but if I don't need the variable, how would I order the sprite to move and attack? should I place the code inside the class and call a move() or fight() function?
 
Weakrefs seem pointless here, and certainly aren't worth confusing a relative beginner.


Now oventoaster, you haven't really told us a lot about your code, so I don't know how you make your units move and attack. Having move and fight methods within the Red_Infantry class seems sensible to me - that would be the standard object-oriented approach. But each unit needs to be able to move and attack independently; having just one redI variable means you can't access all your units! Instead, you should operate on that sprite list; loop through, and allow each unit in it the opportunity to move or attack.
 
Here is the entire main loop, global and main variables initialized, and other important details: (Wall of code!)



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


what works is the ability to spawn the first unit, and move each unique sprite individually. If you want, I can post the .py file as well.
 
Last edited by a moderator:
Back
Top