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

Re: [pygame] Python and Speed



 
Ian,
 
    Below is a simple check knowing only the angle of the vector. The while loop is moving along that vector in steps. Now this is what is needed inside a normal screen with 2 or more objects. For the angle between them is what is being used here. This assumes one is static and one moving, but that also can be compensated for by drawing the point where they meet, which is still direction, and still the same ending point where both agree, or are the same. In other words, the static point where they are both going to meet eventually.
 
    The while loop steps through to the full length of the vector, but that is not needed if you no the distance to the outer edge of your object.
 
    For if you know the angle, the distance to the outer edge of your object surface then you compare the point created by this vector. Once the point is the same for both objects, then you have a collision. Not tracking pixels for a merge, but just the distance to the outer edge of the object you have based on the angle between them.
 
    So this is the simplest way and least time consuming way to find collisions. Granted it is fun and neat to use pixels, but is that really necessary? I guess if your trying to find the edge of your object based on angle, you may have to do that to find where the color changes, but is that necessary?
 
    Like having a mapping of each object and over-laying the angle onto it then finding the edge is how to find that edge point. But, once the edge based on the angle is found, then comparison of the same point is when the collision has happened.
 
    Your static object copy will have an edge point changing based on the angle, so trace that change based on angle. Only a few math steps required, not a sweeping massive array of all pixels...
 
    My grid used is an 8X8 matrix but that can easily be replaced with the screen coordinates values, like 900X900, for the angle and final landing point on that matrix is all were talking about.
 
    Now to get within a pixel area, then expand the screen size outward, like instead of 900X900 make it 9000X9000 and if the area falls within 801X802 then expanded out we will fall within 8010 and 8020, or within 10 which is like saying we have fallen within a box 10X10 so as not to be exact on calculations and allowing for rounding factors...
 
    This module is used for all calculations, including weapon firing that travels through space also, ship movement, and even landing at Star Bases, or commonly referred to as Docking.

def NAV(GG, SS, Dir, Warp):
    "NAVIGATE USING ONLY AN ANGLE FROM A GIVEN SHIP, IN A GIVEN GALAXY/TABLE!"
    Angle = GG.Dir2Ang( Dir) # CONVERT DIRECTION BACK TO ANGLE!
    qy = SS.Qy
    qx = SS.Qx
    sy = SS.Sy
    sx = SS.Sx
    z = 1.0
    Hit = 0
    last4sx = sx
    last4sy = sy
    last4z =2
    Warp = float( Warp) #TO MAKE SURE THE NUMBER IS A NUMBER AND NOT TEXT!
 
    Sin = sin(Angle) #opposite over hypotenuse, VECTOR!
    GG.SIN = Sin #GLOBAL VARIABLE!
    Cos = cos(Angle) #ADJACENT OVER HYPOTENUSE, VECTOR!
    GG.COS = Cos #GLOBAL VARIABLE!
 
#SET TO PLAY SOUND AS IT TRAVELS!
#  Clear_Wait() #MAY SET IF ANY STOP PLAY NEEDED!
 
    if GG.COM=="TOR":
        print "The %s Is Now Firing " % SS.N,
        SS.T -= 1
        if SS.IMG==GG.ESI:
            ps = randint(1,3)
            if ps == 1: print "Photon Torpedos!"; PlaySound( "Federation_Photons.ogg", 0, .8)
            elif ps >= 2: print "Quantom Torpedos!"; PlaySound( "Federation_Quantums.ogg", 0, .8)
        else:
            ps = randint(1,4)
            if ps == 1: print "Photon Torpedos!"; PlaySound( "Klingon_Photons.ogg", 0, 1.2)
            elif ps == 2: print "Cardassian Photon Torpedos!"; PlaySound( "Cardassian_Photons.ogg", 0, 1.2)
            elif ps >= 3: print "Romulan Photon Torpedos!"; PlaySound( "Romulan_Photons.ogg", 0, 1.2)
    if GG.COM=="PHA":
        print "The %s Is Now Firing " % SS.N,
        SS.P -= SS.P/10.0
        if SS.IMG==GG.ESI:
            print "Phasers!"; PlaySound( "Federation_Phaser.ogg", 0, 3)
        else:
            ps = randint(1,5)
            if ps == 1: print "Klingon Disruptors!"; PlaySound( "Klingon_Disruptor.ogg", 0, 1.2)
            elif ps == 2: print "Cardassian Disruptors!"; PlaySound( "Cardassian_Disruptor.ogg", 0, 1.2)
            elif ps >= 3: print "Romulan Disruptors!"; PlaySound( "Romulan_Disruptor.ogg", 0, 1.2)
            elif ps >= 4: print "Phasers!"; PlaySound( "Defiant_Phaser.ogg", 0, 3.5)
 
    #UPDATE THE STAR DATE BASED ON WARP!
    if GG.COM=="NAV":
        #START ENGINES, SET ENERGY, AND STARDATE!
        SS.E -= Warp*10.0 +10.0
        if SS.IMG==GG.ESI:
            GG.SAFE = 0
            GG.SDT += Warp
            print "Command(%s) %s" % (GG.COM, SS.N)
            print " Moving From Quadrant(%d,%d) Sector(%d,%d) At DIR: %1.2f WARP: %2.2f" % (qy,qx,sy,sx, Dir, Warp)
            PlaySound( "Federation_Warp.ogg", 0, -1)
 
    if SS.IMG == GG.ESI and GG.COM != "TAR":
        print "TRACK: "
 
    if Warp>=1:
        Warp *= GG.RM #CONVERT TO SECTOR LIMITS!
    else:
        Warp *= 10 #DO DECIMAL EQUIVALENT!
 
    while z <= Warp:
    #DELAY BEFORE TURNING OFF!
        sx = round( SS.Sx +Cos*z)
        sy = round( SS.Sy -Sin*z)
        if SS.IMG == GG.ESI and GG.COM != "NAV" and GG.COM != "TAR" and (last4sy != sy or last4sx != sx):
            print " (%d,%d)" % (sy,sx)
            sleep(1.0)
        txt = ""
       #AT QUADRANT EDGE?
        if sx<1 or sx>GG.CM or sy<1 or sy>GG.RM: Hit=-1; break
