problem with exam task for college


J

jeltedeproft

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 bygravity and is able to manouver to safely land on the moon.<br>right now iam 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 worksfine 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:

from visual import *
import time
import math
import random
from datetime import datetime
import operator


class lunar_lander(object):
def __init__(self):
scene.title = 'mini star wars'
scene.width = 375
scene.height = 550
scene.center = (0,0)
self.pos = (0,0)
self.axis = 0
self.brandstofmeter = brandstofmeter()
self.ruimteschip = ruimteschip()
self.view = game_view(self)



def play(self):
t=0
dt=0.01
while t<999999999:
time.sleep(0.01)
self.brandstofmeter.update
self.ruimteschip.update(dt)
t = t + dt



class game_view(object):
def __init__(self,owner):
autoscale=True
box(pos=( 0, -375, 0), length=500, height=5, width=0, color= color.white)
box(pos=(0,375, 0), length=500, height=5, width=0, color = color.white)
box(pos=(-250,0, 0), length=5, height=750, width=0, color = color.white)
box(pos=(250,0, 0), length=5, height=750, width=0, color = color.white)

maan = curve(pos=[(-250,-353),(-240,-341),(-210,-354),(-199.5,-374)],color=color.red)
maana = curve(pos=[(-199.5,-374),(-166,-374)],color=color.green)
maanb = curve(pos=[(-166,-374),(-140,-357),(-80,-319),(-40,-361),(0,-321),(40,-329),(80,-347)],color=color.red)
maanc = curve(pos=[(80,-347),(140,-347)],color=color.green)
maand = curve(pos=[(140,-347),(162,-337),(189.5,-365),(210,-355),(240,-372),(250,-338)],color=color.red)

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)


class brandstofmeter(object):
def __init__(self):
self.size = (25,45)
axis = 0
self.pos = (220,345)
self.view = brandstofmeter_view(self)

def update(self):
while True:
if scene.kb.keys:
s = scene.kb.getkey()
if (s == 'up'):
self.view.update(self)




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)




class ruimteschip(object):
def __init__(self):
self.pos = vector(0,330)
self.acceleration = vector(0,-88,0)
self.axis = (1,0,0)
self.hoek = (90*math.pi)/180
self.graden = math.degrees(self.hoek)
self.gas = vector(10 * cos(self.hoek),10 * sin (self.hoek))
self.velocity = vector(0,0,0)
self.angle = (1,0,0)
self.view = ruimteschip_view(self)
self.vlam = self.view.vlam
self.frame = self.view.frame





def update(self,dt):
self.velocity = self.velocity + (self.acceleration * dt)
self.pos = self.pos + self.velocity * dt
a = 0
b = 0
if scene.kb.keys:
a = a + 0.001
s = scene.kb.getkey()
if (s == "up"):
self.velocity = self.velocity + self.gas
self.vlam.visible = True
b = b + 2
if (s == "left"):
self.gas = rotate(self.gas,angle = math.pi/10, axis =(0,0,1))
c = list(self.frame.axis)
c[2] -= 0.1
c = tuple(c)
c = self.frame.axis
if (s == "right") :
self.gas = rotate(self.gas,angle = -(math.pi/10), axis = (0,0,1))
c = list(self.frame.axis)
c[2] += 0.1
c = tuple(c)
c = self.frame.axis
if (a == 0):
self.vlam.visible = False


if self.pos.x > 250:
self.pos.x = -250
if self.pos.x < -250:
self.pos.x = 250
self.view.update(self)

class ruimteschip_view(object):
def __init__(self,owner):
self.owner = owner
self.frame = frame(pos = owner.pos,axis = owner.axis)
self.motor = curve(frame = self.frame,pos=[(0,24.0),(22.0,24.0),(22.0,39.0),(-22.0,39.0),(-22,24),(0,24)])
self.capsule = curve(frame = self.frame,color = color.blue ,pos=[(0,39),(-3,39),(-9,44),(-12,46),(-18,48),(-22,50),(-18,52),(-12,54),(-9,56),(-3,61),(0,61),(3,59),(9,56),(12,54),(18,52),(22,50),(18,48),(12,46),(9,44),(3,39),(0,39)])
self.poota = curve(frame = self.frame,pos = [(-18,24),(-20,24),(-20,0),(-18,0),(-18,24)])
self.pootb = curve(frame = self.frame,pos = [(18,24),(20,24),(20,0),(18,0),(18,24)])
self.vlam = curve(frame = self.frame,color = color.orange , visible=false,pos = [(0,24.0),(-9.0,14.0),(0,-5.0),(9,14.0),(0,24.0)])

def update(self,owner):
self.frame.axis = owner.axis
self.frame.pos = owner.pos
 
Ad

Advertisements

C

Chris Angelico

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 nowi 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 , butthe 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
 
C

Chris Angelico

Google tells me that brandstofmeter might mean "Babylon 9"

And by the way, in case it didn't come across, I'm jesting there. What
I mean is that Google didn't have any useful and obvious results
indicating what this actually means. But I'm guessing it's a fuel or
altitude gauge.

ChrisA
 
M

MRAB

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


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.
[snip]
Google Translate says it's Dutch:

ruimteschip -> spaceship
brandstofmeter -> fuel gauge
 
J

Joshua Landau

And by the way, in case it didn't come across, I'm jesting there. What
I mean is that Google didn't have any useful and obvious results
indicating what this actually means. But I'm guessing it's a fuel or
altitude gauge.

It might measure a brand in femtometers ;).

