TTRPG Movement

Joined
Jun 29, 2021
Messages
2
Reaction score
0
Working one the movement for a tactical turnbased 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. Any help will be great.


Python:
# This is the Sprites.py
import pygame as pg
from settings import *

# define some colors (R, G, B)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
DARKGREY = (40, 40, 40)
LIGHTGREY = (100, 100, 100)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
CYAN = (0, 255, 255)

# game settings
WIDTH = 1024   # 16 * 64 or 32 * 32 or 64 * 16
HEIGHT = 768  # 16 * 48 or 32 * 24 or 64 * 12
FPS = 60
TITLE = "Tilemap Testing"
BGCOLOR = DARKGREY

TILESIZE = 32
GRIDWIDTH = WIDTH / TILESIZE
GRIDHEIGHT = HEIGHT / TILESIZE


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(CYAN)
        self.rect = self.image.get_rect()
        self.x = x
        self.y = y
        self.rect.x = self.x * TILESIZE
        self.rect.y = self.y * TILESIZE

    def move(self, vect):
        x = vect[0] - self.rect.x
        y = vect[1] - self.rect.y

        self.rect.move_ip(x, y)

    def collide_with_walls(self, dx=0, dy=0):
        for wall in self.game.walls:
            if wall.x >= self.x + dx and wall.y >= self.y + dy:
                return True
        return False
 
Joined
Jun 29, 2021
Messages
2
Reaction score
0
Some Updates, does about the same thing but got it working with the engine better. I'm thinking it's gonna be a for x range() arg that I need to do.

Python:
# Sprites.py
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

    def collide_with_walls(self, dx=0, dy=0):
        for wall in self.game.walls:
            if wall.x == self.x + dx and wall.y == self.y + dy:
                return True
        return False

    def update(self):
        self.rect.x = self.x * TILESIZE
        self.rect.y = self.y * TILESIZE

class Wall(pg.sprite.Sprite):
    def __init__(self, game, x, y):
        self.groups = game.all_sprites, game.walls
        pg.sprite.Sprite.__init__(self, self.groups)
        self.game = game
        self.image = pg.Surface((TILESIZE, TILESIZE))
        self.image.fill(GREEN)
        self.rect = self.image.get_rect()
        self.x = x
        self.y = y
        self.rect.x = x * TILESIZE
        self.rect.y = y * TILESIZE

Python:
# main.py
import pygame as pg
import sys
from os import path
from settings import *
from sprites import *


class Game:
    def __init__(self):
        pg.init()
        self.screen = pg.display.set_mode((WIDTH, HEIGHT))
        pg.display.set_caption(TITLE)
        self.clock = pg.time.Clock()
        pg.key.set_repeat(500, 100)
        self.load_data()

    def load_data(self):
        game_folder = path.dirname(__file__)
        self.map_data = []
        with open(path.join(game_folder, 'map.txt'), 'rt') as f:
            for line in f:
                self.map_data.append(line)

    def new(self):
        # initialize all variables and do all the setup for a new game
        self.all_sprites = pg.sprite.Group()
        self.walls = pg.sprite.Group()
        # This enumerates the map.txt file and creates a wall based on the #w and a player based on 1
        for row, tiles in enumerate(self.map_data):
            for col, tile in enumerate(tiles):
                if tile == 'w':
                    Wall(self, col, row)
                if tile == '1':
                    self.player = Player(self, col, row)
                if tile == 'm':
                    Monster(self, col, row)


    def run(self):
        # game loop - set self.playing = False to end the game
        self.playing = True
        while self.playing:
            self.dt = self.clock.tick(FPS) / 1000
            self.events()
            self.update()
            self.draw()

    def quit(self):
        pg.quit()
        sys.exit()

    def update(self):
        # update portion of the game loop
        self.all_sprites.update()

    def draw_grid(self):
        for x in range(0, WIDTH, TILESIZE):
            pg.draw.line(self.screen, LIGHTGREY, (x, 0), (x, HEIGHT))
        for y in range(0, HEIGHT, TILESIZE):
            pg.draw.line(self.screen, LIGHTGREY, (0, y), (WIDTH, y))

    def draw(self):
        self.screen.fill(BGCOLOR)
        self.draw_grid()
        self.all_sprites.draw(self.screen)
        pg.display.flip()

    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:
                print(pg.mouse.get_pos())
                x = pg.mouse.get_pos()[0] // TILESIZE
                y = pg.mouse.get_pos()[1] // TILESIZE

                self.player.move(dx=x, dy=y)


    def show_start_screen(self):
        pass

    def show_go_screen(self):
        pass


# create the game object
g = Game()
g.show_start_screen()
while True:
    g.new()
    g.run()
    g.show_go_screen()
 
Last edited:
Joined
Jun 29, 2021
Messages
1
Reaction score
2
Hi! So. I took a look at your program. Am I correct that the only change you want to make is to show the movement of the player toward a selected x,y corrds? If so, I think the following might work.

In class Game in main.py:

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:
print(pg.mouse.get_pos())
x = pg.mouse.get_pos()[0] // TILESIZE
y = pg.mouse.get_pos()[1] // TILESIZE

self.player.move_toward(dx=x, dy=y)

So, here, instead of calling "move" you're calling "move_toward".

----

Put this in class Player in sprites.py:

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)

Clear as mud? ;)
Let me know how it goes, or if you have any questions. :)

Chris Strange
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top