Posts: 4
Threads: 2
Joined: Mar 2019
May-26-2019, 08:10 PM
(This post was last modified: May-28-2019, 07:03 PM by metulburr.)
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
I want store distance in variable a,b,c,d,e
Posts: 5,151
Threads: 396
Joined: Sep 2016
May-26-2019, 11:47 PM
(This post was last modified: May-26-2019, 11:47 PM by metulburr.)
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
Recommended Tutorials:
Posts: 544
Threads: 15
Joined: Oct 2016
May-27-2019, 02:49 AM
(This post was last modified: May-27-2019, 02:50 AM by Windspar.)
Can also use pygame.Vector2.
a = pygame.Vector2(2, 2)
b = pygame.Vector2(5, 5)
a.distance_to(b)
99 percent of computer problems exists between chair and keyboard.
Posts: 4
Threads: 2
Joined: Mar 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?
Posts: 544
Threads: 15
Joined: Oct 2016
You sure you need 5 eyes. Probably could just use a rect. 4 corners and a center point.
99 percent of computer problems exists between chair and keyboard.
Posts: 4
Threads: 2
Joined: Mar 2019
May-28-2019, 06:07 PM
(This post was last modified: May-28-2019, 06:25 PM by N0m1t.)
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?
Posts: 544
Threads: 15
Joined: Oct 2016
Maybe something like this.
My state machine that I been playing around with.
import pygame
import os
# Simple interface
class State:
states = {}
def __init__(self, state_name=None, keep_alive=True):
if keep_alive:
if state_name is None:
state_name = self.__class__.__name__
State.states[state_name] = self
def draw(self, surface):
surface.fill(pygame.Color('gray40'))
def drop(self):
pass
def event(self, event):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
StateMachine.running = False
elif event.type == pygame.QUIT:
StateMachine.running = False
def focus(self, *args, **kwargs):
pass
def new(self, *args, **kwargs):
pass
def update(self, delta, ticks):
pass
# Simple static pygame setup
class StateMachine:
@classmethod
def setup(cls, caption, width, height):
# basic pygame setup
pygame.display.set_caption(caption)
cls.surface = pygame.display.set_mode((width, height))
cls.rect = cls.surface.get_rect()
cls.clock = pygame.time.Clock()
cls.running = False
cls.ticks = 0
cls.delta = 0
cls.fps = 30
# State
cls._state = State()
# Add extension callbacks
cls.extension = []
@classmethod
def _flip(cls, state_name, new=False, *args, **kwargs):
cls._state.drop()
if isinstance(state_name, State):
cls._state = state_name
else:
cls._state = State.states[state_name]
if new:
cls._state.new(*args, **kwargs)
else:
cls._state.focus(*args, **kwargs)
@classmethod
def flip(cls, state_name, *args, **kwargs):
cls._flip(state_name, False, *args, **kwargs)
@classmethod
def flip_new(cls, state_name, *args, **kwargs):
cls._flip(state_name, True, *args, **kwargs)
@classmethod
def mainloop(cls):
cls.running = True
while cls.running:
cls.ticks = pygame.time.get_ticks()
for event in pygame.event.get():
cls._state.event(event)
cls._state.update(cls.delta, cls.ticks)
cls._state.draw(cls.surface)
for extension in cls.extension:
extension(cls)
pygame.display.flip()
cls.delta = cls.clock.tick(cls.fps)
@staticmethod
def screen_center():
os.environ['SDL_VIDEO_CENTERED'] = '1'
@staticmethod
def screen_position(x, y):
os.environ['SDL_VIDEO_WINDOW_POS'] = '{0}, {1}'.format(x, y)
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()
99 percent of computer problems exists between chair and keyboard.
|