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 richtig Arme (Fixed Camera)


Inhaltsbereich

Top Gun für richtig Arme (Fixed Camera)

Wie zuvor, jedoch mit fixierter Kamera zur Präsentation der Looping-Methode, an der wir gefühlte 25 Stunden am Tag rumhockten. Und ein Beweis dafür ist, dass wir nicht alles falsch gemacht haben. ^^

Top Gun für richtig Arme fixcam.py — text/python-source, 7 KB (7426 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 = True
scene.userzoom = False
scene.background = (0,0,0)             # Farbe Hintergrund
scene.stereodepth = 2                       # Stereotiefe
scene.show_rendertime = False               # Anzeige der Berechnungszeiten
scene.fullscreen = False                    # Vollbildmodus

ring1 = ring(pos=(0,0,-100), radius=10, color=color.blue, axis=(0,0,1))


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,1), v=(0,0,-5)):
        Thread.__init__(self)
        frame.__init__(self, pos=p, axis = (0,0,1))
        self.jacket = sphere(radius=(0.1), pos=self.pos, frame=self)
        self.v = v
        self.p = p
        self.axis = axis

    def run (self):
        counter = 0
        while counter < 100:
            counter = counter + 1
            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.bulletcount = 0
        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)
        self.v = self.v+vvec  # SINUSPROBLEM! FRAGEN!
        self.pos = self.pos+self.v # 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 self.bulletcount < self.bulletMax:
            self.bulletcount = self.bulletcount + 1
            b = Bullet(axis=self.axis, p=self.pos, v=self.v)
            b.start()
            print 'Ammo: ',str(self.bulletMax-self.bulletcount)
            
        else:
            print 'LADE NACH!'
            self.bulletcount = 0

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)
                print info, 'test'
            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))
        seitenplatte1 = box (pos=(0,0,-1000), size=(1000,1000,1), color=color.red)
        seitenplatte2 = box (pos=(1000,0,0), size=(1,1000,1000), color=color.yellow)
        seitenplatte3 = box (pos=(0,0,1000), size=(1000,1000,1), color=color.white)

        
        while j[id].health != 0:
            key = scene.kb.getkey()
            rate(50)
            newpos = j[id].pos
            newv = j[id].v
            #self.f.sendto("j[id].pos =("+str(newpos.x)+","+str(newpos.y)+","+str(newpos.z)+")", (self.host, self.fport))
            #self.f.sendto("j[id].v ="+str(newv.x)+","+str(newv.y)+","+str(newv.z)+")", (self.host, self.fport))
            #self.f.sendto("j[id].fly()", (self.host, self.fport))
            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+j[id].pos+(0,5,5)
            scene.center = (0,5,5)
            print 'Health: ',str(j[id].health)

            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