Links und Funktionen
Sprachumschaltung

Navigationspfad
Sie sind hier: Startseite / Probestudium / 2010 / WS I: 3D-Programmierung / Pacman 3D


Inhaltsbereich

Pacman 3D

Pacman3DGeister.py — text/python-source, 12 KB (12388 bytes)

Dateiinhalt

# -*- coding: cp1252 -*-
# Pacman
##################################

from visual import *
from threading import *  
from random import *
##from time import *





geschwindigkeit=1
Score=0


wandPos =[
                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                [0,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],
                [0,3,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3],
                [0,3,2,1,1,1,1,1,1,2,1,2,1,1,1,1,1,1,2,3],
                [0,3,2,2,2,2,1,2,2,2,1,2,2,2,1,2,2,2,2,3],
                [0,3,1,2,1,2,1,2,1,1,1,1,1,2,1,2,1,2,1,3],
                [0,3,2,2,1,2,2,2,2,2,2,2,2,2,2,2,1,2,2,3],
                [0,3,2,1,1,2,1,1,1,2,1,2,1,1,1,2,1,1,2,3],
                [0,3,2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,3],
                [0,3,1,2,1,2,1,2,1,1,1,1,1,2,1,2,1,1,2,3],
                [0,3,1,2,1,2,1,2,2,2,2,2,2,2,1,2,1,1,2,3],
                [0,3,1,2,1,2,1,2,1,1,1,1,1,2,1,2,1,1,2,3],
                [0,3,2,2,2,2,2,2,1,4,4,4,1,2,2,2,2,2,2,3],
                [0,3,2,1,1,2,1,2,1,1,0,1,1,2,1,2,1,2,1,3],
                [0,3,2,1,1,2,1,2,2,2,2,2,2,2,1,2,1,2,1,3],
                [0,3,2,1,1,2,1,1,1,2,1,2,1,1,1,2,1,2,1,3],
                [0,3,2,2,2,2,1,2,2,2,1,2,2,2,1,2,2,2,2,3],
                [0,3,2,1,1,2,1,2,1,1,1,1,1,2,1,2,1,1,2,3],
                [0,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3],
                [0,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],
            ]
class Gespenst (Thread):
    def __init__(self,x=0,y=0,z=0,rx=1,rz=0,farbe=color.white):
        Thread.__init__(self)
        self.rahmen = frame(pos=(x,y,z), axis=(rx,0,rz))
        self.kopf = sphere(frame=self.rahmen,pos=(0,1.5,0), color=farbe, radius=1.5)
        self.koerper = cone(frame=self.rahmen,pos=(0,0,0),color=farbe, axis =(0,3,0),radius=1.8)
        self.rAuge = sphere(frame=self.rahmen,pos=(0.9,2.5,0.4),color=color.white,radius=0.4)
        self.lAuge = sphere(frame=self.rahmen,pos=(0.85,2.5,-0.4),color=color.gray(0.5),radius=0.3)
        self.rPupille = sphere(frame=self.rahmen,pos=(1.2,2.5,0.4),color=color.black,radius=0.15)
        self.lPupille = sphere(frame=self.rahmen,pos=(0.97,2.65,-0.58),color=color.red,radius=0.08)
        self.last=(x,z)
    def run(self):
        while True:
            time.sleep(2)
            self.x=int((self.rahmen.x+2.5)/5)
            self.z=int((self.rahmen.z+2.5)/5)
            self.go=[]
            if wandPos[self.x][self.z+1] != 3 and wandPos[self.x][self.z+1] != 1:
                self.go.append((self.x,self.z+1))
            if wandPos[self.x+1][self.z] != 3 and wandPos[self.x+1][self.z] != 1:
                self.go.append((self.x+1,self.z))
            if wandPos[self.x][self.z-1] != 3 and wandPos[self.x][self.z-1] != 1:
                self.go.append((self.x,self.z-1))
            if wandPos[self.x-1][self.z] != 3 and wandPos[self.x-1][self.z] != 1:
                self.go.append((self.x-1,self.z))
            self.c=choice(self.go)
            self.rahmen.x=self.c[0]*5-2.5
            self.rahmen.z=self.c[1]*5-2.5 
            
        
        
        
