Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Random useful functions
- #Bobby Clarke
- #Open Source License
- class misc():
- def spaceTuple(totuple):
- """Takes a string and creates a tuple from it, elements are seperated by spaces"""
- newtuple = []
- newelement = ""
- for char in str(totuple):
- if char != " ":
- newelement += char
- print(newelement)
- else:
- newtuple.append(newelement)
- newelement = ""
- if newelement:
- newtuple.append(newelement)
- return tuple(newtuple)
- def get_dir(files = False):
- """Get the directory the program is in, have True as a parameter to get a list of the files within said directory"""
- import sys
- import os
- selfdir = os.path.dirname(sys.argv[0])
- if files:
- return os.listdir(selfdir)
- else:
- return selfdir
- class maths():
- import math
- def isWholeNumber(num):
- if num == round(num):
- return True
- else:
- return False
- def median(nums):
- """Take a list of numbers and return the median"""
- import math
- halflen = len(nums) / 2
- nums.sort()
- if isWholeNumber(halflen):
- return nums[halflen]
- else:
- return nums[math.floor(halflen)] + nums[math.ceil(halflen)] / 2
- def adv_range(limit1, limit2 = None, increment = 1.):
- """Range function that accepts floats (and integers)."""
- if limit2 is None:
- limit2, limit1 = limit1, 0.
- else:
- limit1 = float(limit1)
- count = int(math.ceil(limit2 - limit1)/increment)
- return (limit1 + n*increment for n in range(count))
- def chance(num, outof):
- """returns a bool, the chance of it being true = num / outof"""
- import random
- if random.randint(1, outof) in range(num):
- return True
- else:
- return False
- class pygame():
- try:
- import pygame
- except ImportError:
- raise ImportError("You do not appear to have pygame, get it at www.pygame.org")
- def imageSizeRatio(surface1, surface2):
- """Finds the width and height ratio between two pygame.Surface objects and returns them,
- the screen can also be used for use in resizable or fullscreen applications"""
- if not isinstance(surface1, pygame.Surface) or not isinstance(surface2, pygame.Surface):
- raise TypeError("imageSizeRatio requires two pygame.Surface objects")
- scale1 = (surface1.get_width(), surface1.get_height())
- scale2 = (surface2.get_width(), surface2.get_height())
- #Index 0 represents width and index 1 represents height
- if scale1[0] > scale2[0]:
- widthRatio = scale1[0] / scale2[0]
- else:
- widthRatio = scale2[0] / scale1[0] #also catches if equal, thus returning 1
- if scale1[1] > scale2[1]:
- heightRatio = scale1[1] / scale2[1]
- else:
- heightRatio = scale2[1] / scale1[1]
- return widthRatio, heightRatio
- def areaclick(area, mouse = pygame.mouse.get_pos()):
- """mouse must be a tuple or list with 2 elements describing the x and y co - ordinates of the mouse, use pygame.mouse.get_pos(),
- area must be a tuple or list of 4 numbers creating a rectangle like so: (top left, bottom left, top right, bottom right), returns a bool"""
- if mouse[0] in range(area[0], area[1]) and mouse[1] in range(area[2], area[3]):
- return True
- else:
- return False
- if __name__ == "__main__":
- print("This is a group of useful open source functions crated by Bobby Clarke")
- input()
Advertisement
Add Comment
Please, Sign In to add comment