Python Forum
[PyGame] move randomly sprites
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] move randomly sprites
#1
Hello,
I trying to make my sprites move randomly, I have defined x, y randomly according to the window size, than gave vx, vy value for speed, and every clock.tick i am updating the x and y value with vx and vy. I tried to set an angle and move the sprite from the new angle but it wasn't good.I will be glad if someone will help me.


the object class (a part from it)
[/font][/size]
def __init__(self, x, y, number_image):

        try:

            with open(number_image):

                image_file = number_image

        except IOError:

            print "I/O error no such file"

        super(self.__class__, self).__init__()

        self.image = pygame.image.load(image_file).convert()

        self.image.set_colorkey(WHITE)

        self.rect = self.image.get_rect()

        self.angle = math.atan2(-x, -y)/math.pi*180.0

        self.rect.x = x * self.angle

        self.rect.y = y * self.angle

        self.click = False

        self.__vx = 2

        self.__vy = 1
        self.speed = 1

the update pos from the object class:
def update_loc(self):

        self.rect.x += self.__vx
        self.rect.y += self.__vy

def main():

    game_over = False

    clock = pygame.time.Clock()

    size = (WINDOW_WIDTH, WINDOW_HEIGHT)

    try:

        with open(BACKGROUND_PIC):

            img_back = pygame.image.load(BACKGROUND_PIC)

    except IOError:

        print "I/O error no such file"

    screen = pygame.display.set_mode(size)

    init_screen(screen)

    finish = False

    list_plane = pygame.sprite.Group()

    global count_points

    count_points = 0

    global sign

    sign = '+'

    while not finish:

        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                finish = True



            """stop the planes and move to the to the mouse click position"""

            if event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:

                if not game_over:

                    for plane in list_plane:

                        if plane.rect.collidepoint(event.pos):

                            plane.update_click(True)

                            count_points -= 1

            if event.type == pygame.MOUSEBUTTONUP and event.button == LEFT:

                if not game_over:

                    for plane in list_plane:

                        if plane.rect.collidepoint(event.pos):

                            plane.update_click(False)



            '''start a game'''

            if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:

                game_over = False

                list_plane = pygame.sprite.Group()

                build_list_planes(list_plane)

                count_points = 0

                screen.blit(messege(' '), (100, 100))



        screen.blit(img_back, (0, 0)) #this is the point that i want to set every tick random movement

        for plane in list_plane:

            plane.update(screen)

            plane.update_loc()

            plane.bounce()

        if count_points == 1000 or crash_plane(list_plane, screen):

            game_over = True

            for plane in list_plane:

                    plane.update_v(0, 0)

            screen.blit(messege("Game Over press space for start again, points: " + str(count_points)), (100, 100))

        list_plane.draw(screen)

        pygame.display.flip()

        if not game_over:

            count_points += 1

        clock.tick(REFRESH_RATE)

    pygame.quit()





def build_list_planes(plans_list):

    """params: planes list

    do: add to plane list new plane"""

    pictures = [PIC_ONE, PIC_TWO, PIC_TREE, PIC_FOUR]

    for i in range(0, 4):

        x, y = random.randrange(50, 950, 100), random.randrange(50, 950, 100)

        plane = CrazyPlane(x, y, pictures[i])

        plane.update_loc()

        plans_list.add(plane)


if __name__ == '__main__':
    main()[size=small][font=Tahoma, sans-serif]
Moderator Larz60+: Added Code tags - In future posts please single line, no formatting, and use code tags
Reply


Messages In This Thread
move randomly sprites - by reutB - Mar-25-2017, 06:40 PM
RE: move randomly sprites - by metulburr - Mar-25-2017, 09:03 PM
RE: move randomly sprites - by reutB - Mar-26-2017, 05:17 AM
RE: move randomly sprites - by Larz60+ - Mar-26-2017, 10:46 AM
RE: move randomly sprites - by metulburr - Mar-29-2017, 01:12 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pygame, sprites, and rects menator01 12 2,124 Dec-07-2023, 02:37 AM
Last Post: Benixon
  [PyGame] Sprites just randomly appear and dissapear in my pygame.sprite.GoupeSingle trueShadoWrr 2 2,044 Feb-13-2023, 09:34 AM
Last Post: Vadanane
  player just randomly teleporting to the edge of a platform in pygame BliepMonster 5 2,405 Jan-26-2023, 10:12 PM
Last Post: deanhystad
  [PyGame] I found a way to generate sprites without .blit. Is it effecient? xBlackHeartx 19 8,643 Dec-07-2019, 01:06 PM
Last Post: metulburr
  Randomly selecting sprite from group? michael1789 5 4,215 Nov-14-2019, 10:43 PM
Last Post: michael1789
  [PyGame] Having 4 players(Sprites) all being able to jump ElijahCastle 5 4,077 May-07-2019, 05:04 PM
Last Post: SheeppOSU
  Sprites and Actor error ajlconsulting 6 9,459 Jan-30-2019, 12:50 AM
Last Post: metulburr
  draw not showing all sprites ethanstrominger 0 2,638 Jan-25-2019, 10:10 PM
Last Post: ethanstrominger
  How can I get rid of the line following the sprites? Houston11 1 3,781 Jan-06-2017, 10:14 PM
Last Post: Mekire

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020