Monday, 7 April 2014

Capturing Code Snippets - Part One

Long time no blog!

I've been spending more time with Dart so something had to get out of the way. However I have been revising my Python in time for the PyWeek game jam next month. I thought I would break radio silence on the blog and record a few snippets so 1. I can find them 2. On the off chance they are a handy copy paste for someone else especially beginners! Some of this is from my abandoned (due to illness) previous PyWeek so it never got off my laptop. Care to blog anything yourself? :-)

This first post will cover MISC. Some runnable GFX stuff to come in a follow up post.

Two very simple snippets mainly for moving through colour indexes for graphics primitives but could easily be used for position e.g. a bad guy or obstacle going back and forth.

def keepWithinRange(i,l,u):
    if i<l:
    elif i>u:
    return i

def loopWithinRange(i,l,u):
    if i<l:
    elif i>u:
    return i
Maybe better to rewrite those with Min/Max at some point!

Misc code from various PyGame experiments and Pyweeks!

import random
import pygame
from pygame.locals import *

def CreateBackground(screen):
    bg = pygame.Surface(screen.get_size())
    bg = bg.convert()
    return bg

def DrawText(bg, x, y, text, size=24, color=(255, 255, 255)):
    inst1_font = pygame.font.Font(None, size)
    inst1_surf = inst1_font.render(text, 1, color)
    bg.blit(inst1_surf, [x, y])

def RND(num):
    return random.randint(1,num)
def randomPlusMinus1():
    return random.randint(0,2) -1
def returnTrue():
    return True

No comments:

Post a Comment