The start of a game in pygame, blitting images to the screen











up vote
1
down vote

favorite












The small bit of code below blits and image to the screen within a class. I call this 4 times to display the image 4 times, each image has a different y coordinate and doesn't need different x but when they are blitted they make the code MUCH slower. I know it is the blitting as well due to the fact that if i just draw rects instead, the program moves smoothly.



tableTex = pygame.transform.scale(pygame.image.load('./Sources/Table.png'), (270, 240))

def display(self):
if not self.occupied:
screen.blit(tableTex, (self.x, self.y))
else:
pygame.draw.rect(screen, (127, 0, 0), self.rect, 0)


A link to the full code can be found here if needed.
I just want to know if there is any reason this is so slow and if there are any other methods I could use that are faster?



Thanks










share|improve this question
















bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • You should add the definition of tableTex to the code, since it is actually quite relevant.
    – Graipher
    Jul 3 at 15:49








  • 1




    @Graipher My bad - Updated it now
    – Thomas Ayling
    Jul 3 at 15:51















up vote
1
down vote

favorite












The small bit of code below blits and image to the screen within a class. I call this 4 times to display the image 4 times, each image has a different y coordinate and doesn't need different x but when they are blitted they make the code MUCH slower. I know it is the blitting as well due to the fact that if i just draw rects instead, the program moves smoothly.



tableTex = pygame.transform.scale(pygame.image.load('./Sources/Table.png'), (270, 240))

def display(self):
if not self.occupied:
screen.blit(tableTex, (self.x, self.y))
else:
pygame.draw.rect(screen, (127, 0, 0), self.rect, 0)


A link to the full code can be found here if needed.
I just want to know if there is any reason this is so slow and if there are any other methods I could use that are faster?



Thanks










share|improve this question
















bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • You should add the definition of tableTex to the code, since it is actually quite relevant.
    – Graipher
    Jul 3 at 15:49








  • 1




    @Graipher My bad - Updated it now
    – Thomas Ayling
    Jul 3 at 15:51













up vote
1
down vote

favorite









up vote
1
down vote

favorite











The small bit of code below blits and image to the screen within a class. I call this 4 times to display the image 4 times, each image has a different y coordinate and doesn't need different x but when they are blitted they make the code MUCH slower. I know it is the blitting as well due to the fact that if i just draw rects instead, the program moves smoothly.



tableTex = pygame.transform.scale(pygame.image.load('./Sources/Table.png'), (270, 240))

def display(self):
if not self.occupied:
screen.blit(tableTex, (self.x, self.y))
else:
pygame.draw.rect(screen, (127, 0, 0), self.rect, 0)


A link to the full code can be found here if needed.
I just want to know if there is any reason this is so slow and if there are any other methods I could use that are faster?



Thanks










share|improve this question















The small bit of code below blits and image to the screen within a class. I call this 4 times to display the image 4 times, each image has a different y coordinate and doesn't need different x but when they are blitted they make the code MUCH slower. I know it is the blitting as well due to the fact that if i just draw rects instead, the program moves smoothly.



tableTex = pygame.transform.scale(pygame.image.load('./Sources/Table.png'), (270, 240))

def display(self):
if not self.occupied:
screen.blit(tableTex, (self.x, self.y))
else:
pygame.draw.rect(screen, (127, 0, 0), self.rect, 0)


A link to the full code can be found here if needed.
I just want to know if there is any reason this is so slow and if there are any other methods I could use that are faster?



Thanks







python object-oriented pygame






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 3 at 15:52

























asked Jul 3 at 12:08









Thomas Ayling

194




194





bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • You should add the definition of tableTex to the code, since it is actually quite relevant.
    – Graipher
    Jul 3 at 15:49








  • 1




    @Graipher My bad - Updated it now
    – Thomas Ayling
    Jul 3 at 15:51


















  • You should add the definition of tableTex to the code, since it is actually quite relevant.
    – Graipher
    Jul 3 at 15:49








  • 1




    @Graipher My bad - Updated it now
    – Thomas Ayling
    Jul 3 at 15:51
















You should add the definition of tableTex to the code, since it is actually quite relevant.
– Graipher
Jul 3 at 15:49






You should add the definition of tableTex to the code, since it is actually quite relevant.
– Graipher
Jul 3 at 15:49






1




1




@Graipher My bad - Updated it now
– Thomas Ayling
Jul 3 at 15:51




@Graipher My bad - Updated it now
– Thomas Ayling
Jul 3 at 15:51










1 Answer
1






active

oldest

votes

















up vote
0
down vote













As noted in the beginners guide, you should convert your image right after loading it. So instead of



tableTex = pygame.transform.scale(pygame.image.load('./Sources/Table.png'), (270, 240))


Do



tableTex = pygame.transform.scale(pygame.image.load('./Sources/Table.png').convert(), (270, 240))


As noted in the guide linked above, this can lead to a speed-up of about a factor 6. The reason is:




