Moving a drawn shape in pygame

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2009
Posts: 43
Reputation: thehivetyrant is an unknown quantity at this point 
Solved Threads: 0
thehivetyrant thehivetyrant is offline Offline
Light Poster

Moving a drawn shape in pygame

 
0
  #1
Oct 28th, 2009
Greetings Pythoneers.

I've got this problem and the problem is thus:

i have drawn a shape using pygame.draw.circle

i am finding it hard to make it move.
i tried self.move_to(self.x-5,self.y) and also self.forward(random.randint(0,10)) (Where forward is defined as a function as self.x,self.y + 10 )<== I dont think i defined the function right in this test though.
Since (random.randint(0,10)) should move it a random number between 0,10 shouldnt it?

anyway any help on how to get it to move would be appreciated.
I hope i gave enough info. And you can keep it simple if you'd like i'm sure i'll probably be able to adapt it to my needs ^^

Thanks in advance


PS: I already have all the blitting and display flips and such in place.
Last edited by thehivetyrant; Oct 28th, 2009 at 12:41 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 243
Reputation: masterofpuppets is an unknown quantity at this point 
Solved Threads: 60
masterofpuppets's Avatar
masterofpuppets masterofpuppets is offline Offline
Posting Whiz in Training
 
0
  #2
Oct 28th, 2009
hi
I haven't really used pygame but here's an example on how to do this using Tkinter. I guess it would be something similar:

  1. from Tkinter import *
  2. import time
  3.  
  4. root = Tk(); root.geometry( "200x200+400+200" )
  5. c = Canvas( root, width = 200, height = 200 ); c.pack()
  6.  
  7. ball = c.create_oval( 0, 0, 0, 0 )
  8. xInc = 3
  9. ballX = 30
  10. while True:
  11. time.sleep( 0.05 )
  12. c.delete( ball )
  13. ball = c.create_oval( ballX, 100, ballX + 5, 105, fill = "blue" )
  14. if ballX > 200 or ballX < 0:
  15. xInc = -xInc
  16. ballX += xInc
  17. c.update()
  18.  
  19. mainloop()

hope this helps. again haven't really used pygame
My site ->> http://8masterofpuppets8.webs.com/
"My belief is stronger than your doubt."
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 54
Reputation: fallopiano is an unknown quantity at this point 
Solved Threads: 3
fallopiano's Avatar
fallopiano fallopiano is offline Offline
Junior Poster in Training
 
0
  #3
Oct 28th, 2009
when you draw a circle on the screen w/ pygame, you are not making a class instance. you are calling a function. you would need to have a seperate x and y variable incrementing up every game loop to make it look like it is moving
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 43
Reputation: thehivetyrant is an unknown quantity at this point 
Solved Threads: 0
thehivetyrant thehivetyrant is offline Offline
Light Poster
 
0
  #4
Oct 28th, 2009
oops
Last edited by thehivetyrant; Oct 28th, 2009 at 4:27 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 43
Reputation: thehivetyrant is an unknown quantity at this point 
Solved Threads: 0
thehivetyrant thehivetyrant is offline Offline
Light Poster
 
0
  #5
Oct 28th, 2009
Hmm i do have a tuple that says what x,y co-ords the circle is drew at:

self.Circle1 = x,y = 100,200

i thought i could just increase that?

i need to create another x,y?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 950
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 147
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345
 
0
  #6
Oct 28th, 2009
Yes you would increase that, but you would also need to completely redraw the whole circle. As well as that you would need to clear the last circle.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 43
Reputation: thehivetyrant is an unknown quantity at this point 
Solved Threads: 0
thehivetyrant thehivetyrant is offline Offline
Light Poster
 
0
  #7
Oct 28th, 2009
It is in a loop = TRUE and i do also do a display flip.

I know the screen works because i get name=labelfont.render to appear and move x+=1 and that works but the circle is killing me.

i have: Defintion of forward
def forward(distance):
        self.x = self.x + distance
        self.y = self.y + distance

And then: definition of step
class Circle1(Circles):
    def step(self,x,y):
        d=distance(x,y,self.x,self.y)
        if d<3: #circle should stop moving if close to the other circle
            return
        
        #if the x,y of the second circle is greater or less than the first 
        #then turn the circle accordingly.
        if x<self.x and y<self.y:
            self.angle=math.radians(315)
        elif x>self.x and y<self.y:
            self.angle=math.radians(45)
        elif x<self.x and y>self.y:
            self.angle=math.radians(225)
        elif x>self.x and y>self.y:
            self.angle=math.radians(135)
        self.forward(random.randint(0,10)) # move forward

and later : Calling that function
loop=True
    while loop:
        #Clear
        screen.fill(BLACK)
        
        Circle1.step(Circle2.getPos()[0],Circle2.getPos()[1])

It seems to just skip it though and decide not to do anything.

I apologise for the spliced up code as i shouldnt post all of it up since it's homework.
I have the feeling thats going to be hard to work with though.
Last edited by thehivetyrant; Oct 28th, 2009 at 5:36 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,141
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 947
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #8
Oct 29th, 2009
Here is a simple example that just erases and redraws the shape ...
  1. # exploring module pygame
  2. # draw a white circle on a black screen and move it
  3. # by erasing and redrawing the circle within a set time delay
  4.  
  5. import pygame as pg
  6.  
  7. # (r, g, b) tuple --> black
  8. black = (0, 0, 0)
  9.  
  10. # window/screen width and height
  11. w = 600
  12. h = 400
  13. # default background is black
  14. screen = pg.display.set_mode((w, h))
  15.  
  16. # set up some of the circle specs
  17. # (r, g, b) tuple --> white
  18. color = (255, 255, 255)
  19. radius = 150
  20. # optional width of the circle's border
  21. # if width = 0 (or not given) the shape is filled with color
  22. width = 0
  23.  
  24. for delta in range(0, 180, 2):
  25. x = 200 + delta
  26. y = 200
  27. position = (x, y) # (x,y) coordinates of center
  28. # this erases the old sreen with black
  29. screen.fill(black)
  30. pg.draw.circle(screen, color, position, radius, width)
  31. # update screen
  32. pg.display.flip()
  33. # small time delay
  34. milliseconds = 50
  35. pg.time.delay(milliseconds)
  36.  
  37. # event loop ...
  38. running = True
  39. while running:
  40. for event in pg.event.get():
  41. # quit when window corner x is clicked
  42. if event.type == pg.QUIT:
  43. running = False
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 337 | Replies: 7
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC