def test_mod (self):
c1 = pygame.Color (0xFFFFFFFF)
self.assertEquals (c1.r, 255)
self.assertEquals (c1.g, 255)
self.assertEquals (c1.b, 255)
self.assertEquals (c1.a, 255)
c2 = pygame.Color (2, 4, 8, 16)
self.assertEquals (c2.r, 2)
self.assertEquals (c2.g, 4)
self.assertEquals (c2.b, 8)
self.assertEquals (c2.a, 16)
c3 = c1 % c2
self.assertEquals (c3.r, 1)
self.assertEquals (c3.g, 3)
self.assertEquals (c3.b, 7)
self.assertEquals (c3.a, 15)
python类Color()的实例源码
def test_int (self):
# This will be a long
c = pygame.Color (0xCC00CC00)
self.assertEquals (c.r, 204)
self.assertEquals (c.g, 0)
self.assertEquals (c.b, 204)
self.assertEquals (c.a, 0)
self.assertEquals (int (c), int (0xCC00CC00))
# This will be an int
c = pygame.Color (0x33727592)
self.assertEquals (c.r, 51)
self.assertEquals (c.g, 114)
self.assertEquals (c.b, 117)
self.assertEquals (c.a, 146)
self.assertEquals (int (c), int (0x33727592))
def test_long (self):
# This will be a long
c = pygame.Color (0xCC00CC00)
self.assertEquals (c.r, 204)
self.assertEquals (c.g, 0)
self.assertEquals (c.b, 204)
self.assertEquals (c.a, 0)
self.assertEquals (long_ (c), long_ (0xCC00CC00))
# This will be an int
c = pygame.Color (0x33727592)
self.assertEquals (c.r, 51)
self.assertEquals (c.g, 114)
self.assertEquals (c.b, 117)
self.assertEquals (c.a, 146)
self.assertEquals (long_ (c), long_ (0x33727592))
def test_set_item (self):
c = pygame.Color (204, 38, 194, 55)
self.assertEquals (c[0], 204)
self.assertEquals (c[1], 38)
self.assertEquals (c[2], 194)
self.assertEquals (c[3], 55)
c[0] = 33
self.assertEquals (c[0], 33)
c[1] = 48
self.assertEquals (c[1], 48)
c[2] = 173
self.assertEquals (c[2], 173)
c[3] = 213
self.assertEquals (c[3], 213)
# Now try some 'invalid' ones
self.assertRaises (ValueError, _assign_item, c, 0, 95.485)
self.assertEquals (c[0], 33)
self.assertRaises (ValueError, _assign_item, c, 1, -83)
self.assertEquals (c[1], 48)
self.assertRaises (ValueError, _assign_item, c, 2, "Hello")
self.assertEquals (c[2], 173)
def colorspaces_converted_should_not_raise (self, prop):
fails = 0
x = 0
for c in rgba_combos_Color_generator():
x += 1
other = pygame.Color(0)
try:
setattr(other, prop, getattr(c, prop))
#eg other.hsla = c.hsla
except ValueError:
fails += 1
self.assert_(x > 0, "x is combination counter, 0 means no tests!")
self.assert_((fails, x) == (0, x))
def colorspaces_converted_should_equate_bar_rounding (self, prop):
for c in rgba_combos_Color_generator():
other = pygame.Color(0)
try:
setattr(other, prop, getattr(c, prop))
#eg other.hsla = c.hsla
self.assert_(abs(other.r - c.r) <= 1)
self.assert_(abs(other.b - c.b) <= 1)
self.assert_(abs(other.g - c.g) <= 1)
# CMY and I1I2I3 do not care about the alpha
if not prop in ("cmy", "i1i2i3"):
self.assert_(abs(other.a - c.a) <= 1)
except ValueError:
pass # other tests will notify, this tests equation
def test_set_at(self):
#24bit surfaces
s = pygame.Surface( (100, 100), 0, 24)
s.fill((0,0,0))
# set it with a tuple.
s.set_at((0,0), (10,10,10, 255))
r = s.get_at((0,0))
self.failUnless(isinstance(r, pygame.Color))
self.assertEqual(r, (10,10,10, 255))
# try setting a color with a single integer.
s.fill((0,0,0,255))
s.set_at ((10, 1), 0x0000FF)
r = s.get_at((10,1))
self.assertEqual(r, (0,0,255, 255))
def test_set_colorkey(self):
# __doc__ (as of 2008-06-25) for pygame.surface.Surface.set_colorkey:
# Surface.set_colorkey(Color, flags=0): return None
# Surface.set_colorkey(None): return None
# Set the transparent colorkey
s = pygame.Surface((16,16), pygame.SRCALPHA, 32)
colorkeys = ((20,189,20, 255),(128,50,50,255), (23, 21, 255,255))
for colorkey in colorkeys:
s.set_colorkey(colorkey)
for t in range(4): s.set_colorkey(s.get_colorkey())
self.assertEquals(s.get_colorkey(), colorkey)
def todo_test_get_at(self):
surf = pygame.Surface((2, 2), 0, 24)
c00 = pygame.Color((1, 2, 3))
c01 = pygame.Color((5, 10, 15))
c10 = pygame.Color((100, 50, 0))
c11 = pygame.Color((4, 5, 6))
surf.set_at((0, 0), c00)
surf.set_at((0, 1), c01)
surf.set_at((1, 0), c10)
surf.set_at((1, 1), c11)
c = surf.get_at((0, 0))
self.failUnless(isinstance(c, pygame.Color))
self.failUnlessEqual(c, c00)
self.failUnlessEqual(surf.get_at((0, 1)), c01)
self.failUnlessEqual(surf.get_at((1, 0)), c10)
self.failUnlessEqual(surf.get_at((1, 1)), c11)
for p in [(-1, 0), (0, -1), (2, 0), (0, 2)]:
self.failUnlessRaises(IndexError, surf.get_at, p, "%s" % (p,))
def test_set_palette_at(self):
pygame.init()
try:
pygame.display.set_mode((100, 50))
surf = pygame.Surface((2, 2), 0, 8)
original = surf.get_palette_at(10)
replacement = Color(1, 1, 1, 255)
if replacement == original:
replacement = Color(2, 2, 2, 255)
surf.set_palette_at(10, replacement)
self.failUnlessEqual(surf.get_palette_at(10), replacement)
next = tuple(original)
surf.set_palette_at(10, next)
self.failUnlessEqual(surf.get_palette_at(10), next)
next = tuple(original)[0:3]
surf.set_palette_at(10, next)
self.failUnlessEqual(surf.get_palette_at(10), next)
self.failUnlessRaises(IndexError,
surf.set_palette_at,
256, replacement)
self.failUnlessRaises(IndexError,
surf.set_palette_at,
-1, replacement)
finally:
pygame.quit()
def __init__(self, x, y, sx, sy):
self.x = x
self.y = y
self.rect = pygame.Rect((x, y), (sx, sy))
self.click = False
self.rcolor = ["Red", "Green", "Blue", "White", "Yellow", "Black"]
self.color = pygame.Color("White")
self.ccolor = random.choice(self.rcolor)
self.vxy = 0
self.vx = 0
self.vy = 0
self.rvx = 0
self.rvy = 0
self.vc = 0
self.rc = 0
self.rcc = 0
self.vcc = 0
self.vcc2 = 0
def update(self, screen_rect):
self.vcc2 = abs(self.rvx) + abs(self.rvy)
if self.click:
self.rect.move_ip(pygame.mouse.get_rel())
self.rect.clamp_ip(screen_rect)
if self.rc == 3:
if self.rvx > 0:
self.rvx -= 1
if self.rvx < 0:
self.rvx += 1
if self.rvy > 0:
self.rvy -= 1
if self.rvy < 0:
self.rvy += 1
if self.vcc >= 30:
self.color = pygame.Color(random.choice(self.rcolor))
if self.rc == 3 and self.vcc2 < 30:
self.color = pygame.Color(self.ccolor)
self.rect.move_ip(self.rvx, self.rvy)
self.rect.clamp_ip(screen_rect)
def draw(self):
if self.S0[0] == 1:
self.M1.draw(self.screen)
if self.S0[1] == 1:
self.screen.fill((pygame.Color("Light Green")))
self.B1.draw(self.screen)
# Draw grid
self.entry_tile.Draw(self.screen, self.width * 0.028, self.height * 0.05)
# Update Player
self.P1.Update()
self.P1.Draw(self.screen, self.width * 0.028, self.height * 0.05)
self.D1.update(self.screen_rect)
self.D1.draw(self.screen)
self.D1.vel(self.width, self.height)
if self.S0[2] == 1:
self.screen.fill((pygame.Color("Light Blue")))
self.B2.draw(self.screen)
self.H1.draw(self.screen)
self.H2.draw(self.screen)
if self.S0[3] == 1:
self.screen.fill((pygame.Color("Yellow")))
self.B3.draw(self.screen)
pygame.display.update()
def __init__(self, x, y, sx, sy):
self.x = x
self.y = y
self.rect = pygame.Rect((x, y), (sx, sy))
self.click = False
self.rcolor = ["Red", "Green", "Blue", "White", "Yellow", "Black"]
self.color = pygame.Color("White")
self.ccolor = random.choice(self.rcolor)
self.vxy = 0
self.vx = 0
self.vy = 0
self.rvx = 0
self.rvy = 0
self.vc = 0
self.rc = 0
self.rcc = 0
self.vcc = 0
self.vcc2 = 0
def update(self, screen_rect):
self.vcc2 = abs(self.rvx) + abs(self.rvy)
if self.click:
self.rect.move_ip(pygame.mouse.get_rel())
self.rect.clamp_ip(screen_rect)
if self.rc == 3:
if self.rvx > 0:
self.rvx -= 1
if self.rvx < 0:
self.rvx += 1
if self.rvy > 0:
self.rvy -= 1
if self.rvy < 0:
self.rvy += 1
if self.vcc >= 30:
self.color = pygame.Color(random.choice(self.rcolor))
if self.rc == 3 and self.vcc2 < 30:
self.color = pygame.Color(self.ccolor)
self.rect.move_ip(self.rvx, self.rvy)
self.rect.clamp_ip(screen_rect)
print(self.rc, self.vcc)
def __init__(self):
self.width = 800
self.height = 600
self.size = (self.width, self.height)
self.caption = "Button Test"
self.color = pygame.Color("Grey")
self.M0 = 1
self.M1 = 0
pygame.init()
self.b1 = Button(self.width * 0.85, self.height * 0.05, self.width * 0.1, self.height * 0.05, "Menu")
self.Menu1 = MainMenu(self.width, self.height)
self.screen = pygame.display.set_mode(self.size)
pygame.display.set_caption(self.caption)
def draw(self, surface):
mouse = pygame.mouse.get_pressed()
self.sound = Sound1
if not self.rect.collidepoint(pygame.mouse.get_pos()):
surface.blit(self.I, (self.rect))
elif self.rect.collidepoint(pygame.mouse.get_pos()):
if mouse[0]:
self.sound.play()
self.I = pygame.transform.scale(self.I, (int(self.sx * 1.02), int(self.sy * 1.02)))
self.rect = pygame.Rect((int(self.x - (self.sx * 1.02 - self.sx)), int(self.y - (self.sy * 1.02 - self.sy))), (int(self.sx), int(self.sy)))
self.srect = pygame.Surface((int(self.sx * 1.02), int(self.sy * 1.04)))
self.srect.fill(pygame.Color("Black"))
self.srect.set_alpha(68)
surface.blit(self.srect, (int(self.x), int(self.y)))
surface.blit(self.I, (self.rect))
def __init__(self, x, y, btext, size, width, rI, color=pygame.Color("black"), underline=None):
self.x = x
self.y = y
self.size = size
self.width = width
self.font = pygame.font.Font("Assets/Berlin Sans FB.ttf", self.size)
self.underline = underline
if self.underline == True:
print("underline = true")
self.font.set_underline(True)
elif self.underline == False:
print("underline = true")
self.font.set_underline(False)
self.color = color
self.btext = btext
self.atext = ""
self.etext = self.font.render(self.btext + self.atext, 1, (self.color))
self.shifted = False
self.maxlength = 60
self.focus = 0
self.rect = pygame.Rect((self.x, self.y), (self.width, self.size))
self.srect = pygame.Surface((int(self.width), int(self.size)))
self.rI = rI
def __init__(self, x, y, btext, size, width, rI, color=pygame.Color("black"), underline=None):
self.x = x
self.y = y
self.size = size
self.width = width
self.font = pygame.font.Font("Assets/Berlin Sans FB.ttf", self.size)
self.underline = underline
if self.underline == True:
print("underline = true")
self.font.set_underline(True)
elif self.underline == False:
print("underline = true")
self.font.set_underline(False)
self.color = color
self.btext = btext
self.atext = ""
self.etext = self.font.render(self.btext + self.atext, 1, (self.color))
self.shifted = False
self.maxlength = 26
self.focus = 0
self.rect = pygame.Rect((self.x, self.y), (self.width, self.size))
self.srect = pygame.Surface((int(self.width), int(self.size)))
self.rI = rI
def __init__(self, x, y, width, height, editable = False, font = textwidget.defaultFont, selectioncolor = selectiontextwidget.defaultSelection):
"""
Initialisation of an Listbox
parameters: int x-coordinate of the Listbox (left)
int y-coordinate of the Listbox (top)
int width of the Listbox
int height of the Listbox
boolean if the Listbox should be editable
pygame.font.Font font of the Listbox
tuple of format pygame.Color representing the Listbox's selection-color
return values: -
"""
super(Listbox, self).__init__(x, y, width, height, "", font, selectioncolor = selectiontextwidget.defaultSelection)
self._list = []
self._editable = editable
def makeCars(self):
index1= list(range(len(self.lanePos1)))
index2 = list(range(len(self.lanePos2)))
shuffle(index1)
shuffle(index2)
color_pro = np.random.sample(self.numCar) < 0.5
lane_pro = np.random.sample(self.numCar) < 0.5
car = []
for i in xrange(0, self.numCar):
color = 1
if color_pro[i]:
color = 0
if lane_pro[i]: ## make use pop method.
idx = index1.pop()
lane = self.lanePos1[idx]
else:
idx = index2.pop()
lane = self.lanePos2[idx]
car.append(Car(0, lane[0], lane[1], self.space, self.width, self.height, goal = self.goal,
car_radius=self.car_radius, agentId=i, color=color))
return car
def __init__(self, r, x, y, space, width, height, goal, car_radius=25, agentId=0, color=0):
self.color = color # 0:green, 1:red
self.agentId = agentId
self.car_radius = car_radius
self.space = space
self.width = width
self.height = height
self.vmax = 200
self.redGoal = goal[0]
self.greenGoal = goal[1]
mass = 1
inertia = pymunk.moment_for_circle(mass, 0, 14, (0, 0))
self.car_body = pymunk.Body(mass, inertia)
self.car_body.position = x, y
self.car_shape = pymunk.Circle(self.car_body, car_radius)
if self.color == 0:
self.car_shape.color = THECOLORS["green"]
else:
self.car_shape.color = THECOLORS["red"]
self.space.add(self.car_body, self.car_shape)
def makeCars(self):
index1= list(range(len(self.lanePos1)))
index2 = list(range(len(self.lanePos2)))
shuffle(index1)
shuffle(index2)
color_pro = np.random.sample(self.numCar) < 0.5
lane_pro = np.random.sample(self.numCar) < 0.5
car = []
for i in xrange(0, self.numCar):
color = 1
if color_pro[i]:
color = 0
if lane_pro[i]: ## make use pop method.
idx = index1.pop()
lane = self.lanePos1[idx]
else:
idx = index2.pop()
lane = self.lanePos2[idx]
car.append(Car(0, lane[0], lane[1], self.space, self.width, self.height, goal = self.goal,
goal2 = self.goal2, car_radius=self.car_radius, agentId=i, color=color))
return car
def getBack(self, test=False):
checkingList = []
if test:
backPoint = self.backPoint-300
else:
backPoint = self.backPoint
for i in xrange(self.numCar):
if self.Cars[i].check_getback(backPoint):
checkingList.append(i)
nCar = len(checkingList)
if nCar != 0:
choise = self.newY
shuffle(choise)
color_pro = np.random.sample(nCar) < 0.5
for i, index in enumerate(checkingList):
if color_pro[i]: color = 0
else : color = 1
pos_y = choise[i%len(choise)]
self.Cars[index].resetCar(color, 0, pos_y)
def __init__(self, r, x, y, space, width, height, goal, goal2, car_radius=25, agentId=0, color=0):
self.color = color # 0:green, 1:red
self.agentId = agentId
self.car_radius = car_radius
self.space = space
self.width = width
self.height = height
self.vmax = 200
self.redGoal = goal[0]
self.greenGoal = goal[1]
self.eRedGoal = goal2[0]
self.eGreenGoal = goal2[1]
mass = 1
inertia = pymunk.moment_for_circle(mass, 0, 14, (0, 0))
self.car_body = pymunk.Body(mass, inertia)
self.car_body.position = x, y
self.car_shape = pymunk.Circle(self.car_body, car_radius)
if self.color == 0:
self.car_shape.color = THECOLORS["green"]
else:
self.car_shape.color = THECOLORS["red"]
self.space.add(self.car_body, self.car_shape)
def check_dest(self):
"""
I think I do not this function.
"""
my_color = self.color
my_angle = self.car_body.angle
my_velocity = self.car_body.velocity
if my_color == 0:
angle1 = self._get_angle(self.redGoal[0])
angle2 = self._get_angle(self.redGoal[1])
reward = max(my_angle*math.cos(angle1), my_angle*math.cos(angle2))
else:
angle1 = self._get_angle(self.greenGoal[0])
angle2 = self._get_angle(self.greenGoal[1])
reward = max(my_angle*math.cos(angle1), my_angle*math.cos(angle2))
return reward
def makeCars(self):
index1= list(range(len(self.lanePos1)))
index2 = list(range(len(self.lanePos2)))
shuffle(index1)
shuffle(index2)
color_pro = np.random.sample(self.numCar) < 0.5
lane_pro = np.random.sample(self.numCar) < 0.5
car = []
for i in xrange(0, self.numCar):
color = 1
if color_pro[i]:
color = 0
if lane_pro[i]: ## make use pop method.
idx = index1.pop()
lane = self.lanePos1[idx]
else:
idx = index2.pop()
lane = self.lanePos2[idx]
car.append(Car(0, lane[0], lane[1], self.space, self.width, self.height, goal = self.goal,
goal2 = self.goal2, car_radius=self.car_radius, agentId=i, color=color))
return car
def __init__(self, r, x, y, space, width, height, goal, goal2, car_radius=25, agentId=0, color=0):
self.color = color # 0:green, 1:red
self.agentId = agentId
self.car_radius = car_radius
self.space = space
self.width = width
self.height = height
self.vmax = 200
self.redGoal = goal[0]
self.greenGoal = goal[1]
self.eRedGoal = goal2[0]
self.eGreenGoal = goal2[1]
mass = 1
inertia = pymunk.moment_for_circle(mass, 0, 14, (0, 0))
self.car_body = pymunk.Body(mass, inertia)
self.car_body.position = x, y
self.car_shape = pymunk.Circle(self.car_body, car_radius)
if self.color == 0:
self.car_shape.color = THECOLORS["green"]
else:
self.car_shape.color = THECOLORS["red"]
self.space.add(self.car_body, self.car_shape)
def check_dest(self):
"""
I think I do not this function.
"""
my_color = self.color
my_angle = self.car_body.angle
my_velocity = self.car_body.velocity
if my_color == 0:
angle1 = self._get_angle(self.redGoal[0])
angle2 = self._get_angle(self.redGoal[1])
reward = max(my_angle*math.cos(angle1), my_angle*math.cos(angle2))
else:
angle1 = self._get_angle(self.greenGoal[0])
angle2 = self._get_angle(self.greenGoal[1])
reward = max(my_angle*math.cos(angle1), my_angle*math.cos(angle2))
return reward
def init_pockets(space):
pockets = []
for i in [(44.1, 44.1), (755.9, 44.1), (755.9, 755.9), (44.1, 755.9)]:
inertia = pymunk.moment_for_circle(0.1, 0, POCKET_RADIUS, (0, 0))
body = pymunk.Body(0.1, inertia)
body.position = i
shape = pymunk.Circle(body, POCKET_RADIUS, (0, 0))
shape.color = POCKET_COLOR
shape.collision_type = 2
shape.filter = pymunk.ShapeFilter(categories=0b1000)
space.add(body, shape)
pockets.append(shape)
del body
del shape
return pockets
# Initialize striker with force