But, seriously, it's Dutch for Fuel Gauge. Google told me, in case you
think I know Dutch, but it's in the Python Spirit either way.

ruimteschip -> Spaceship
hoek -> angle
sterren -> stars
poot -> leg
vlam -> flame
graden -> degrees
maan -> moon

I'd say they'd be good names if you're in the Netherlands.
 
C

Chris Angelico

It might measure a brand in femtometers ;).
LOL!

But, seriously, it's Dutch for Fuel Gauge. Google told me, in case you think
I know Dutch, but it's in the Python Spirit either way.

ruimteschip -> Spaceship
hoek -> angle
sterren -> stars
poot -> leg
vlam -> flame
graden -> degrees
maan -> moon

I'd say they'd be good names if you're in the Netherlands.

Yep, I'd agree, those are fine names. I still would expect to see the
class names in uppercase though; a single leading capital letter
strongly suggests that it's a single-word name, where all-lowercase
could just be rammed up together non-delimited. But yeah, that's a
pretty minor point. Those name are well suited to their tasks. (And
quite a few of them can be figured out from context without even
turning to the translator, like hoek == angle.)

ChrisA
 
Ad

Advertisements

J

jeltedeproft

woow jeezes, thanks for the amazingly fast and detailed response, you guys are amazing.



let's clear a few things up :

1) points is a module in vpython to make points, the points do in fact appear although its not really the effect i intended to have, the points are "lined up" in stead of randomly separated, if you know what i mean

2) sorry for the dutch names :)

3) i put the parenteces there and then i got a bunch of errors which i fixed






2 problems:

a) my program seems to be stuck in the update phase of brandstoftank(=fuel tank), it doesnt get to the update of the spaceship

b) the problem i originally described in my first post was not resolved, by means of elimination i could conclude that the problem situates itself in this lines :

if (s == "left"):
self.gas = rotate(self.gas,angle = math.pi/10, axis = (0,0,1))
c = list(self.frame.axis)
c[2] -= 0.1
c = tuple(c)
c = self.frame.axis
if (s == "right") :
self.gas = rotate(self.gas,angle = -(math.pi/10), axis = (0,0,1))
c = list(self.frame.axis)
c[2] += 0.1
c = tuple(c)
c = self.frame.axis

i will put the full revisited code here :


from visual import *
import time
import math
import random
from datetime import datetime
import operator


class lunar_lander(object):
def __init__(self):
scene.title = 'mini star wars'
scene.width = 375
scene.height = 550
scene.center = (0,0)
self.pos = (0,0)
self.axis = 0
self.brandstofmeter_view = brandstofmeter_view()
self.ruimteschip = ruimteschip()
self.view = game_view(self)



def play(self):
t=0
dt=0.01
while t<999999999:
time.sleep(0.01)
self.brandstofmeter_view.update()
self.ruimteschip.update(dt)
t = t + dt



class game_view(object):
def __init__(self,owner):
autoscale=True
box(pos=( 0, -375, 0), length=500, height=5, width=0, color = color.white)
box(pos=(0,375, 0), length=500, height=5, width=0, color = color.white)
box(pos=(-250,0, 0), length=5, height=750, width=0, color = color.white)
box(pos=(250,0, 0), length=5, height=750, width=0, color = color.white)

maan = curve(pos=[(-250,-353),(-240,-341),(-210,-354),(-199.5,-374)],color=color.red)
maana = curve(pos=[(-199.5,-374),(-166,-374)],color=color.green)
maanb = curve(pos=[(-166,-374),(-140,-357),(-80,-319),(-40,-361),(0,-321),(40,-329),(80,-347)],color=color.red)
maanc = curve(pos=[(80,-347),(140,-347)],color=color.green)
maand = curve(pos=[(140,-347),(162,-337),(189.5,-365),(210,-355),(240,-372),(250,-338)],color=color.red)

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)




class brandstofmeter_view(object):
def __init__(self):
axis = 0
self.pos = (220,345)
self.meter = box(pos = self.pos, lenght = 25, height = 45,color = color.green)

def update (self):
s = scene.kb.getkey()
if (s == "up"):
self.meter.height = self.meter.height - 1





class ruimteschip(object):
def __init__(self):
self.pos = vector(0,330)
self.acceleration = vector(0,-88,0)
self.axis = (1,0,0)
self.hoek = (90*math.pi)/180
self.graden = math.degrees(self.hoek)
self.gas = vector(10 * cos(self.hoek),10 * sin (self.hoek))
self.velocity = vector(0,0,0)
self.angle = (1,0,0)
self.view = ruimteschip_view(self)
self.vlam = self.view.vlam
self.frame = self.view.frame





def update(self,dt):
self.velocity = self.velocity + (self.acceleration * dt)
self.pos = self.pos + self.velocity * dt
a = 0
b = 0
if scene.kb.keys:
a = a + 0.001
s = scene.kb.getkey()
if (s == "up"):
self.velocity = self.velocity + self.gas
self.vlam.visible = True
b = b + 2
if (s == "left"):
self.gas = rotate(self.gas,angle = math.pi/10, axis = (0,0,1))
c = list(self.frame.axis)
c[2] -= 0.1
c = tuple(c)
c = self.frame.axis
if (s == "right") :
self.gas = rotate(self.gas,angle = -(math.pi/10), axis = (0,0,1))
c = list(self.frame.axis)
c[2] += 0.1
c = tuple(c)
c = self.frame.axis
if (a == 0):
self.vlam.visible = False


if self.pos.x > 250:
self.pos.x = -250
if self.pos.x < -250:
self.pos.x = 250
self.view.update(self)

