Help: Turn Based Strategy game


oventoaster

Still Fresh
Joined
Apr 17, 2012
Messages
7
I am currently building a turn based strategy game using Python/pygame. While unit spawning, movement and turn order work perfectly, I have hit a snag in the combat block.


Say for example, I use a Red archer to attack a Blue Archer, as shown here:



Code:
if mouseX in range(bluA.rect.left, bluA.rect.right) and mouseY in range(bluA.rect.top, bluA.rect.bottom):

						-snip-

						if redA.selected == 1:

							if bluA.rect.center - redA.rect.center >= (50, 50) and bluA.rect.center - redA.rect.center <= (300, 300):

								print 'The red archer attacks the blue archer'

						-snip-

the (if mouseX in range...) tests if a Blue Archer is being attacked



the (if redA.selected == 1) lets pygame know that a Red archer is attacking a Blue archer



if bluA.rect.center - redA.rect.center >= (50, 50) and bluA.rect.center - redA.rect.center <= (300, 300): print 'The red archer attacks the blue archer'



This should check if the Red archer can attack within it's range - which is between 50 and 300 pixels away. Since in this situation, the Red Archer is in range, I initiate the attack.



When I initiate the attack, this error comes up:





Code:
Traceback (most recent call last):

  File "C:\Users\Oventoaster\Desktop\Table Wars\Table Wars.py", line 770, in <module>

	main()

  File "C:\Users\Oventoaster\Desktop\Table Wars\Table Wars.py", line 152, in main

	if bluA.rect.center - redA.rect.center >= (50, 50) and bluA.rect.center - redA.rect.center <= (300, 300):

TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'


and the game freezes.


My question is this: How do I check if the Red archer is in range to attack the Blue Archer? Can the same be done for the Blue archer when he attacks?
 
This would give you circular attack area (remember pythagoras? a² + b² = c²):



Code:
distanceSquared = (bluA.rect.center.x - redA.rect.center.x) ** 2 + (bluA.rect.center.y - redA.rect.center.y) ** 2

if distanceSquared >= 50**2 and distanceSquared <= 300**2:

  ...



If you want rectangular (diamond-shape):





Code:
distance = abs(bluA.rect.center.x - redA.rect.center.x) + abs(bluA.rect.center.y - redA.rect.center.y)

if distance >= 50 and distance <= 300:

  ...



If you want rectangle:





Code:
minDistance = min(abs(bluA.rect.center.x - redA.rect.center.x), abs(bluA.rect.center.y - redA.rect.center.y))

maxDistance = max(abs(bluA.rect.center.x - redA.rect.center.x), abs(bluA.rect.center.y - redA.rect.center.y))

if minDistance >= 50 and maxDistance <= 300:

  ...


EDIT: Tuples are not vectors in the mathematical sense. You can't calculate with them.
 
Last edited by a moderator:
This would give you circular attack area (remember pythagoras? a² + b² = c²):



Code:
distanceSquared = (bluA.rect.center.x - redA.rect.center.x) ** 2 + (bluA.rect.center.y - redA.rect.center.y) ** 2

if distanceSquared >= 50**2 and distanceSquared <= 300**2:

  ...



If you want rectangular (diamond-shape):





Code:
distance = abs(bluA.rect.center.x - redA.rect.center.x) + abs(bluA.rect.center.y - redA.rect.center.y)

if distance >= 50 and distance <= 300:

  ...



If you want rectangle:





Code:
minDistance = min(abs(bluA.rect.center.x - redA.rect.center.x), abs(bluA.rect.center.y - redA.rect.center.y))

maxDistance = max(abs(bluA.rect.center.x - redA.rect.center.x), abs(bluA.rect.center.y - redA.rect.center.y))

if minDistance >= 50 and maxDistance <= 300:

  ...



EDIT: Tuples are not vectors in the mathematical sense. You can't calculate with them.


I tried what you said for diamond shape, but now I'm getting a different error:





Code:
Traceback (most recent call last):

  File "C:\Users\Oventoaster\Desktop\Table Wars\Table Wars.py", line 772, in <module>

	main()

  File "C:\Users\Oventoaster\Desktop\Table Wars\Table Wars.py", line 166, in main

	distance = abs(bluA.rect.center.x - redA.rect.center.x) + abs(bluA.rect.center.y - redA.rect.center.y)

AttributeError: 'tuple' object has no attribute 'x'


Do I use pos_x rather than x?
 
Last edited by a moderator:
Oh right :D [0] for x and [1] for y.


EDIT: for example



Code:
distanceSquared = (bluA.rect.center[0] - redA.rect.center[0]) ** 2 + (bluA.rect.center[1] - redA.rect.center[1]) ** 2





actually looking at pygame documentation:



Code:
distanceSquared = (bluA.rect.centerx - redA.rect.centerx) ** 2 + (bluA.rect.centery - redA.rect.centery) ** 2


should work
 
Last edited by a moderator:
[0] for x and [1] for y? I get syntax errors when I do that. If it helps, I'm running on Windows 7
 
Back
Top