[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] Sprite Surface very slow to draw
Hi!
I've got a problem. Maybe this is normal, and there's a workaround,
but I don't get why it is so slow :
- I have a background that I draw on the whole screen to erase it
(pure & simple copy, this is *very* quick (at least quick enough for
me)
- if the mouse is in a certain area, I just want to create a Sprite
"green square" around this area and keep it like this as long as the
mouse is in this area. Once the mouse gets out of this Rect, I want
this sprite to fade to black then remove it.
I based my code on the chimp.py (see google) example (which is a very
good example to me).
The problem is that the "highlight sprite" is very, very, very, very,
very, very slow to be drawn.
Am I missing something?
So here is my code :
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import os, pygame, sys, time, random, copy, pprint
5 from pygame.locals import *
6
7 class SMouse(pygame.sprite.Sprite):
8 def __init__(self,width,height):
9 pygame.sprite.Sprite.__init__(self) #call Sprite
initializer
10 self.width = width
11 self.height = height
12 self.rect = pygame.Rect(0, 0, width, height)
13 self.image = pygame.Surface( (self.rect.width,
self.rect.height) )
14 self.image.set_colorkey((0,0,0))
15 self.image.fill((0, 0, 0))
16 pygame.draw.rect(self.image, (205,250,130), self.rect, 5)
17 self.image = self.image.convert()
18
19 def update(self):
20 self.rect.topleft = pygame.mouse.get_pos()
21
22 class Contour(pygame.sprite.Sprite):
23 def __init__(self,width,height,speed):
24 pygame.sprite.Sprite.__init__(self) #call Sprite
initializer
25 self.width = width
26 self.height = height
27 self.rect = pygame.Rect(0, 0, width, height)
28 self.image = pygame.Surface( (self.rect.width,
self.rect.height) )
29 self.image.set_colorkey((0,0,0))
30 self.image.fill((0, 0, 0))
31 pygame.draw.rect(self.image, (105,250,230), self.rect, 5)
32 self.image = self.image.convert()
33 self.fadeout = False
34 if speed<1:
35 raise Exception("Speed must be > 0")
36 self.speed = speed
37 self.alpha = 255
38 self.done = False
39 self.image.convert_alpha()
40
41 def update(self):
42 if not self.fadeout:
43 if not
self.rect.collidepoint( pygame.mouse.get_pos() ):
44 self.fadeout = True
45 else:
46 self.alpha -= self.speed
47 if self.alpha<0:
48 self.alpha = 0
49 self.done = True
50 self.image.set_alpha(self.alpha)
51
52 def main():
53 pygame.init()
54 pygame.mouse.set_visible(0)
55 screen = pygame.display.set_mode( (0,0),
56 pygame.FULLSCREEN | pygame.DOUBLEBUF | pygame.HWSURFACE )
57 if pygame.display.Info().bitsize < 24:
58 print "This game needs a 24 bits display"
59 pygame.quit()
60
61 background = pygame.Surface(screen.get_size())
62 background = background.convert()
63 background.fill((0, 0, 0))
64 screen.blit(background, (0, 0))
65 pygame.display.flip()
66 clock = pygame.time.Clock()
67 c = Contour(500, 500, 15)
68 c.rect.center = pygame.mouse.get_pos()
69 group_sprites = pygame.sprite.RenderPlain( [ SMouse(50, 50),
c ] )
70
71 going = True
72 while going:
73 clock.tick(60)
74 for event in pygame.event.get():
75 if event.type == QUIT:
76 going = False
77 elif event.type == KEYDOWN:
78 if event.key == K_ESCAPE:
79 going = False
80 elif event.key == K_q:
81 going = False
82 elif event.key == K_RETURN:
83 going = False
84
85 group_sprites.update()
86 screen.blit(background, (0, 0))
87 group_sprites.draw(screen)
88 for sprite in group_sprites:
89 if isinstance(sprite, Contour):
90 if sprite.done:
91 group_sprites.remove(sprite)
92 pygame.display.flip()
93
94 if __name__ == '__main__':
95 main()
The problem is that the "highlight sprite" is very, very, very, very,
very, very slow to be drawn.
Am I missing something?
Regards, Olivier Pons
You can write to me directly to olivier dot pons at gmail
Thanks!