Nuggets



What is a Nugget?


In PyNugget, a Nugget is a useful piece of code you may want to re-use. We encourage users to write Nuggets of code which they can use for various scenarios, for example, the one below.



Nuggets

We made some Nuggets; here are all of the ones so far:


Pixel Rendering


We like pixel games . . . and so do you. So use this pixel renderer to draw pixels on the screen!

def render_pixel(x,y,size,color):
    render_text("■", round(x/size)*size + size / 2, round(y/size)*size + size / 4, "center", size * 2.3, "Roboto", color, "0px 2px 4px #00000000")






Round to X


Python doesn't have this without a special library - so make your own.

def roundto(r1,to):
    return round(r1 / to) * to






Ball Math


I wrote this a while back for ball bouncing. If it makes sense; cool.

def bounce_x():
    global bd, bs, bx, by
    bd = 180 - bd
    
    
def bounce_y():
    global bd, bs, bx, by
    bd = -bd
    
    

def change_pos():
    global bd, bs, bx, by
    r = math.radians(bd)
    bx += (math.cos(r) * bs)
    by += (math.sin(r) * bs)






Buttons


Buttons are the fundamental basis of all UX, so we made a basic button for you!

cursor("auto")
    button_data = [stage_width / 2, 30, 80, 25, "Button_1.svg","Button_Hovered.svg", "Button Text"]
    #-------------[ Button X ------ Y-width-height- image1 - ---- image hovered ------ Text-----]
    
    render_image(button_data[4],button_data[0],button_data[1],100,100,1)
    if (abs(button_data[0] - mouse_x) < button_data[2]) and (abs(button_data[1] - mouse_y) < button_data[3]):
        cursor("pointer")
        render_image(button_data[5],button_data[0],button_data[1],100,100,1)
        if mouse_down:
            new_cloud()
    render_text(button_data[6], button_data[0], button_data[1], "center", 20, "Roboto", "#000000", "0px 2px 4px #00000000")






Line Rendering


Lines can be very useful - just swap out the image source and adjust ic accordingly so the line fits properly.

def render_line(x1,y1,x2,y2,thickness=2):
    ic = 4
    mx = (x1 + x2) / 2; my = (y1 + y2) / 2
    dx = x2 - x1; dy = y2 - y1
    ar = math.atan2(dy, dx); ad = math.degrees(ar)
    distance = math.sqrt(dx*dx + dy*dy)
    render_image("Square808080.png",mx,my,thickness,distance/ic,1,ad+90)