f3a5647e5b
Im now working next on shoorting for turret Added new sand map and new cannon tower images aswell as function to play animations, showing range, placing turrets on allowed tiles for towers Buy and cancel button to buy towers
74 lines
2.0 KiB
Python
74 lines
2.0 KiB
Python
from game.util import *
|
|
import pygame as pg
|
|
import math
|
|
|
|
class Enemy(pg.sprite.Group):
|
|
def __init__(self, game, speed, health):
|
|
super().__init__()
|
|
self.game = game
|
|
self.waypoints = self.game.tilemap.waypoints
|
|
|
|
self.speed = speed
|
|
self.angle = 0
|
|
self.health = health
|
|
|
|
self.image = pg.transform.rotate(self.game.assets['enemy'].copy(), self.angle)
|
|
self.rect = self.image.get_rect()
|
|
|
|
self.pos = pg.Vector2(
|
|
tile_to_pixel(self.waypoints[0], self.game.tilemap.tile_size, True)
|
|
)
|
|
|
|
self.target_index = 1
|
|
|
|
self.rect.center = self.pos
|
|
|
|
def update(self):
|
|
self.move()
|
|
self.rotate()
|
|
|
|
def move(self):
|
|
if self.target_index >= len(self.waypoints):
|
|
return
|
|
|
|
tile_size = self.game.tilemap.tile_size
|
|
|
|
self.target = pg.Vector2(
|
|
tile_to_pixel(
|
|
self.waypoints[self.target_index],
|
|
tile_size,
|
|
True
|
|
)
|
|
)
|
|
|
|
direction = self.target - self.pos
|
|
distance = direction.length()
|
|
|
|
if distance <= self.speed:
|
|
self.pos = self.target
|
|
self.target_index += 1
|
|
else:
|
|
direction = direction.normalize()
|
|
self.pos += direction * self.speed
|
|
|
|
def rotate(self):
|
|
dist = self.target - self.pos
|
|
|
|
self.angle = math.degrees(math.atan2(-dist[1], dist[0]))
|
|
self.image = pg.transform.rotate(self.game.assets['enemy'].copy(), self.angle)
|
|
self.rect = self.image.get_rect()
|
|
self.rect.center = self.pos
|
|
|
|
def render(self, surf, camera):
|
|
zoom = camera.zoom
|
|
scaled_rect = self.rect.scale_by(zoom)
|
|
|
|
scaled_image = pg.transform.scale(
|
|
self.image,
|
|
(scaled_rect.size[0], scaled_rect.size[1])
|
|
)
|
|
|
|
scaled_pos = camera.world_to_screen(self.pos)
|
|
scaled_rect.center = scaled_pos
|
|
|
|
surf.blit(scaled_image, scaled_rect) |