class ruimteschip_view(object):
def __init__(self,owner):
self.owner = owner
self.frame = frame(pos = owner.pos,axis = owner.axis)
self.motor = curve(frame = self.frame,pos=[(0,24.0),(22.0,24.0),(22.0,39.0),(-22.0,39.0),(-22,24),(0,24)])
self.capsule = curve(frame = self.frame,color = color.blue ,pos=[(0,39),(-3,39),(-9,44),(-12,46),(-18,48),(-22,50),(-18,52),(-12,54),(-9,56),(-3,61),(0,61),(3,59),(9,56),(12,54),(18,52),(22,50),(18,48),(12,46),(9,44),(3,39),(0,39)])
self.poota = curve(frame = self.frame,pos = [(-18,24),(-20,24),(-20,0),(-18,0),(-18,24)])
self.pootb = curve(frame = self.frame,pos = [(18,24),(20,24),(20,0),(18,0),(18,24)])
self.vlam = curve(frame = self.frame,color = color.orange , visible=false,pos = [(0,24.0),(-9.0,14.0),(0,-5.0),(9,14.0),(0,24.0)])

def update(self,owner):
self.frame.axis = owner.axis
self.frame.pos = owner.pos



thanks again, are you guys getting paid for this or is this voluntarily? either way i really appreciate it
 
J

jeltedeproft

woow jeezes, thanks for the amazingly fast and detailed response, you guys are amazing.



let's clear a few things up :

1) points is a module in vpython to make points, the points do in fact appear although its not really the effect i intended to have, the points are "lined up" in stead of randomly separated, if you know what i mean

2) sorry for the dutch names :)

3) i put the parenteces there and then i got a bunch of errors which i fixed






2 problems:

a) my program seems to be stuck in the update phase of brandstoftank(=fuel tank), it doesnt get to the update of the spaceship

b) the problem i originally described in my first post was not resolved, by means of elimination i could conclude that the problem situates itself in this lines :

if (s == "left"):
self.gas = rotate(self.gas,angle = math.pi/10, axis = (0,0,1))
c = list(self.frame.axis)
c[2] -= 0.1
c = tuple(c)
c = self.frame.axis
if (s == "right") :
self.gas = rotate(self.gas,angle = -(math.pi/10), axis = (0,0,1))
c = list(self.frame.axis)
c[2] += 0.1
c = tuple(c)
c = self.frame.axis

i will put the full revisited code here :


from visual import *
import time
import math
import random
from datetime import datetime
import operator


class lunar_lander(object):
def __init__(self):
scene.title = 'mini star wars'
scene.width = 375
scene.height = 550
scene.center = (0,0)
self.pos = (0,0)
self.axis = 0
self.brandstofmeter_view = brandstofmeter_view()
self.ruimteschip = ruimteschip()
self.view = game_view(self)



def play(self):
t=0
dt=0.01
while t<999999999:
time.sleep(0.01)
self.brandstofmeter_view.update()
self.ruimteschip.update(dt)
t = t + dt



class game_view(object):
def __init__(self,owner):
autoscale=True
box(pos=( 0, -375, 0), length=500, height=5, width=0, color = color.white)
box(pos=(0,375, 0), length=500, height=5, width=0, color = color.white)
box(pos=(-250,0, 0), length=5, height=750, width=0, color = color.white)
box(pos=(250,0, 0), length=5, height=750, width=0, color = color.white)

maan = curve(pos=[(-250,-353),(-240,-341),(-210,-354),(-199.5,-374)],color=color.red)
maana = curve(pos=[(-199.5,-374),(-166,-374)],color=color.green)
maanb = curve(pos=[(-166,-374),(-140,-357),(-80,-319),(-40,-361),(0,-321),(40,-329),(80,-347)],color=color.red)
maanc = curve(pos=[(80,-347),(140,-347)],color=color.green)
maand = curve(pos=[(140,-347),(162,-337),(189.5,-365),(210,-355),(240,-372),(250,-338)],color=color.red)

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)




class brandstofmeter_view(object):
def __init__(self):
axis = 0
self.pos = (220,345)
self.meter = box(pos = self.pos, lenght = 25, height = 45,color = color.green)

def update (self):
s = scene.kb.getkey()
if (s == "up"):
self.meter.height = self.meter.height - 1





class ruimteschip(object):
def __init__(self):
self.pos = vector(0,330)
self.acceleration = vector(0,-88,0)
self.axis = (1,0,0)
self.hoek = (90*math.pi)/180
self.graden = math.degrees(self.hoek)
self.gas = vector(10 * cos(self.hoek),10 * sin (self.hoek))
self.velocity = vector(0,0,0)
self.angle = (1,0,0)
self.view = ruimteschip_view(self)
self.vlam = self.view.vlam
self.frame = self.view.frame





def update(self,dt):
self.velocity = self.velocity + (self.acceleration * dt)
self.pos = self.pos + self.velocity * dt
a = 0
b = 0
if scene.kb.keys:
a = a + 0.001
s = scene.kb.getkey()
if (s == "up"):
self.velocity = self.velocity + self.gas
self.vlam.visible = True
b = b + 2
if (s == "left"):
self.gas = rotate(self.gas,angle = math.pi/10, axis = (0,0,1))
c = list(self.frame.axis)
c[2] -= 0.1
c = tuple(c)
c = self.frame.axis
if (s == "right") :
self.gas = rotate(self.gas,angle = -(math.pi/10), axis = (0,0,1))
c = list(self.frame.axis)
c[2] += 0.1
c = tuple(c)
c = self.frame.axis
if (a == 0):
self.vlam.visible = False


