0

hello im very new to python and i want to read some numbers as a vector2 point from a txt file the .TXT file is :

map editor this is line 1 
(700, 500)
(100, 500)
(700, 100)
(700, 100)

and the code that is supposed to read it is:

import pygame
import keyboard

pygame.init()
#define a window
window = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Map_editor")

_map = open('Map.txt', 'r+')
_points = _map.readlines()[1:]
print(_points[0])
point1 = _points[0]
point2 = _points[1]
#void update base
run = True
while run:
    pygame.time.delay(10)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
#=====================================================================================================================================================================
    #code goes here
    pygame.draw.line(window, (0, 255, 100), point1, point2, 10)




#=====================================================================================================================================================================
    pygame.display.update()
_map.close()
pygame.quit()
i get this error:File "C:\Users******\Desktop\AGame\Mapeditor.py", line 23, in <module>pygame.draw.line(window, (0, 255, 100), point1, point2, 10)TypeError: invalid start_pos argument

i tried adding int(_points[0]) but it did nothing

1 Answers1

1

You need to parse your line. Otherwise, your readlines() will just contain each line as a string, instead of as tuples.

For example:

_points[0]

This will be just string "(700, 500)" instead of tuple (700, 500).

This question has answers that show how to do this. You will need to do this for each line before referring to them as point1, point2 and pass them into your drawing method.

Since I don't have all your dependencies, here are some brief tests about your first few lines:

_map = open('Map.txt', 'r+')
_points = _map.readlines()[1:]
print(_points)

import re
_points = list(map(lambda row: tuple(map(int, re.findall(r'[0-9]+', row))), _points))
print(_points)

Output:

['(700, 500)\n', '(100, 500)\n', '(700, 100)\n', '(700, 100)']
[(700, 500), (100, 500), (700, 100), (700, 100)]

Note that the first print statement prints what YOU have, which corresponds to the first line in the output, i.e., a list of strings. The second print statement prints the parsed list, so it is actually a list of tuples.

charlesz
  • 291
  • 5