I need help QUICKLY!!! Problems with racing game!

My problem is really simple. My school is having an open house and I promised to do a simple driving game so people can play it there. The idea is to have some kind of championship and reward the fastest driver.

I have two problems with it:

  1. I need a timer for it. How can I make it and is it even possible.
  2. I would need to have the lap time to be shown in the end and the player to be able to write his/her name and phone number and they should be saved on the computer or on a server.

Is it possible to make this kind of game or am I doomed to fail.

I don’t mean to rush anyone, but I would like to have an answer soon. I only have time till next Friday to do it and if it’s not possible with blender, then I have to search for an alterative ways to do it.

I think one of the BlenderArt magazines talks about using Python to connect to servers within Blender.

It’s definitely possible. For a basic timer, you CAN use a timer property (just add a property to the object and make it of type “Timer” instead of “int”). If you want to save a lap time, when the player completes a lap just copy that property (via python or a copy property actuator) to another variable (probably something called “lap1”).

Saving scores is going to require python, but it most definitely can be done. Look up Python’s Pickle module, which I believe has read/write capabilities. There are also a ton of save/load scripts on this forum, just use the search feature.

OK! I got the timer now and it works fine. Now there is just the little problem with saving those laptimes. I’m really a noob and this is really annoying. I have tried to display the fastest laptime under the current laptime but I don’t really know how to do it. and I would really appreciate if anyone could tell me how to do it. Also I tried to find out if there is a way to ask the player his phone number and name and save those and his laptime.

For saving/recalling lap times, use this tutorial (blendenzo’s save/load).
For text input, usethis tutorial (also blendenzo’s).

Thanks! I try those and hope I get it working

I tried those and the text input was easy, but I really can’t get the saving part work. I need to get the laptime and player name and phone number in the same txt file so I can then use MySQL to move it on the server. Can anyone help me with this?

You can edit a MySQL database directly with Python, I just don’t know how to do it. I wish I would have picked up that book on writing web applications with Python at the library’s used book sale a couple of weeks ago…

Anyway, could you share the Python script you are using to read from and write to text files? It’s hard to know why it isn’t working for you without being able to see what you’ve got going.

To do an on screen timer, here is another great tutorial from blendenzo :smiley:

http://www.blendenzo.com/tutTimer.html

I actually don’t have the script anymore. I got mad and deletet it. I got it from there:
http://www.blendenzo.com/tutSaveLoad.html

I then tried to change it to meet my needs, but it would have been easyer if I knew what it actually does on every line. I am really noob with python and I know just the basics of Blender. This wouldn’t be a problem if I had more time, but I only have this week. It MUST be done before Friday.

I used this code for my timer if it helps anyone:

own = GameLogic.getCurrentController().getOwner()

minutes = int(own.timer/60)
seconds = int(own.timer%60)
tenths = ((own.timer%60) - int(own.timer%60)) * 100

own.Text = str(minutes) + ":" + str("%02i"% seconds) + ":" + str(int(tenths))

Got that from a tutorial and I don’t have even a smallest idea what it does, but it works.

This is the code I tried to change to meet my needs. I got it from a tutorial:

### Add objects to be saved to this list ###
cube = GameLogic.getCurrentScene().getObjectList()["OBCube"]
cubePosi = cube.getPosition()
cubeRot = cube.getOrientation()

### Open the file "Game1.sav" in write mode
saveFile = open("Game1.sav", "w")
### Write header to file
saveFile.write("This is a valid save file
")

### Write game data to file
for x in range(len(cubePosi)):
    saveFile.write(str(cubePosi[x]) + "
")
for x in range(len(cubeRot)):
    for y in range(len(cubeRot[x])):
        saveFile.write(str(cubeRot[x][y]) + "
")

### Close save file
saveFile.close()

I just don’t any idea, how am I supposed to change to get it save the time. I just don’t get it. I know it can’t be hard, but I just can’t understand it.

I am really a noob with python and this is really getting annoying. I have spent 3 days just trying to figure this out.

I used this code as my timer, if it helps you to help me:

own = GameLogic.getCurrentController().getOwner()

minutes = int(own.timer/60)
seconds = int(own.timer%60)
tenths = ((own.timer%60) - int(own.timer%60)) * 100

own.Text = str(minutes) + ":" + str("%02i"% seconds) + ":" + str(int(tenths))

I attach my work so you can get a better picture of my problems.

The thing is that I want to save the time of the whole race to a single file where there will also be the players name and phone number. I just can’t get it to work.

The file is from a tutorial! I have just added the timers and a sensor that ignores the first time you pass the finishline. It restarts when you have completed 4 laps and that is just to show myself where I want the saving to happen.

I would be really happy if anyone could help me with this. I am really a noob with python.

Attachments

raceLaps_timer.blend (423 KB)

I am now very close! I know I’m on the right track. I just need a little help. If I have a variable “A” and I want to save the value of that variable in the save file, how can I do it. And how do I get the property named “Time” from an object “Plane” to be saved in the same file.

“out_file.write(A)” didn’t work!
and I don’t have any idea how I can get the property as a variable so I can save it.

If there is anyone at all who knows the answer or can help me find it, I would really appreciate it. I just can’t believe there’s no one who can help me. There must be someone who knows the answer.

PLEASE help me! I got two more days to do it so it HAS TO BE done before Monday! I am really scared now. I don’t want to be the one who ruins everything. Please help me!

if the variable A is defined within your script (example)

 A = 4.2
out_file.write(str(A)) 

and then when you import, you need to convert to float.

Thanks! That helps a lot, but I still have a problem with the properties. Does anyone know how I can save those. I tried to do it this way:

plane = GameLogic.getCurrentScene().getObjectList()["OBPlane"]
planeProp = plane.getAllProperties()
...
...
...
out_file.write(str(planeProp))

That didn’t work! Is there something wrong in the code or is it totally useless?

You identify an object’s property as

obj.prop

so for your example…

plane = GameLogic.getCurrentScene().getObjectList()["OBPlane"]
out_file.write(str(plane.prop))
out_file.write(str(plane.prop2)) 

THANK YOU!! You really saved me. I can’t believe it was that simple. Most of the time I was very near, but couldn’t do it right. Thank you!

Now another question. Is there a way to add variables in an existing txt file without deleting the data that is already saved there?

You’re very welcome. I’m glad I could help. Let me see what I can come up with about the save file.

You can use the “a” option to append stuff to a file (instead of using the “w” option)
So…

file = open("my_file.txt", "a") 

You have just saved my life! I can’t thank you enough. I can’t believe it was that simple. Just one letter. ONE LETTER!

I was almost panicking here. Days passed by and I wasn’t moving anywhere. But thanks to you I will get this done in time. I owe you one.