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.

Member Avatar for masterofpuppets

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:

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

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

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?

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.

oops

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.

Here is a simple example that just erases and redraws the shape ...

# 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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.