def update(self): # peut-être déplacer plus de traitement du côté handle_event vertical_move = 1 if globals.keyPressed['down'] else -1 if globals.keyPressed['up'] else 0 horizontal_move = 1 if globals.keyPressed['right'] else -1 if globals.keyPressed['left'] else 0 if vertical_move != 0 or horizontal_move != 0: globals.hero.move((horizontal_move, vertical_move)) else: self.state = 0 # on passe en idle self.change_animation(self.direction + '_idle') # oops, une ligne ne représenta pas un état pour un perso car "immobile" et "marche" # sont sur la même ligne... il faut adapter animatedsprite! AnimatedSprite.update(self) # update l'animation en cours
def update(self, adventure_state): # peut-être déplacer plus de traitement du côté handle_event horizontal_move, vertical_move = self.get_next_move(adventure_state) if vertical_move != 0 or horizontal_move != 0: self.move((horizontal_move, vertical_move)) else: self.state = 0 # on passe en idle self.change_animation(self.direction + '_idle') # oops, une ligne ne représenta pas un état pour un perso car "immobile" et "marche" # sont sur la même ligne... il faut adapter animatedsprite! AnimatedSprite.update(self) # update l'animation en cours
def __init__(self, spritesheet_name, position, max_life, atk, max_speed) : animation_info_hash = { 'down_idle': {'start': 0, 'end': 0, 'duration': 0}, 'down_walk': {'start': 0, 'end': 3, 'duration': 20}, 'left_idle': {'start': 4, 'end': 4, 'duration': 0}, 'left_walk': {'start': 4, 'end': 7, 'duration': 20}, 'right_idle': {'start': 8, 'end': 8, 'duration': 0}, 'right_walk': {'start': 8, 'end': 11, 'duration': 20}, 'up_idle': {'start': 12, 'end': 12, 'duration': 0}, 'up_walk': {'start': 12, 'end': 15, 'duration': 20}, } AnimatedSprite.__init__(self, spritesheet_name, globals.CHARACTER_WIDTH, globals.CHARACTER_HEIGHT, animation_info_hash, 'down_idle') self.position = position self.direction = 'down' self.rect = pg.rect.Rect(position[0], position[1], globals.CHARACTER_WIDTH, globals.CHARACTER_HEIGHT) # will change self.speed = max_speed self.life = max_life self.atk = atk # attaque de l'entité self.state = 0 # Défini l'état de l'entité (0 : immobile, 1: mouvement, 2: attaque, 3: touché) self.aim = 0 # "angle" de visé de l'entité
def update(self, adventure_state): # déplacement continu sur un clic gauche prolongé if adventure_state.mouse_buttons['left']: # vérifier si la position recherchée est "convenable" self.target = adventure_state.mouse_buttons['left'] if self.target is not None: self._step_to(self.target) # ou move_to avec mémoire # si le perso est assez proche de sa cible, arrêter le mouvement if vector.distance(self.position, self.target) < 5: self.target = None # if vertical_move != 0 or horizontal_move != 0: # self.move((horizontal_move, vertical_move)) # else: # self.state = 0 # on passe en idle # self.change_animation(self.direction + '_idle') # oops, une ligne ne représenta pas un état pour un perso car "immobile" et "marche" # sont sur la même ligne... il faut adapter animatedsprite! AnimatedSprite.update(self) # update l'animation en cours
def main(): images = load_images(path="../Assets/Foundation (d2) - copy") # Make sure to provide the relative or full path to the images directory. player = AnimatedSprite(position=(0, 0), images=images) all_sprites = pygame.sprite.Group(player) # Creates a sprite group and adds 'player' to it. running = True while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() pygame.quit() while running: dt = 1 # Amount of seconds between each loop. for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() all_sprites.update(dt) # Calls the 'update' method on all sprites in the list (currently just the player). screen.fill(bg_color) all_sprites.draw(screen) pygame.display.update() clock.tick(100) if player.index == 56: running = False break screen.blit(home_image, (0, 0)) screen.blit(play, [115, 300]) pygame.display.update() playing1 = True while playing1: for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: ## if mouse is pressed get position of cursor ## pos = pygame.mouse.get_pos() ## check if cursor is on button ## if play_button.collidepoint(pos): playing1=False break #Group all of Ryan's code as a function and call it when mouse is pressed #main1(screen, screen_settings.bg_color, clock) while True: global ball_speed_x global ball_speed_y global ball_angle_increment global ball_angle global ball_velocity_increment global ball_velocity global gravity global lives global intro global walking global player_x global playing for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() while intro: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: player_x -= 1.5 walking = True if event.key == pygame.K_RIGHT: player_x += 1.5 walking = True if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT: walking = False if event.key == pygame.K_RIGHT: walking = False if event.key == pygame.K_RETURN: if player_x < screen_width / 100 * 25 and player_x > screen_width / 100 * 18: intro = False playing = True break #print(walking) player_animation() clock.tick(60) # Key Actions if event.type == pygame.KEYDOWN: if event.key == pygame.K_DOWN: ball_angle_increment += 0.02 if event.key == pygame.K_UP: ball_angle_increment -= 0.02 if event.key == pygame.K_LEFT: ball_velocity_increment -= 0.01 if event.key == pygame.K_RIGHT: ball_velocity_increment += 0.01 if event.key == pygame.K_RETURN: if ball_speed_x == 0 or ball_speed_y == 0: ball_speed_x = ball_velocity * math.cos(ball_angle * math.pi/180) ball_speed_y = ball_velocity * math.sin(ball_angle * math.pi/180) else: j = 5 if event.type == pygame.KEYUP: if event.key == pygame.K_DOWN: ball_angle_increment = 0 if event.key == pygame.K_UP: ball_angle_increment = 0 if event.key == pygame.K_LEFT: ball_velocity_increment = 0 if event.key == pygame.K_RIGHT: ball_velocity_increment = 0 ball_animation() update() #ball_velocity += ball_velocity_increment #ball_angle += ball_angle_increment ball_speed_y += gravity/60 launcher_copy = pygame.transform.rotate(launcher, -1 * ball_angle) screen.blit(launcher_copy, (screen_width / 120 * 27 - int(launcher_copy.get_width() / 4), screen_height / 120 * 95 - int(launcher_copy.get_height() /2))) ''' ball_animation() update() #ball_velocity += ball_velocity_increment #ball_angle += ball_angle_increment ball_speed_y += gravity/60 ''' ''' #Visuals screen.fill(bg_color) screen.blit(background, (0,0)) screen.blit(target, (screen_width * i/48, screen_height * 36/40)) target_rect = pygame.Rect(screen_width * i/48, screen_height * 69/72, target.get_width(), 2) pygame.draw.ellipse(screen, (0,200,200), ball) pygame.draw.rect(screen, (0,0,0), line) text = font.render("Angle " + str(-1 * ball_angle), 30, (200,0,0)) text1 = font.render("Velocity " + str(ball_velocity), 30, (200,0,0)) textlives = font.render("Lives Left: " + str(lives), 30, (200,0,0)) screen.blit(textlives, (screen_width * 6/8, screen_height * 1/12)) screen.blit(text1, (screen_width/6, screen_height/8)) screen.blit(text, (screen_width/6, screen_height/12)) #Launcher Animation launcher_copy = pygame.transform.rotate(launcher, -1 * ball_angle) screen.blit(launcher_copy, (screen_width / 120 * 27 - int(launcher_copy.get_width() / 4), screen_height / 120 * 95 - int(launcher_copy.get_height() /2))) ''' pygame.display.flip() clock.tick(30)