The 'format' that convert() refers to isn't the file format (ie PNG,
JPEG, GIF), it's what's called the 'pixel format'. This refers to the
particular way that a surface records individual colors in a specific
pixel. If the surface format isn't the same as the display format, SDL [what pygame uses underneath]
will have to convert it on-the-fly for every blit -- a fairly
time-consuming process. Don't worry too much about the explanation;
just note that convert() is necessary if you want to get any kind of
speed out of your blits.







share|improve this answer























  • Thanks very much for the reply, however using convert adds some very strange deformities to my loaded images imgur.com/a/htautZi
    – Thomas Ayling
    Jul 3 at 15:55










  • @ThomasAyling: I don't see them. But I just changed my answer to call convert on the loaded image, not the scaled one. Does that change anything?
    – Graipher
    Jul 3 at 15:57










  • No, i still get the same problem imgur.com/a/80IPaj7 (New image)
    – Thomas Ayling
    Jul 3 at 15:59












  • @ThomasAyling: That does indeed look weird (comparing it to the original in the github). Maybe the picture contains transparency which is wrongly interpreted?
    – Graipher
    Jul 3 at 16:01










  • Possibly, it works properly without the convert in still so maybe there were some issues with compression, i'll try re-saving it from Photoshop
    – Thomas Ayling
    Jul 3 at 16:03











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197718%2fthe-start-of-a-game-in-pygame-blitting-images-to-the-screen%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













As noted in the beginners guide, you should convert your image right after loading it. So instead of



tableTex = pygame.transform.scale(pygame.image.load('./Sources/Table.png'), (270, 240))


Do



tableTex = pygame.transform.scale(pygame.image.load('./Sources/Table.png').convert(), (270, 240))


As noted in the guide linked above, this can lead to a speed-up of about a factor 6. The reason is:




The 'format' that convert() refers to isn't the file format (ie PNG,
JPEG, GIF), it's what's called the 'pixel format'. This refers to the
particular way that a surface records individual colors in a specific
pixel. If the surface format isn't the same as the display format, SDL [what pygame uses underneath]
will have to convert it on-the-fly for every blit -- a fairly
time-consuming process. Don't worry too much about the explanation;
just note that convert() is necessary if you want to get any kind of
speed out of your blits.







share|improve this answer























  • Thanks very much for the reply, however using convert adds some very strange deformities to my loaded images imgur.com/a/htautZi
    – Thomas Ayling
    Jul 3 at 15:55










  • @ThomasAyling: I don't see them. But I just changed my answer to call convert on the loaded image, not the scaled one. Does that change anything?
    – Graipher
    Jul 3 at 15:57










  • No, i still get the same problem imgur.com/a/80IPaj7 (New image)
    – Thomas Ayling
    Jul 3 at 15:59












  • @ThomasAyling: That does indeed look weird (comparing it to the original in the github). Maybe the picture contains transparency which is wrongly interpreted?
    – Graipher
    Jul 3 at 16:01










  • Possibly, it works properly without the convert in still so maybe there were some issues with compression, i'll try re-saving it from Photoshop
    – Thomas Ayling
    Jul 3 at 16:03















up vote
0
down vote













As noted in the beginners guide, you should convert your image right after loading it. So instead of



tableTex = pygame.transform.scale(pygame.image.load('./Sources/Table.png'), (270, 240))


Do



tableTex = pygame.transform.scale(pygame.image.load('./Sources/Table.png').convert(), (270, 240))


As noted in the guide linked above, this can lead to a speed-up of about a factor 6. The reason is:




The 'format' that convert() refers to isn't the file format (ie PNG,
JPEG, GIF), it's what's called the 'pixel format'. This refers to the
particular way that a surface records individual colors in a specific
pixel. If the surface format isn't the same as the display format, SDL [what pygame uses underneath]
will have to convert it on-the-fly for every blit -- a fairly
time-consuming process. Don't worry too much about the explanation;
just note that convert() is necessary if you want to get any kind of
speed out of your blits.







share|improve this answer























  • Thanks very much for the reply, however using convert adds some very strange deformities to my loaded images imgur.com/a/htautZi
    – Thomas Ayling
    Jul 3 at 15:55










  • @ThomasAyling: I don't see them. But I just changed my answer to call convert on the loaded image, not the scaled one. Does that change anything?
    – Graipher
    Jul 3 at 15:57










  • No, i still get the same problem imgur.com/a/80IPaj7 (New image)
    – Thomas Ayling
    Jul 3 at 15:59












  • @ThomasAyling: That does indeed look weird (comparing it to the original in the github). Maybe the picture contains transparency which is wrongly interpreted?
    – Graipher
    Jul 3 at 16:01










  • Possibly, it works properly without the convert in still so maybe there were some issues with compression, i'll try re-saving it from Photoshop
    – Thomas Ayling
    Jul 3 at 16:03













up vote
0
down vote










up vote
0
down vote









As noted in the beginners guide, you should convert your image right after loading it. So instead of



tableTex = pygame.transform.scale(pygame.image.load('./Sources/Table.png'), (270, 240))


Do



tableTex = pygame.transform.scale(pygame.image.load('./Sources/Table.png').convert(), (270, 240))


