Re: [Tutor] Fwd: circular movement in pygame
for what ever it's worth I'd like to share the code below with you. I got the code to do what I wanted. Object falls onto a rotating platform and stays there without falling off. video is here - https://www.youtube.com/watch?v=I6c5cKhLuo4 import sys, os, pygame from pygame.locals import * from standard_object_creator import * from math import sin, cos, pi, radians SCREENW = 800 SCREENH = 700 pygame.init() FPSCLOCK = pygame.time.Clock() FONT1= data\Cookie-Regular.ttf if sys.platform == 'win32' or sys.platform == 'win64': #os.environ['SDL_VIDEO_CENTERED'] = '2'# center of screen os.environ['SDL_VIDEO_WINDOW_POS'] = %d,%d % (10,30)#top left corner SCREEN = pygame.display.set_mode((SCREENW, SCREENH)) pygame.display.set_caption('rotating object') BLUE = (0, 50, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) PURPLE = (145, 0, 100) yellow = (220,220, 0) FPS = 160 # frames per second platforms = pygame.sprite.Group() boxes = pygame.sprite.Group() ## this is the center of rotation. objects will be placed on the radius according to the step cx = SCREENW / 2 # x pos in relation to screen width cy = SCREENH / 2 # y pos in relation to screen height ## self, posx, posy, imagelist, speedx = 0, speedy = 0, value = 0 plat = pygame.image.load(platform.png).convert_alpha() box = pygame.image.load(box.png).convert_alpha() box = object_factory ([box], cx, cy - 300, 0, 2) boxes.add(box) RADIUS = 100 # distance from the center angle = radians(0) # angular distance between objects omega = 1 for x in xrange(1): xpos = (cos(angle * x) * RADIUS) + cx ypos = (sin(angle * x) * RADIUS) + cy obj = object_factory([plat], xpos, ypos) obj.cx = cx obj.cy = cy obj.difx = 0 obj.dify = 0 obj.angle = radians(60 * x) # position each object around (cx,cy) obj.radius = RADIUS # distance from the center obj.omega = radians(omega) obj.offsetx = cos(obj.angle) * obj.radius obj.offsety = -sin(obj.angle) * obj.radius platforms.add(obj) mouseposlist = [] okkoma = [platforms, boxes] x = 0 l = [] while True: SCREEN.fill(BLACK) x += 1 ##-- pygame.event.pump() keys = pygame.key.get_pressed() for event in pygame.event.get(): if event.type == MOUSEBUTTONDOWN: if event.button == 1: pos = pygame.mouse.get_pos() val = [pos[0], pos[1], 0, 0] print val mouseposlist.append(val) elif event.button == 3 and mouseposlist != []: mouseposlist.pop(-1) if event.type == KEYDOWN and event.key == K_ESCAPE: print mouseposlist pygame.quit() sys.exit() for hp in boxes: hp.move() hp.collide(platforms) ## MOVE THE SPRITE IN A CIRCLE center of rotation = cx,cy. Each object is placed by varying the step) pygame.draw.circle(SCREEN, BLUE, (cx, cy), RADIUS, 2) for obj in platforms: obj.angle += obj.omega # larger value increases speed ( angle gets larger) #obj.radius += .1 # this will make the object move in an expanding spiral obj.offsetx = cos(obj.angle) * obj.radius obj.offsety = -sin(obj.angle) * obj.radius obj.difx = obj.offsetx - obj.rect.x obj.dify = obj.offsety - obj.rect.y obj.speedx = round(obj.cx + obj.difx, 2) obj.speedy = round(obj.cy + obj.dify, 2) l.append([obj.speedx, obj.speedy]) obj.move() for ekak in okkoma: ekak.update() ekak.draw(SCREEN) ## pygame.draw.line(SCREEN, BLUE, (0, SCREENH / 2), (SCREENW, SCREENH / 2), 2) pygame.draw.line(SCREEN, BLUE, (SCREENW / 2, 0), (SCREENW / 2, SCREENH), 2) pygame.display.update() FPSCLOCK.tick(FPS) pygame.time.wait(10) print l On Thu, Apr 30, 2015 at 12:27 PM, diliup gabadamudalige dili...@gmail.com wrote: Thanks all for the very informative responses especially to Alan for being descriptive. I am now going to make my movement linear and move away from my current circular one. I hope a little bit of fun and Thank you emails fall into the order of the day.. :) On Thu, Apr 30, 2015 at 9:13 AM, Dave Angel da...@davea.name wrote: On 04/29/2015 02:37 PM, diliup gabadamudalige wrote: I do not understand how Alan does not get the code that is in this thread. There are at least 3 ways of posting to this thread: A) email B) newsgroup C) googlegroups and at least 5 ways of looking at this thread A) email B) email digest C) newsgroup D) googlegroups E) various archives To get messages from one region to another involves going through a gateway, and most of them damage some of the messages going through. To minimize the likelihood that what looks good on your screen will be missing or different on mine or on Alan's, follow a few rules:
Re: [Tutor] Fwd: circular movement in pygame
Thanks all for the very informative responses especially to Alan for being descriptive. I am now going to make my movement linear and move away from my current circular one. I hope a little bit of fun and Thank you emails fall into the order of the day.. :) On Thu, Apr 30, 2015 at 9:13 AM, Dave Angel da...@davea.name wrote: On 04/29/2015 02:37 PM, diliup gabadamudalige wrote: I do not understand how Alan does not get the code that is in this thread. There are at least 3 ways of posting to this thread: A) email B) newsgroup C) googlegroups and at least 5 ways of looking at this thread A) email B) email digest C) newsgroup D) googlegroups E) various archives To get messages from one region to another involves going through a gateway, and most of them damage some of the messages going through. To minimize the likelihood that what looks good on your screen will be missing or different on mine or on Alan's, follow a few rules: 1) avoid html 2) avoid attachments There are others, but those seem to be the biggies. Many other guidelines will help readability and consistency, like not top-posting, using proper quoting and attribution, giving enough information but not too much, specifying the whole environment in the FIRST message of a thread, etc. -- DaveA ___ Tutor maillist - Tutor@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. ** ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: circular movement in pygame
Thanks all for the responses. Charles Cossé - yes I can write a simple pygame program that makes a sprite move in a circle but it may not be the style and order that many may approve or accept. I consider myself a student. :) No one has pointed out why the object moves in a circle properly in the bit of code where it is addressed directly and why it does not when the same code is applied thru a class. I know that an object can be made to move around the circumference of a circle by constantly calculating the point and giving the x and y values 1. x = some code That is what I used and it worked. I was trying to find how we can do the same thing by updating the current position by doing 2. x += some code Above 1 works. 2 does not. Why is that? Both the same code. The same variable is used with different names cause some others were confused when they were the same. I hope this time I have answered correctly. No more attachments. (e mail or otherwise. :) ) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: circular movement in pygame
On 29/04/15 08:15, diliup gabadamudalige wrote: 1. x = some code That is what I used and it worked. I was trying to find how we can do the same thing by updating the current position by doing 2. x += some code Above 1 works. 2 does not. Why is that? Because they do completely different things! 1 is simply x = some code 2 is x = x + some code In 2 you are adding x to some code. Of course they don't work the same. -- 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@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: circular movement in pygame
On 29/04/15 09:03, diliup gabadamudalige wrote: Thank you Alan. Understood. I already knew that. My question is How to to do it the second way. some code = what? The point is you cannot do it. The second approach always uses the current value of x. The first approach may or may not use the current value of x. If it does not use x then the += style cannot be made to work (unless you tag a -x on at the end! x = 42 x += 42-x Which is just stupid. -- 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@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: circular movement in pygame
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@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: circular movement in pygame
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.ga...@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
Re: [Tutor] Fwd: circular movement in pygame
On Wed, Apr 29, 2015 at 11:37 AM, diliup gabadamudalige dili...@gmail.com wrote: I do not understand how Alan does not get the code that is in this thread. Hi Dilliup, Please try to avoid using the phrase, I don't understand how X does not get Y You might not realize it, but it's radioactive: it's has a very negative connotation that is toxic to collaborative communities. For more information on this, there's a very good book called Team Geek that talks about these issues: https://books.google.com/books/about/Team_Geek.html?id=Iwk_pKeBc9gChl=en Also see Hacker School's Social Rules: https://www.recurse.com/manual#sub-sec-social-rules Thanks! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: circular movement in pygame
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.ga...@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
Re: [Tutor] Fwd: circular movement in pygame
On 29/04/15 21:19, diliup gabadamudalige wrote: 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). OK, Curiosity is good. But the conversion is mainly a math challenge rather than a programming one. And its a very seductive but dangerous mind set to try to force a problem into a pattern that fits how you usually do it. Especially if the transformed code looks less intuitive than the original. When I started in Python it didn't even have the += style of augmented assignment, it was mainly introduced to save some typing, nothing more. Remember that += is still an assignment to x. It is not significantly different or in any way 'better' than the standard assignment. In languages like C '+=' (and even more so '++' which doesn't exist in Python) used to result in performance improvements but optimising compilers have removed even that reason for using it. Now it should just be considered a typing shortcut. -- 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@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: circular movement in pygame
question to Lucas. Not sure if this is what you're looking for, but I would also use cos for the x coordinate as suggested. If: self.rect.x = self.radius * math.cos(self.angle) + self.center_x self.rect.y = self.radius * math.sin(self.angle) + self.center_y Take a derivative (assume: angle += angular_vel * dt for every increment in time dt), so that: self.rect.x += -self.radius * math.sin(self.angle) * self.angular_vel * dt self.rect.y += self.radius * math.cos(self.angle) * self.angular_vel * dt what is dt? On Wed, Apr 29, 2015 at 12:45 PM, diliup gabadamudalige dili...@gmail.com wrote: Thanks all for the responses. Charles Cossé - yes I can write a simple pygame program that makes a sprite move in a circle but it may not be the style and order that many may approve or accept. I consider myself a student. :) No one has pointed out why the object moves in a circle properly in the bit of code where it is addressed directly and why it does not when the same code is applied thru a class. I know that an object can be made to move around the circumference of a circle by constantly calculating the point and giving the x and y values 1. x = some code That is what I used and it worked. I was trying to find how we can do the same thing by updating the current position by doing 2. x += some code Above 1 works. 2 does not. Why is that? Both the same code. The same variable is used with different names cause some others were confused when they were the same. I hope this time I have answered correctly. No more attachments. (e mail or otherwise. :) ) -- 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. ** ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: circular movement in pygame
Thank you Alan. Understood. I already knew that. My question is How to to do it the second way. some code = what? My question is, how does one arrive at the code to put in place of some code? The code I used does not work correctly. The code is in this chain of email. I hope I am clear in what I am trying to do. On Wed, Apr 29, 2015 at 1:18 PM, Alan Gauld alan.ga...@btinternet.com wrote: On 29/04/15 08:15, diliup gabadamudalige wrote: 1. x = some code That is what I used and it worked. I was trying to find how we can do the same thing by updating the current position by doing 2. x += some code Above 1 works. 2 does not. Why is that? Because they do completely different things! 1 is simply x = some code 2 is x = x + some code In 2 you are adding x to some code. Of course they don't work the same. -- 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@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. ** ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: circular movement in pygame
x=42 good x += 42 -x ? That does not go by a mile of what I asked. 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 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? my orginal question was *Why does the object NOT move in a circle when implemented as a class?* All the code was provided. The above is what I asked. (I hope this is clear enough) On Wed, Apr 29, 2015 at 1:56 PM, Alan Gauld alan.ga...@btinternet.com wrote: On 29/04/15 09:03, diliup gabadamudalige wrote: Thank you Alan. Understood. I already knew that. My question is How to to do it the second way. some code = what? The point is you cannot do it. The second approach always uses the current value of x. The first approach may or may not use the current value of x. If it does not use x then the += style cannot be made to work (unless you tag a -x on at the end! x = 42 x += 42-x Which is just stupid. -- 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. ** ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: circular movement in pygame
On 04/29/2015 02:37 PM, diliup gabadamudalige wrote: I do not understand how Alan does not get the code that is in this thread. There are at least 3 ways of posting to this thread: A) email B) newsgroup C) googlegroups and at least 5 ways of looking at this thread A) email B) email digest C) newsgroup D) googlegroups E) various archives To get messages from one region to another involves going through a gateway, and most of them damage some of the messages going through. To minimize the likelihood that what looks good on your screen will be missing or different on mine or on Alan's, follow a few rules: 1) avoid html 2) avoid attachments There are others, but those seem to be the biggies. Many other guidelines will help readability and consistency, like not top-posting, using proper quoting and attribution, giving enough information but not too much, specifying the whole environment in the FIRST message of a thread, etc. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: circular movement in pygame
On 04/28/2015 02:37 PM, diliup gabadamudalige wrote: I thank all those who responded to my question Here is the code that I had written. When updating is applied to a surface object the rotation works but when it is applied through a class to an object it goes wrong in about 3 rotations. As far as I can see the code is the same. What is wrong? If you can correct some code and show me would help. By top-posting, you're messing up the readability of your response. And by trying to use an attachment, you're messing up a large portion of the people reading this thread. Post a simplified example, inline in your message, and *following* any quote you're using. If your symptom is that the data diverges eventually from the intended trajectory, the problem is that you're accumulating errors. Each point you do you're rounding the calculation by storing it in finite precision. After enough roundoffs, the error becomes visible. If you need to reliably move an object in a circle, you'll want to store the location in angular terms, center, radius, and angle. Then each time around the loop, increment the angle by a non-rounded amount, and recalculate the x/y coordinates. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: circular movement in pygame
I thank all those who responded to my question Here is the code that I had written. When updating is applied to a surface object the rotation works but when it is applied through a class to an object it goes wrong in about 3 rotations. As far as I can see the code is the same. What is wrong? If you can correct some code and show me would help. On Tue, Apr 28, 2015 at 11:58 PM, Oscar Benjamin oscar.j.benja...@gmail.com wrote: On 28 April 2015 at 16:38, diliup gabadamudalige dili...@gmail.com wrote: Looking at the code on this page lines 47 48 http://programarcadegames.com/python_examples/show_file.php?file=sprite_circle_movement.py 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 There's no way to do that. The second version ignores the previous value of self.rect.x/y. I'm going to guess that the angle has only changed by a small amount delta_angle between iterations in which case there would be a way to do it approximately. The real question is just why though? The second version is correct and will be correct for ever. The first version would only be approximately correct and over time you'd probably find that the position would drift so that the ball was effectively at a different radius. In any case if x = r * sin(theta) then dx/dt = r * cos(theta) * dtheta/dt. Since y = r * cos(theta) that's dy/dt = x * dtheta/dt. So you could update y approximately with: y += x * dtheta/dt * deltat where deltat is your timestep. In your code that would be: self.rect.x += self.rect.y * self.speed self.rect.y -= self.rect.x * self.speed Really what you have now is better though. ___ Tutor maillist - Tutor@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. ** import sys, os, pygame, itertools from math import sin,cos,pi, radians from pygame.locals import * os.environ['SDL_VIDEO_WINDOW_POS'] = %d,%d % (50,50) #Set window position pygame.init() clock = pygame.time.Clock() FPS = 1000 SCREENW = 800 #screen width SCREENH = 740 #screen height BLACK = (0, 0, 0) BLUE = (0, 0, 255) ORANGE = (128, 100, 30) FONT1= Cookie-Regular.ttf SCREEN = pygame.display.set_mode((SCREENW, SCREENH), 0, 32) #display screen clock = pygame.time.Clock() #--- def maketext(msg,fontsize, colour = ORANGE, font = FONT1): mafont = pygame.font.Font(font, fontsize) matext = mafont.render(msg, True, colour) matext = matext.convert_alpha() return matext #--- def print_info(): textcos = maketext(str(round(obj.rect.x, 2)) + + str(round(obj.rect.y, 2)), 30) SCREEN.blit(textcos, (obj.rect.x, obj.rect.y + 30)) #--- class object_factory(pygame.sprite.Sprite): def __init__(self, imagelist, xpos, ypos, speedx = 0, speedy = 0, value = 0): Constructor pygame.sprite.Sprite.__init__(self) self.name = self.frame = 0 self.imagelist = imagelist self.image = imagelist[self.frame] self.mask = pygame.mask.from_surface(self.image) # pixelmask self.rect = self.image.get_rect() self.rect.x = xpos self.rect.y = ypos #self.speedx = speedx #self.speedy = speedy self.timer = 0 self.timerlimit = 10 #-- #def move(self): # wallsprites, Herosprite, looptime #self.rect.x += self.speedx #self.rect.y += self.speedy #-- def update(self): self.image = self.imagelist[self.frame] if self.timer = self.timerlimit: self.frame += 1 if self.frame =
Re: [Tutor] Fwd: circular movement in pygame
On 28/04/15 16:38, diliup gabadamudalige wrote: -- Forwarded message -- From: diliup gabadamudalige dili...@gmail.com Date: Tue, Apr 28, 2015 at 6:22 PM Subject: circular movement in pygame To: pygame-us...@seul.org It's good that you tried the python-game list for a pygame question. But if pygame-users can't give a definitive answer then its even less likely that the tutor list will. Although you might need to allow a full 24 hours to get answers, email is not an instant delivery mechanism. Looking at the code on this page lines 47 48 http://programarcadegames.com/python_examples/show_file.php?file=sprite_circle_movement.py I didn't look at the code in that tutorial, but... is there a way to do self.rect.x +*= some value* self.rect.y += some value Yes, that's just normal Python syntax. It adds/multiplies the current value of self.rect with 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 But its unlikely to have the same effect as this since the above two lines are calculating a new value based on radius, angle, and centre rather than one based on the current values of self.rect I tired it in the code attached and strangely it works ok but only OUTSIDE THE class. When I implement the exact code in a class it does something weird. I can't find where I have gone wrong. I can't see your code but are you sure you are using self in all the appropriate places in your class based code? -- 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@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: circular movement in pygame
On 28 April 2015 at 16:38, diliup gabadamudalige dili...@gmail.com wrote: Looking at the code on this page lines 47 48 http://programarcadegames.com/python_examples/show_file.php?file=sprite_circle_movement.py 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 There's no way to do that. The second version ignores the previous value of self.rect.x/y. I'm going to guess that the angle has only changed by a small amount delta_angle between iterations in which case there would be a way to do it approximately. The real question is just why though? The second version is correct and will be correct for ever. The first version would only be approximately correct and over time you'd probably find that the position would drift so that the ball was effectively at a different radius. In any case if x = r * sin(theta) then dx/dt = r * cos(theta) * dtheta/dt. Since y = r * cos(theta) that's dy/dt = x * dtheta/dt. So you could update y approximately with: y += x * dtheta/dt * deltat where deltat is your timestep. In your code that would be: self.rect.x += self.rect.y * self.speed self.rect.y -= self.rect.x * self.speed Really what you have now is better though. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: circular movement in pygame
On 28 April 2015 at 19:37, diliup gabadamudalige dili...@gmail.com wrote: I thank all those who responded to my question Here is the code that I had written. When updating is applied to a surface object the rotation works but when it is applied through a class to an object it goes wrong in about 3 rotations. As far as I can see the code is the same. What is wrong? If you can correct some code and show me would help. Your code is too long and complicated for me to read through it all. You should simplify your problem before posting it somewhere like this. Make a simpler program that does something similar but without all the pygame stuff. For example your program might just loop through printing out the positions of the object at different times e.g.: # game.py # # This program simulates a ball moving at constant speed # in a two dimensional space. # # Initial position of ball xpos = 0 ypos = 0 # Horizontal and vertical speed xspeed = 1 yspeed = 2 # Timestep deltat = 0.125 # Run through 10 iterations of simulation for n in range(10): # Update state xpos += xspeed * deltat ypos += yspeed * deltat # Output current state print('Position = (%.3f, %.3f)' % (xpos, ypos)) $ python game.py # Show the output Position = (0.125, 0.250) Position = (0.250, 0.500) Position = (0.375, 0.750) Position = (0.500, 1.000) Position = (0.625, 1.250) Position = (0.750, 1.500) Position = (0.875, 1.750) Position = (1.000, 2.000) Position = (1.125, 2.250) Position = (1.250, 2.500) Once you have made a simpler program carefully explain what it is that you want it to do and show us how what it does is different. Don't expect people on this list to download your code and run it. Also please put your code in the email and not in an attachment and please reply back to the tutor list rather than directly to me. -- Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor