Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 

99 lines
2.6 KiB

#!/usr/bin/env python3
import sys
import pygame
from classes.towers.bubbler import Bubbler
from classes.enemies.ogre import Ogre
from classes.settings import Settings
from classes.levels import Levels
from classes.map import Map
settings = Settings()
levels = Levels()
map = Map()
# Init pygame and set up screen.
pygame.init()
screen = pygame.display.set_mode(settings.size)
# Storage of towers and projectiles
towers = []
enemies = []
projectiles = []
while True:
for event in pygame.event.get():
pressedKeys = pygame.key.get_pressed()
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
newTowerCoordinates = map.canBuildOnCooardinate(event.pos, towers)
if newTowerCoordinates:
towers.append(Bubbler(newTowerCoordinates))
if event.type == pygame.KEYDOWN:
if pressedKeys[pygame.K_SPACE]:
enemies.append(Ogre(settings.enemyStart))
# Spawn enemies according to the level
newEnemy = levels.get_enemy()
if newEnemy:
enemies.append(newEnemy)
# Draw map to screen
screen = map.draw(screen)
# Draw all enemies
for enemy in enemies:
# If move return false, remove the projectile
if not enemy.move():
enemies.remove(enemy)
continue
# Remove enemies that got through to base
if enemy.inBase:
enemies.remove(enemy)
# If enemy got low health, remove it
if enemy.get_health() <= 0:
enemies.remove(enemy)
continue
screen = enemy.draw(screen)
# Draw all the towers on the screen
for tower in towers:
projectile = None
# Try to shoot (if we got an enemy)
if len(enemies) > 0:
projectile = tower.shoot(enemies[0].rect)
# If successfull, store projectile
if projectile is not None:
projectiles.append(projectile)
# Draw the tower
screen = tower.draw(screen)
# Draw, move and remove projectiles on the screen
for projectile in projectiles:
# If move return false, remove the projectile
if not projectile.move():
projectiles.remove(projectile)
continue
# If projectile collides with an enemy, projectile
# will deal damage to the enemy and then we remove
# the projectile
if projectile.collidesWith(enemies):
projectiles.remove(projectile)
continue
# Draw the projectile
screen = projectile.draw(screen)
# Draw screen
pygame.display.flip()