reset password
Author Message
hal255
Posts: 51
Posted 13:45 Apr 26, 2015 |

Hello all,

Has anyone figured out how to start the sprite at a random position?

I tried something like:

def main():

ball1 = Ball(randNum(500), randNum(500), randNum(5), randNum(5))

...

...

def randNum(maxInt):

return random.randInt(0, maxInt)

 

The sprite always started at position (0,0), but the movement speed varies, so I know the last 2 numbers work, but not the first 2.

Is there something I need to change in the class Ball as well?

 

 

Last edited by hal255 at 13:46 Apr 26, 2015.
cliffordtzlin
Posts: 11
Posted 13:50 Apr 26, 2015 |

It's weird that you're able to still generate random speeds but I believe you import randint from random not randInt. 

cliffordtzlin
Posts: 11
Posted 13:52 Apr 26, 2015 |

Also make sure you have: 

self.rect = self.image.get_rect()

self.rect.x = x
self.rect.y = y

or something along those lines to initiate a location

hal255
Posts: 51
Posted 14:13 Apr 26, 2015 |
cliffordtzlin wrote:

Also make sure you have: 

self.rect = self.image.get_rect()

self.rect.x = x
self.rect.y = y

or something along those lines to initiate a location

Yea, had those in the Ball class. So I thought when creating the new Ball object, we give it some values (ball1 = Ball(0,0,4,5)), and the __init__ function of Ball class would set these values with the instructions above. Guess I'll pester Keenan on Tuesday.

kknaur
Posts: 540
Posted 14:31 Apr 26, 2015 |

Email me your code, I can look at it.

ggonza83
Posts: 14
Posted 16:56 Apr 26, 2015 |

For the random placement of the sprite I suggest that you use the randomint function to give it random x and y coordinates

Example:

import random

random.randint(1, 10)  # Integer from 1 to 10, endpoints included

Hopefully that helps