Pyglet Image Rendering

0 votes

I'm working on a sort of a 2D Minecraft clone for my first in depth Pyglet project and I've run across a problem. Whenever I have a decent number of blocks on screen, the frame rate drops dramatically.

I use a dictionary with the key being a tuple(which represents the coordinate for the block) and the item being a texture.

I loop through the entire dictionary and render each block:

for key in self.blocks:
    self.blocks[key].blit(key[0] * 40 + sx,key[1] * 40+ sy)

Is there a way to more efficiently render each block?

Jul 16 in Python by ana1504.k
• 7,890 points
82 views

1 answer to this question.

+1 vote

I will assume you have something along the lines of:

self.blocks['monster001'] = pyglet.image.load('./roar.png')

This is all fine and dandy, if you want to load a static image that you don't want to do much with. However, you're making a game and you are going to use a hell of a lot more sprites and objects than just one simple image file.

Now this is where shared objects, batches and sprites come in handy. First off, input your image into a sprite, it's a good start.

sprite = pyglet.sprite.Sprite(pyglet.image.load('./roar.png'))
sprite.draw() # This is instead of blit. Position is done via sprite.x = ...

Now, draw is a hell of a lot quicker than .blit() for numerous of reasons, but we'll skip why for now and just stick with blazing speed upgrades.

answered Jul 16 by SDeb
• 13,210 points

Related Questions In Python

+1 vote
2 answers

How to resize the image canvas to maintain a square aspect ratio

down voteacceptedUsing OpenCVYou can use resize() in OpenCV to ...READ MORE

answered Apr 4, 2018 in Python by charlie_brown
• 7,720 points
217 views
0 votes
1 answer

How to resize image canvas to maintain square aspect ratio in Python, OpenCv

Building on Alexander-Reynolds answer above, here is ...READ MORE

answered Sep 4, 2018 in Python by Priyaj
• 56,960 points
1,436 views
0 votes
1 answer

How to save Numpy array as image in python?

If you have matplotlib, you can do: import ...READ MORE

answered Nov 16, 2018 in Python by Nymeria
• 3,520 points
2,690 views
0 votes
1 answer
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 399 views
0 votes
1 answer

Convert HTML to an image in Python

webkit2png. The original version is OSX-only, but luckily ...READ MORE

answered Feb 4 in Python by SDeb
• 13,210 points
3,219 views
0 votes
1 answer

Python pygame error : Failed loading libpng.dylib: dlopen(libpng.dylib, 2): image not found

Try installing libpng You can do it with ...READ MORE

answered May 31 in Python by SDeb
• 13,210 points
87 views