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.