problem with exam task for college

Chris Angelico rosuav at gmail.com
Fri Jan 4 13:59:06 EST 2013


On Sat, Jan 5, 2013 at 5:23 AM,  <jeltedeproft at hotmail.com> wrote:
> hy everyone, for my exam this year i had to write a computer game on vpython (visualpython).we had to make a lunar lander game where the ship drops by gravity and is able to manouver to safely land on the moon.<br>right now i am completely stuck on trying to make the visual of the ship rotate.<br>i'm new to this forum, i guess i'll just paste my code here. Everything works fine on the game, except the rotation of the ship. however the when you press "up" the after rotating the velocity actually changes direction , but the visual doesn't. i'm getting kinda nervous because due date is coming, any help is appreciated, here is the code:

Ha, I remember playing a game along those lines that was drawn in pure
ASCII text... the visuals change, the concept doesn't :)

>         self.brandstofmeter = brandstofmeter()
>         self.ruimteschip = ruimteschip()

I'm having trouble understanding these names, and am guessing they're
either aggressively abbreviated or not English, but it's hard to tell
which. It's conventional in Python code to capitalize separate words
in class names, and to either capitalize or use underscores (more
usually the latter) between words in instance variables and method
names. Google tells me that brandstofmeter might mean "Babylon 9" and
ruimteschip is German for "spaceship", but that would be more obvious
if I were not trying to figure out what "brands-t-of-meter" might
mean.

>             self.brandstofmeter.update
>             self.ruimteschip.update(dt)

But I'm guessing that this right here is your problem. The second
update method is called, but the first one isn't. Python evaluates the
name, then does nothing with it. Unlike in BASIC, Python allows you to
manipulate functions just like you do integers and strings, so
actually _calling_ a function requires the parentheses, empty if you
don't need any args:

self.brandstofmeter.update()

>         for i in random.sample(range (-250,250),20):
>             for j in random.sample(range (-375,375),20):
>                 sterren = points(pos = [i,j,0],size = 2, color=color.white)

Without seeing the definition of points() anywhere, I can't say
whether this is effective or not; or is that something from module
visual? This is where "from x import *" can quickly get confusing -
it's not obvious whether the name 'points' has come from your code or
someone else's.

The result is being put into sterren and then ignored. You can happily
omit this if you don't need that return value; Python will quietly do
nothing with it:

points(pos = [i,j,0],size = 2, color=color.white)

> class brandstofmeter_view(object):
>     def __init__(self,owner):
>         self.owner = owner
>         meter = box(pos = owner.pos,size = owner.size,color = color.green)
>
>     def update (self,owner):
>         self.size = self.size - (0,0.45)

Is self.size set anywhere? You may find that, when you fix the above
problem with this method not being called, it begins bombing. What
data type is self.size supposed to be? If it's a tuple, this won't
work, and you'll need to do something like:

self.size = (self.size[0], self.size[1]-0.45)

Or alternatively, use a mutable type such as a list. (Possibly the
vector that you use elsewhere will do. I'm not familiar with that
class, so it may be that you can subtract a tuple from it.)

>     def update(self,dt):
>         self.velocity = self.velocity + (self.acceleration * dt)
>         self.pos = self.pos + self.velocity * dt

Tip: Use augmented assignment for these sorts of statements. It's
clearer what you're doing:

self.velocity += self.acceleration * dt
self.pos += self.velocity * dt

Your class structure feels overengineered, to me. The
model/view/controller system is overkill in most of the places it's
used. Lots of your code is just passing information around from place
to place; instead of the separate _view class, you could simply have a
class ruimteschip that knows how to draw itself.

Final comment: Your code is fairly well laid out, and your query is
clear. Thanks! It's so much easier to read than something that's vague
about what's actually wrong :) Just one thing. Your subject line
doesn't actually say what's going on (though again, your honesty about
it being an exam task is appreciated); something describing the
problem would have been helpful. But that's fairly minor. Your post is
a joy to answer. Thanks!

Chris Angelico



More information about the Python-list mailing list