[Tutor] Fwd: circular movement in pygame

diliup gabadamudalige diliupg at gmail.com
Wed Apr 29 22:19:59 CEST 2015


Thanks Alan.

It is partly due to curiosity but I also wanted to have a common update
method. And as I almost always use the x+= some code, y+=some code for
animation stuff I wanted to know how it could be done using this method
rather than finding each position separately and assigning with x= some
code, y = some code (new position).

But here is STILL something curious in this whole matter.

If I update the rect positions with

obj.rect.x+ = some code
obj.rect.y+ = some code

then the rotation are NOT circular.
BUT
if I use

obj.x += some code
obj.y += some code

and then do

obj.rect.x, obj.rect.y = obj.x, obj.y

then the rotation works correct!!
This is really weird and I have no explanation as to why this happens!!!
Can you or anyone else explain this strange behaviour? After all a position
is a position.(at least that;s how I understand as long as you stick with
the same coordinate through out.)
The code is below.

import sys, os, pygame, itertools
from math import sin,cos,pi
from pygame.locals import *
SCREENW = 800
SCREENH = 700
class object_factory(pygame.sprite.Sprite):
    def __init__(self, image, xpos = 0, ypos = 0):
        """Constructor"""
        pygame.sprite.Sprite.__init__(self)
        self.image = image
        self.mask = pygame.mask.from_surface(self.image) # pixelmask
        self.rect = self.image.get_rect()
        self.rect.x = xpos
        self.rect.y = ypos
        self.x=xpos
        self.y=ypos
pygame.init()
clock = pygame.time.Clock()
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (50,50) #Set window position
SCREEN = pygame.display.set_mode((SCREENW, SCREENH))
clock = pygame.time.Clock()
pygame.display.set_caption('rotating object')
ball = pygame.image.load("ball.jpg")
pin=pygame.image.load("center.jpg");
platforms = pygame.sprite.Group()
center_x = SCREENW/2
center_y = SCREENH/2
radius = 200
angle = pi/4 # starting angle 45 degrees
omega = 0.1 #Angular velocity
for _ in itertools.repeat(None, 6):
    xpos = center_x + radius * cos(angle) #Starting position x
    ypos = center_y - radius * sin(angle) #Startinh position y
    obj = object_factory(ball, xpos, ypos)
    obj.angle = angle
    obj.omega = omega  #angula velocity
    obj.radius = radius
    platforms.add(obj)
    angle += pi/.5
#----------------------------------------------------------------------
while True:
    clock.tick(24)
    pygame.event.pump()
    keys = pygame.key.get_pressed()
    for event in pygame.event.get():
        if event.type == QUIT or (event.type == KEYDOWN and event.key ==
K_ESCAPE):
            pygame.quit()
            sys.exit()
    SCREEN.fill((0, 0, 0))
    SCREEN.blit(pin,(center_x - pin.get_width() / 2, center_y -
pin.get_height() / 2))
    pygame.draw.circle(SCREEN, (0, 0, 255), (center_x, center_y), radius, 2)

    for b in platforms:
        b.angle+=b.omega
        #Rect Base Rotation
        #b.rect.x += b.radius * b.omega * cos(b.angle + pi / 2)
        #b.rect.y -= b.radius * b.omega * sin(b.angle + pi / 2)
        #SCREEN.blit(ball,(b.rect.x,b.rect.y))
        #Normal Rotation
        b.x+= b.radius * b.omega * cos(b.angle + pi / 2)
        b.y-= b.radius * b.omega * sin(b.angle + pi / 2)

        b.rect.x, b.rect.y = b.x - b.rect.width / 2, b.y - b.rect.height / 2

        #SCREEN.blit(ball,(b.x,b.y)) # this and the line below gives the
same result
        #SCREEN.blit(ball, (b.rect.x, b.rect.y))

    platforms.update()
    platforms.draw(SCREEN)

    pygame.time.wait(100)
    pygame.display.update()



On Thu, Apr 30, 2015 at 12:25 AM, Alan Gauld <alan.gauld at btinternet.com>
wrote:

> On 29/04/15 19:37, diliup gabadamudalige wrote:
>
>> I do not understand how Alan does not get the code
>>
>
>
> Replying off list.
>
> I don't get it because text based email systems often strip out attachments
> since they are a security risk. As the name says they are *text* based.
>
> However, now that I've seen it and I see your solution I can better
> understand
> what you were trying to ask, which was the solution to a very specific
> scenario.
>
> I'm still not sure why you wanted to use augmented assignment, other
> than curiosity perhaps, but at least I now see what you were trying to do.
> Thanks for posting the solution.
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>


-- 
Diliup Gabadamudalige

http://www.diliupg.com
http://soft.diliupg.com/

**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************


More information about the Tutor mailing list