def new_game(self): """Start new game""" # start a new game generuj() self.grid = {} # self.trees = pygame.sprite.Group() self.boosters = pygame.sprite.Group() self.active_boosters = pygame.sprite.Group() self.magics = pygame.sprite.Group() self.explosions = pygame.sprite.Group() self.items = pygame.sprite.Group() self.all_creatures = pygame.sprite.Group() self.arrows = pygame.sprite.Group() self.items_factory = Factory(self) self.equipment = Equipment(self) crafting.equipment = self.equipment self.spells = Spells(self) self.health_bar = HealthBar(self) self.mana_bar = ManaBar(self) self.player = Player(self, self.equipment, self.health_bar, self.mana_bar, self.spells) self.background = Background(self, self.player) self.items_engine = ItemsEngine(self) self.main_position = PVector(*self.get_main_stage_position()) self.last_main_position = PVector(*self.get_main_stage_position()) self.creatures_engine = CreaturesEngine(self) self.actual_terrain = [] create_world(self.grid, self.items_factory) self.waiting = True # test bacon = self.items_factory.create("bacon", 1750, 100) self.bacon = bacon self.items.add(bacon) # Tutaj testowanie dodawania boosterów position = vector(1193, 478) position2 = vector(1294, 487) position3 = vector(500, 170) position4 = vector(400, 10) position5 = vector(500, 470) position6 = vector(1400, 470) boost_test1 = TweeningBooster(self, position, "health") boost_test2 = TweeningBooster(self, position2, "mana") boost_test3 = SpeedBooster(self, position3) boost_test4 = DamageBooster(self, position4) boost_test5 = DefenseBooster(self, position5) boost_test6 = AccuracyBooster(self, position6) self.run()
def bounce_from_platform(src, blocks): """If creature collides with blocks, moves in opposite direction.""" hits = pg.sprite.spritecollide(src, blocks, False) if hits: if src.rect.left < hits[0].rect.left: src.apply_force(PVector(-src.maxforce, 0)) elif src.rect.right > hits[0].rect.right: src.apply_force(PVector(src.maxforce, 0)) if src.rect.top < hits[0].rect.top: src.apply_force(PVector(0, -src.maxforce)) elif src.rect.bottom > hits[0].rect.bottom: src.apply_force(PVector(0, +src.maxforce))
def push_away(src, player, damage): """Pushes the opponent of the player.""" delta = src.position - player.position direction = delta.xdirection() force = PVector(direction * damage * MAX_XPUSH, -damage * MAX_YPUSH) src.apply_force_no_limit(force)
def __init__(self, x, y, info, game, do_scale=True): pygame.sprite.Sprite.__init__(self) self.name = info.name self.description = info.description self.variety = info.variety self.angle = info.angle self.info = info self.game = game self.damage = 5 self.env_damage = 1 self.durability = 100000 self.range = 60 self.vel_y = 0 self.acc_y = 0 self.image = Item.load_image(self.name) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y if do_scale: Item.scale_item(self, BLOCK_SIZE // 1.6) self.position = PVector(x, y)
def action(self, mouse_pos, player): """By default it makes a little damage and a little env_damage in low range""" if math.hypot(mouse_pos[0] - player.rect.center[0], mouse_pos[1] - player.rect.center[1]) <= self.range: damaged = False for creature in self.game.all_creatures: if creature.rect.collidepoint(mouse_pos): creature.hit(player, self.damage) damaged = True break else: position = Item.get_mouse_position_on_map(player, mouse_pos) blok_pos = PVector( *Item.cursor_to_grid(position.x, position.y)) for block in Item.get_neighbours(blok_pos, (3, 3), self.game.grid, noncollidable_objects=True): if block.rect.collidepoint(mouse_pos): block.hit(self.env_damage) damaged = True break if damaged: self.durability -= 1 if self.durability <= 0: return True return False
def push_from_platform(src, blocks): """Inverts speed vector if creature reaches platform edge.""" hits = pg.sprite.spritecollide(src, blocks, False) if hits: # reaction(src) if src.rect.centerx > hits[0].rect.right: desired = PVector(2, src.velocity.y) steer = desired - src.velocity src.apply_force(steer) elif src.rect.centerx < hits[0].rect.left: desired = PVector(-2, src.velocity.y) steer = desired - src.velocity src.apply_force(steer)
def __init__(self, x, y): super(AbstractSprite, self).__init__() # variables self.radius = None self.angle = None self.mass = None # limits self.maxspeed = None self.maxforce = None self.maxhp = None self.manoeuvrability = None # flags self.is_enemy = False self.is_hpbar = False self.is_hitbox = True self.is_fixpos = False self.is_target = False # counters self.anim_count = None self.bite_count = None self.shot_count = None self.freeze_count = None # vectors self.position = PVector(x, y) self.velocity = PVector(0, 0) self.acceleration = PVector(0, 0) self.acceleration_no_limit = PVector(0, 0) # body, hitbox self.rect = None self.body = None self.hpbar = None # sprite specific self.hp = None self.items = [] self.damage = None self.defense = None
def edges_bounce(src): """Checks if the object does not go beyond the screen. If true, object turns back with the opposite direction and max velocity.""" # horizontal if src.rect.left < EDGE_LIMIT: desired = PVector(src.maxspeed, src.velocity.y) steer = desired - src.velocity src.apply_force(steer) elif src.rect.right > WIDTH - EDGE_LIMIT: desired = PVector(-src.maxspeed, src.velocity.y) steer = desired - src.velocity src.apply_force(steer) # vertical if src.rect.top < EDGE_LIMIT: desired = PVector(src.velocity.x, src.maxspeed) steer = desired - src.velocity src.apply_force(steer) elif src.rect.bottom > HEIGHT - EDGE_LIMIT: desired = PVector(src.velocity.x, -src.maxspeed) steer = desired - src.velocity src.apply_force(steer)
def fly(src): """Random flying according to maxspeed and manoeuvrability.""" # desired direction src.angle += random.uniform(-ARC_STEP, ARC_STEP) src.angle = math.fmod(src.angle, ARC_LENGTH) rand = PVector(math.sin(src.angle), math.cos(src.angle)) rand *= src.maxspeed * src.manoeuvrability # desired length if src.velocity.mag(): desired = src.velocity.copy() desired.normalize() else: desired = PVector.random() desired *= src.maxspeed desired += rand # steering force steer = desired - src.velocity src.apply_force(steer)
def create(self, x, y, manoeuvrability, maxspeed, maxforce, maxhp, mass, items, damage, defense): # variables self.radius = min(self.width, self.height) self.angle = 0.0 self.mass = mass # limits self.maxspeed = maxspeed self.maxforce = maxforce self.maxhp = maxhp self.manoeuvrability = manoeuvrability # flags self.is_enemy = True self.is_hpbar = False self.is_hitbox = False self.is_fixpos = True # counters self.anim_count = 0 self.bite_count = 0 self.shot_count = 0 self.freeze_count = 0 # vectors self.position = PVector(x, y) self.velocity = PVector(0, 0.001) self.acceleration = PVector(0, 0) self.acceleration_no_limit = PVector(0, 0) # body w, h = self.width, self.height self.rect = pg.rect.Rect(0, 0, w, h) self.rect.center = self.position.repr() self.body = pg.rect.Rect(self.rect) self.hpbar = HpBar(self.body.midtop) # sprite specific self.hp = maxhp self.items = items self.damage = damage self.defense = defense
def bullet(src, src_location, dest_location): """Calculates and applies force to reach dest from src.""" x0 = src_location.x y0 = src_location.y x1 = dest_location.x y1 = dest_location.y dx = x1 - x0 dy = y1 - y0 vx = dx / 100 if dx == 0: return vy = vx * abs(dy / dx) + 0.5 * GRAVITY_BULLET.y * abs(dx / vx) force = PVector(vx, -vy) src.apply_force(force)
def init_move(src): """Initializes velocity if creature isn't moving.""" if src.velocity.y == 0 and src.velocity.x == 0: force = PVector(-src.maxspeed, 0) src.apply_force(force)
def jump(src): """Performs single jump""" jmp = PVector(src.velocity.xdirection() * src.maxspeed, -src.maxspeed / 8) src.apply_force(jmp)
from settings import * from creatures.vector import PVector import pygame as pg import random import math # forces WIND = PVector(-0.05, 0.0) GRAVITY = PVector(0, 0.2) GRAVITY_BULLET = PVector(0, 0.1) REACTION = PVector(0, -0.19) REACTION_BULLET = PVector(0, -0.1) # constants MI = 0.05 EDGE_LIMIT = -200 ARC_LENGTH = 2 * math.pi ARC_STEP = ARC_LENGTH / 15 MOVE_LENGTH = 10 MOVE_STEP = MOVE_LENGTH / 15 MAX_XPUSH = 50 MAX_YPUSH = 15 PLAYER_DISTANCE = 1000 def gravity(src): """Simple force of gravity. Q = m*g """ grav = GRAVITY.copy() src.apply_force(grav)
def get_mouse_position_on_map(player, mouse_pos): mouse_pos = PVector(mouse_pos[0], mouse_pos[1]) return PVector(player.position.x, player.position.y) + mouse_pos - PVector( WIDTH / 2, HEIGHT / 2)
class Sprite(AbstractSprite): def __init__(self, x, y): super(Sprite, self).__init__(x, y) # self.create(x, y, **OBJECT) def create(self, x, y, manoeuvrability, maxspeed, maxforce, maxhp, mass, items, damage, defense): # variables self.radius = min(self.width, self.height) self.angle = 0.0 self.mass = mass # limits self.maxspeed = maxspeed self.maxforce = maxforce self.maxhp = maxhp self.manoeuvrability = manoeuvrability # flags self.is_enemy = True self.is_hpbar = False self.is_hitbox = False self.is_fixpos = True # counters self.anim_count = 0 self.bite_count = 0 self.shot_count = 0 self.freeze_count = 0 # vectors self.position = PVector(x, y) self.velocity = PVector(0, 0.001) self.acceleration = PVector(0, 0) self.acceleration_no_limit = PVector(0, 0) # body w, h = self.width, self.height self.rect = pg.rect.Rect(0, 0, w, h) self.rect.center = self.position.repr() self.body = pg.rect.Rect(self.rect) self.hpbar = HpBar(self.body.midtop) # sprite specific self.hp = maxhp self.items = items self.damage = damage self.defense = defense def draw(self, win): pass def bite(self, player): if self.is_enemy and self.rect.colliderect(player): if self.bite_count > 0: self.bite_count -= 1 else: direction = -1 if self.position.x < player.position.x: direction = 1 player.hit(self.damage, direction) self.bite_count = 20 def hit(self, player, damage_points): self.hp -= damage_points push_away(self, player, damage_points / self.maxhp) print(damage_points / self.maxhp) self.is_hpbar = True def update(self, player, blocks, map_position, items_factory): # dead if self.hp <= 0: self.die(items_factory) return # alive self.update_forces(player, blocks) self.move(map_position) self.fix_move(blocks, map_position) def move(self, map_position): # velocity self.velocity += self.acceleration self.velocity.xlimit(self.maxspeed) self.velocity += self.acceleration_no_limit self.position += self.velocity self.acceleration.zero() self.acceleration_no_limit.zero() # update body self.body.topleft = (self.position + map_position).repr() self.rect.topleft = (self.position + map_position).repr() self.hpbar.center(self.body.midtop) # update animation counter self.anim_count -= 1 self.anim_count %= self.animation_ticks def fix_move(self, blocks, map_position): dx, dy = 0.0, 0.0 hits = pg.sprite.spritecollide(self, blocks, False) if hits: hit = hits[0].rect rect = self.rect # vertical up if rect.top < hit.top and abs(rect.bottom - hit.bottom) > abs( rect.bottom - hit.top): dy = hit.top - rect.bottom self.velocity.y = 0.0 # vertical down elif rect.bottom > hit.bottom and rect.left > hit.left and rect.right < hit.right: dy = hit.bottom - rect.top self.velocity.y = 0.0 self.position.y += dy self.body.topleft = (self.position + map_position).repr() self.rect.topleft = (self.position + map_position).repr() self.hpbar.center(self.body.midtop) hits = pg.sprite.spritecollide(self, blocks, False) if hits: dx = 0.0 hit = hits[0].rect rect = self.rect # vertical if rect.left < hit.left: dx = hit.left - rect.right self.velocity.x = 0.0 reaction(self) elif rect.right > hit.right: dx = hit.right - rect.left self.velocity.x = 0.0 reaction(self) self.position.x += dx self.body.topleft = (self.position + map_position).repr() self.rect.topleft = (self.position + map_position).repr() self.hpbar.center(self.body.midtop) def apply_force(self, force): force.limit(self.maxforce) self.acceleration += force def apply_force_no_limit(self, force): self.acceleration_no_limit += force def die(self, items_factory): # creature has got its own items if self.items: for item_name in self.items: items_factory.add_item(item_name, *self.position.repr()) # creature hasn't got its own items else: items_factory.add_random_item(*self.position.repr()) self.kill() def quiet_die(self): self.kill() def update_forces(self, player, blocks): pass