Permalink
Browse files

All

  • Loading branch information...
0 parents commit 726e3432e56b24cc03321a403bd6d96989ed4685 @Dolkar committed Oct 6, 2011
@@ -0,0 +1,91 @@
+import pygame
+import Image
+
+class Animation():
+ '''
+ Support for animated images in pygame.
+ '''
+
+ def __init__(self, path, mode = 'RGB', play_once = False, colorkey = None):
+ self.data = Image.open(path)
+ self.size = self.data.size
+ self.mode = mode
+ self.play_once = play_once
+ self.colorkey = (0, 0, 0) # Dummy
+ self.rewind() # Just getting the first frame
+ if colorkey:
+ self.colorkey = colorkey
+ else:
+ self.colorkey = self.frame.get_at((0, 0))
+ self.rewind()
+
+ def _update(self):
+ frame = self.data.convert(self.mode).tostring()
+ self.frame = pygame.image.frombuffer(frame, self.size, self.mode)
+ self.frame.set_colorkey(self.colorkey)
+
+ def next(self):
+ '''
+ Jumps to next frame.
+ Rewinds back to first frame when done. Set GifViewer.play_once to True
+ to make it stop.
+ '''
+ try:
+ self.data.seek(self.data.tell() + 1)
+ except EOFError: # End of file, this was the last frame
+ if self.play_once:
+ raise
+ else:
+ self.rewind()
+ self._update()
+ return self.frame
+
+ def rewind(self):
+ '''Rewinds back to frame 0'''
+ self.data.seek(0)
+ self._update()
+
+ def getFrameNumber(self):
+ '''Returns the current frame number'''
+ self.data.tell()
+
+ def getFrame(self):
+ '''Returns the surface of the current frame'''
+ return self.frame
+
+if __name__ == '__main__':
+ '''
+ A simple usage example, first asks for a gif file and runs it in infinitely.
+ Holding any key will pause the animation.
+ Pressing a mouse button will restart the animation.
+ '''
+
+ from pygame.locals import *
+ from sys import exit
+
+ pygame.init()
+ gif = Animation(raw_input('Enter path to the image: '))
+ screen = pygame.display.set_mode((100, 100))
+ clock = pygame.time.Clock()
+ animating = True
+ print "Okey, everything goes well, let's take a look..."
+
+ while True:
+ clock.tick(15)
+
+ for event in pygame.event.get():
+ if event.type == QUIT:
+ exit()
+ elif event.type == KEYDOWN:
+ animating = False
+ elif event.type == KEYUP:
+ animating = True
+ elif event.type == MOUSEBUTTONDOWN:
+ gif.rewind()
+
+ screen.fill((128, 128, 128))
+ screen.blit(gif.getFrame(), (20, 20))
+ if animating:
+ gif.next()
+
+ pygame.display.update()
@@ -0,0 +1,73 @@
+from collections import deque
+
+EMPTY = 0
+LINE = 1
+FILLED = 2
+
+ADJ_NODES = [(-1, 0), (0, -1), (1, 0), (0, 1)]
+
+class Node(object):
+ def __init__(self, x, y, offset, distance):
+ self.val = (x, y, offset, distance)
+
+ def __repr__(self):
+ return str(self.val)
+
+ def __hash__(self):
+ return self.val[0:2].__hash__()
+
+ def __eq__(self, other):
+ return self.val[0:2] == other
+
+ def __cmp__(self, other):
+ return self.val[3].__cmp__(other.val[3])
+
+ def __getitem__(self, key):
+ return self.val[key]
+
+
+class PathFinder(object):
+ def __init__(self, start, end, grid):
+ self.grid = grid
+ self.start = tuple(start)
+ self.end = tuple(end)
+ self.distance = abs(self.start[0] - self.end[0]) + \
+ abs(self.start[1] - self.end[1])
+
+ def _getAdjNodes(self, node):
+ res = set()
+ for offset in ADJ_NODES:
+ row = node[0] + offset[0]
+ col = node[1] + offset[1]
+ if self.grid.isEmpty(row, col) \
+ or (row, col) == self.end:
+ distance = abs(row - self.end[0]) + abs(col - self.end[1])
+ res.add(Node(row, col, offset, distance))
+ return res
+
+ def solve(self):
+ a = (self.start) + (None, self.distance)
+ opened = set([Node(*a)])
+ closed = {}
+ running = True
+
+ while running:
+ if not opened:
+ raise ValueError("Path not found!")
+ best = min(opened)
+ running = bool(best[3])
+ closed[best[0:2]] = best[2]
+ opened.discard(best)
+ adj_nodes = self._getAdjNodes(best)
+ opened.update(adj_nodes.difference(closed))
+
+ res = deque()
+ curr_node = self.end
+ while curr_node != self.start:
+ offset = closed[curr_node]
+ res.append(offset)
+ curr_node = (curr_node[0] - offset[0],
+ curr_node[1] - offset[1])
+
+ res.reverse()
+ return res
@@ -0,0 +1,4 @@
+A not well documented utility for measuring the latency to a given IP adress.
+ping.py and SysTrayIcon.py are not mine. Found them over the internet.
+
+It measures not only the average ping over several seconds, but also spikes. Also has the ability to log.
Oops, something went wrong.

0 comments on commit 726e343

Please sign in to comment.