Skoči na vsebino

P1 2021/22 - 12 Bitka - resitev Starc Aljaz

Warning

Ta resitev deluje, a je napacna, saj ne uporablja koncepta objektnega programiranja (kar je celoten point naloge)

from typing import List, Tuple
import risar
import signal, sys
import random
import itertools

app_timeout_sec = 20
app_wait_cycle_delay = .0025
app_speed_per_cycle = 1
app_amount_of_circles = 10
app_circle_radius = 10
app_mouse_circle_radius = 30

# EXIT AFTER {app_timeout_sec} seconds
def end_it_all(signum, frame): sys.exit(1)
signal.signal(signal.SIGALRM, end_it_all)
signal.setitimer(signal.ITIMER_REAL, app_timeout_sec)


def rnd_num (start: int|float, end: int|float, disallowed: List[Tuple[int|float, int|float]]):
    while True:
        num = random.uniform(start, end)
        # check if generated number is in any disallowed range
        for rng in disallowed:
            # if it is, just break the loop and generete a new number
            if (rng[0] < num < rng[1]):
                break
        else:
            # number is not in any disallowed ranges!
            # we return it :)
            return num



def rnd_ladja (x = None, y = None, r = None, barva = None, sirina = 1):
    if not x:
        x, _ = risar.nakljucne_koordinate()
    if not y:
        _, y = risar.nakljucne_koordinate()
    if not r:
        r = random.randint(2, 20)
    if not barva:
        barva = risar.nakljucna_barva()

    return risar.krog(x=x, y=y, r=r, barva=barva, sirina=sirina)


def do_they_collide (x1, y1, r1, x2, y2, r2):
    distSq = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); 
    radSumSq = (r1 + r2) * (r1 + r2); 
    return distSq <= radSumSq

def fade_it (krog, alpha):
    brush = krog.pen().color().lighter()
    brush.setAlpha(alpha)
    krog.setBrush(brush)



ladje = [ {
    "l": rnd_ladja(r=app_circle_radius),
    "r": app_circle_radius,
    "gx": rnd_num(-1, 1, [(-.6, .6)]),
    "gy": rnd_num(-1, 1, [(-.6, .6)]),
    "expl": None
} for _ in range(app_amount_of_circles) ]

miska_krog = risar.krog(*risar.miska(), r=app_mouse_circle_radius, barva=risar.nakljucna_barva(), sirina=1)
miska_krog_fixed = None
miska_krog_alpha = None

while True:

    if miska_krog:

        if not miska_krog_fixed and risar.klik():
            miska_krog_fixed = risar.miska()
            miska_krog_alpha = 255
        
        miska_krog.setPos(*(miska_krog_fixed or risar.miska()))

        if miska_krog_fixed:
            fade_it(miska_krog, miska_krog_alpha)
            miska_krog_alpha -= 1
            if (miska_krog_alpha <= 0):
                risar.odstrani(miska_krog)
                miska_krog_fixed = None
                miska_krog = None
    
    for ladja, i in zip(ladje, itertools.count()):

        if ladja["expl"] is not None:
            fade_it(ladja["l"], ladja["expl"])
            ladja["expl"] -= 1
            if ladja["expl"] == 0:
                risar.odstrani(ladja["l"])
                del ladje[i]

            continue

        x = ladja["l"].x()
        y = ladja["l"].y()

        o_left = x - ladja["r"] <= 0
        o_right = x + ladja["r"] >= risar.maxx
        o_top = y - ladja["r"] <= 0
        o_bottom = y + ladja["r"] >= risar.maxy


        # levi del kroga je izven okna, desni se ni
        if (o_left and not o_right): ladja["gx"] = abs(ladja["gx"])
        # desni del kroga je izven okna, levi se ni 
        if (not o_left and o_right): ladja["gx"] = 0 - abs(ladja["gx"])

        # spodnji del kroga je izven okna, zgornji se ni 
        if (not o_bottom and o_top): ladja["gy"] = abs(ladja["gx"])
        # spodnji del kroga je izven okna, zgornji se ni
        if (o_bottom and not o_top): ladja["gy"] = 0 - abs(ladja["gy"])

        # premaknemo ladjo
        ladja["l"].setPos(
            ladja["l"].x() + ladja["gx"] * app_speed_per_cycle,
            ladja["l"].y() + ladja["gy"] * app_speed_per_cycle
        )

        for x1, y1, r1 in [ (l["l"].x(), l["l"].y(), l["r"]) for l in ladje if (l is not ladja and l["expl"]) ] + list([ (*miska_krog_fixed, app_mouse_circle_radius) ] if miska_krog_fixed else []):
            if do_they_collide(x, y, ladja["r"], x1, y1, r1):
                ladja["expl"] = 255

    
    risar.cakaj(app_wait_cycle_delay)

Zadnja posodobitev: May 7, 2022