本文整理汇总了Python中pygame.compat.xrange_函数的典型用法代码示例。如果您正苦于以下问题:Python xrange_函数的具体用法?Python xrange_怎么用?Python xrange_使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了xrange_函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_to_string__premultiplied
def test_to_string__premultiplied(self):
""" test to make sure we can export a surface to a premultiplied alpha string
"""
def convertRGBAtoPremultiplied(surface_to_modify):
for x in xrange_(surface_to_modify.get_width()):
for y in xrange_(surface_to_modify.get_height()):
color = surface_to_modify.get_at((x, y))
premult_color = (color[0] * color[3] // 255,
color[1] * color[3] // 255,
color[2] * color[3] // 255,
color[3])
surface_to_modify.set_at((x, y), premult_color)
test_surface = pygame.Surface((256, 256), pygame.SRCALPHA, 32)
for x in xrange_(test_surface.get_width()):
for y in xrange_(test_surface.get_height()):
i = x + y*test_surface.get_width()
test_surface.set_at((x,y), ((i*7) % 256, (i*13) % 256, (i*27) % 256, y))
premultiplied_copy = test_surface.copy()
convertRGBAtoPremultiplied(premultiplied_copy)
self.assertPremultipliedAreEqual(pygame.image.tostring(test_surface, "RGBA_PREMULT"),
pygame.image.tostring(premultiplied_copy, "RGBA"),
pygame.image.tostring(test_surface, "RGBA"))
self.assertPremultipliedAreEqual(pygame.image.tostring(test_surface, "ARGB_PREMULT"),
pygame.image.tostring(premultiplied_copy, "ARGB"),
pygame.image.tostring(test_surface, "ARGB"))
no_alpha_surface = pygame.Surface((256, 256), 0, 24)
self.assertRaises(ValueError, pygame.image.tostring, no_alpha_surface, "RGBA_PREMULT")
示例2: AreSurfacesIdentical
def AreSurfacesIdentical(surf_a, surf_b):
if surf_a.get_width() != surf_b.get_width() or surf_a.get_height() != surf_b.get_height():
return False
for y in xrange_(surf_a.get_height()):
for x in xrange_(surf_b.get_width()):
if surf_a.get_at((x,y)) != surf_b.get_at((x,y)):
return False
return True
示例3: convertRGBAtoPremultiplied
def convertRGBAtoPremultiplied(surface_to_modify):
for x in xrange_(surface_to_modify.get_width()):
for y in xrange_(surface_to_modify.get_height()):
color = surface_to_modify.get_at((x, y))
premult_color = (color[0] * color[3] // 255,
color[1] * color[3] // 255,
color[2] * color[3] // 255,
color[3])
surface_to_modify.set_at((x, y), premult_color)
示例4: equal_images
def equal_images(s1, s2):
size = s1.get_size()
if s2.get_size() != size:
return False
w, h = size
for x in xrange_(w):
for y in xrange_(h):
if s1.get_at((x, y)) != s2.get_at((x, y)):
return False
return True
示例5: vPlayer
def vPlayer( fName ):
global ovl
f= open( fName, 'rb' )
fmt= f.readline().strip()
res= f.readline().strip()
col= f.readline().strip()
if fmt!= "P5":
print ('Unknown format( len %d ). Exiting...' % len( fmt ))
return
w,h= [ int(x) for x in res.split( ' ' ) ]
h= ( h* 2 )/ 3
# Read into strings
y= f.read( w*h )
u= []
v= []
for i in xrange_( 0, h/2 ):
u.append( f.read( w/2 ))
v.append( f.read( w/2 ))
u= ''.join(u)
v= ''.join(v)
# Open overlay with the resolution specified
ovl= pygame.Overlay(pygame.YV12_OVERLAY, (w,h))
ovl.set_location(0, 0, w, h)
ovl.display((y,u,v))
while 1:
pygame.time.wait(10)
for ev in pygame.event.get():
if ev.type in (pygame.KEYDOWN, pygame.QUIT):
return
示例6: test_set_num_channels
def test_set_num_channels(self):
mixer.init()
for i in xrange_(1, mixer.get_num_channels() + 1):
mixer.set_num_channels(i)
self.assert_(mixer.get_num_channels() == i)
mixer.quit()
示例7: RotateARGBtoRGBA
def RotateARGBtoRGBA(str_buf):
byte_buf = array.array("B", str_buf)
num_quads = len(byte_buf)//4
for i in xrange_(num_quads):
alpha = byte_buf[i*4 + 0]
byte_buf[i*4 + 0] = byte_buf[i*4 + 1]
byte_buf[i*4 + 1] = byte_buf[i*4 + 2]
byte_buf[i*4 + 2] = byte_buf[i*4 + 3]
byte_buf[i*4 + 3] = alpha
return byte_buf.tostring()
示例8: assertPremultipliedAreEqual
def assertPremultipliedAreEqual(self, string1, string2, source_string):
self.assertEqual(len(string1), len(string2))
block_size = 20
if string1 != string2:
for block_start in xrange_(0, len(string1), block_size):
block_end = min(block_start + block_size, len(string1))
block1 = string1[block_start:block_end]
block2 = string2[block_start:block_end]
if block1 != block2:
source_block = source_string[block_start:block_end]
msg = "string difference in %d to %d of %d:\n%s\n%s\nsource:\n%s" % (block_start, block_end, len(string1), block1.encode("hex"), block2.encode("hex"), source_block.encode("hex"))
self.fail(msg)
示例9: test_get_pixel
def test_get_pixel (self):
for bpp in (8, 16, 24, 32):
sf = pygame.Surface ((10, 20), 0, bpp)
sf.fill ((0, 0, 255))
for x in xrange_ (20):
sf.set_at ((1, x), (0, 0, 11))
for x in xrange_ (10):
sf.set_at ((x, 1), (0, 0, 11))
ar = pygame.PixelArray (sf)
ar2 = ar.__getitem__ (0).__getitem__ (0)
self.assertEqual (ar2, sf.map_rgb ((0, 0, 255)))
ar2 = ar.__getitem__ (1).__getitem__ (0)
self.assertEqual (ar2, sf.map_rgb ((0, 0, 11)))
ar2 = ar.__getitem__ (-4).__getitem__ (1)
self.assertEqual (ar2, sf.map_rgb ((0, 0, 11)))
ar2 = ar.__getitem__ (-4).__getitem__ (5)
self.assertEqual (ar2, sf.map_rgb ((0, 0, 255)))
示例10: test_wait
def test_wait(self):
# __doc__ (as of 2008-06-28) for pygame.threads.WorkerQueue.wait:
# waits until all tasks are complete.
wq = WorkerQueue()
for i in xrange_(2000): wq.do(lambda x: x+1, i)
wq.wait()
self.assertRaises(Empty, wq.queue.get_nowait)
wq.stop()
示例11: setUp
def setUp(self):
pygame.display.init()
self.screen = pygame.display.set_mode(screen_dims, flags)
self.screen.fill([0,0,0])
pygame.display.flip()
sprite_surface = pygame.image.load(os.path.join(data_dir, "asprite.bmp"))
sprite_surface2 = pygame.image.load(os.path.join(data_dir, "static.png"))
if use_rle:
sprite_surface.set_colorkey([0xFF, 0xFF, 0xFF], SRCCOLORKEY|RLEACCEL)
sprite_surface2.set_colorkey([0xFF, 0xFF, 0xFF], SRCCOLORKEY|RLEACCEL)
else:
sprite_surface.set_colorkey([0xFF, 0xFF, 0xFF], SRCCOLORKEY)
sprite_surface2.set_colorkey([0xFF, 0xFF, 0xFF], SRCCOLORKEY)
if use_alpha:
sprite_surface = sprite_surface.convert_alpha()
sprite_surface2 = sprite_surface2.convert_alpha()
else:
sprite_surface = sprite_surface.convert()
sprite_surface2 = sprite_surface2.convert()
Thingy.images = [sprite_surface]
if use_static:
Static.images = [sprite_surface2]
self.sprites = None
if use_FastRenderGroup:
## sprites = FRG.FastRenderGroup()
self.sprites = FRG.LayeredDirty()
else:
if update_rects:
self.sprites = pygame.sprite.RenderUpdates()
else:
self.sprites = pygame.sprite.Group()
for i in xrange_(0, self.numsprites):
if use_static and i%2==0:
self.sprites.add(Static())
self.sprites.add(Thingy())
self.background = pygame.Surface(self.screen.get_size())
self.background = self.background.convert()
self.background.fill([0,0,0])
示例12: test_stop
def test_stop(self):
# __doc__ (as of 2008-06-28) for pygame.threads.WorkerQueue.stop:
# Stops the WorkerQueue, waits for all of the threads to finish up.
#
wq = WorkerQueue()
self.assert_(len(wq.pool) > 0)
for t in wq.pool: self.assert_(t.isAlive())
for i in xrange_(200): wq.do(lambda x: x+1, i)
wq.stop()
for t in wq.pool: self.assert_(not t.isAlive())
self.assert_(wq.queue.get() is STOP)
示例13: test_tmap
def test_tmap(self):
# __doc__ (as of 2008-06-28) for pygame.threads.tmap:
# like map, but uses a thread pool to execute.
# num_workers - the number of worker threads that will be used. If pool
# is passed in, then the num_workers arg is ignored.
# worker_queue - you can optionally pass in an existing WorkerQueue.
# wait - True means that the results are returned when everything is finished.
# False means that we return the [worker_queue, results] right away instead.
# results, is returned as a list of FuncResult instances.
# stop_on_error -
func, data = lambda x:x+1, xrange_(100)
tmapped = list(tmap(func, data))
mapped = list(map(func, data))
self.assertEqual(tmapped, mapped)
示例14: _clip_and_draw_line_width
def _clip_and_draw_line_width(surface, c_color, width, start, end):
x0, y0 = start
x1, y1 = end
xinc = yinc = 0
if abs(x1 - x0) > abs(y1 - y0):
yinc = 1
else:
xinc = 1
# XXX: Instead of getting the minimum and maximum for each direction (which
# we do here), pygame gets the minimum of the start coords and the
# maximum of the end coords. I think we're right, but we should maybe
# match what pygame does instead even though it's more of a pain to
# implement.
points = set()
p0 = (x0, y0)
p1 = (x1, y1)
if _clip_and_draw_line(surface, c_color, p0, p1):
points.update((p0, p1))
for i in xrange_(width // 2):
p0 = (x0 + xinc * (i + 1), y0 + yinc * (i + 1))
p1 = (x1 + xinc * (i + 1), y1 + yinc * (i + 1))
if _clip_and_draw_line(surface, c_color, p0, p1):
points.update((p0, p1))
# When the width is odd, we only draw the +xinc case
# on the last pass through the loop
if (2 * i + 2 < width):
p0 = (x0 - xinc * (i + 1), y0 - yinc * (i + 1))
p1 = (x1 - xinc * (i + 1), y1 - yinc * (i + 1))
if _clip_and_draw_line(surface, c_color, p0, p1):
points.update((p0, p1))
if points:
# points would be empty if nothing was drawn
return _make_drawn_rect(points, surface)
return None
示例15: main
def main(update_rects = True,
use_static = False,
use_FastRenderGroup = False,
screen_dims = [640, 480],
use_alpha = False,
flags = 0,
):
"""Show lots of sprites moving around
Optional keyword arguments:
update_rects - use the RenderUpdate sprite group class (default True)
use_static - include non-moving images (default False)
use_FastRenderGroup - Use the FastRenderGroup sprite group (default False)
screen_dims - Pygame window dimensions (default [640, 480])
use_alpha - use alpha blending (default False)
flags - additional display mode flags (default no addiontal flags)
"""
if use_FastRenderGroup:
update_rects = True
#pygame.init()
pygame.display.init()
#if "-fast" in sys.argv:
screen = pygame.display.set_mode(screen_dims, flags)
# this is mainly for GP2X, so it can quit.
pygame.joystick.init()
num_joysticks = pygame.joystick.get_count()
if num_joysticks > 0:
stick = pygame.joystick.Joystick(0)
stick.init() # now we will receive events for the joystick
screen.fill([0,0,0])
pygame.display.flip()
sprite_surface = pygame.image.load(os.path.join(data_dir, "asprite.bmp"))
sprite_surface2 = pygame.image.load(os.path.join(data_dir, "static.png"))
if use_rle:
sprite_surface.set_colorkey([0xFF, 0xFF, 0xFF], SRCCOLORKEY|RLEACCEL)
sprite_surface2.set_colorkey([0xFF, 0xFF, 0xFF], SRCCOLORKEY|RLEACCEL)
else:
sprite_surface.set_colorkey([0xFF, 0xFF, 0xFF], SRCCOLORKEY)
sprite_surface2.set_colorkey([0xFF, 0xFF, 0xFF], SRCCOLORKEY)
if use_alpha:
sprite_surface = sprite_surface.convert_alpha()
sprite_surface2 = sprite_surface2.convert_alpha()
else:
sprite_surface = sprite_surface.convert()
sprite_surface2 = sprite_surface2.convert()
Thingy.images = [sprite_surface]
if use_static:
Static.images = [sprite_surface2]
if len(sys.argv) > 1:
try:
numsprites = int(sys.argv[-1])
except Exception:
numsprites = 100
else:
numsprites = 100
sprites = None
if use_FastRenderGroup:
## sprites = FRG.FastRenderGroup()
sprites = FRG.LayeredDirty()
else:
if update_rects:
sprites = pygame.sprite.RenderUpdates()
else:
sprites = pygame.sprite.Group()
for i in xrange_(0, numsprites):
if use_static and i%2==0:
sprites.add(Static())
sprites.add(Thingy())
done = False
frames = 0
start = time()
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill([0,0,0])
while not done:
if not update_rects:
screen.fill([0,0,0])
## for sprite in sprites:
#.........这里部分代码省略.........