Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #36128 > unrolled thread

problem with exam task for college

Started byjeltedeproft@hotmail.com
First post2013-01-04 10:23 -0800
Last post2013-01-14 13:54 -0500
Articles 5 on this page of 25 — 9 participants

Back to article view | Back to comp.lang.python


Contents

  problem with exam task for college jeltedeproft@hotmail.com - 2013-01-04 10:23 -0800
    Re: problem with exam task for college Chris Angelico <rosuav@gmail.com> - 2013-01-05 05:59 +1100
    Re: problem with exam task for college Chris Angelico <rosuav@gmail.com> - 2013-01-05 06:00 +1100
    Re: problem with exam task for college MRAB <python@mrabarnett.plus.com> - 2013-01-04 19:17 +0000
    Re: problem with exam task for college Joshua Landau <joshua.landau.ws@gmail.com> - 2013-01-04 19:18 +0000
    Re: problem with exam task for college Chris Angelico <rosuav@gmail.com> - 2013-01-05 06:30 +1100
      Re: problem with exam task for college jeltedeproft@hotmail.com - 2013-01-04 12:01 -0800
        Re: problem with exam task for college Chris Angelico <rosuav@gmail.com> - 2013-01-05 07:36 +1100
        Re: problem with exam task for college Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-01-05 16:09 -0500
      Re: problem with exam task for college jeltedeproft@hotmail.com - 2013-01-04 12:01 -0800
    Re: problem with exam task for college Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-01-04 17:11 -0500
    Re: problem with exam task for college jeltedeproft@hotmail.com - 2013-01-05 04:24 -0800
      Re: problem with exam task for college Chris Angelico <rosuav@gmail.com> - 2013-01-05 23:39 +1100
    Re: problem with exam task for college jeltedeproft@hotmail.com - 2013-01-05 05:14 -0800
      Re: problem with exam task for college Chris Angelico <rosuav@gmail.com> - 2013-01-06 00:32 +1100
    Re: problem with exam task for college jeltedeproft@hotmail.com - 2013-01-07 08:22 -0800
      Re: problem with exam task for college Vincent Vande Vyvre <vincent.vandevyvre@swing.be> - 2013-01-07 18:40 +0100
      Re: problem with exam task for college Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-01-07 15:36 -0500
    Re: problem with exam task for college jeltedeproft_8@hotmail.com - 2013-01-13 04:29 -0800
    Re: problem with exam task for college jeltedeproft@hotmail.com - 2013-01-13 04:48 -0800
    Re: problem with exam task for college jeltedeproft@hotmail.com - 2013-01-13 05:57 -0800
      Re: problem with exam task for college Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-01-13 17:28 +0000
    Re: problem with exam task for college stefaang@gmail.com - 2013-01-14 04:57 -0800
    Re: problem with exam task for college jeltedeproft@hotmail.com - 2013-01-14 08:01 -0800
      Re: problem with exam task for college Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-01-14 13:54 -0500

Page 2 of 2 — ← Prev page 1 [2]


#36735

Fromjeltedeproft@hotmail.com
Date2013-01-13 05:57 -0800
Message-ID<02d0ea1d-e810-4f30-8af1-320d675a2c65@googlegroups.com>
In reply to#36128
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
        

                          

[toc] | [prev] | [next] | [standalone]


#36742

FromOscar Benjamin <oscar.j.benjamin@gmail.com>
Date2013-01-13 17:28 +0000
Message-ID<mailman.473.1358098132.2939.python-list@python.org>
In reply to#36735
On 13 January 2013 13:57,  <jeltedeproft@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

[toc] | [prev] | [next] | [standalone]


#36783

Fromstefaang@gmail.com
Date2013-01-14 04:57 -0800
Message-ID<8649092b-fd1d-4131-84c3-9c7fc60183fa@googlegroups.com>
In reply to#36128
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..

[toc] | [prev] | [next] | [standalone]


#36796

Fromjeltedeproft@hotmail.com
Date2013-01-14 08:01 -0800
Message-ID<54dc9b58-8a7b-4a7a-a6d1-8d9a02a0477c@googlegroups.com>
In reply to#36128
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 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.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
        
        

                          

[toc] | [prev] | [next] | [standalone]


#36810

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2013-01-14 13:54 -0500
Message-ID<mailman.513.1358189680.2939.python-list@python.org>
In reply to#36796
On Mon, 14 Jan 2013 08:01:03 -0800 (PST), jeltedeproft@hotmail.com
declaimed the following in gmane.comp.python.general:

> 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)
>

	It's a trite saying but:

	How would you perform this vector multiplication by hand? Write down
the steps... Now convert those steps into Python code.

	Or -- download/install something like NumPy and recode your program
using NumPy vector types which should support the needed
multiplications.
 
>>> import numpy
>>> a = numpy.array(	(2.78, 3.14, 1958)	)
>>> b = numpy.array(	(1.5, 2.0, 1.0)	)
>>> 
>>> a*b
array([    4.17,     6.28,  1958.  ])
>>> a.dot(b)
1968.45
>>> 
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.python


csiph-web