由于某种原因,我的代码在读取文件时无法前进

时间:2019-06-06 23:06:17

标签: python python-3.x pygame

基本上,我正在尝试让程序读取文件并采用值绘制一些简单的形状。代码没有前进,它只是将形状指定为第一行,不会做其他任何事情。

这是我要读取的文件:

SQUARE
r
b
200#200
40
RECTANGLE
b
g
100#100
10
SQUARE
r
b
500#355
40
SQUARE
g
b
600#60
15
END

我曾尝试将readline()移到该位置,但似乎并不能解决问题,就像在whileloop之类的之前。

import pygame
pygame.init()
screen = pygame.display.set_mode((640,640))
R = (255,0,0)
B = (0,0,255)
G = (0,255,0)
shapefile = open("read_file.txt","r")
donereadingfile = True

line = shapefile.readline()
while donereadingfile:
   if (line == "END\n") or (line == "END"):
       donereadingfile = False

   elif(line == "RECTANGLE\n"):
       RECTANGLE = line
       RECTANGLE = RECTANGLE.rstrip("\n")

       line = shapefile.readline()
       circle_colour = line
       circle_colour = circle_colour.rstrip("\n")
       if circle_colour == ("r"):
           circle_colour = R
       elif circle_colour == ("b"):
           circle_colour = B
       elif circle_colour == ("g"):
           circle_colour = G

       line = shapefile.readline()
       border_colour = line
       border_colour = border_colour.rstrip("\n")

       if border_colour == ("r"):
           border_colour = R
       elif border_colour == ("b"):
           border_colour = B
       elif border_colour == ("g"):
           border_colour = G

       line = shapefile.readline()
       coordinate = line
       coordinate = coordinate.rstrip("\n")
       coordinate = coordinate.split("#")
       x = int(coordinate[0])
       y= int(coordinate[1])


       line = shapefile.readline()
       diameter = line
       diameter = diameter.rstrip("\n")
       diameter = int(diameter)

       pygame.draw.rect(screen,border_colour,(int(x),int(y),3*diameter,diameter),3)
       pygame.draw.circle(screen,circle_colour,x,y,(diameter/2))
       pygame.display.update()

   elif (line == "SQUARE\n"):
       SQUARE = line
       SQUARE = SQUARE.rstrip("\n")

       line = shapefile.readline()
       circle_colour = line
       circle_colour = circle_colour.rstrip("\n")
       if circle_colour == ("r"):
           circle_colour = R
       elif circle_colour == ("b"):
           circle_colour = B
       elif circle_colour == ("g"):
           circle_colour = G

       line = shapefile.readline()
       border_colour = line
       border_colour = border_colour.rstrip("\n")

       if border_colour == ("r"):
           border_colour = R
       elif border_colour == ("b"):
           border_colour = B
       elif border_colour == ("g"):
           border_colour = G

       line = shapefile.readline()
       coordinate = line
       coordinate = coordinate.rstrip("\n")
       coordinate = coordinate.split("#")
       x = int(coordinate[0])
       y= int(coordinate[1])



       line = shapefile.readline()
       diameter = line
       diameter = diameter.rstrip("\n")
       diameter = int(diameter)

       pygame.draw.rect(screen,border_colour,(x,y,diameter,diameter),3)
       pygame.draw.circle(screen,(circle_colour,x,y,(diameter/2))
       pygame.display.update()

shapefile.close()

它说“ x = int(coordinate [0]) ValueError:int()以10为基的无效文字:“ SQUARE”

我不明白为什么,但是它应该打印出应该的形状。

1 个答案:

答案 0 :(得分:0)

您可以改用for循环:

with open( 'file', 'r') as f:
    for line in f:
        print(line)

那样,您不必使用停止条件,并且调试起来更容易。