As noted in the guide linked above, this can lead to a speed-up of about a factor 6. The reason is:




The 'format' that convert() refers to isn't the file format (ie PNG,
JPEG, GIF), it's what's called the 'pixel format'. This refers to the
particular way that a surface records individual colors in a specific
pixel. If the surface format isn't the same as the display format, SDL [what pygame uses underneath]
will have to convert it on-the-fly for every blit -- a fairly
time-consuming process. Don't worry too much about the explanation;
just note that convert() is necessary if you want to get any kind of
speed out of your blits.







share|improve this answer














As noted in the beginners guide, you should convert your image right after loading it. So instead of



tableTex = pygame.transform.scale(pygame.image.load('./Sources/Table.png'), (270, 240))


Do



tableTex = pygame.transform.scale(pygame.image.load('./Sources/Table.png').convert(), (270, 240))


As noted in the guide linked above, this can lead to a speed-up of about a factor 6. The reason is:




The 'format' that convert() refers to isn't the file format (ie PNG,
JPEG, GIF), it's what's called the 'pixel format'. This refers to the
particular way that a surface records individual colors in a specific
pixel. If the surface format isn't the same as the display format, SDL [what pygame uses underneath]
will have to convert it on-the-fly for every blit -- a fairly
time-consuming process. Don't worry too much about the explanation;
just note that convert() is necessary if you want to get any kind of
speed out of your blits.








share|improve this answer














share|improve this answer



share|improve this answer








edited Jul 3 at 15:56

























answered Jul 3 at 15:49









Graipher

22.6k53384




22.6k53384












  • Thanks very much for the reply, however using convert adds some very strange deformities to my loaded images imgur.com/a/htautZi
    – Thomas Ayling
    Jul 3 at 15:55










  • @ThomasAyling: I don't see them. But I just changed my answer to call convert on the loaded image, not the scaled one. Does that change anything?
    – Graipher
    Jul 3 at 15:57










  • No, i still get the same problem imgur.com/a/80IPaj7 (New image)
    – Thomas Ayling
    Jul 3 at 15:59












  • @ThomasAyling: That does indeed look weird (comparing it to the original in the github). Maybe the picture contains transparency which is wrongly interpreted?
    – Graipher
    Jul 3 at 16:01










  • Possibly, it works properly without the convert in still so maybe there were some issues with compression, i'll try re-saving it from Photoshop
    – Thomas Ayling
    Jul 3 at 16:03


















  • Thanks very much for the reply, however using convert adds some very strange deformities to my loaded images imgur.com/a/htautZi
    – Thomas Ayling
    Jul 3 at 15:55










  • @ThomasAyling: I don't see them. But I just changed my answer to call convert on the loaded image, not the scaled one. Does that change anything?
    – Graipher
    Jul 3 at 15:57










  • No, i still get the same problem imgur.com/a/80IPaj7 (New image)
    – Thomas Ayling
    Jul 3 at 15:59












  • @ThomasAyling: That does indeed look weird (comparing it to the original in the github). Maybe the picture contains transparency which is wrongly interpreted?
    – Graipher
    Jul 3 at 16:01










  • Possibly, it works properly without the convert in still so maybe there were some issues with compression, i'll try re-saving it from Photoshop
    – Thomas Ayling
    Jul 3 at 16:03
















Thanks very much for the reply, however using convert adds some very strange deformities to my loaded images imgur.com/a/htautZi
– Thomas Ayling
Jul 3 at 15:55




Thanks very much for the reply, however using convert adds some very strange deformities to my loaded images imgur.com/a/htautZi
– Thomas Ayling
Jul 3 at 15:55












@ThomasAyling: I don't see them. But I just changed my answer to call convert on the loaded image, not the scaled one. Does that change anything?
– Graipher
Jul 3 at 15:57




@ThomasAyling: I don't see them. But I just changed my answer to call convert on the loaded image, not the scaled one. Does that change anything?
– Graipher
Jul 3 at 15:57












No, i still get the same problem imgur.com/a/80IPaj7 (New image)
– Thomas Ayling
Jul 3 at 15:59






No, i still get the same problem imgur.com/a/80IPaj7 (New image)
– Thomas Ayling
Jul 3 at 15:59














@ThomasAyling: That does indeed look weird (comparing it to the original in the github). Maybe the picture contains transparency which is wrongly interpreted?
– Graipher
Jul 3 at 16:01




@ThomasAyling: That does indeed look weird (comparing it to the original in the github). Maybe the picture contains transparency which is wrongly interpreted?
– Graipher
Jul 3 at 16:01












Possibly, it works properly without the convert in still so maybe there were some issues with compression, i'll try re-saving it from Photoshop
– Thomas Ayling
Jul 3 at 16:03




Possibly, it works properly without the convert in still so maybe there were some issues with compression, i'll try re-saving it from Photoshop
– Thomas Ayling
Jul 3 at 16:03


















draft saved

draft discarded




















































Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197718%2fthe-start-of-a-game-in-pygame-blitting-images-to-the-screen%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Morgemoulin

Scott Moir

Souastre