if self.pos.x > 250:
self.pos.x = -250
if self.pos.x < -250:
self.pos.x = 250
self.view.update(self)

class ruimteschip_view(object):
def __init__(self,owner):
self.owner = owner
self.frame = frame(pos = owner.pos,axis = owner.axis)
self.motor = curve(frame = self.frame,pos=[(0,24.0),(22.0,24.0),(22.0,39.0),(-22.0,39.0),(-22,24),(0,24)])
self.capsule = curve(frame = self.frame,color = color.blue ,pos=[(0,39),(-3,39),(-9,44),(-12,46),(-18,48),(-22,50),(-18,52),(-12,54),(-9,56),(-3,61),(0,61),(3,59),(9,56),(12,54),(18,52),(22,50),(18,48),(12,46),(9,44),(3,39),(0,39)])
self.poota = curve(frame = self.frame,pos = [(-18,24),(-20,24),(-20,0),(-18,0),(-18,24)])
self.pootb = curve(frame = self.frame,pos = [(18,24),(20,24),(20,0),(18,0),(18,24)])
self.vlam = curve(frame = self.frame,color = color.orange , visible=false,pos = [(0,24.0),(-9.0,14.0),(0,-5.0),(9,14.0),(0,24.0)])

def update(self,owner):
self.frame.axis = owner.axis
self.frame.pos = owner.pos



thanks again, are you guys getting paid for this or is this voluntarily? either way i really appreciate it
 
D

Dennis Lee Bieber

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:

Except for the initial de-orbit burn, the lunar landers didn't
really rotate during the descent phase. The main engine was used to
control the fall from gravity (vertical speed), and maneuvering
thrusters on all sides were used to "slide" the LEM horizontally (and
somewhat vertically, until actually "stopping" the descent to land). One
reason is that "riding the main engine" down (rotating) would result in
being unable to see the landing point -- instead of "hovering" on the
main jet while traversing for the best flat spot.

Instead of rotating the LEM to fire X-lbs of fuel at, say, a 45
degree angle, they'd fire X/Y1 vertically, and X/Y2 horizontally, where
the Y1&Y2 values result in a vector sum equivalent to X at 45 degrees.

Most all of the graphical lunar lander games I've seen start with
the LEM in the vertical/upright position and only control descent and
horizontal velocity.

My college version (all text in those days, no graphics at all) had
one minor bug... No check was made on input values; one could enter:

burn -100 lbs of fuel
at 180 degrees (0 degrees was straight down)...

The result: one gained fuel while still slowing the ship. I used to
crash with more fuel than I started out with. The game scored by how
close to the designated target sight one came, and how hard the impact
was... I once managed a near perfect accuracy score, with a low vertical
impact. Problem -- in real life I'd have left the landing gear some 30
miles away... My vertical rate was measured in inches per hour, but the
horizontal rate started in miles per hour... I SLID into the landing
zone!
 
J

jeltedeproft

hy again,thanx, i updated my code with your more efficient approach :), so that possibly resolves problem number 2, but still leaves me with problem n°1, my code is still stuck in the first update of the fuel tank (brandstoftank), for the sake of your easyness i'll paste the code again

from visual import *
import time
import math
import random
from datetime import datetime
import operator


class lunar_lander(object):
def __init__(self):
scene.title = 'mini star wars'
scene.width = 375
scene.height = 550
scene.center = (0,0)
self.pos = (0,0)
self.axis = 0
self.brandstofmeter_view = brandstofmeter_view()
self.ruimteschip = ruimteschip()
self.view = game_view(self)



def play(self):
t=0
dt=0.01
while t<999999999:
time.sleep(0.01)
self.brandstofmeter_view.update()
self.ruimteschip.update(dt)
t = t + dt



class game_view(object):
def __init__(self,owner):
autoscale=True
box(pos=( 0, -375, 0), length=500, height=5, width=0, color= color.white)
box(pos=(0,375, 0), length=500, height=5, width=0, color = color.white)
box(pos=(-250,0, 0), length=5, height=750, width=0, color = color.white)
box(pos=(250,0, 0), length=5, height=750, width=0, color = color.white)

maan = curve(pos=[(-250,-353),(-240,-341),(-210,-354),(-199.5,-374)],color=color.red)
maana = curve(pos=[(-199.5,-374),(-166,-374)],color=color.green)
maanb = curve(pos=[(-166,-374),(-140,-357),(-80,-319),(-40,-361),(0,-321),(40,-329),(80,-347)],color=color.red)
maanc = curve(pos=[(80,-347),(140,-347)],color=color.green)
maand = curve(pos=[(140,-347),(162,-337),(189.5,-365),(210,-355),(240,-372),(250,-338)],color=color.red)

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)




class brandstofmeter_view(object):
def __init__(self):
axis = 0
self.pos = (220,345)
self.meter = box(pos = self.pos, lenght = 25, height = 45,color = color.green)

def update (self):
s = scene.kb.getkey()
if (s == "up"):
self.meter.height = self.meter.height - 1





class ruimteschip(object):
def __init__(self):
self.pos = vector(0,330)
self.acceleration = vector(0,-88,0)
self.axis = (1,0,0)
self.hoek = (90*math.pi)/180
self.graden = math.degrees(self.hoek)
self.gas = vector(10 * cos(self.hoek),10 * sin (self.hoek))
self.velocity = vector(0,0,0)
self.angle = (1,0,0)
self.view = ruimteschip_view(self)
self.vlam = self.view.vlam
self.frame = self.view.frame





