0
\$\begingroup\$

I'm making a racing game in python and I kind of wrote myself into a corner. This for loop:

 for i in objects:     
     if i.name == "road":
         if i.y == 0:
             road = Road(road_x,-20,road_width,20)
             objects.append(road)

is how I generate new road tiles as the old ones move down the screen. When a block spawns 20 pixels offscreen, it falls down until its top left corner hits 0 and then a new block spawns above it. if you run this code: https://github.com/hailfire006/economy_game/blob/master/racing%20game you can see that it works great, right up until the player accelerates by pressing up or "w", at which point the road stops spawning.

What I think is happening is that the road is skipping the 0 mark and that's why no new road is spawning, but increasing the acceleration in the code doesn't seem to mess it up, it's only when it's changed dynamically in game. Can soneone explain what's happening here because nothing I try is working and I've been stuck on this for days...

\$\endgroup\$
0
\$\begingroup\$

You're currently checking if i.y == 0 and only creating the new road section if this is true. If you accelerate and the roads stop lining up perfectly with the bounds of the world, this statement will never be true because the y value will jump from positive to negative without hitting exactly zero. Try i.y<=0; this way, even if the road stops lining up perfectly, you will still create new road tiles when one goes off screen.

Keep in mind, though, that if you create the road tiles at a set position, there will be unsightly spaces in between tiles when you accelerate. To remedy this, create the new tile a set y-distance away from the deleted tile.

\$\endgroup\$
  • \$\begingroup\$ but i.y<=0 would make new road tiles as long as there are tiles offscreen. I tried it and it crashed the game. How can I set the condition so that every time a single road tile passes 0 even if it jumps over 0 only one new road tile is made? \$\endgroup\$ – user3150635 Aug 19 '15 at 0:18
  • \$\begingroup\$ You need to do one of two things: either delete tiles which go offscreen or add a boolean variable in the Road object which starts off and is turned on when a new Road is created. Your if statement would then also check if i.hasMadeNewRoad (or whatever you want to call it) is true and, if so, set it to false and make the new road. \$\endgroup\$ – Ryan Aug 19 '15 at 0:55
  • \$\begingroup\$ that's it! I need to set a check to make sure I'm not putting road on top of one another, and then your if condition works! Thanks, this is solved now. \$\endgroup\$ – user3150635 Aug 19 '15 at 1:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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