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