Processing Game Help
Peter Otten
__peter__ at web.de
Mon Dec 11 16:04:17 EST 2017
Chris Angelico wrote:
> On Tue, Dec 12, 2017 at 5:46 AM, Peter Otten <__peter__ at web.de> wrote:
>> You can test for a collision and exit if an asteroid is close to a
>> spaceship with
>>
>> for s in spaceship:
>> for a in asteroid:
>> if calculate_distance(s, a) < 62.5:
>> print("One of your ships was hit by an asteroid")
>> exit()
>>
>> There are two loops because you want to test every asteroid for every
>> spaceship (although you only have one at the moment).
>
> Then shouldn't they be "spaceships" and "asteroids" in the plural?
Yes.
(I started out with
for spaceship in spacehips:
for asteroid in asteroids:
if distance(spaceship, asteroid) < 62.5:
...
but then decided that while better in principle this slope was too slippery
for me, and, after only a few minor changes, the OP would no longer
recognize her script...)
> It
> looks wrong to iterate over a singular asteroid. (And I'd prefer to
> iterate over a collection of spaceships in the plural even if I know
> it's a list of one, though I can understand that a list of one is a
> tricky case for some people.)
Here's another line
aX = random.randint(0, 640)
that "hurts" almost as much as the missing plural.
There are other naming problems (did you see the Spaceship class, the
drawing methods?). If you write code for some time you develop a set of
conventions that help you avoid certain errors, and allows others with
similar experience to do sanity checks quickly.
However, the script mostly works, has some nice graphics and responds to
user input. That's quite an achievement.
More information about the Python-list
mailing list