class Ebene(object):
    def __init__(self, y=0, s=95, m=materials.wood):
        self.block = frame() ##Alle W�nde+Boden
        self.ball = frame() ##Alle Kugeln
        self.geist = frame() ##Alle Geister
        self.boden = box(pos=(s/2,y-0.5,s/2), size=(s,1,s), material=m, frame=self.block)
        y=(y+1)*2.5
        self.geist = 1
        for i in range(len(wandPos)):
            for j in range(len(wandPos[i])):
                ##Boxen und Ecken
                if wandPos[i][j] == 1:
                    b=box(pos=(i*5-2.5,y,j*5-2.5),size=(5,5,5),opacity=0.3, frame=self.block) ##Wand
                    if wandPos[i][j-1]!=3 and wandPos[i][j-1]!=1: ##Ist -x frei -> Oben
                        r=cylinder(pos=(i*5-5,y+2.5,j*5-5), length=5, axis=(1,0,0), radius=0.1, opacity=0.5, material=materials.emissive, color=color.red)
                        if wandPos[i+1][j]!=3 and wandPos[i+1][j]!=1: ## und +z frei -> Ausen
                            r=cylinder(pos=(i*5,y+2.5,j*5-5), length=5, axis=(0,-1,0), radius=0.1, opacity=0.5, material=materials.emissive, color=color.red)
                        if wandPos[i-1][j]!=3 and wandPos[i-1][j]!=1: ## und -z frei -> Ausen
                            r=cylinder(pos=(i*5-5,y+2.5,j*5-5), length=5, axis=(0,-1,0), radius=0.1, opacity=0.5, material=materials.emissive, color=color.red)
                    else:
                        if wandPos[i+1][j]!=0 and wandPos[i+1][j]!=2 and wandPos[i+1][j-1]!=3 and wandPos[i+1][j-1]!=1: ##NICHT und +z belegt und -x+z frei -> Innen
                            r=cylinder(pos=(i*5-5,y+2.5,j*5), length=5, axis=(0,-1,0), radius=0.1, opacity=0.5, material=materials.emissive, color=color.red)
                        if wandPos[i-1][j]!=0 and wandPos[i-1][j]!=2 and wandPos[i-1][j-1]!=3 and wandPos[i-1][j-1]!=1: ##NICHT und -z belegt und -x-z frei -> Innen
                            r=cylinder(pos=(i*5-5,y+2.5,j*5-5), length=5, axis=(0,-1,0), radius=0.1, opacity=0.5, material=materials.emissive, color=color.red)
                    if wandPos[i][j+1]!=3 and wandPos[i][j+1]!=1: ##Ist +x frei -> Oben
                        r=cylinder(pos=(i*5-5,y+2.5,j*5), length=5, axis=(1,0,0), radius=0.1, opacity=0.5, material=materials.emissive, color=color.red)
                        if wandPos[i-1][j]!=3 and wandPos[i-1][j]!=1: ##und -z frei -> Ausen
                            r=cylinder(pos=(i*5-5,y+2.5,j*5), length=5, axis=(0,-1,0), radius=0.1, opacity=0.5, material=materials.emissive, color=color.red)
                        if wandPos[i+1][j]!=3 and wandPos[i+1][j]!=1: ##und +z frei -> Ausen
                            r=cylinder(pos=(i*5,y+2.5,j*5), length=5, axis=(0,-1,0), radius=0.1, opacity=0.5, material=materials.emissive, color=color.red)
                    else:
                        if wandPos[i-1][j]!=0 and wandPos[i-1][j]!=2 and wandPos[i+1][j-1]!=3 and wandPos[i+1][j-1]!=1: ##NICHT und -z belegt und +x-z frei -> Innen
                            r=cylinder(pos=(i*5,y+2.5,j*5-5), length=5, axis=(0,-1,0), radius=0.1, opacity=0.5, material=materials.emissive, color=color.red)
                        if wandPos[i+1][j]!=0 and wandPos[i+1][j]!=2 and wandPos[i+1][j+1]!=3 and wandPos[i+1][j+1]!=1: ##NICHT und +z belegt und +x+z frei -> Innen
                            r=cylinder(pos=(i*5,y+2.5,j*5), length=5, axis=(0,-1,0), radius=0.1, opacity=0.5, material=materials.emissive, color=color.red)
                    if wandPos[i+1][j]!=3 and wandPos[i+1][j]!=1: ##Ist +z -> Oben
                        r=cylinder(pos=(i*5,y+2.5,j*5-5), length=5, axis=(0,0,1), radius=0.1, opacity=0.5, material=materials.emissive, color=color.red)
                    if wandPos[i-1][j]!=3 and wandPos[i-1][j]!=1: ##Ist -z frei -> Oben
                        r=cylinder(pos=(i*5-5,y+2.5,j*5-5), length=5, axis=(0,0,1), radius=0.1, opacity=0.5, material=materials.emissive, color=color.red)
                ##Kugeln
                elif wandPos[i][j] == 2:
                    k=sphere(pos=(i*5-2.5,y,j*5-2.5), radius=0.3, color=color.yellow, material = materials.rough, frame=self.ball)
                ##Ausenboxen
                elif wandPos[i][j] == 3:
                    b=box(pos=(i*5-2.5,y,j*5-2.5),size=(5,5,5), material = materials.rough, color=color.red, frame=self.block)
                ##Geister
                elif wandPos[i][j] == 4 and self.geist ==1:
                    G1=Gespenst(i*5-2.5,y-1.5,j*5-2.5, farbe=color.red)
                    self.geist+=1
                    G1.start()
                elif wandPos[i][j] == 4 and self.geist ==2:
                    G2=Gespenst(i*5-2.5,y-1.5,j*5-2.5, farbe=color.green)
                    self.geist+=1
                    G2.start()
                elif wandPos[i][j] == 4 and self.geist ==3:
                    G3=Gespenst(i*5-2.5,y-1.5,j*5-2.5, farbe=color.yellow)
                    G3.start()
                    






