[Tutor] Fwd: circular movement in pygame

diliup gabadamudalige diliupg at gmail.com
Wed Apr 29 20:37:05 CEST 2015


I do not understand how Alan does not get the code that is in this thread.
Anyway I can conclude by saying that a friend of mine who is not in this
thread solved the problem and I would like to share it with you. He has
changed only a very smal portion in the original code that i posted in this
thread. and it works perfectly. Apparently the problem is in the way the
rect assignment is done in the Python/Pygame rect class. So as conclusion
the code below works perfectly. No drifting. Class implemented. I hope
everyone gets updated. Thanks for your time.


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/3
#----------------------------------------------------------------------
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((255,255,255))
    SCREEN.blit(pin,(center_x, center_y))

#Class
implementation--------------------------------------------------------------------------------------------------------------
    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)
        SCREEN.blit(ball,(b.x,b.y))
    pygame.display.update()


On Wed, Apr 29, 2015 at 10:00 PM, Alan Gauld <alan.gauld at btinternet.com>
wrote:

> On 29/04/15 10:15, diliup gabadamudalige wrote:
>
>> x=42 good
>> x += 42 -x ?
>>
>> That does not go by a mile of what I asked.
>>
>
> You've asked three different things:
>
> 1) Why the += form gives different results from the assignment form,
> 2) Why some code you apparently wriotre worked outside a class
>    but not in a class
> 3) A specific question about some code for moving an object
>    in PyGame around in a circle
>
> Number 1 is the only one I can comment on as I can't see your attachments.
> That is what I (and several others) have answered.
> They are fundamentally different. There is no general way
> to make an augmented assignment such that
>
> x = f(y) and
> x += f(y)  (or even g(y))
>
> produce the same result except by the trivial
>
> x += g(y) - x
>
>  Alan, are you telling me that there is no way that one can arrive at X2
>> which is the NEXT place that X1 will be after "some increment"?
>> old x position = X1
>>
>
> No, that is a completely separate question and has nothing
> to do with using augmented assignment. But it does depend
> on the specifics of the situation and is mainly a mathematical
> problem rather than a Python one..
>
>  new x position= X1 + some increment value
>> which can be written as :new X = oldX + some increment value
>>
>> which can be simplified to X += some increment value?
>>
>
> That may or may not be possible, it depends on what the
> original equation in the pure assignment model is.
>
>  my orginal question was
>>
>> *Why does the object NOT move in a circle when implemented as a class?*
>>
>
> Your original question was:
>
> is there a way to do
> self.rect.x +*= some value*
> self.rect.y += some value
>
> rather than
>
> self.rect.x = self.radius * math.sin(self.angle) + self.center_x
> self.rect.y = self.radius * math.cos(self.angle) + self.center_y
>
> Which is about the use of augmented assignment.
>
>  All the code was provided.
>>
>
> No it wasn't. I have not seen any of your code because you persist
> in sending it as attachments which (as you've been told) do not
> work reliably in a text based mailing list. If you want
> comments on the code either post it inline in your message
> or as a link to a web site where we can all see it.
>
>  The above is  what I asked. (I hope this is clear enough)
>>
>
> Your class based code should work but without sight of it
> I cannot begin to guess about that aspect of your problem.
>
> --
> 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
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
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