Links und Funktionen
Sprachumschaltung

Navigationspfad
Sie sind hier: Startseite / Probestudium / 2012 / WS I: 3D-Programmierung / Arbeitsgruppen / Top Gun für (richtig) Arme / Top Gun für Arme (Non-Multiplayer Beta)


Inhaltsbereich

Top Gun für Arme (Non-Multiplayer Beta)

Obwohl "beta", noch nicht lauffähig. Es gibt noch zu tun: - Kollisionsabfrage + Health Change - Schießen fixen - Sinusfunktion fixen

Top Gun.py — text/python-source, 7 KB (7309 bytes)

Dateiinhalt

# -*- coding: utf-8 -*-
from visual import *
from threading import Thread
import socket

projectname = 'Top Gun für Arme'
projectstatus = 'Multiplayer Demo'

# Portnummer und Hostadresse des Fremdservers: bei Bedarf aendern
FremdPORT=8000                      # Port des Fremdservers 
HOST="141.84.220.92"               # Adresse des Gegenueber
EigenPORT=8001                      # Port des eigenen Servers

scene.title = projectname+' '+projectstatus # Fenstertitel
scene.x = 0	                            # Fensterpos v. links
scene.y = 0	                            # Fensterpos v. oben
scene.height = 480                          # Fensterhöhe
scene.width = 640                           # Fensterbreite
scene.range = (1,1,1)                       # Abstand
scene.userspin = False
scene.userzoom = False
scene.background = (0.8,0.85,1)             # Farbe Hintergrund
scene.stereodepth = 2                       # Stereotiefe
scene.show_rendertime = False               # Anzeige der Berechnungszeiten
scene.fullscreen = False                    # Vollbildmodus

b1=box(pos=(10,0,-100),color=color.red)         #Boxen im 1.Fenster
b2=box(pos=(-10,0,-500),color=color.red)        


b3=box(pos=(10,0,-100)) #Boxen im 2.Fenster
b4=box(pos=(-10,0,-500))

def xgeben (frame, local):
    x_axis = norm(frame.axis)
    z_axis = norm(cross(frame.axis, frame.up))
    y_axis = norm(cross(z_axis, x_axis))
    return x_axis

def ygeben (frame, local):
    x_axis = norm(frame.axis)
    z_axis = norm(cross(frame.axis, frame.up))
    y_axis = norm(cross(z_axis, x_axis))
    return y_axis

def zgeben (frame, local):
    x_axis = norm(frame.axis)
    z_axis = norm(cross(frame.axis, frame.up))
    y_axis = norm(cross(z_axis, x_axis))
    return z_axis

class Spieler(Thread):
    """Schnelle Verbindung zu Server"""
    def __init__(self, host=HOST, fport=FremdPORT, eport=EigenPORT):
        Thread.__init__(self)
        self.host = host
        self.fport = fport
        self.eport = eport
        # UDP-Socket fuer Clientfunktion
        self.f = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        # UDP-Socket fuer Eigenserver
        self.e = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.e.bind(("",eport))
        
    def run(self):                  # interne Startmethode des Threads
        while True:                 # Endlosschleife der Serverausgabe (50 Zeichen)
            try:                    # Versuch, die Nachricht vom Gegenueber umzusetzen
                info = self.e.recvfrom(50)[0]
                exec(info)
            except:
                print "Fehler beim Ausfuehren von Serveranweisung"

class Bullet(Thread, frame):
    def __init__(self, p=(0,0,0), axis=(0,0,0)):
        Thread.__init__(self)
        frame.__init__(self, pos=p, axis = (0,0,1))
        self.jacket = sphere(radius=(0.1), pos=self.pos+vector(0,5,0), frame=self)
        self.v = vector(0,0,-10)

    def fly (self):
        self.pos = self.pos+self.v

        
