Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I would like to create a simple tank using pygame. One would drive the tank by controlling he velocities of two caterpillar tracks. They should be the two single movement points of the vehicle.

enter image description here

I'm looking for directions in which pygame methods and objects to check or tutorials regarding the topic.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

Regarding vectors: this guy 'Phyces' made a few tutorials and he included a really useful 2D vector class. vec2d class - you could use it to define your position and velocity of each element, and use its rotate() method. Bear in mind that when you blit the image you'll need to convert the position back to a tuple using the inttup() method. If you do use it, it can't hurt to do Phyces video tutorials, they're dead short and really clear - they helped me loads. He also shows you how to do rotated images.

Hope that Helps.

Also... with the two tracks: your velocity at each track is v = w/r (where w is the rotational velocity, r is the distance to the centre of the body) and the rotational velocity of the whole body will be w1 + w2 = wT. Bear in mind that if they are both facing forward, w1 will be opposite to w2 and if they are equal magnitude wT = 0. So:

  • Calculate the rotational velocity at each track
  • Add them together
  • Increment your body angle (rotation) by the total rotational velocity
  • Reset you track velocities so that they stay tangential

Something like this:

rotational_velocity = (v1.length/r - v2.length/r)*(180/pi) //to get into degrees from radians, 

also this will only work if the tracks can't go backwards, otherwise you'll need to use a sine function.

current_angle = current_angle + rotational_velocity // this is the tanks angle

v1.rotate(current_angle) // make it all tangential again
v2.rotate(current_angle)

When you press the button to make a track go you may do something like this:

v1.length = 10
v1.angle = current_angle // assuming you use the vec2d class

Again, I hope that helps.

share|improve this answer
add comment

A Surface is just a graphic - it doesn't have a position on the screen or in the world and so the concept of grouping them doesn't really make any sense. However, Pygame does have Sprites which are basically in-game objects. You need to set their image member to the surface you want and their rect member to the shape of the surface. The internet says that this is a good tutorial for Pygame sprites, so it's worth a look.

However, Pygame uses a software renderer which isn't well suited to performing rotations. To display an image rotated you are expected to create a new rotated copy of the original surface and display that instead. (Via pygame.transform.) This is relatively slow and also tricky to get right (as the dimensions of the returned surfaces will change as you rotate). The speed issue can be solved by performing all your rotations in advance and saving the results. The dimensions issue is a bit trickier but basically involves you positioning sprites/surfaces based on their mid-point rather than their 0,0 corner.

Personally I would probably recommend a different library if rotations form a large part of your game. My preference is pyglet but I'm sure there are others.

As for the actual movement mechanic, if you're happy to either move in a straight line or to rotate around one point, then it's fairly simple. Normally you use 2D vectors for your object positions and movement, which can be translated for linear movement and rotated for turning, but pygame won't help you here. (I've always coded up my own vector classes in Python.) A real tank would probably allow for more variety in the movement by having the treads turn at different speeds, and resolving that correctly is beyond my knowledge (although I expect it could be done quite easily with a physics engine, eg. pymunk).

share|improve this answer
    
Thank you. I'm giving a try at pyglet, i don't know if i'm misinterpreting the functions of pygame, but i don't see the point of missing support for vector manipulation. –  Alan Nov 28 '12 at 12:01
    
pygame is basically a wrapper for SDL, which is a wrapper for media and input/output functions. None of them really deal with geometry or gameplay in any way so vectors aren't part of the package. I don't think pyglet gives you vectors either to be honest. –  Kylotan Nov 28 '12 at 12:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.