Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > problem with exam task for college

Reply
Thread Tools

problem with exam task for college

 
 
Chris Angelico
Guest
Posts: n/a
 
      01-05-2013
On Sat, Jan 5, 2013 at 11:24 PM, <(E-Mail Removed)> 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 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
 
Reply With Quote
 
 
 
 
jeltedeproft@hotmail.com
Guest
Posts: n/a
 
      01-05-2013
that's probably it, how do i solve it?
 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      01-05-2013
On Sun, Jan 6, 2013 at 12:14 AM, <(E-Mail Removed)> 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 D&D combat.
(It's amazing how your party of bumbling characters suddenly become
lightning-fast strategists as soon as initiative is rolled!)

ChrisA
 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      01-05-2013
On Fri, 4 Jan 2013 12:01:10 -0800 (PST), http://www.velocityreviews.com/forums/(E-Mail Removed)
declaimed the following in gmane.comp.python.general:


> 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...
--
Wulfraed Dennis Lee Bieber AF6VN
(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
jeltedeproft@hotmail.com
Guest
Posts: n/a
 
      01-07-2013
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
 
Reply With Quote
 
Vincent Vande Vyvre
Guest
Posts: n/a
 
      01-07-2013
Le 07/01/13 17:22, (E-Mail 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

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

with angle = PI / 2.0

--
Vincent V.V.
Oqapy <https://launchpad.net/oqapy> . Qarte
<https://launchpad.net/qarte> . PaQager <https://launchpad.net/paqager>
 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      01-07-2013
On Mon, 7 Jan 2013 08:22:28 -0800 (PST), (E-Mail Removed)
declaimed the following in gmane.comp.python.general:

> 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"
--
Wulfraed Dennis Lee Bieber AF6VN
(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
jeltedeproft_8@hotmail.com
Guest
Posts: n/a
 
      01-13-2013
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,-33],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,4,(-22,50),(-18,52),(-12,54),(-9,56),(-3,61),(0,61),(3,59),(9,56),(12,54),(18,52),(22,50) ,(18,4,(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



 
Reply With Quote
 
jeltedeproft@hotmail.com
Guest
Posts: n/a
 
      01-13-2013
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,-33],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,4,(-22,50),(-18,52),(-12,54),(-9,56),(-3,61),(0,61),(3,59),(9,56),(12,54),(18,52),(22,50) ,(18,4,(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



 
Reply With Quote
 
jeltedeproft@hotmail.com
Guest
Posts: n/a
 
      01-13-2013
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,-33],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,4,(-22,50),(-18,52),(-12,54),(-9,56),(-3,61),(0,61),(3,59),(9,56),(12,54),(18,52),(22,50) ,(18,4,(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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
UR SCHOOL AND COLLEGE'S, PLEASE REGISTER UR SCHOOL AND COLLEGE NAME'SIN THIS SITE. "" IF U HAVE TIME THEN DO IT. PLEASE I REQUEST. """ jack Python 0 06-07-2008 06:34 AM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola MCSE 4 11-15-2006 02:40 AM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola Microsoft Certification 3 11-14-2006 05:18 PM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd realexxams@yahoo.com Microsoft Certification 0 05-10-2006 02:35 PM



Advertisments