| | |
Moving a drawn shape in pygame
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2009
Posts: 43
Reputation:
Solved Threads: 0
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
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.
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.
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:
hope this helps. again haven't really used pygame
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:
Python Syntax (Toggle Plain Text)
from Tkinter import * import time root = Tk(); root.geometry( "200x200+400+200" ) c = Canvas( root, width = 200, height = 200 ); c.pack() ball = c.create_oval( 0, 0, 0, 0 ) xInc = 3 ballX = 30 while True: time.sleep( 0.05 ) c.delete( ball ) ball = c.create_oval( ballX, 100, ballX + 5, 105, fill = "blue" ) if ballX > 200 or ballX < 0: xInc = -xInc ballX += xInc c.update() mainloop()
hope this helps. again haven't really used pygame
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
Check out my Site | and join us on IRC | Python Specific IRC
•
•
Join Date: Jan 2009
Posts: 43
Reputation:
Solved Threads: 0
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
i have: Defintion of forward
And then: definition of step
and later : Calling that function
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.
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 + distanceAnd 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 forwardand 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.
0
#8 Oct 29th, 2009
Here is a simple example that just erases and redraws the shape ...
python Syntax (Toggle Plain Text)
# exploring module pygame # draw a white circle on a black screen and move it # by erasing and redrawing the circle within a set time delay import pygame as pg # (r, g, b) tuple --> black black = (0, 0, 0) # window/screen width and height w = 600 h = 400 # default background is black screen = pg.display.set_mode((w, h)) # set up some of the circle specs # (r, g, b) tuple --> white color = (255, 255, 255) radius = 150 # optional width of the circle's border # if width = 0 (or not given) the shape is filled with color width = 0 for delta in range(0, 180, 2): x = 200 + delta y = 200 position = (x, y) # (x,y) coordinates of center # this erases the old sreen with black screen.fill(black) pg.draw.circle(screen, color, position, radius, width) # update screen pg.display.flip() # small time delay milliseconds = 50 pg.time.delay(milliseconds) # event loop ... running = True while running: for event in pg.event.get(): # quit when window corner x is clicked if event.type == pg.QUIT: running = False
May 'the Google' be with you!
![]() |
Similar Threads
- Moving an object with control keys in a sprite (Python)
- problem with pygame (Python)
- Help with pygame (Python)
- problems with pygame (Python)
- Turtle graph (Python)
- Hello JavaGuys/Ladies! Panel Layers being funny... (Java)
- shape tween (Graphics and Multimedia)
- Multiple keypresses at once in pygame (Python)
Other Threads in the Python Forum
- Previous Thread: Hi from Python noob :D
- Next Thread: How to integrate vumeter
| Thread Tools | Search this Thread |
accessdenied apache application argv array beginner book builtin change color converter countpasswordentry dan08 dictionary dynamic edit editing enter examples excel file filename float format function gui homework import inches input java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysql newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl pysimplewizard python random recursion redirect remote reverse scrolledtext session simple smtp software sprite statictext string strings syntax table tennis terminal text textarea thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable windows wordgame wxpython






