10from .flyweight_helpers
import Helpers
11from .flyweight_classes
import BigResourceManager, Flyweight_Class, Flyweight_Context
30 numImages = 3
if numImages < 3
else numImages
31 numImages = 9
if numImages > 9
else numImages
33 height = max(3, height)
36 for row
in range(0, height):
37 image_row = bytearray()
38 for imageIndex
in range(0, numImages):
39 if row == 0
or ((row + 1) == height):
41 image_row.extend(bytearray(
"+{0}+".format(
'-' * (width - 2)).encode()))
45 image_row.extend(bytearray(
"|{0}|".format(c * (width - 2)).encode()))
46 image.append(image_row)
48 resourceId = BigResourceManager.AddResource(image)
58 for index
in range(0, len(row)):
73 for row
in range(0, height):
74 display.append(bytearray((
' ' * width).encode()))
86 print(
" {}".format(row.decode()))
103 for flyweight
in flyweightInstances:
104 context = flyweight.Context
105 image_width = flyweight.ImageWidth
106 image_height = flyweight.ImageHeight
107 newx = context.Position_X + context.Velocity_X
108 newy = context.Position_Y + context.Velocity_Y
110 if newx < 0
or (newx + image_width) > display_width:
112 context.Velocity_X = -context.Velocity_X
116 newx = display_width - image_width
118 if newy < 0
or (newy + image_height) > display_height:
120 context.Velocity_Y = -context.Velocity_Y
124 newy = display_height - image_height
126 context.Position_X = newx
127 context.Position_Y = newy
128 flyweight.Context = context
142 for flyweight
in flyweightInstances:
143 context = flyweight.Context
144 flyweight.Render(displayArea, context.OffsetXToImage, context.ImageWidth, context.ImageHeight, int(context.Position_X), int(context.Position_Y))
154 speed = ((random.random() * 5) + 1) / 5.0
155 direction = 1.0
if ((random.random() * 100) > 50)
else -1.0
156 return speed * direction
182 image_width : int, image_height : int, display_width : int, display_height : int) -> list[Flyweight_Class]:
183 flyweightInstances = []
187 for index
in range(0, numFlyweights):
189 context.OffsetXToImage = index * image_width
190 context.ImageWidth = image_width
191 context.ImageHeight = image_height
193 context.Position_X = random.random() * (display_width - image_width)
194 context.Position_Y = random.random() * (display_height - image_height)
201 flyweightInstances.append(BigResourceManager.CreateFlyweight(bigResourceId, context))
202 return flyweightInstances
226 print(
"Flyweight Exercise")
234 NUM_ITERATIONS = 1000
238 IMAGE_WIDTH, IMAGE_HEIGHT, DISPLAY_WIDTH, DISPLAY_HEIGHT)
247 print(
" The image rendered {0} times:".format(NUMFLYWEIGHTS))
255 cursorLeft, cursorTop = helpers.getcursorposition()
256 if cursorLeft != -1
and cursorTop != -1:
257 cursorTop -= DISPLAY_HEIGHT + 1
259 for index
in range(0, NUM_ITERATIONS):
261 helpers.setcursorposition(cursorLeft, cursorTop - 1)
262 print(
" {0:5}/{1} iterations [press a key to exit early]".format(index + 1, NUM_ITERATIONS))
264 helpers.setcursorposition(cursorLeft, cursorTop)
271 if helpers.checkforkey():
Represents the context for an instance of the Flyweight_Class.
Class containing a number of helper methods for use in the Flyweight Pattern example.
def Flyweight_Exercise()
Example of using the Flyweight Pattern.
int _Flyweight_GenerateBigResource(int numImages, int width, int height)
Generate a big resource, in this case, a text master "image" of the specified height,...
None _Flyweight_RenderFlyweights(list[Flyweight_Class] flyweightInstances, list[bytearray] displayArea)
Render the image into the display, once for each flyweight instance.
None _Flyweight_MoveFlyweights(list[Flyweight_Class] flyweightInstances, int display_width, int display_height)
Move the given flyweight instances within the display, bouncing them off the edges of the display.
list[Flyweight_Class] _Flyweight_GenerateFlyweightClasses(int bigResourceId, int numFlyweights, int image_width, int image_height, int display_width, int display_height)
Helper method to generate the specified number of flyweight class instances and associate those insta...
None _Flyweight_ShowDisplay(list[bytearray] display)
Render the display to the screen.
list[bytearray] _Flyweight_GenerateDisplay(int width, int height)
Generate a display area in which to render the big resource.
float GenerateVelocity()
Generate a random velocity, which includes a speed and a direction.
None _Flyweight_ClearDisplay(list[bytearray] display)
Clear the "display" to a background image, erasing whatever was there before.