Skip to content

Instantly share code, notes, and snippets.

@cbscribe
cbscribe / Preferences.sublime-settings
Created March 8, 2013 20:57
Settings.SublimeText
{
"auto_complete_commit_on_tab": true,
"bold_folder_labels": true,
"color_scheme": "Packages/Tomorrow Color Schemes/Tomorrow-Night.tmTheme",
"font_face": "Ubuntu Mono",
"font_options":
[
"subpixel_antialias"
],
"font_size": 13.0,
@cbscribe
cbscribe / swapvars.py
Created March 11, 2013 07:24
trick - swap two variables without a tempvar
a = 10
b = 20
a = a + b
b = a - b
a = a - b
# a = 20
# b = 10
@cbscribe
cbscribe / euler.py
Created March 12, 2013 07:26
beauty - Euler's Algorithm
def gcd(x, y):
while y:
x, y = y, x % y
return x
@cbscribe
cbscribe / mcpi-create_dirt_plane
Created April 19, 2013 00:14
clears everything above sea level, and makes everything below dirt
#written by Hiradur
#Replaces any block above sea level with air and every block beneath it with dirt
#to keep processing time short it only replaces blocks from above sea level to level 64
#if you built higher you have to change the parameter
#import minecraft.py module for interaction with the world
import minecraft.minecraft as minecraft
#import minecraft block module for block ids
import minecraft.block as block
#import time for delays
@cbscribe
cbscribe / mcpi_erase_sea_level
Last active December 16, 2015 09:59
erase everything above sea level
#written by Hiradur
#Replaces any block above sea level with air
#too keep processing time short it only replaces blocks from above sea level to level 64
#if you built higher you have to change the parameter
#import minecraft.py module for interaction with the world
import minecraft.minecraft as minecraft
#import minecraft block module for block ids
import minecraft.block as block
#import time for delays
@cbscribe
cbscribe / mcpi_tnt
Created May 26, 2013 22:06
Explode a TNT block
import minecraft
import time
mc = minecraft.Minecraft.create();
while True:
hits = mc.events.pollBlockHits()
for hit in hits:
block = mc.getBlockWithData(hit.pos.x, hit.pos.y, hit.pos.z);
block.data = (block.data + 1) & 0xf;
@cbscribe
cbscribe / pi_monte_carlo.py
Created March 14, 2014 15:24
Calculate Pi using the Monte Carlo method. Happy Pi Day!
# Calculate Pi via the Monte Carlo method
# Throw darts at a square region and count how many fall into a
# quarter-circle. The ratio of that count to the total number of darts
# is 1/4 * Pi!
# More information (and a cool graphic) here:
# http://en.wikipedia.org/wiki/Monte_Carlo_method
#
# A classroom project by KidsCanCode - http://kidscancode.org/
from random import random
import math
@cbscribe
cbscribe / gist:0a46117ec3d05122bdf5387cb6ce1c86
Last active January 24, 2017 04:37
pygame_draw_text_basic
font_name = pg.font.match_font('hack')
def draw_text(text, size, color, x, y, align="topleft"):
font = pg.font.Font(font_name, size)
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect(**{align: (x, y)})
screen.blit(text_surface, text_rect)
@cbscribe
cbscribe / spritesheet.py
Created January 24, 2017 04:41
basic spritesheet class
class Spritesheet:
# utility class for loading and cutting spritesheets
def __init__(self, filename):
self.spritesheet = pg.image.load(filename).convert_alpha()
def get_image_by_rect(self, x, y, w, h):
r = pg.Rect(x, y, w, h)
return self.spritesheet.subsurface(r)
@cbscribe
cbscribe / camera.py
Created January 24, 2017 04:42
Simple pygame scrolling camera
class Camera:
def __init__(self, width, height):
self.camera = pg.Rect(0, 0, width, height)
self.width = width
self.height = height
def apply(self, rect):
return rect.move(self.camera.topleft)
def update(self, target):