Yes, if you're not getting it then an example is probably better than more explanation. Here's your program with the changes I had in mind.
""" MoveCircleSizeCircle.py
Makes a circle that changes
it's position and size by the
keyboard, 10/3/10"""
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
class Circle(pygame.sprite.Sprite):
def __init__(self, startPos, direction):
pygame.sprite.Sprite.__init__(self)
self.rect = pygame.Rect(0,0,50,50)
self._base_image = self._make_image()
self.image = self._base_image
self.rect.center = startPos
self.dx,self.dy = direction
self.dw,self.dh = 0,0
def update(self):
"""update size and position of sprite each frame"""
if (self.dw,self.dh) != (0,0):
self.resize(self.dw, self.dh)
self.rect.centerx += self.dx #move rect (x, y)
self.rect.centery += self.dy
if self.rect.right > screen.get_width():
self.rect.left = screen.get_width() - 50
if self.rect.left < 0:
self.rect.right = 50
if self.rect.bottom > screen.get_height():
self.rect.top = screen.get_height() - 50
if self.rect.top < 0:
self.rect.bottom = 50
def resize(self, x=0, y=0):
"""resize sprite once"""
if (x,y) == (0,0):
return
r = self.rect
save_center = r.center
r.size = r.width+x, r.height+y
if r.height > 200:
r.height = 200
if r.width > 200:
r.width = 200
if r.height < 2:
r.height = 2
if r.width < 2:
r.width = 2
r.center = save_center
## CHOICE: either remake or scale the image
if False:
self.image = self._make_image()
else:
## but scaled images do not look as neat
self._inflate_image()
def _inflate_image(self):
"""use transform on base image"""
self.image = pygame.transform.smoothscale(
self._base_image, (self.rect.size))
def _make_image(self):
"""make image from scratch"""
image = pygame.Surface(self.rect.size)
image.fill((255, 255, 255))
rect = image.get_rect()
w,h = self.rect.size
radius = min(w, h) / 2
pygame.draw.circle(image, (0, 0, 255), rect.center, radius)
return image
def main():
pygame.display.set_caption("Move the Circle with Arrows, Resize it with(0)+(9)-")
background = ""> background.fill((255, 255, 255))
screen.blit(background, (0, 0))
circle = Circle([320, 240], [0, 0])
pygame.mouse.set_visible(False)
allSprites = pygame.sprite.Group(circle)
clock = pygame.time.Clock()
keepGoing = True
while keepGoing:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
circle.dy = 2
if event.key == pygame.K_UP:
circle.dy = -2
if event.key == pygame.K_RIGHT:
circle.dx = 2
if event.key == pygame.K_LEFT:
circle.dx = -2
if event.key == pygame.K_ESCAPE:
keepGoing = False
if event.key == pygame.K_9: #resize by -2
circle.dw,circle.dh = (-2, -2)
if event.key == pygame.K_0:
circle.dw,circle.dh = (2, 2) #resize by +2
elif event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN:
circle.dy = 0
if event.key == pygame.K_UP:
circle.dy = 0
if event.key == pygame.K_LEFT:
circle.dx = 0
if event.key == pygame.K_RIGHT:
circle.dx = 0
if event.key == pygame.K_9: #stop resize
circle.dw,circle.dh = (0,0)
if event.key == pygame.K_0:
circle.dw,circle.dh = (0,0) #stop resize
allSprites.clear(screen, background)
allSprites.update()
allSprites.draw(screen)
pygame.display.flip()
pygame.mouse.set_visible(True)
if __name__ == "__main__":
main()
pygame.quit()
GummOn Thu, Oct 7, 2010 at 8:08 PM, kevin hayes
<kevinosky@xxxxxxxxx> wrote:
I put some thought into your suggestions, but I'm still not piecing it together. I tried to use the inflate, transform.scale in my update function, but that didn't work. Also, I don't understand your hint (hint: move some of the constructor code to a resize method and call that from in constructor, as well as the main) By constructor you mean the first __init__( variable, variable...) ? This stuff doesn't come naturally to me, but I find it fascinating. If you could just show me (more explicitly) how to do this, that would help.
Thanks for the links, I will check them out.
You have to resize the rect *and* the image. You can use pygame functions Rect.inflate* and transform.scale, respectively, or you can do-it-yerself (hint: move some of the constructor code to a resize method and call that from in constructor, as well as the main loop).
Also, the += and -= in the event handler cases are probably not doing what you intend. If you get the resize working so you can see the circle change and it grows or moves at a strange rate, re-examine those calculations.
Gumm