[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] more circles



if you change the size of the sprite, you need to redraw it.  the bit
you should move from the constructor to the updating func (or a resize
func) is the bit that actually draws your circle onto the sprites
buffer "self.image".

On Fri, Oct 8, 2010 at 11:08 AM, 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.
> On Wed, Oct 6, 2010 at 10:02 PM, B W <stabbingfinger@xxxxxxxxx> wrote:
>>
>> 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
>>
>> On Wed, Oct 6, 2010 at 6:07 PM, kevin hayes <kevinosky@xxxxxxxxx> wrote:
>>>
>>> Hi,
>>> Well, I got it working.  The circle moves correctly according to keyboard
>>> input. The trouble I am having is with resizing the circle/sprite.
>>> Everything looks logical to me, but it won't function the way I want it to.
>>> I tried using the rect.inflate(x,y) method, but I couldn't see how to
>>> incorporate it. If someone could show me how to add the some resizing
>>> functionality, that would be great. I am going to be getting a tutor soon,
>>> so I won't be bringing my beginner questions to this board.  Thanks again to
>>> everyone who has assisted me.
>>> Here she is:
>>> """ 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, reSize):
>>>         pygame.sprite.Sprite.__init__(self)
>>>         self.image = pygame.Surface((50, 50))
>>>         self.image.fill((255, 255, 255))
>>>         pygame.draw.circle(self.image, (0, 0, 255), (25, 25), 25)
>>>         self.rect = self.image.get_rect()
>>>         self.rect.center = startPos
>>>         self.dx,self.dy = direction
>>>         self.da,self.db = reSize
>>>
>>>     def update(self):
>>>
>>>         self.rect.width += self.da   #add to rects size by (x,y) values
>>>         self.rect.height += self.db
>>>
>>>         if self.rect.height > 199:
>>>             self.rect.height = 200
>>>         if self.rect.width > 199:
>>>             self.rect.width = 200
>>>         if self.rect.height < 1:
>>>             self.rect.height = 1
>>>         if self.rect.width < 1:
>>>             self.rect.width = 1
>>>
>>>         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 main():
>>>     pygame.display.set_caption("Move the Circle with Arrows, Resize it
>>> with(0)+(9)-")
>>>
>>>     background = pygame.Surface(screen.get_size())
>>>     background.fill((255, 255, 255))
>>>     screen.blit(background, (0, 0))
>>>
>>>
>>>     circle = Circle([320, 240], [0, 0], [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.da += -2
>>>                 if event.key == pygame.K_0:
>>>                     circle.db += 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:
>>>                     circle.da = 0
>>>                 if event.key == pygame.K_0:
>>>                     circle.db = 0
>>>
>>>         allSprites.clear(screen, background)
>>>         allSprites.update()
>>>         allSprites.draw(screen)
>>>
>>>         pygame.display.flip()
>>>
>>>     pygame.mouse.set_visible(True)
>>>
>>> if __name__ == "__main__":
>>>     main()
>>>
>>> pygame.quit()
>>>
>>
>
>