def update(self,dt):
self.velocity = self.velocity + (self.acceleration * dt)
self.pos = self.pos + self.velocity * dt
a = 0
b = 0
if scene.kb.keys:
a = a + 0.001
s = scene.kb.getkey()
if (s == "up"):
self.velocity = self.velocity + self.gas
self.vlam.visible = True
b = b + 2
if (s == "left"):
self.gas = rotate(self.gas,angle = math.pi/10, axis =(0,0,1))
(x,y,z) = self.frame.axis
self.frame.axis = (x,y,z-0.1)
if (s == "right") :
self.gas = rotate(self.gas,angle = -(math.pi/10), axis = (0,0,1))
(x,y,z) = self.frame.axis
self.frame.axis = (x,y,z+0.1)
if (a == 0):
self.vlam.visible = False


if self.pos.x > 250:
self.pos.x = -250
if self.pos.x < -250:
self.pos.x = 250
self.view.update(self)

class ruimteschip_view(object):
def __init__(self,owner):
self.owner = owner
self.frame = frame(pos = owner.pos,axis = owner.axis)
self.motor = curve(frame = self.frame,pos=[(0,24.0),(22.0,24.0),(22.0,39.0),(-22.0,39.0),(-22,24),(0,24)])
self.capsule = curve(frame = self.frame,color = color.blue ,pos=[(0,39),(-3,39),(-9,44),(-12,46),(-18,48),(-22,50),(-18,52),(-12,54),(-9,56),(-3,61),(0,61),(3,59),(9,56),(12,54),(18,52),(22,50),(18,48),(12,46),(9,44),(3,39),(0,39)])
self.poota = curve(frame = self.frame,pos = [(-18,24),(-20,24),(-20,0),(-18,0),(-18,24)])
self.pootb = curve(frame = self.frame,pos = [(18,24),(20,24),(20,0),(18,0),(18,24)])
self.vlam = curve(frame = self.frame,color = color.orange , visible=false,pos = [(0,24.0),(-9.0,14.0),(0,-5.0),(9,14.0),(0,24.0)])

def update(self,owner):
self.frame.axis = owner.axis
self.frame.pos = owner.pos
 
C

Chris Angelico

hy again,thanx, i updated my code with your more efficient approach :), so that possibly resolves problem number 2, but still leaves me with problemn°1, my code is still stuck in the first update of the fuel tank (brandstoftank), for the sake of your easyness i'll paste the code again

Not sure what you mean by "stuck", but is it that scene.kb.getkey()
waits for a key?

ChrisA
 
Ad

Advertisements

C

Chris Angelico

that's probably it, how do i solve it?

I'm afraid I can't help you there. Check out the docs for the getkey
function and see if it can be put into non-blocking mode; if not, you
may have to completely change your model. For instance, if I were
writing this sort of game today, I'd probably use a GUI system that
fires an event whenever a key is pressed/released, and then have a
timer-tick for the descent, rather than explicit sleep() calls. But it
depends on your UI facilities.

The LANDER.BAS that I played with - yes, it was written in BASIC - was
fully prompt-driven. To move to the next time unit, you had to answer
all its prompts. Advantage: No need to code a Pause feature.
Disadvantage: The game ended up way too scientific, like D&D combat.
(It's amazing how your party of bumbling characters suddenly become
lightning-fast strategists as soon as initiative is rolled!)

ChrisA
 
D

Dennis Lee Bieber

c = list(self.frame.axis)
c[2] -= 0.1
c = tuple(c)
c = self.frame.axis

I suspect you want

self.frame.axis = c
if (s == "right") :
self.gas = rotate(self.gas,angle = -(math.pi/10), axis = (0,0,1))
c = list(self.frame.axis)
c[2] += 0.1
c = tuple(c)
c = self.frame.axis
Note that you have a lot of repeated code that could be put outside
the if-block...
 
J

jeltedeproft

ok after another round of reparations, my update works again and it updatesthe fuel meter, but i still can't get the view of the spaceship to rotate,for now only the direction the spaceship accelerates when pressing "up" changes after a rotation, but the spaceship itself keeps pointing up. This isthe code for the rotating : p.s : the problem has to be in this code because the update of the view of the position of the spaceship does work.


def update(self,dt):
self.velocity = self.velocity + (self.acceleration * dt)
self.pos = self.pos + self.velocity * dt
a = 0
b = 0
if scene.kb.keys:
a = a + 0.001
s = scene.kb.getkey()
if (s == "up"):
if self.zicht.meter.height != 0:
self.velocity = self.velocity + self.gas
self.vlam.visible = True
b = b + 2
self.zicht.meter.height = self.zicht.meter.height - 0..1
self.zicht.update
if (s == "left"):
self.gas = rotate(self.gas,angle = math.pi/10, axis =(0,0,1))
(x,y,z) = self.frame.axis
self.frame.axis = (x,y,z-0.1)
if (s == "right") :
self.gas = rotate(self.gas,angle = -(math.pi/10), axis = (0,0,1))
(x,y,z) = self.frame.axis
self.frame.axis = (x,y,z+0.1)
if (a == 0):
self.vlam.visible = False
 
V

Vincent Vande Vyvre

Le 07/01/13 17:22, (e-mail address removed) a écrit :
ok after another round of reparations, my update works again and it updates the fuel meter, but i still can't get the view of the spaceship to rotate, for now only the direction the spaceship accelerates when pressing "up" changes after a rotation, but the spaceship itself keeps pointing up. This is the code for the rotating : p.s : the problem has to be in this code because the update of the view of the position of the spaceship does work.


