python - pygame issue: sprite running over trees -


I'm making a game where you can find an area, always random & amp; The computer generated player, which is a phantom, is supposed to be run on the grass block only, and it should not run in the trees. In other words, the player can only run on the grass.

However, despite a few hours of debugging, the player can still walk on the trees, which is quite upset.

Clock = pygame.time.Clock () create_world () # Make a map of the world while walking: clock.tick (30) hit = pygame.sprite.spritecollide (player, block, wrong) location = player. Correct.centerx # Tree # Player is defined as the place of return in the case of the player. Player example, Block is a block of all block # blocks. Block class is a feature called 'type'; Grass = 'block' for tree in Pygame.event.get () # tree = 'tree': If E. Type == pygame.QUIT: run = wrong if E. Type == pygame.KEYDOWN: if e.key = = Pygame.K_UP: speed = [-30, 0] #hit [0]. Type == Tree: #Hit Pe peer player. Correct.centerx = location ... if APIP = = Pygame.KEYUP: reset_speed () # Speed ​​= [0, 0], to prevent the steady movement player. Update (speed) # player reset_speed update () # not really useful render_world () # world blit & amp; Player pygame.quit ()

if The object should work, but it does not work. Why? Any help is appreciated.

Therefore, in a loop, before handling standard handling to help yourself, then Take care of the collision, in this way you are private to both the situation and the collision.

As the player in your current loop can kill the tree but it is going to the same place.

Read through the loop step through the phase and try it:

For example

  loop en: hit return [] location (posn) Speed ​​is set to set player (posN + 1) loopN + 1: hit return [tree] is set to position (posN + 1) speed set player is reset to "location", but it only (posN + 1) Player moves (posN + 2)  

and so on and so on. Hopefully you can see that this work is not going to be just as expected, I can dig the old phrase about the computer, always right and not what

solution :

Consider writing it a little bit and dealing with the first movement, then examine the second collision inside the loop and it will be easy to understand.


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -