Hey all,

I must say that I've had a lot of success with my questions here at DaniWeb, which is why (in my time of need), I'm back and seeking the help of the wise! Hopefully, someone can help me with this and has some basic knowledge of Pygame? Heh, anyway... let me keep it short:

I am writing a Ant simulation-type program in which I have a file containing data (such as the ant number, the x and y coordinates of that ant, a timestamp (when the ant is moving/visible), and finally the orientation of the ant (the direction/angle the ant is facing).

The "final" result of the program is supposed to read the file (containing the data) and use the contents (ie. the id num of the ant, x and y coordinates, etc.) to draw to a surface an image of an ant that corresponds to the data given.

I just have two (fairly major I think) problems that I'll need to resolve before this is going to be working satisfactory. Here's the two issues (hopefully I've noticed them all)...

1. I need to figure out how to pass "variables" and "parameters" as various arguments (say, rect.x = xCoordinate)

and

2. I need to figure out how to CORRECTLY get the time (in milliseconds). (P.s. The ants are timestamped for every 33 milliseconds... and by using: pygame.time.get_ticks() - it's returning milliseconds every 20 or so seconds... If I could set it to check every millisecond or even every 33 milliseconds - that'd be great!)

So, here is my code... well, just basically my problem areas.
First my new and improved File Read/Instantiate iteration:

#--- Start the file read and Instantiate --#
data = []
allSprites = []

with open('id.txt') as f:
    for line in f:
        # Do line parsing
        data.append(line)
        token = line.split("\t") # Splits into tab-delimited tokens
        ant = Ant(token[0], token[1], token[2], token[3], token[4])
        allSprites.append(ant)

#--- End the file read ----#

Next is my MAIN game loop... the problem that comes here is when I call my update method for my Sprite class:

# -- Main Game Loop -- #
clock = pygame.time.Clock()

while(not gameOver):
    clock.tick(33.33) # Set frame rate
    
    surface.fill((255, 255, 255))

    print "CLOCK TIME: ", pygame.time.get_ticks()
    for sprite in allSprites:
        sprite.update()

    for event in pygame.event.get():
        if event.type == QUIT:
            gameOver = True
            break

    screen.blit(surface, (255, 255))
    pygame.display.update()

And finally, my update method for my Ant() sprite class:

def update(self):
        global surface

        center = self.rect.center

        if self.timestamp == pygame.time.get_ticks():
            print "TEST: Got in Here."
            self.isVisible = True
            surface.blit(self.image, (10, 120)) # want to actually blit to xCoord and yCoord, but doesn't work
        else:
            self.isVisible = False

As you can see.. the ant is supposed to be blited to the screen if it's timestamp matches the current tick. Also, in the piece that is:

surface.blit(self.image, (10, 120))

this is supposed to read "(self.xPos, self.yPos)" in place of "10" and "120" respectively - problem is, I don't know how to pass non-integers into the blit function. I even tried casting "self.xPos" and "self.yPos" as an integer with:

int(self.xPos)

Though, casting didn't work.

here's the error I received when I tried casting with: "int(self.xPos, 10)":
C:\Documents and Settings\HP_Administrator\Ant>python ant.py
Traceback (most recent call last):
File "ant.py", line 86, in <module>
ant = Ant(token[0], token[1], token[2], token[3], token[4])
File "ant.py", line 28, in __init__
self.rect.x = int(xCoord, 10)


Hmm, hopefully all this code makes sense and someone can help me with this... it's been three days of struggling! Heh, save me from this madness!
ValueError: invalid literal for int() with base 10: '\x003\x007\x000\x00'

EDIT: By the way, here's how the data looks that I'm reading:
id yimage ximage timestamp timage
3 284 370 66 1.936544
2 255 337 66 0.820845
1 212 379 66 4.026659654
0 135 475 66 4.333737654
3 280 365 99 2.164339
2 255 336 99 0.792491

Where, "id" is the id of the ant, "yimage" is the y coordinate, "ximage" is the x coordinate of that ant, "timestamp" is the time in milliseconds at which that ant is visible/moving/being recorded, and "timate" is the orientation/angle in radians that the ant is facing.

Edited 6 Years Ago by Krysis: To add data synatx and explain the fields

Is there anyway that you can attach the entire program as a zip or pm it. I feel like I can fix the problem, but really need to try it out on the program.

Hey, looks like I managed to fix my problem tbone2ask! Thanks for your attention and willingness to help.

For the record: my problem came from reading from the file, which was tab delimited. Problem is, the tokens returned have a null-terminated character (or perhaps it was the "tab" character) attached to it and the casting function "int(string_2_cast)" could not cast it with that null-terminated character. I had to separate each data field into it's own file to read then I was able to work from there.

Anyway, thanks again! Fortunately, I have a new project on my hands (but in C) that I'll be posting about momentarily. :-)

This article has been dead for over six months. Start a new discussion instead.