You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.6 KiB

  1. #!/usr/bin/env python3
  2. import sys
  3. import pygame
  4. from classes.towers.bubbler import Bubbler
  5. from classes.enemies.ogre import Ogre
  6. from classes.settings import Settings
  7. from classes.levels import Levels
  8. from classes.map import Map
  9. settings = Settings()
  10. levels = Levels()
  11. map = Map()
  12. # Init pygame and set up screen.
  13. pygame.init()
  14. screen = pygame.display.set_mode(settings.size)
  15. # Storage of towers and projectiles
  16. towers = []
  17. enemies = []
  18. projectiles = []
  19. while True:
  20. for event in pygame.event.get():
  21. pressedKeys = pygame.key.get_pressed()
  22. if event.type == pygame.QUIT:
  23. sys.exit()
  24. if event.type == pygame.MOUSEBUTTONDOWN:
  25. newTowerCoordinates = map.canBuildOnCooardinate(event.pos, towers)
  26. if newTowerCoordinates:
  27. towers.append(Bubbler(newTowerCoordinates))
  28. if event.type == pygame.KEYDOWN:
  29. if pressedKeys[pygame.K_SPACE]:
  30. enemies.append(Ogre(settings.enemyStart))
  31. # Spawn enemies according to the level
  32. newEnemy = levels.get_enemy()
  33. if newEnemy:
  34. enemies.append(newEnemy)
  35. # Draw map to screen
  36. screen = map.draw(screen)
  37. # Draw all enemies
  38. for enemy in enemies:
  39. # If move return false, remove the projectile
  40. if not enemy.move():
  41. enemies.remove(enemy)
  42. continue
  43. # Remove enemies that got through to base
  44. if enemy.inBase:
  45. enemies.remove(enemy)
  46. # If enemy got low health, remove it
  47. if enemy.get_health() <= 0:
  48. enemies.remove(enemy)
  49. continue
  50. screen = enemy.draw(screen)
  51. # Draw all the towers on the screen
  52. for tower in towers:
  53. projectile = None
  54. # Try to shoot (if we got an enemy)
  55. if len(enemies) > 0:
  56. projectile = tower.shoot(enemies[0].rect)
  57. # If successfull, store projectile
  58. if projectile is not None:
  59. projectiles.append(projectile)
  60. # Draw the tower
  61. screen = tower.draw(screen)
  62. # Draw, move and remove projectiles on the screen
  63. for projectile in projectiles:
  64. # If move return false, remove the projectile
  65. if not projectile.move():
  66. projectiles.remove(projectile)
  67. continue
  68. # If projectile collides with an enemy, projectile
  69. # will deal damage to the enemy and then we remove
  70. # the projectile
  71. if projectile.collidesWith(enemies):
  72. projectiles.remove(projectile)
  73. continue
  74. # Draw the projectile
  75. screen = projectile.draw(screen)
  76. # Draw screen
  77. pygame.display.flip()