[pygame] distance detector - 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] distance detector (/thread-18670.html) |
[pygame] distance detector - N0m1t - May-26-2019 Hi, how I can calculate the distance of moving object to the walls in the labyrinth like on the picture? https://photos.app.goo.gl/D9YyTanhhfWSSJSd8 [attachment=638] I want store distance in variable a,b,c,d,e RE: [pygame] distance detector - metulburr - May-26-2019 To calculate the distance between two points dist = sqrt( (x2 - x1)**2 + (y2 - y1)**2 )or dist = math.hypot(x2 - x1, y2 - y1)Where x1 and y1 is one point, and x2 and y2 is the other point. https://en.wikipedia.org/wiki/Pythagorean_theorem RE: [pygame] distance detector - Windspar - May-27-2019 Can also use pygame.Vector2. a = pygame.Vector2(2, 2) b = pygame.Vector2(5, 5) a.distance_to(b) RE: [pygame] distance detector - N0m1t - May-28-2019 Thanks for this snippet of code. I want to figure out how I can monitor the distance to the wall we say in the range of 20. Plus object is in constant move forward so the five points will constantly change and If you can see on the picture I need read distance from 5 independent "eyes". Any idea how to bite that? RE: [pygame] distance detector - Windspar - May-28-2019 You sure you need 5 eyes. Probably could just use a rect. 4 corners and a center point. RE: [pygame] distance detector - N0m1t - May-28-2019 I'm pretty sure that I need 5 eyes in exactly those directions to garher the data. (May-28-2019, 04:53 PM)Windspar Wrote: You sure you need 5 eyes. Probably could just use a rect. 4 corners and a center point. You give me some idea of how to do that... Now my question is how to monitor the distance we say one point, middle of the character to the walls if coordinates of the walls are constantly changing? RE: [pygame] distance detector - Windspar - May-28-2019 Maybe something like this. My state machine that I been playing around with. Requires my state machine code above. import pygame from statemachine import StateMachine, State from pygame.sprite import Sprite, Group class TextSprite(Sprite): def __init__(self, image, position, wall, *groups): Sprite.__init__(self, *groups) self.image = image self.rect = image.get_rect() self.rect.topleft = position self.ticks = 3000 self.wall = wall def update(self): self.ticks -= StateMachine.delta if self.ticks <= 0: self.wall.trigger = False self.kill() class PlayerSprite(Sprite): def __init__(self, image, position, *groups): Sprite.__init__(self, *groups) self.image = image self.rect = image.get_rect() self.rect.topleft = position self.position = pygame.Vector2(position) self.speed = 3 / StateMachine.fps def update(self): keys = pygame.key.get_pressed() if keys[pygame.K_w] or keys[pygame.K_UP]: self.position.y -= self.speed * StateMachine.delta self.rect.topleft = self.position if keys[pygame.K_s] or keys[pygame.K_DOWN]: self.position.y += self.speed * StateMachine.delta self.rect.topleft = self.position if keys[pygame.K_a] or keys[pygame.K_LEFT]: self.position.x -= self.speed * StateMachine.delta self.rect.topleft = self.position if keys[pygame.K_d] or keys[pygame.K_RIGHT]: self.position.x += self.speed * StateMachine.delta self.rect.topleft = self.position class WallSprite(Sprite): def __init__(self, image, position, *groups): Sprite.__init__(self, *groups) self.image = image self.rect = image.get_rect() self.rect.topleft = position self.trigger = False class Detection(State): def __init__(self): State.__init__(self) self.sprites = Group() self.walls = Group() self.texts = Group() self.create_images() self.create_grid() def create_grid(self): self.grid = [[0 for x in range(20)] for y in range(20)] self.grid[0] = [1 for x in range(20)] self.grid[19] = [1 for x in range(20)] for i in range(1, 19): self.grid[i][0] = 1 self.grid[i][19] = 1 for x, row in enumerate(self.grid): for y, value in enumerate(row): if value == 1: WallSprite(self.images['wall'], (x * 30, y * 30), self.sprites, self.walls) def create_images(self): self.images = {} wall = pygame.Surface((29, 29)) wall.fill(pygame.Color('forestgreen')) self.images['wall'] = wall player = pygame.Surface((29, 29)) player.fill(pygame.Color('dodgerblue')) self.images['player'] = player center = StateMachine.rect.center self.player = PlayerSprite(self.images['player'], center, self.sprites) font = pygame.font.Font(None, 12) text = font.render('20', 1, pygame.Color('firebrick')) self.images['wall text'] = text def draw(self, surface): surface.fill(pygame.Color('black')) self.sprites.draw(surface) self.sprites.update() def update(self, delta, ticks): # detection 20 around ship rect = self.player.rect.inflate(40, 40) text_center = self.images['wall text'].get_rect().center for wall in self.walls: if not wall.trigger: if rect.colliderect(wall.rect): wall.trigger = True pos = pygame.Vector2(wall.rect.center) pos -= text_center TextSprite(self.images['wall text'], pos, wall, self.sprites, self.texts) def main(): pygame.init() StateMachine.screen_center() StateMachine.setup("Detection Example", 600, 600) StateMachine.flip(Detection()) StateMachine.mainloop() pygame.quit() if __name__ == "__main__": main() |