Re: problem with exam task for college
Hi, I only skimmed through the code, but I think your problem resides at the while True in your brandstofmeter update(). This function never stops.. -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with exam task for college
this is what i have right now, it tells me i can't multiply a vector with a vector, i have to find a way to multiply the speed with the updated angle (=hoek in dutch) code : from visual import * import time import math import random 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 t9: 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.axis = (1,0,0) self.view = ruimteschip_view(self) self.vlam = self.view.vlam self.frame = self.view.frame self.hoek = pi / 2 def update(self,dt): zwaartekracht = vector(0,-2,0) self.gas = vector(0,10,0) self.acceleration = zwaartekracht self.axis = (1,0,0) if scene.kb.keys: s = scene.kb.getkey() if (s == up): self.acceleration =+ self.gas * vector(math.cos(self.hoek),math.sin(self.hoek)) if (s == left): self.hoek =+ pi/12 if (s == right) : self.hoek =- pi/12 self.pos = self.pos + self.acceleration if self.pos.x 250: self.pos.x = -250 if self.pos.x -250: self.pos.x = 250 if self.acceleration != (0,-2,0): self.vlam.visible = True else : self.vlam.visible = False 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with exam task for college
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 t9: 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 =
Re: problem with exam task for college
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 t9: 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)])
Re: problem with exam task for college
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 t9: 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
Re: problem with exam task for college
On 13 January 2013 13:57, jeltedepr...@hotmail.com wrote: 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 [SNIP] 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) [SNIP] The function above is poorly organised. You should break this into several logical steps: 1) Process keypresses to update discrete state variables (such as whether or not the ship is thrusting. 2) Compute from the discrete state variables of the system what the acceleration will be, e.g.: acceleration = gravity if thrusting: acceleration += gas 3) Only once the acceleration is known apply the formulas to update the continuous state (position/velocity) of your system. If you rearrange it like this then you will probably be able to fix the logic bugs in the function above. In particular it is important that there should be only one place where you actually change each kind of state variable (velocity is modified in three places in the function above and I'm sure that this is related to the bugs you are having). Similar considerations apply to updating the angle of the ship. Also the update formula for the position is wrong. If I've understood your physics correctly, you should be using the equations of constant acceleration (my students know these as the suvat equations). In that case the formula should look like: newposition = oldposition + oldvelocity*dt + acceleration*dt**2 newvelocity = oldvelocity + acceleration*dt Your code is missing the quadratic term at the end of the first line above. (Actually the situation would be more complicated than this when the ship is also rotating but I don't understand exactly what your rotation physics is supposed to be). Oscar -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with exam task for college
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with exam task for college
Le 07/01/13 17:22, jeltedepr...@hotmail.com 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 -- with angle = PI / 2.0 -- Vincent V.V. Oqapy https://launchpad.net/oqapy . Qarte https://launchpad.net/qarte . PaQager https://launchpad.net/paqager -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with exam task for college
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 t9: 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 =
Re: problem with exam task for college
On Sat, Jan 5, 2013 at 11:24 PM, jeltedepr...@hotmail.com wrote: 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 Not sure what you mean by stuck, but is it that scene.kb.getkey() waits for a key? ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with exam task for college
that's probably it, how do i solve it? -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with exam task for college
On Sun, Jan 6, 2013 at 12:14 AM, jeltedepr...@hotmail.com wrote: 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 DD combat. (It's amazing how your party of bumbling characters suddenly become lightning-fast strategists as soon as initiative is rolled!) ChrisA -- http://mail.python.org/mailman/listinfo/python-list
problem with exam task for college
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.brright now i am completely stuck on trying to make the visual of the ship rotate.bri'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: 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 t9: 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 =
Re: problem with exam task for college
On Sat, Jan 5, 2013 at 5:23 AM, jeltedepr...@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.brright now i am completely stuck on trying to make the visual of the ship rotate.bri'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 -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with exam task for college
On Sat, Jan 5, 2013 at 5:59 AM, Chris Angelico ros...@gmail.com wrote: 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with exam task for college
On 2013-01-04 18:59, Chris Angelico wrote: On Sat, Jan 5, 2013 at 5:23 AM, jeltedepr...@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.brright now i am completely stuck on trying to make the visual of the ship rotate.bri'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. [snip] Google Translate says it's Dutch: ruimteschip - spaceship brandstofmeter - fuel gauge -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with exam task for college
On 4 January 2013 19:00, Chris Angelico ros...@gmail.com wrote: On Sat, Jan 5, 2013 at 5:59 AM, Chris Angelico ros...@gmail.com wrote: 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. 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with exam task for college
On Sat, Jan 5, 2013 at 6:18 AM, Joshua Landau joshua.landau...@gmail.com wrote: 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with exam task for college
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 t9: 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
Re: problem with exam task for college
On Sat, Jan 5, 2013 at 7:01 AM, jeltedepr...@hotmail.com wrote: woow jeezes, thanks for the amazingly fast and detailed response, you guys are amazing. thanks again, are you guys getting paid for this or is this voluntarily? either way i really appreciate it We're all volunteers (and it's now 7:30AM Saturday and I've been up all night, so this post quite probably doesn't carry the hallmarks of intelligence). To be more strictly correct, we are members of a community - people helping people. Far as I know, there's not one person here who has never asked a question. I tend to join a mailing list to ask one or more questions, and hang around answering them long after my personal needs are satisfied, kinda like seeding back a torrent after you've downloaded it. We answer questions for a variety of reasons. Partly, because this is one of the best forms of help that the Python community can offer, which means that supporting Python in this way strengthens the language. Partly, because our names are connected to our posts, and we are seen to be helpful people (and, since the list/newsgroup is archived on the web, potential employers who search the web for our names will see us being helpful and knowledgeable). Partly, because we're repaying the community for the benefits we've gained from it. Partly, because in answering questions, we ourselves learn. And there are other reasons too. 2) sorry for the dutch names :) No probs; as was said in several follow-ups, those are well-chosen names. 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 : c = list(self.frame.axis) c[2] -= 0.1 c = tuple(c) c = self.frame.axis This code is looking quite messy, here. But the most obvious problem is that you're setting up 'c' to be the modified axis, and then... overwriting c with the old axis. Try doing the assignment the other way in the last line: self.frame.axis = c Alternatively, you could do the whole thing more cleanly by unpacking and then repacking the tuple: (x, y, z) = self.frame.axis self.frame.axis = (x, y, z-0.1) Two lines that do the job of the above four. Note that the parentheses are optional here; they look nice since axis is holding coordinates, but Python doesn't care. Hope that helps! ChrisA -- http://mail.python.org/mailman/listinfo/python-list