本文整理汇总了Python中field.Field.clear方法的典型用法代码示例。如果您正苦于以下问题:Python Field.clear方法的具体用法?Python Field.clear怎么用?Python Field.clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类field.Field
的用法示例。
在下文中一共展示了Field.clear方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Saver
# 需要导入模块: from field import Field [as 别名]
# 或者: from field.Field import clear [as 别名]
class Saver():
def __init__(self, balls=int(random.random() * 100), trail=" "):
self.field = Field(title="Term Saver")
self.balls = [Ball(x=int(random.random() * self.field.x-1)+1, y=int(random.random() * self.field.y-1)+1) for x in range(balls)]
self.speed = 0.009
self.trail = trail
return
def update(self):
for ball in self.balls:
hitWall = self.walled(ball)
if hitWall: # wall collision
ball.bounce(hitWall)
# ball collision
self.clearTrail(ball, self.trail, True)
ball.move()
self.field.write_at(item=ball.image, coords=ball.getPosition())
# clear the field randomly (.1% chance)
if random.choice(range(1000)) == 1:
self.field.clear()
self.field.deploy()
return
def walled(self, ball):
direction = []
if ball.x < 1:
direction.append('right')
elif ball.x >= self.field.x-1:
direction.append('left')
if ball.y < 1:
direction.append('down')
elif ball.y >= self.field.y-1:
direction.append('up')
if len(direction):
return ' '.join(direction)
return None
def run(self):
run = 1
while run:
c = self.field.display.getch()
if c == ord('q'):
run = 0
self.update()
time.sleep(self.speed)
self.field.destroy()
return
def clearTrail(self, obj, remains=" ", centered=False):
for i in range(len(obj.image)):
self.field.write_at(item=remains, coords=[obj.x+i, obj.y], centered=centered)
return
示例2: __init__
# 需要导入模块: from field import Field [as 别名]
# 或者: from field.Field import clear [as 别名]
class DiamondShift:
def __init__(self):
pygame.init()
pygame.mouse.set_visible(0)
self.screen = pygame.display.set_mode((320, 240), 0, 32)
self.gems = pygame.image.load("gems.png").convert_alpha()
self.bg = pygame.image.load("bg.png").convert_alpha()
self.gems = self.separate_gems(self.gems)
self.gemsize = self.gems[0].get_width()
self.grey = (75, 75, 75)
self.cursor = pygame.Surface((self.gemsize, self.gemsize)).convert_alpha()
self.cursor.fill(self.grey)
self.font = pygame.font.Font(None, 24)
self.score = 0
self.field = Field(8, 8, len(self.gems) - 1)
self.ctrl = Control(Point(7, 7))
self.state = 'standby' # | swap | swap-back | fill | check | score
self.swap = None
self.winners = None
def run(self):
while self.loop():
self.read()
self.step()
# self.draw()
def loop(self):
return not self.ctrl.quit
def read(self):
update(self.ctrl)
def step(self):
delay = 16
if self.state == 'standby':
if self.ctrl.direction:
self.swap = deepcopy((self.ctrl.pos, self.ctrl.direction))
self.state = 'swap'
elif self.state == 'swap':
self.field.swaps(self.swap[0].x, self.swap[0].y, self.swap[1])
self.winners = self.field.find_winners()
if self.winners:
self.state = 'score'
else:
self.state = 'swap-back'
delay = 150
elif self.state == 'swap-back':
self.field.swaps(self.swap[0].x, self.swap[0].y, self.swap[1])
self.state = 'standby'
delay = 150
elif self.state == 'fill':
again = self.field.fall()
again = self.field.pour() or again
if not again:
self.state = 'check'
delay = 150
elif self.state == 'check':
self.winners = self.field.find_winners()
if self.winners:
self.state = 'score'
else:
self.state = 'standby'
delay = 150
elif self.state == 'score':
def score(n):
return (n - 1)**2
self.score += score(len(self.winners))
self.field.clear(self.winners)
self.state = 'fill'
delay = 150
self.draw()
pygame.time.delay(delay)
def draw(self):
xoffset = 11
yoffset = 24
size = self.gems[0].get_width()
self.screen.blit(self.bg,(0,0))
self.screen.blit(self.cursor, (xoffset + self.gemsize * self.ctrl.pos.x, yoffset + self.gemsize * self.ctrl.pos.y))
for x in xrange(self.field.width):
for y in xrange(self.field.height):
self.screen.blit(self.gems[self.field.map.get((x, y), 0)],(xoffset + x * size, yoffset + y * size))
self.screen.blit(self.font.render("%d" % self.score, 1, (255, 255, 255)), (15, 3))
pygame.display.flip()
def separate_gems(self, gems):
ng = []
size = self.gems.get_height()
count = self.gems.get_width()/gems.get_height()
for c in xrange(count):
s = pygame.Surface((size,size),pygame.SRCALPHA, 32).convert_alpha()
s.blit(self.gems, (0, 0), pygame.Rect(c * size, 0, (c + 1) * size, size))
ng.append(s)
#.........这里部分代码省略.........
示例3: MaskSaver
# 需要导入模块: from field import Field [as 别名]
# 或者: from field.Field import clear [as 别名]
class MaskSaver(Saver):
def __init__(self, balls=int(random.random() * 100), trail=" ", mask=None):
self.field = Field(title="Term Saver")
self.balls = [Ball(x=int(random.random() * self.field.x-1)+1, y=int(random.random() * self.field.y-1)+1) for x in range(balls)]
self.speed = 0.009
self.trail = trail
self.addMask(mask)
def addMask(self, mask):
"""
Given a 2D array depciting some image to mask out
e.g. a box or a name or a picture of peeve
shrink or fatten it up to fit the shape of our field/grid
dimensions must be at least.... 4 x 4 ? e.g.
. . . .
. x x .
. x x .
. . . .
The players on the field should never write to
the 'x'd out areas.
but our grid will probably be larger than this...
so what is the maths behind making this fit properly?
e.g. a 4 x 4 mask supplied for a 64 x 64 grid
let's start small and just double it
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . x x x x x x . . . . . . . . .
. . . . . . . . . x x x x x x . . . x x x x . .
. . . . . . . . . x x x x x x . . . x x x x . .
. . . . . . . . => . x x x x x x . or . . x x x x . .
. . . . . . . . . x x x x x x . . . x x x x . .
. . . . . . . . . x x x x x x . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
bad good
I think the result where we look at the proportionality works best.
The first transformation has a single border like the original,
and the second maintains the proportions (e.g. 50%).
What happens when it's more awkward?
. . . . . . . . . . . . . . . . . .
. . . . . . => . x x x x . or . . x x . .
. . . . . . . . . . . . . . . . . .
bad good
I still like the second transformation.
So I guess when taking 1/2 of an odd, round down?
"""
pass
def update(self):
for ball in self.balls:
hitWall = self.walled(ball)
if hitWall: # wall collision
ball.bounce(hitWall)
# ball collision
self.clearTrail(ball, self.trail, True)
ball.move()
self.field.write_at(item=ball.image, coords=ball.getPosition())
# clear the field randomly (.1% chance)
if random.choice(range(1000)) == 1:
self.field.clear()
self.field.deploy()