def update(self,dt):
self.velocity = self.velocity + (self.acceleration * dt)
self.pos = self.pos + self.velocity * dt
a = 0
b = 0
if scene.kb.keys:
a = a + 0.001
s = scene.kb.getkey()
if (s == "up"):
if self.zicht.meter.height != 0:
self.velocity = self.velocity + self.gas
self.vlam.visible = True
b = b + 2
self.zicht.meter.height = self.zicht.meter.height - 0.1
self.zicht.update
if (s == "left"):
self.gas = rotate(self.gas,angle = math.pi/10, axis = (0,0,1))
(x,y,z) = self.frame.axis
self.frame.axis = (x,y,z-0.1)
if (s == "right") :
self.gas = rotate(self.gas,angle = -(math.pi/10), axis = (0,0,1))
(x,y,z) = self.frame.axis
self.frame.axis = (x,y,z+0.1)
if (a == 0):
self.vlam.visible = False
Are you sure with this code:

(x,y,z) = self.frame.axis ?

frame.axis is a 'cvisual.vector' and this unpacking cause a program
crashes with a segfault.

For left-rigth moves of the LEM, this code seems works:

--------------------------------------

if scene.kb.keys:
key = scene.kb.getkey()
if key == "left":
# Set left deviation
self.frame.axis -= (0, 0, 0.05)
self.gas = vector(-sin(self.angle), cos(self.angle))

elif key == "right":
# Set right deviation
self.frame.axis += (0, 0, 0.05)
self.gas = vector(sin(self.angle), cos(self.angle))

elif key == "up":
self.deviate()

def deviate(self):
# Set modified velocity
self.frame.velocity += self.gas
self.frame.pos += self.frame.velocity
# Reset falling velocity
self.frame.velocity -= self.gas
 
Ad

Advertisements

D

Dennis Lee Bieber

ok after another round of reparations, my update works again and it updates the fuel meter, but i still can't get the view of the spaceship to rotate, for now only the direction the spaceship accelerates when pressing "up" changes after a rotation, but the spaceship itself keeps pointing up. This is the code for the rotating : p.s : the problem has to be in this code because the update of the view of the position of the spaceship does work.

So where is the code that draws this "spaceship"
 
J

jeltedeproft_8

sorry for the extremely late reply, i have been very very busy :s

i implemented the solution that was posted in the second to last post. but now it gets a little confusing because i think a couple terms where deleted that i refered to in other code blocks, so once again i''l post the full code here



from visual import *
import time
import math
import random
from datetime import datetime
import operator


class lunar_lander(object):
def __init__(self):
scene.title = 'mini star wars'
scene.width = 375
scene.height = 550
scene.center = (0,0)
self.pos = (0,0)
self.axis = 0
self.brandstofmeter = brandstofmeter()
self.ruimteschip = ruimteschip()
self.view = game_view(self)



def play(self):
t=0
dt=0.01
self.ruimteschip.updatemeter = False
while t<999999999:
time.sleep(0.01)
self.ruimteschip.update(dt)
t = t + dt
if self.ruimteschip.updatemeter == True:
self.brandstofmeter.update





class game_view(object):
def __init__(self,owner):
autoscale=True
box(pos=( 0, -375, 0), length=500, height=5, width=0, color = color.white)
box(pos=(0,375, 0), length=500, height=5, width=0, color = color.white)
box(pos=(-250,0, 0), length=5, height=750, width=0, color = color.white)
box(pos=(250,0, 0), length=5, height=750, width=0, color = color.white)

maan = curve(pos=[(-250,-353),(-240,-341),(-210,-354),(-199.5,-374)],color=color.red)
maana = curve(pos=[(-199.5,-374),(-166,-374)],color=color.green)
maanb = curve(pos=[(-166,-374),(-140,-357),(-80,-319),(-40,-361),(0,-321),(40,-329),(80,-347)],color=color.red)
maanc = curve(pos=[(80,-347),(140,-347)],color=color.green)
maand = curve(pos=[(140,-347),(162,-337),(189.5,-365),(210,-355),(240,-372),(250,-338)],color=color.red)

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)




class brandstofmeter(object):
def __init__(self):
axis = 0
self.pos = (210,345)
self.length = 25
self.height = 45
self.meter = box(pos = self.pos, length = self.length, height = self.height,color = color.green)
def update(self):
self.height = self.height - 0.2
print "ok"




class ruimteschip(object):
def __init__(self):
self.pos = vector(0,330)
self.acceleration = vector(0,-88,0)
self.axis = (1,0,0)
self.hoek = (90*math.pi)/180
self.graden = math.degrees(self.hoek)
self.gas = vector(10 * cos(self.hoek),10 * sin (self.hoek))
self.velocity = vector(0,0,0)
self.angle = pi / 2
self.updatemeter = False
self.view = ruimteschip_view(self)
self.vlam = self.view.vlam
self.frame = self.view.frame
self.zicht = brandstofmeter()



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

if scene.kb.keys:
key = scene.kb.getkey()
if key == "left":
# Set left deviation
self.frame.axis -= (0, 0, 0.05)
self.gas = vector(-sin(self.angle), cos(self.angle))

elif key == "right":
# Set right deviation
self.frame.axis += (0, 0, 0.05)
self.gas = vector(sin(self.angle), cos(self.angle))

elif key == "up":
self.deviate()

def deviate(self):
# Set modified velocity
self.frame.velocity += self.gas
self.frame.pos += self.frame.velocity
# Reset falling velocity
self.frame.velocity -= self.gas



