I am working one the movement for a tactical turn-based pygame. I'm stumped as to how to get a selected object to move one tile at a time up to it's max movement to the mouse's pos. Right now this is what I have. I'm not sure how to proceed.
def move_toward(self, new_location): def do_calculations(): # ... code ... return dx, dy # ---- # you have the current x,y for the player. # each click of the clock you want to move the player # closer to the end location. delta = .01 # or whatever # get <delta> amount closer to the end corrds. <-- do this each tick of the clock. new_x, new_y = do_calculations() self.move(dx=new_x, dy=new_y)
import pygame as pg from settings import * class Player(pg.sprite.Sprite): def __init__(self, game, x, y): self.groups = game.all_sprites pg.sprite.Sprite.__init__(self, self.groups) self.game = game self.image = pg.Surface((TILESIZE, TILESIZE)) self.image.fill(YELLOW) self.rect = self.image.get_rect() self.x = x self.y = y def move(self, dx=0, dy=0): if not self.collide_with_walls(dx, dy): self.x = dx self.y = dy self.rect = self.rect.move(dx * TILESIZE, dy * TILESIZE)
def events(self): # catch all events here for event in pg.event.get(): if event.type == pg.QUIT: self.quit() if event.type == pg.KEYDOWN: if event.key == pg.K_ESCAPE: self.quit() if event.type == pg.MOUSEBUTTONDOWN: x = pg.mouse.get_pos() // TILESIZE y = pg.mouse.get_pos() // TILESIZE print(x, y) self.player.move(dx=x, dy=y)