Pygame Enemys Wont Move Down - Tower Defense Game
Solution 1:
I recommend to move the scorpion along a list of points (spider_move
). Use a variable that states the index of the next point (spider_move_to
) and move the scorpion to this point (next_p
). Every time when a point is reached, then increment the index (spider_move_to += 1
). If no more point is in the list, then stop the movement. To move the scorpion, compute the difference ( dx
, dy
) between the current position of spider
and the point from the list (next_p
). Increment or decrement to position of the scorpion (spider.x
, spider.y
) by spider.speed
, dependent on the amount of dx
and dy
:
spider_move_to = 0
spider_move = [
(240, 330), (240, 550), (540, 550), (540, 160),
(770, 160), (770, 550), (870, 550)]
runninggame = True
while runninggame:
# [...]
if spider_move_to < len(spider_move):
next_p = spider_move[spider_move_to]
dx, dy = (next_p[0] - spider.x, next_p[1] - spider.y)
if dx > spider.speed:
spider.x += spider.speed
elif dx < -spider.speed:
spider.x -= spider.speed
else:
spider.x = next_p[0]
if dy > spider.speed:
spider.y += spider.speed
elif dy < -spider.speed:
spider.y -= spider.speed
else:
spider.y = next_p[1]
if spider.x == next_p[0] and spider.y == next_p[1]:
spider_move_to += 1
# [...]
If you have multiple scorpions, then I recommend to make spider_move_to
an attribute of the class spider
class spider:
def __init__(self,x,y,height,width,color):
# [...]
self.spider_move_to
Use the attribute instead of the global variable:
spider_move = [
(240, 330), (240, 550), (540, 550), (540, 160),
(770, 160), (770, 550), (870, 550)]
runninggame = True
while runninggame:
# [...]
for spider in spiders:
if spider.spider_move_to < len(spider_move):
next_p = spider_move[spider.spider_move_to]
dx, dy = (next_p[0] - spider.x, next_p[1] - spider.y)
# [...]
if spider.x == next_p[0] and spider.y == next_p[1]:
spider .spider_move_to += 1
# [...]
Solution 2:
A youtuber Tech With Tim has created a tower defense game with pygame in his 12 hour livestream . You will find his code on this github-link and you can figure it out where is the error.
Note: The assets are not included in this github code doesn't contain the assets(images and sounds)so you can create your assets folder and reference your images and sounds in your game script.
Solution 3:
When looking at your code I noticed this oddity
elif spider.x > 540:
spider.y -= spider.speed
...
elif spider.x < 628:
spider.y += spider.speed
this is based on the order you provided. The second elif can Never run as the spider doesn't ever go - x meaning we will never be able to get a result that is < 540 meaning > 540 is always true which also means that since Elif always runs the first true condition then we cannot actually get a value that will cause the condition < 628 to run before >540. I would suggest modifying the code to remove the possibility of this occurring by adding self.checkpoint as a property of each spider. Then when it reaches a check point you do a check in your if as so
elif spider.x > 540 and (spider.checkpoint = 3 or spider.checkpoint = 4):
if spider.checkpoint != 4:
spider.checkpoint = 4
spider.y -= spider.speed
for each checkpoint this will increment it to the next one for the first run and keep it there so that when you reach the next checkpoint just increment each number by 1 so that the spider always knows which checkpoint it is travelling to.