if self.pos.x > 250:
self.pos.x = -250
if self.pos.x < -250:
self.pos.x = 250
self.view.update(self)

class ruimteschip_view(object):
def __init__(self,owner):
self.owner = owner
self.frame = frame(pos = owner.pos,axis = owner.axis)
self.motor = curve(frame = self.frame,pos=[(0,24.0),(22.0,24.0),(22.0,39.0),(-22.0,39.0),(-22,24),(0,24)])
self.capsule = curve(frame = self.frame,color = color.blue ,pos=[(0,39),(-3,39),(-9,44),(-12,46),(-18,48),(-22,50),(-18,52),(-12,54),(-9,56),(-3,61),(0,61),(3,59),(9,56),(12,54),(18,52),(22,50),(18,48),(12,46),(9,44),(3,39),(0,39)])
self.poota = curve(frame = self.frame,pos = [(-18,24),(-20,24),(-20,0),(-18,0),(-18,24)])
self.pootb = curve(frame = self.frame,pos = [(18,24),(20,24),(20,0),(18,0),(18,24)])
self.vlam = curve(frame = self.frame,color = color.orange , visible=false,pos = [(0,24.0),(-9.0,14.0),(0,-5.0),(9,14.0),(0,24.0)])

def update(self,owner):
self.frame.axis = owner.axis
self.frame.pos = owner.pos
 
J

jeltedeproft

corrected a bit, loop works, gas doesn't work


from visual import *
import time
import math
import random
from datetime import datetime
import operator


class lunar_lander(object):
def __init__(self):
scene.title = 'mini star wars'
scene.width = 375
scene.height = 550
scene.center = (0,0)
self.pos = (0,0)
self.axis = 0
self.brandstofmeter = brandstofmeter()
self.ruimteschip = ruimteschip()
self.view = game_view(self)



def play(self):
t=0
dt=0.01
self.ruimteschip.updatemeter = False
while t<999999999:
time.sleep(0.01)
self.ruimteschip.update(dt)
t = t + dt
if self.ruimteschip.updatemeter == True:
self.brandstofmeter.update





class game_view(object):
def __init__(self,owner):
autoscale=True
box(pos=( 0, -375, 0), length=500, height=5, width=0, color = color.white)
box(pos=(0,375, 0), length=500, height=5, width=0, color = color.white)
box(pos=(-250,0, 0), length=5, height=750, width=0, color = color.white)
box(pos=(250,0, 0), length=5, height=750, width=0, color = color.white)

maan = curve(pos=[(-250,-353),(-240,-341),(-210,-354),(-199.5,-374)],color=color.red)
maana = curve(pos=[(-199.5,-374),(-166,-374)],color=color.green)
maanb = curve(pos=[(-166,-374),(-140,-357),(-80,-319),(-40,-361),(0,-321),(40,-329),(80,-347)],color=color.red)
maanc = curve(pos=[(80,-347),(140,-347)],color=color.green)
maand = curve(pos=[(140,-347),(162,-337),(189.5,-365),(210,-355),(240,-372),(250,-338)],color=color.red)

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)




class brandstofmeter(object):
def __init__(self):
axis = 0
self.pos = (210,345)
self.length = 25
self.height = 45
self.meter = box(pos = self.pos, length = self.length, height = self.height,color = color.green)
def update(self):
self.height = self.height - 0.2
print "ok"




class ruimteschip(object):
def __init__(self):
self.pos = vector(0,330)
self.acceleration = vector(0,-88,0)
self.axis = (1,0,0)
self.hoek = (90*math.pi)/180
self.graden = math.degrees(self.hoek)
self.gas = vector(10 * cos(self.hoek),10 * sin (self.hoek))
self.velocity = vector(0,0,0)
self.angle = pi / 2
self.updatemeter = False
self.view = ruimteschip_view(self)
self.vlam = self.view.vlam
self.frame = self.view.frame
self.zicht = brandstofmeter()



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

if scene.kb.keys:
key = scene.kb.getkey()
if key == "left":
# linkerafwijking
self.frame.axis -= (0, 0, 0.05)
self.gas = vector(-sin(self.angle), cos(self.angle))
self.vlam.visible = True
self.updatemeter = True

elif key == "right":
# rechterafwijking
self.frame.axis += (0, 0, 0.05)
self.gas = vector(sin(self.angle), cos(self.angle))

elif key == "up":
self.deviate()
self.vlam.visible = False
self.updatemeter = False

if self.pos.x > 250:
self.pos.x = -250
if self.pos.x < -250:
self.pos.x = 250
self.view.update(self)

def deviate(self):
# zet veranderde snelheid
self.frame.velocity += self.gas
self.frame.pos += self.frame.velocity
# Reset valsnelheid
self.frame.velocity -= self.gas





class ruimteschip_view(object):
def __init__(self,owner):
self.owner = owner
self.frame = frame(pos = owner.pos,axis = owner.axis)
self.motor = curve(frame = self.frame,pos=[(0,24.0),(22.0,24.0),(22.0,39.0),(-22.0,39.0),(-22,24),(0,24)])
self.capsule = curve(frame = self.frame,color = color.blue ,pos=[(0,39),(-3,39),(-9,44),(-12,46),(-18,48),(-22,50),(-18,52),(-12,54),(-9,56),(-3,61),(0,61),(3,59),(9,56),(12,54),(18,52),(22,50),(18,48),(12,46),(9,44),(3,39),(0,39)])
self.poota = curve(frame = self.frame,pos = [(-18,24),(-20,24),(-20,0),(-18,0),(-18,24)])
self.pootb = curve(frame = self.frame,pos = [(18,24),(20,24),(20,0),(18,0),(18,24)])
self.vlam = curve(frame = self.frame,color = color.orange , visible=false,pos = [(0,24.0),(-9.0,14.0),(0,-5.0),(9,14.0),(0,24.0)])

