[PyGame] move randomly sprites - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: [PyGame] move randomly sprites (/thread-2571.html) |
move randomly sprites - reutB - Mar-25-2017 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
RE: move randomly sprites - metulburr - Mar-25-2017 fix your post. It is missing lines, with undefined variables all over. Make it so we can run your code. RE: move randomly sprites - reutB - Mar-26-2017 import pygame import math import random #Constants WHITE = (255, 255, 255) PLANE_SIZE = 65 WIDTH = 1000 HEIGHT = 1000 class CrazyPlane(pygame.sprite.Sprite): 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.rect.y = y self.click = False self.__vx = 2 self.__vy = 1 self.speed = 1 print(self.angle) def update(self, surface): """params: screen object return: if self click is true move the sprite to the mouse position""" if self.click: self.rect.center = pygame.mouse.get_pos() surface.blit(self.image, self.rect) def update_click(self, click): """update click value""" self.click = click def get_click(self): """return click value""" return self.click def update_pos(self, x, y): """update mouse position""" self.rect.x = x self.rect.y = y def update_v(self, vx, vy): """update v value""" self.__vx = vx self.__vy = vy def update_loc(self): self.rect.x += self.__vx self.rect.y -= self.__vy def move(self): self.rect.move_ip(random.randint(0, 999), random.randint(0, 999)) def update_move(self): self.x += self.__vx self.y -= self.__vy def get_pos(self): """return sprite position""" return self.rect.x, self.rect.y def get_v(self): """update v value""" return self.__vx, self.__vy def bounce(self): """keeps the sprite inside the image frame""" x, y = self.get_pos() vx, vy = self.get_v() if x + PLANE_SIZE > WIDTH or x < 0: vx *= -1 if y + PLANE_SIZE > HEIGHT or y < 0: vy *= -1 self.update_v(vx, vy) the game #Constants WINDOW_WIDTH = 1000 WINDOW_HEIGHT = 1000 BACK_COLOR = (30, 144, 255) LEFT = 1 SCROLL = 2 RIGHT = 3 REFRESH_RATE = 30 PIC_ONE = 'pone.png' PIC_TWO = 'ptwo.png' PIC_TREE = 'pthree.png' PIC_FOUR = 'pfour.png' BACKGROUND_PIC = 'backgroundplanegame.png' def init_screen(screen): """params: the screen do: initializing the screen for the game""" pygame.init() pygame.display.set_caption("Game") screen.fill(BACK_COLOR) pygame.display.flip() def crash_plane(list_plane, screen): """params: list plane do: check if there are crash planes""" new_plane_list = pygame.sprite.Group() new_plane_list.empty() for plane in list_plane: plane_hit_list = pygame.sprite.spritecollide(plane, list_plane, False) if len(plane_hit_list) == 1: new_plane_list.add(plane) if len(list_plane) != len(new_plane_list): return True return False def messege(msg): '''do: return text game over''' font = pygame.font.Font(None, 50) font_color = (204, 0, 0) font_background = (255, 255, 255) txt = font.render(msg, True, font_color, font_background) return txt 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)) 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() RE: move randomly sprites - Larz60+ - Mar-26-2017 You did not use code tags. see: https://python-forum.io/misc.php?action=help&hid=25 RE: move randomly sprites - metulburr - Mar-29-2017 We need to be able to test run your code and you have images loaded that we cannot load. It would be best to create a repo where anyone can download your code and images to test run to help you. |