Links und Funktionen
Sprachumschaltung

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


Inhaltsbereich

Top Gun fuer Arme Multiplayer Demo Beta

Langer Name, wenig Sinn und Funktion. Weiterhin fixen

Top Gun MP.py — text/python-source, 6 KB (6665 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.136"               # 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 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]

j = [Jet(), Jet(p=(0,0,-30), c=(0.5,0.5,0.5))]

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"

    def action(self, id):
        self.start()
        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))
        
        while j[id].health != 0:
            key = scene.kb.getkey()
            rate(50)
            j[id].fly()
            scene.center = j[id].axis+j[id].pos#+(0,5,5)  #Kamera fixen?
            scene.forward = -j[id].axis
            scene.up = j[id].axis
            print 'Health: ',str(j[id].health),'| Ammo: ',str(j[id].bulletMax-(len(j[id].bullets)-1))

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

            if key == 'left':
                j[id].roll(vturn=pi/90)
            elif key == 'right':
                j[id].roll(vturn=-pi/90)

            if key == 'up':
                j[id].nose(False)
            elif key == 'down':
                j[id].nose(True)

            if key == '-':
                j[id].shoot()

            if j[id].health == 0:
                print j[id],' lost! Game over.'
            

            
        
if __name__ == "__main__":
    print 'Initializing "',projectname,' ', projectstatus
    print 'By Darko Jankovic & Felix Schneider'
    print 'LMU Probestudium Informatik 2012'
    print '-------------------------------------------'
    print 'Initializing network...'
    s = Spieler()
    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!'
    s.action(0)

Funktionsleiste