Hello
I am trying to create a grid of images in pyglet and python and I am not sure where exactly I am going wrong.
The goal is for it to be a Breakout/Arkanoid clone. The problem I am having is getting the brick images to display in a grid.
Here is the code that as far as I can tell, should place the bricks in the correct position.
class Brick():
def __init__(self, space):
# Create the list to hold different sprite bricks and load images
self.batch = pyglet.graphics.Batch()
self.brick_images = ['brick1.png', 'brick2.png']
self.brick_sprites = []
# 1 out of 5 chance to drop a power pill
self.chance_to_drop = 1
# Set the images anchor point to its center and create sprites
for i in range(len(self.brick_images)):
img = pyglet.image.load(self.brick_images[i])
img.anchor_x = img.width // 2
img.anchor_y = img.height // 2
self.brick_sprites.append(pyglet.sprite.Sprite(img))
for x in range(7):
for y in range(7):
self.body = pymunk.Body(body_type=pymunk.Body.KINEMATIC)
# The position where each pymunk body will be placed
self.body.position = x * 100 + 75, y * 30 + 340
self.brick_type = random.randint(0, len(self.brick_sprites) - 1)
if self.brick_type == 0:
sprite = self.brick_sprites[0]
# Set the sprite to the same position as the pymunk body
sprite.set_position(self.body.position.x, self.body.position.y)
sprite.batch = self.batch
elif self.brick_type == 1:
sprite = self.brick_sprites[1]
sprite.set_position(self.body.position.x, self.body.position.y)
sprite.batch = self.batch
self.shape = pymunk.Segment(self.body, (0, 0), (50, 0), 6)
self.shape.elasticity = 0.80
self.shape.collision_type = collision_types['brick']
space.add(self.body, self.shape)
handler = space.add_collision_handler(collision_types['brick'], collision_types['ball'])
handler.separate = self.remove_brick
So what I am trying to accomplish is have 7 rows of 7 bricks. As far as I can see the sprites are being created in the loop, but when I run the program only 2 bricks are being displayed. I am sure there is something wrong with the way I am looping but honestly just cannot see where I am going wrong. I have spent some time, trying to see the error but simply cannot see where I am going wrong.
I can see that the pyglet brick sprites are NOT being set to the correct x, y of the pymunk body, even though, using the same formula for the player paddle object lines up the sprite perfectly.
#Set the sprite to pymunk object position
self.image = pyglet.image.load('paddle.png')
self.image.anchor_x = self.image.width // 2
self.image.anchor_y = self.image.height // 2
self.sprite = pyglet.sprite.Sprite(self.image, x=self.position.x, y=self.position.y)
I am very confused with this one and I just hope I have explained everything clearly enough. Thank you for any help or assistance in any way.