def update(self,owner):
self.frame.axis = owner.axis
self.frame.pos = owner.pos
self.frame.velocity = owner.velocity
 
Ad

Advertisements

J

jeltedeproft

this is again a newer version, right now the velocity does in fact turn, but the view doesn't follow, it keeps the ship vertical.

i'm also having trouble letting the flame appear when pressing the "up" button

and when the ship rotates the horizontal velocity keeps getting bigger and bigger

i also have to make the game end when the ship hits the moon on the wrong place

i'm kinda stressed out because this has to be done by the 15th i've been studying like crazy the past week. If anyone could help me out i will deeply appreciate it, here is the code


from visual import *
import time
import math
import random
from datetime import datetime
import operator


class lunar_lander(object):
def __init__(self):
scene.title = 'mini star wars'
scene.width = 375
scene.height = 550
scene.center = (0,0)
self.pos = (0,0)
self.axis = 0
self.brandstofmeter = brandstofmeter()
self.ruimteschip = ruimteschip()
self.view = game_view(self)



def play(self):
t=0
dt=0.01
self.ruimteschip.updatemeter = False
while t<999999999:
time.sleep(0.01)
self.ruimteschip.update(dt)
t = t + dt
if self.ruimteschip.updatemeter == True:
self.brandstofmeter.update





class game_view(object):
def __init__(self,owner):
autoscale=True
box(pos=( 0, -375, 0), length=500, height=5, width=0, color = color.white)
box(pos=(0,375, 0), length=500, height=5, width=0, color = color.white)
box(pos=(-250,0, 0), length=5, height=750, width=0, color = color.white)
box(pos=(250,0, 0), length=5, height=750, width=0, color = color.white)

maan = curve(pos=[(-250,-353),(-240,-341),(-210,-354),(-199.5,-374)],color=color.red)
maana = curve(pos=[(-199.5,-374),(-166,-374)],color=color.green)
maanb = curve(pos=[(-166,-374),(-140,-357),(-80,-319),(-40,-361),(0,-321),(40,-329),(80,-347)],color=color.red)
maanc = curve(pos=[(80,-347),(140,-347)],color=color.green)
maand = curve(pos=[(140,-347),(162,-337),(189.5,-365),(210,-355),(240,-372),(250,-338)],color=color.red)

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)




class brandstofmeter(object):
def __init__(self):
axis = 0
self.pos = (210,345)
self.length = 25
self.height = 45
self.meter = box(pos = self.pos, length = self.length, height = self.height,color = color.green)
def update(self):
self.height = self.height - 0.2
print "ok"




class ruimteschip(object):
def __init__(self):
self.pos = vector(0,330)
self.acceleration = vector(0,-20,0)
self.axis = (1,0,0)
self.hoek = (90*math.pi)/180
self.graden = math.degrees(self.hoek)
self.gas = vector(10 * cos(self.hoek),10 * sin (self.hoek))
self.velocity = vector(0,0,0)
self.angle = pi / 2
self.updatemeter = False
self.view = ruimteschip_view(self)
self.vlam = self.view.vlam
self.frame = self.view.frame
self.zicht = brandstofmeter()



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

if scene.kb.keys:
key = scene.kb.getkey()
if key == "left":
# linkerafwijking
self.frame.axis -= (0, 0, 0.05)
self.gas = vector(-sin(self.angle), cos(self.angle))
self.vlam.visible = True
self.updatemeter = True

elif key == "right":
# rechterafwijking
self.frame.axis += (0, 0, 0.05)
self.gas = vector(sin(self.angle), cos(self.angle))

elif key == "up":
self.velocity += self.gas
self.frame.pos += self.velocity
else:
self.vlam.visible = False
self.updatemeter = False
self.velocity -= self.gas

if self.pos.x > 250:
self.pos.x = -250
if self.pos.x < -250:
self.pos.x = 250
self.view.update(self)



class ruimteschip_view(object):
def __init__(self,owner):
self.owner = owner
self.frame = frame(pos = owner.pos,axis = owner.axis, velocity = owner.velocity)
self.motor = curve(frame = self.frame,pos=[(0,24.0),(22.0,24.0),(22.0,39.0),(-22.0,39.0),(-22,24),(0,24)])
self.capsule = curve(frame = self.frame,color = color.blue ,pos=[(0,39),(-3,39),(-9,44),(-12,46),(-18,48),(-22,50),(-18,52),(-12,54),(-9,56),(-3,61),(0,61),(3,59),(9,56),(12,54),(18,52),(22,50),(18,48),(12,46),(9,44),(3,39),(0,39)])
self.poota = curve(frame = self.frame,pos = [(-18,24),(-20,24),(-20,0),(-18,0),(-18,24)])
self.pootb = curve(frame = self.frame,pos = [(18,24),(20,24),(20,0),(18,0),(18,24)])
self.vlam = curve(frame = self.frame,color = color.orange , visible=false,pos = [(0,24.0),(-9.0,14.0),(0,-5.0),(9,14.0),(0,24.0)])

def update(self,owner):
self.frame.axis = owner.axis
self.frame.pos = owner.pos
self.frame.velocity = owner.velocity
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top