scene.range = (geschwindigkeit,1,1) # Schrittweite

scene.center = (7.5,2.5,7.5) # Festlegung Blickziel

scene.userspin = False # Blickwechsel abgeschaltet

scene.userzoom = False # Benutzerzoom abgeschaltet

scene.fov = 1 # Kamerawinkel
scene.fullscreen = True


scene.background=(0,0,0)
scene.title="Pacman - 3D"
scene.cursor.visible = False

ausgabe=label(pos=scene.center, text=str(Score), xoffset=-scene.width/2+50, yoffset=scene.height/2-12, height=20, border=0.5, box=False, line=False)

e = Ebene()



while True:
    rate(100)
    # Falls Tastatur gedr�ckt...
    if scene.kb.keys :
        for i in range(scene.kb.keys):
            s = scene.kb.getkey()
        ##print s
        if s == 'up': # vorw�rts
            ##print scene.center
            x=int((scene.center[0]+2.5)/5)
            z=int((scene.center[2]+2.5)/5)
            ##print x,z, scene.forward
            if (int(scene.forward[0]),int(scene.forward[1]),int(scene.forward[2])) == (0,0,1) and (wandPos[x][z+1]==3 or wandPos[x][z+1] ==1):
                ##print "STOP!!!"
                pass
            elif (int(scene.forward[0]),int(scene.forward[1]),int(scene.forward[2])) == (0,0,-1) and (wandPos[x][z-1]==3 or wandPos[x][z-1] ==1):
                ##print "STOP!!!"
                pass
            elif (int(scene.forward[0]),int(scene.forward[1]),int(scene.forward[2])) == (1,0,0) and (wandPos[x+1][z]==3 or wandPos[x+1][z] ==1):
                ##print "STOP!!!"
                pass
            elif (int(scene.forward[0]),int(scene.forward[1]),int(scene.forward[2])) == (-1,0,0) and (wandPos[x-1][z]==3 or wandPos[x-1][z] ==1):
                ##print "STOP!!!"
                pass
            else:
                scene.center = scene.center+scene.forward
                ausgabe.pos=scene.center
                time.sleep(0.05)
                scene.center = scene.center+scene.forward
                ausgabe.pos=scene.center
                time.sleep(0.05)
                scene.center = scene.center+scene.forward
                ausgabe.pos=scene.center
                time.sleep(0.05)
                scene.center = scene.center+scene.forward
                ausgabe.pos=scene.center
                time.sleep(0.05)
                scene.center = scene.center+scene.forward
                ausgabe.pos=scene.center
                time.sleep(0.05)
                if  wandPos[int((scene.center[0]+2.5)/5)][int((scene.center[2]+2.5)/5)]==2:
                    wandPos[int((scene.center[0]+2.5)/5)][int((scene.center[2]+2.5)/5)]=0
                    Score+=10
                    for i in e.ball.objects:
                        if i.pos==scene.center:
                            i.visible=False
                            del i
                            ausgabe.text=str(Score)
                            break
                    
            ##print scene.center
        ##elif s == 'down': # r�ckw�rts
        ##    scene.center = scene.center-scene.forward*mag(scene.center-scene.mouse.camera)
        elif s == 'left':
            scene.forward = rotate(scene.forward, axis=scene.up, angle=radians(18))
            time.sleep(0.05)
            scene.forward = rotate(scene.forward, axis=scene.up, angle=radians(18))
            time.sleep(0.05)
            scene.forward = rotate(scene.forward, axis=scene.up, angle=radians(18))
            time.sleep(0.05)
            scene.forward = rotate(scene.forward, axis=scene.up, angle=radians(18))
            time.sleep(0.5)
            scene.forward = rotate(scene.forward, axis=scene.up, angle=radians(18))
            time.sleep(0.05)
        elif s == 'right':
            scene.forward = rotate(scene.forward, axis=scene.up, angle=radians(-18))
            time.sleep(0.05)
            scene.forward = rotate(scene.forward, axis=scene.up, angle=radians(-18))
            time.sleep(0.05)
            scene.forward = rotate(scene.forward, axis=scene.up, angle=radians(-18))
            time.sleep(0.05)
            scene.forward = rotate(scene.forward, axis=scene.up, angle=radians(-18))
            time.sleep(0.05)
            scene.forward = rotate(scene.forward, axis=scene.up, angle=radians(-18))
            time.sleep(0.05)


Funktionsleiste