class Jet (Thread, frame):
    def __init__(self, p=(0,0,0), axis=(0,0,0), c=color.white, leading=true):
        Thread.__init__(self)
        frame.__init__(self, pos=p, axis = (0,0,1))
        self.achse = vector(0,0,1)
        self.body = cylinder(radius = 2, axis=(-10,0,0), pos=(0,0,0), material=materials.wood, color=c, frame=self)
        self.wing = box (length=3, height=1, width=15, pos=(-6,0,0), material=materials.wood, color=c, frame=self)
        self.bullets = [Bullet]
        self.bulletMax = 40
        self.health = 100
        self.xchange = 0
        self.ychange = 0
        self.zchange = 0
        self.v = vector(0,0,-1)
        self.t = 0.0

    def fly (self):
        vvec = vector(self.xchange,self.ychange,self.zchange)
        vchange = self.v+vvec  # SINUSPROBLEM! FRAGEN!
        self.pos = self.pos+vchange # Jet-Bewegung
        print self.pos

    def nose (self, positive):
        self.t = self.t+0.1
        if positive == True:
            self.ychange = sin(self.t)
            self.rotate(angle = -0.1*(pi/3), axis = zgeben(self, self.body), origin = self.body.pos+vector(0,0,6))
        else:
            self.ychange = -sin(self.t)
            self.rotate(angle = 0.1*(pi/3), axis = zgeben(self, self.body), origin = self.body.pos+vector(0,0,6))
        
    def roll(self, vturn):
        self.rotate(angle = vturn, axis=self.axis)

    def shoot (self):
        if len(self.bullets) < self.bulletMax:
            self.bullets.append(Bullet(axis=self.axis))
        else:
            print 'LADE NACH!'
            del self.bullets
            self.bullets = [Bullet]

            
        
if __name__ == "__main__":
    print 'Initializing "',projectname,' ', projectstatus
    print 'By Darko Jankovic & Felix Schneider'
    print 'LMU Probestudium Informatik 2012'
    print '-------------------------------------------'
    print 'Initializing network...'
    #s1 = Spieler()  # Adressen, Ports!
    #s2 = Spieler()
    print 'Initializing Jet 1...'  # DEF ACTION! TODO!

    j1 = Jet()
    
    print '...successful! Jet 1 awaiting orders.'
    print ''
    print 'Initializing Jet 2...'
    
    j2 = Jet(p=(0,0,-30), c=(0.5,0.5,0.5))
    
    print '...successful! Jet 2 awaiting orders.'
    print '-------------------------------------------'
    print 'CONTROLS:'
    print 'Key | Function'
    print '======================'
    print '. | accelerate'
    print ', | decelerate'
    print '[up] | Nose up'
    print '[down] | Nose down'
    print '[left] | Roll left'
    print '[right] | Roll right'
    print '- | Shoot'
    print '-------------------------------------------'
    print 'Press the any key to start the game!'

    scene.center = (0,5,10)
    scene.autoscale = False

    bodenplatte = box (pos=(0,-1000,0), size=(1000,1,1000), color=(0.5,1,0.5))
    himmelplatte = box (pos=(0,1000,0), size=(1000,1,1000), color=(0,0,1))
    
    if scene.kb.getkey() > 0: # hier Abfrage nach dem Any-Key
        running = True

    while running == True:
        key = scene.kb.getkey()
        rate(50)
        j1.fly()
        #j1.bullets[0:(j1.bulletMax-1)].fly()
        scene.center = j1.axis+j1.pos+#(0,5,5)  #Kamera fixen?
        scene.forward = -j1.axis
        scene.up = j1.axis

        print 'Health: ',str(j1.health),'| Ammo: ',str(j1.bulletMax-(len(j1.bullets)-1))

        #if mag(j1.pos-j2-pos)<2:  #unsupported operand type!
         #   print 'Double Fail! Game over.'
          #  break
        #elif mag(j1.pos-object(Bullet).pos)<2:  #Kollisionsabfrage TODO!
         #   j1.health = j1.health-10
        #elif mag(j2.pos-object(Bullet).pos)<2:
         #   j2.health = j2-health-10

        if key == '.' and mag(j1.v)<4.0: #Beschleunigung
            j1.v = j1.v*1.1
        elif key == ',' and mag(j1.v)>0.5: #Bremsung
            j1.v = j1.v*0.9

        if key == 'left':
            j1.roll(vturn=pi/90)
        elif key == 'right':
            j1.roll(vturn=-pi/90)

        if key == 'up':
            j1.nose(False)
        elif key == 'down':
            j1.nose(True)

        if key == '-':
            j1.shoot()
            


        if j1.health == 0:
            print 'Winner: Jet #2! Congrats, mate!'
            break

        elif j2.health == 0:
            print 'Winner: Jet #1! Congrats, mate!'
            break

Funktionsleiste