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.
186 lines
6.6 KiB
186 lines
6.6 KiB
from .unit import Unit |
|
import tkinter as tk |
|
import random |
|
import uuid |
|
import subprocess |
|
import threading |
|
|
|
|
|
class Rat(Unit): |
|
def __init__(self, map, position=(0,0), id=None): |
|
super().__init__(position) |
|
self.id = id if id else uuid.uuid4() |
|
self.map = map |
|
self.position = self.find_next_position() |
|
self.bbox = (0, 0, 0, 0) |
|
self.stop = 0 |
|
self.age = 0 |
|
self.speed = .10 |
|
|
|
|
|
|
|
def calculate_rat_direction(self): |
|
x, y = self.position |
|
x_before, y_before = self.position_before |
|
if x > x_before: |
|
return "RIGHT" |
|
elif x < x_before: |
|
return "LEFT" |
|
elif y > y_before: |
|
return "DOWN" |
|
elif y < y_before: |
|
return "UP" |
|
else: |
|
return "DOWN" |
|
|
|
def find_next_position(self): |
|
neighbors = [] |
|
x, y = self.position |
|
while not neighbors: |
|
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]: |
|
nx, ny = x + dx, y + dy |
|
if not self.map.maze[ny][nx] and not (nx, ny) == self.position_before: |
|
neighbors.append((nx, ny)) |
|
if not neighbors: |
|
self.position_before = self.position |
|
self.position_before = self.position |
|
return neighbors[random.randint(0, len(neighbors) - 1)] |
|
|
|
def move(self): |
|
self.age += 1 |
|
if self.age == 200: |
|
self.update_animation() |
|
self.speed /= 2 |
|
if self.sex == "F" and self.pregnant: |
|
self.pregnant -= 1 |
|
if self.pregnant == self.babies*50: |
|
self.babies -= 1 |
|
self.stop = 20 |
|
if self.partial_move > 0.2: |
|
self.map.new_rat(self.position) |
|
else: |
|
self.map.new_rat(self.position_before) |
|
self.map.play_sound("BIRTH.WAV") |
|
if self.stop: |
|
self.stop -= 1 |
|
return |
|
if self.partial_move < 1: |
|
self.partial_move += self.speed |
|
self.partial_move = round(self.partial_move, 2) |
|
if self.partial_move == 1: |
|
self.partial_move = 0 |
|
self.position = self.find_next_position() |
|
self.direction = self.calculate_rat_direction() |
|
|
|
def collisions(self): |
|
if self.age < 200: |
|
return |
|
units = self.map.units.copy().values() |
|
for unit in units: |
|
if unit.id == self.id: |
|
continue |
|
if unit.age < 200: |
|
continue |
|
x1, y1, x2, y2 = self.bbox |
|
ox1, oy1, ox2, oy2 = unit.bbox |
|
|
|
# Verifica se NON c'è collisione con una tolleranza di sovrapposizione |
|
overlap_tolerance = 20 |
|
if (x1 >= ox2 - overlap_tolerance or # troppo a destra |
|
x2 <= ox1 + overlap_tolerance or # troppo a sinistra |
|
y1 >= oy2 - overlap_tolerance or # troppo in basso |
|
y2 <= oy1 + overlap_tolerance): # troppo in alto |
|
continue |
|
if self.id in self.map.units and unit.id in self.map.units: |
|
if self.sex == unit.sex: |
|
self.die(unit) |
|
pass |
|
else: |
|
if "fuck" in dir(self): |
|
self.fuck(unit) |
|
|
|
def die(self, unit=None): |
|
if not unit: |
|
unit = self |
|
self.map.play_sound("POISON.WAV") |
|
self.map.units.pop(unit.id) |
|
|
|
|
|
def draw(self): |
|
direction = self.calculate_rat_direction() |
|
image = self.rat_images[direction] |
|
self.rat_image = image |
|
partial_x, partial_y = 0, 0 |
|
|
|
if direction in ["UP", "DOWN"]: |
|
partial_y = self.partial_move * self.map.cell_size * (1 if direction == "DOWN" else -1) |
|
else: |
|
partial_x = self.partial_move * self.map.cell_size * (1 if direction == "RIGHT" else -1) |
|
|
|
x_pos = self.position_before[0] * self.map.cell_size + (self.map.cell_size - image.width()) // 2 + partial_x |
|
y_pos = self.position_before[1] * self.map.cell_size + (self.map.cell_size - image.height()) // 2 + partial_y |
|
|
|
self.map.canvas.create_image(x_pos, y_pos, image=image, anchor="nw", tag="unit") |
|
self.bbox = (x_pos, y_pos, x_pos + image.width(), y_pos + image.height()) |
|
self.map.canvas.create_rectangle(self.bbox, outline="red", tag="unit") |
|
|
|
class Male(Rat): |
|
def __init__(self, map, position=(0,0), id=None): |
|
super().__init__(map, position, id) |
|
self.sex = "M" |
|
self.update_animation() |
|
|
|
def update_animation(self): |
|
self.rat_images = self.make_animation() |
|
|
|
def make_animation(self): |
|
directions = ["UP", "DOWN", "LEFT", "RIGHT"] |
|
sex = "MALE" if self.age else "BABY" |
|
rat_images = {} |
|
for direction in directions: |
|
rat_images.update({direction: tk.PhotoImage(file=f"Rat/BMP_{sex}_{direction}.png").zoom(3)}) |
|
|
|
gray_pixels = [] |
|
for y in range(rat_images[direction].height()): |
|
for x in range(rat_images[direction].width()): |
|
if rat_images[direction].get(x, y) == (128, 128, 128): |
|
gray_pixels.append((x, y)) |
|
for x, y in gray_pixels: |
|
rat_images[direction].transparency_set(x, y, 1) |
|
return rat_images |
|
|
|
def fuck(self, unit): |
|
if not unit.pregnant: |
|
self.map.play_sound("SEX.WAV") |
|
self.stop = 100 |
|
unit.stop = 200 |
|
unit.pregnant = 500 |
|
unit.babies = random.randint(1, 3) |
|
|
|
class Female(Rat): |
|
def __init__(self, map, position=(0,0), id=None): |
|
self.sex = "F" |
|
self.pregnant = False |
|
self.babies = 0 |
|
super().__init__(map, position, id) |
|
self.update_animation() |
|
|
|
def update_animation(self): |
|
self.rat_images = self.make_animation() |
|
|
|
def make_animation(self): |
|
directions = ["UP", "DOWN", "LEFT", "RIGHT"] |
|
sex = "FEMALE" if self.age else "BABY" |
|
rat_images = {} |
|
for direction in directions: |
|
rat_images.update({direction: tk.PhotoImage(file=f"Rat/BMP_{sex}_{direction}.png").zoom(3)}) |
|
|
|
gray_pixels = [] |
|
for y in range(rat_images[direction].height()): |
|
for x in range(rat_images[direction].width()): |
|
if rat_images[direction].get(x, y) == (128, 128, 128): |
|
gray_pixels.append((x, y)) |
|
for x, y in gray_pixels: |
|
rat_images[direction].transparency_set(x, y, 1) |
|
return rat_images |
|
|