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

[pygame] more circles




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 = "">
    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()