#DICTIONARY KEY COORDINATE VALUE!
        qs = QS4G(qy, qx, sy, sx)
        if qs in GG.GAL:
            txt = GG.GAL[qs]
#     HIT KLINGON SHIP?
        if txt==GG.KSI: Hit=-2; break
#     DID STAR ABSORB IT!
        if txt==GG.STI: Hit=-3; break
#     DID ENTERPRISE GET HIT!
        if txt==GG.ESI: Hit=-4; break
#     STAR BASE, DOCKING?
        if txt==GG.SBI: Hit=1; break
#UPDATE AFTER CHECKS FOR LAST POSITION OF MOVE PRIOR TO HIT!
        z += 0.5
        last4sy = sy
        last4sx = sx
        #DRAIN ENERGY USAGE FOR PROBE!
        if "PRB" in GG.COM: SS.E -= uniform(.1, 5.0)
        if GG.COM=="TAR": SS.E -= uniform(.1, 2.0)
#        DID WEAPON RUN OUT OF ENERGY?
        if GG.COM=="TOR":
            SS.E -= uniform(.1, 2.0);
            SS.WE -= uniform(1.0, 5.0)
            if SS.WE <= 0: Hit = -5; txt = GG.WE0; break
        if GG.COM=="PHA":
            SS.P -= uniform(.1, 3.0);
            SS.WE -= uniform(1.0, 5.0)
            if SS.WE <= 0: Hit = -5; txt = GG.WE0; break
 
# GO TO NEXT LINE AFTER MOVE DISPLAY!
    if GG.COM != "NAV" and SS.IMG != GG.KSI: print
#NOW DECIDE ON OUTCOME OF THE COMMAND!//
    if GG.COM=="NAV":
        if Hit==0: #MOVED SAFELY WITHIN QUADRANT?
            MoveShip(GG, SS, qy, qx, sy, sx) #YES!
        elif Hit==-1: #MOVED OUTSIDE QUADRANT?
            LeftQuadrant(GG, SS, Angle, Warp) #YES!
            if SS.IMG==GG.ESI:
                Scan4Object("Klingon", GG, 0, SS.Qy, SS.Qx, 0)
        elif Hit<-1: #RAN INTO SOMETHING!
            PoorNav(GG, SS, txt, sy, sx) #YES!
            MoveShip(GG, SS, qy, qx, last4sy, last4sx)
        elif SS.IMG==GG.ESI and Hit==1: #ARE WE DOCKING THE ENTERPRISE?
            MoveShip(GG, SS, qy, qx, last4sy, last4sx)
            if Warp<2: #DISPLAY DOCKED!
                DockShip(GG, SS, sy, sx)
            else: #IDIOT DRIVER!
                PoorNav(GG, SS, txt, sy, sx)
#        UNDOCK IF MOVING!
        if Hit != 1: GG.DOC=0
 
#IS IT A PROBE MOVING?
    if "PRB" in GG.COM:
        if Hit==0 or Hit==-2: #STILL SAFELY IN QUADRANT OR HIT KLINGON?
            LRS(GG, qy, qx) #YES!
        elif Hit==-1: #DID PROBE LEAVE QUADRANT?
            LeftQuadrant(GG, SS, Angle, Warp) #YES!
        elif Hit<-1: #DID IT RUN INTO SOMETHING?
            PoorNav(GG, SS, txt, sy, sx) #YES!
 
#IS IT A WEAPON MOVING?
    if GG.COM=="TOR" or GG.COM=="PHA":
        if Hit==-2 or Hit==-4: #HIT KLINGON OR ENTERPRISE?
            GoodShot(GG, txt, Hit, SS, sy, sx) #YES!
        else: BadShot(GG, txt, Hit, SS, sy, sx)
 
#OR IS IT SAFE TO MOVE TO TARGET CHECK?
    if GG.COM=="TAR":
        if Hit == 1: #HIT STAR BASE?
            GG.SAFE=-9 #PATH NOT SAFE!
        else:
            GG.SAFE= Hit
#WHAT IS THE GAME STATUS?
#    GameCondition()