Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The reason the sprites sometimes become immobile after colliding is because of this code here:</p> <pre><code>pacman_velocity[0] = randint(-1,1) pacman_velocity[1] = randint(-1,1) </code></pre> <p>randint for that range means you're getting an integer value that is either -1, 0, or 1.<br> From the docs: (<a href="http://docs.python.org/2/library/random.html" rel="nofollow">http://docs.python.org/2/library/random.html</a>)</p> <pre><code>random.randint(a, b) Return a random integer N such that a &lt;= N &lt;= b </code></pre> <p>That means that (theoretically) each time there is a collision, there is a 1/3 chance that the x component of the velocity will be 0, and a 1/3 chance that the y component of the velocity will be zero. If you only want to select either 1 or -1, try the random.choice() function:</p> <pre><code>pacman_velocity[0] = random.choice((-1, 1)) pacman_velocity[1] = random.choice((-1, 1)) </code></pre> <p>If you wanted to include 0 as a possibility, but want to reduce its frequency, you could accomplish that with something like this:</p> <pre><code>pacman_velocity[0] = random.choice((-1, -1, -1, 0, 1, 1, 1)) pacman_velocity[0] = random.choice((-1, -1, -1, 0, 1, 1, 1)) </code></pre> <p>The problem with this loop you tried:</p> <pre><code>For sprite in sprite_list(): sprite.left &lt; 0 or sprite.right &gt; WIDTH: sprite_velocity[1] = -1 * sprite_velocity[1] </code></pre> <p>is that you are checking to see if the sprite has passed the left or right boundary of the screen, but then instead of reversing the x component of the sprite's velocity, you are reversing the y component of the velocity. Also, you don't have an <code>if</code> at the beginning of the condition. Try this:</p> <pre><code>For sprite in sprite_list: if sprite.left &lt; 0 or sprite.right &gt; WIDTH: sprite_velocity[0] = -1 * sprite_velocity[0] if sprite.top &lt; 0 or sprite.bottom &gt; HEIGHT: sprite_velocity[1] = -1 * sprite_velocity[1] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload