[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Making A Module



Hi!

    Got it resolved. I first ran it and was looking at the errors and decided to take my seeing eye dog for his needed walk. 
    The error was located with the function and of course after taking the walk I knew I had to place the module name in front of the define, or instance.

    that fixed it up until the error of 'math' not defined. Even though the import math is before the import F_Course module.

    so, after testing everything it came out OK. For some reason even though I have imported the math prior to the module, it is in the .pyc that is the problem. For the math module had to be inside the F_Course module so that after the first run the .pyc is made.

    It did not know where to find the math global statement when not inside the module.

    So it is OK now and works fine. I also deleted some old stuff in my course function. It is not as difficult as I made it once doing the atan function. So above the x-axis the value is 3 and below it is 7 for all tan angles will subtract or add from there...

    So, this afternoon I changed it to have almost all within one class. You will note I have a duplicate but I am testing it all to have it within one class for the ship movements. Below is a function that does the same as the self.Dir does inside the class. This takes care of both problems as long as I import math in. I will get rid of the function(def) statement.

    Below this module is my test file to test the differences if any and the parms inside the class when assigned to x in the example.

This is my module now:
import math
class X4Line:
    """X4LINE CALCULATES THE ANGLE AND LENGTH OF STRAIGHT LINE BETWEEN 2 

POINTS!
    - USING __INT__ TO INITIALIZE CLASS OBJECTS,
    - BY PASSING SELF INIT PARAMETERS IN TO ATTRIBUTES.
    - THEN SELF ATTRIBUTES GET PASSED BACK AS CLASS ATTRIBUTES!
    - PAST BACK ONLY WHEN A VARIABLE, AN INSTANCE, HAS BEEN CREATED!
    - DISTANCE ANGLE CALCULATOR
    - CALCULATES LENGTH AND ANGLE OF A STRAIGHT LINE BETWEEN 2 POINTS!"""
    def __init__(self, X1, Y1, X2, Y2):
        self.X1 = X1
        self.X2 = X2
        self.Y1 = Y1
        self.Y2 = Y2
        self.Factor=3
        self.Dx = X2 - X1
        self.Dy = Y2 - Y1
        if self.Dy==0.0: self.Dy=.0000001
        if self.Dy<0: self.Factor=7
        self.Dist = math.hypot(self.Dx, self.Dy)
        self.Dir = (math.atan( self.Dx/ self.Dy)/ (math.pi/4)) +self.Factor
        self.Tan_Angle = math.tan( self.Dx/ self.Dy)
        self.Ship_Angle = math.atan( self.Dx/ self.Dy)
        self.Sin_Angle = math.asin( self.Dy/ math.hypot( self.Dx, self.Dy))
        self.Cos_Angle = math.acos( self.Dx/ math.hypot( self.Dx, self.Dy))

#SHIP CALCULATIONS!

def Dir(Course):
    """FUNCTION DIR CONVERTS DIRECTION 1 TO 8.999 BACK TO ANGLE.
    - AND NOTE NEGATIVE BECAUSE OF 90 DEGREE ROTATION SHIFT!"""
    return ((Course-1) *(math.pi/4))

def Sin4Dy( Dm, Adjust):
    "COMVERT DY BECAUSE OF Y AXIS REVERSE COUNT AND ADD ROUNDING ADJUSTMENT!"
    Dm*=SIN # GLOBAL SIN VALUE!
    if Dm<0:
        Adjust = -Adjust
    Dm+=Adjust
    return (-Dm)

# CALCULATIONS BELOW ARE FOR A STANDARD X AND Y AXIS!
# STANDARD WHERE Y GOES UP IN VALUE WHEN GOING VERTICALLY UP!
def Ship_Course (dy, dx): #, dz):
    """FUNCTION COURSE4SHIP PASSES IN 2 VALUES DY AND DX.
    - THIS CALCULATES USING A NORMAL X AND Y AXIS!"""
#VARIABLES:
    Factor=3.0
#    C1ANGLE=Angle
    if dy<0: Factor=7.0
    if dy==0:
        if dx<=0: return (1.0)
        else: return (5.0)
    return (math.atan( dx/dy)/ (math.pi/4) +Factor)

    if Angle<0 and dx<0: # IS ANGLE IN QUAD 4?
        Factor=9 # YES, ADJUST FACTOR!
# NOW ADJUST FOR OUR POINTER EQUIVALENT 1 TO 8.99
    Angle= Factor +Angle/(math.pi/4) # ADJUST PER EACH 45 DEGREES.
    return (Angle) # DIRECTION OF ANGLE, 1 TO 9


# FUNCTION USED IN NORMAL X-Y AXIS, NOT IN PROGRAM!
def ship_angle(course):
    "GET SHIP ANGLE FROM DIRECTION 0-9!"
    Angle=0.0
    Factor=0
    Dir=0.0, 

# GET THE DIRECTION INTEGER FACTOR.
    Factor = Math.floor( course)
    Dir = course
    course-=1 # ROTATE RIGHT TO CONVERT TO RADIAN ANGLE FACTOR.

#  if (course>2 and course<4) or course>6: # QUADS 2 AND 4?
#      course*=-1 # YES, MAKE NEGATIVE. 

    Angle= course*(math.pi/4)
    Angle-= math.pi/2
    Angle= -Angle
    Angle= math.tan(Angle)
    return (Angle)


def Dir4Ship2Ship( S1DSS, S2DSS):
    "CALCULATE THE ANGLE FROM SHIP S1 TO SHIP S2!"
    Dy4DSS= Ships[ S1DSS].Sy -Ships[ S2DSS].Sy # SECTOR Y POS!
    Dx4DSS= Ships[ S1DSS].Sx -Ships[ S2DSS].Sx # SECTOR X POS!
    return (Ship_Course( Dy4DSS, Dx4DSS)) # THE ANGLE 1 TO 8.999 GOING 

COUNTERCLOCKWISE!


My test file for the module:

import sys
import math
import random
import pygame
import F_Course

print "Module Location.Name: %s" % F_Course.X4Line
print F_Course.X4Line.__doc__
print F_Course.Ship_Course
print F_Course.Ship_Course.__doc__
for i in range(10):
    x = F_Course.X4Line( random.uniform(1,8), random.uniform(1,8), random.uniform(1,8), random.uniform(1,8))
    print "(%d)" % (i+1)
    print "E(%2.2f,%2.2f) K(%2.2f,%2.2f) Dx= %2.2f Dy= %2.2f Dist= %2.2f Dir= %2.2f[%2.2f]" % (x.Y1, x.X1, x.Y2, x.X2, x.Dx, 

x.Dy, x.Dist, x.Dir, F_Course.Ship_Course(x.Dy, x.Dx))
pygame.quit()

Sent: Monday, October 29, 2007 3:51 PM
Subject: Re: [pygame] Making A Module


Can you make a small sample that demonstrates the error you are seeing
as simply as possible?

Like maybe you send one file that does the import (and fails for you),
and you send some other file that you want to be imported, and finally
you tell us the full path to each of those files on your machine?


On 10/29/07, RR4CLB <chester_lab@xxxxxxxxxxxx> wrote:
>
>     Thanks for all the suggestions. I thought this is the way it should be done but kept coming up with an error in the import statement. Kept saying it could not find it. So I had tried placing it in the main path where Python25 is but it did not work. So I set it in the site packages folder where all the others are and it did not work. SO I will have to check again and see what I am doing wrong.
>
>     I thought you should remove everything but the def statements and it is just not finding the file itself. So I will try again and report what I am doing. Maybe the error is something else besides the load of the file but I am sure the error was at that point.
>
>         Bruce