Links und Funktionen
Sprachumschaltung

Navigationspfad
Sie sind hier: Startseite / Probestudium / 2011 / WS I: 3D-Programmierung / Arbeitsgruppen / Kaptludo (Fangenspiel) / OFFLINEMODUS_SECTOR1_MIT_KOLLISIONSCHECK!!!!!!!!!!


Inhaltsbereich

OFFLINEMODUS_SECTOR1_MIT_KOLLISIONSCHECK!!!!!!!!!!

Man kann nur im Sector 1 fahren. Man kann durch die Eckwände in der Mitte fahren. Man kann nicht durch die Kugeln fahren. DE 2.Mai 2011 Neueste Version. Nächste voraussichtliche Version am 1.Juni 2011.

kaptludoWorldWithBallWithoutCollisionTest-2.py — text/python-source, 10 KB (10458 bytes)

Dateiinhalt

# -*- coding: utf-8 -*-

from visual import *
from random import*

scene.range = (5,5,5)                          # Abstand
scene.center = (122,1,22)                  # Festlegung Blickziel
scene.userspin = False                         # Blickwechsel abgeschaltet
scene.userzoom = False                       # Benutzerzoom abgeschaltet
scene.fov = 2                                      # Kamerawinkel
scene.fullscreen = True

colors = [color.yellow, color.magenta, color.green, color.orange, color.white, color.blue, color.cyan, color.red]
ground = box(pos=(50*2, -1,50*2), length=100*2, height=1, width=100*2, material=materials.rough)
landscape = box(pos=(50, -0.5, 50), length=101, height=30, width=101, material=materials.earth, opacity=0)

class house():    #class house
    def __init__(self, px, pz, pLength, pWidth, pcolor = color.cyan, popacity = 1):                            #__init__
        a = box(pos=(px*2, 4.5, pz*2), length=pLength*2, height=10, width=pWidth*2, color=pcolor, opacity=popacity, material=materials.rough)

#outer walls
wall1 = house(50, 0.5, 100, 1, colors[randint(0,7)])
wall2 = house(50, 99.5, 100, 1, colors[randint(0,7)])
wall3 = house(0.5, 50, 1, 100, colors[randint(0,7)])
wall4 = house(99.5,50,1,100,colors[randint(0,7)])

#inner cross
house1 = house(25,50,40,1,colors[randint(0,7)])
house2 = house(75,50,40,1,colors[randint(0,7)])
house3 = house(50,25,1,40,colors[randint(0,7)])
house4 = house(50,75,1,40,colors[randint(0,7)])

#central walls
house5 = house(43.25,40,7.5,1,colors[randint(0,7)])
house6 = house(56.75,40,7.5,1,colors[randint(0,7)])
house7 = house(43.25,60,7.5,1,colors[randint(0,7)])
house8 = house(56.75,60,7.5,1,colors[randint(0,7)])
house9 = house(40,43.25,1,7.5,colors[randint(0,7)])
house10 = house(60,43.25,1,7.5,colors[randint(0,7)])
house11 = house(40,56.75,1,7.5,colors[randint(0,7)])
house12 = house(60,56.75,1,7.5,colors[randint(0,7)])

#sector I
sec1Radien=[]
bubble1 = sphere(pos=(65*2,1*2,15*2), radius=5*2, color = colors[randint(0,7)], material=materials.rough)
sec1Radien.append(bubble1)
bubble2 = sphere(pos=(75*2,-2*2,23*2), radius=7*2, color = colors[randint(0,7)], material=materials.rough)
sec1Radien.append(bubble2)
bubble3 = sphere(pos=(80*2,4.5*2,35*2), radius=5*2, color = colors[randint(0,7)], material=materials.rough)
sec1Radien.append(bubble3)
bubble4 = sphere(pos=(90*2,1*2,5*2), radius=6*2, color = colors[randint(0,7)], material=materials.rough)
sec1Radien.append(bubble4)
bubble5 = sphere(pos=(70*2,5.5*2,44*2), radius=6*2, color = colors[randint(0,7)], material=materials.rough)
sec1Radien.append(bubble5)
bubble6 = sphere(pos=(60*2,5*2,29*2), radius=5*2, color = colors[randint(0,7)], material=materials.rough)
sec1Radien.append(bubble6)
bubble7 = sphere(pos=(90*2,3*2,23*2), radius=5*2, color = colors[randint(0,7)], material=materials.rough)
sec1Radien.append(bubble7)
bubble8 = sphere(pos=(95*2,5*2,37*2), radius=7*2, color = colors[randint(0,7)], material=materials.rough)
sec1Radien.append(bubble8)
print sec1Radien

#sector II
dice1 = house(34.5,47,6,6,colors[randint(0,7)])
dice2 = house(29.5,42,6,6,colors[randint(0,7)])
dice3 = house(40,34,6,6,colors[randint(0,7)])
dice4 = house(47,25,6,6,colors[randint(0,7)])
dice5 = house(42,15,6,6,colors[randint(0,7)])
dice6 = house(37,10,6,6,colors[randint(0,7)])
dice7 = house(34,25,6,6,colors[randint(0,7)])
dice8 = house(29,30,6,6,colors[randint(0,7)])
dice9 = house(25,15,6,6,colors[randint(0,7)])
dice10 = house(20,12,6,6,colors[randint(0,7)])
dice11 = house(15,17,6,6,colors[randint(0,7)])
dice12 = house(11,9,6,6,colors[randint(0,7)])
dice13 = house(11,29,6,6,colors[randint(0,7)])
dice14 = house(16,34,6,6,colors[randint(0,7)])
dice15 = house(11,39,6,6,colors[randint(0,7)])

#sector III
walllab1 = house(10,54,1,8,colors[randint(0,7)])
walllab2 = house(8,62,16,1,colors[randint(0,7)])
walllab3 = house(16,58.5,1,8,colors[randint(0,7)])
walllab4 = house(5,80,10,1,colors[randint(0,7)])
walllab5 = house(10,80,1,20,colors[randint(0,7)])
walllab6 = house(8,90,5,1,colors[randint(0,7)])
walllab7 = house(10,70,10,1,colors[randint(0,7)])
walllab8 = house(30,60,1,20,colors[randint(0,7)])
walllab9 = house(28,70,5,1,colors[randint(0,7)])
walllab10 = house(26,68,1,4,colors[randint(0,7)])
walllab11 = house(26,92,1,16,colors[randint(0,7)])
walllab12 = house(34,84,17,1,colors[randint(0,7)])
walllab13 = house(42,89,1,10,colors[randint(0,7)])
walllab14 = house(40.5,94,4,1,colors[randint(0,7)])
walllab15 = house(45,70,10,1,colors[randint(0,7)])
walllab16 = house(40,70,1,12,colors[randint(0,7)])

#sector IV
walllab1 = house(60,90,1,20,colors[randint(0,7)],0.1)
walllab2 = house(90,60,20,1,colors[randint(0,7)],0.1)
walllab3 = house(60,80,10,1,colors[randint(0,7)],0.1)
walllab4 = house(80,60,1,10,colors[randint(0,7)],0.1)
walllab5 = house(80,90,20,1,colors[randint(0,7)],0.1)
walllab6 = house(90,80,1,20,colors[randint(0,7)],0.1)
walllab7 = house(70,75,1,11,colors[randint(0,7)],0.1)
walllab8 = house(75,70,11,1,colors[randint(0,7)],0.1)

#main base circle
base = cylinder(pos=(50*2,-0.5,50*2), axis=(0,0.11,0), radius=0.5, material=materials.emissive, color=color.red, opacity=0.3)
baseLamp = local_light(pos=(50*2,2,50*2), color=color.red)

#sector lights
sectorILamp = local_light(pos=(75*2,2,25*2), color=color.cyan)
sectorIILamp = local_light(pos=(25*2,2,25*2), color=color.orange)
sectorIIILamp = local_light(pos=(25*2,2,75*2), color=color.magenta)
sectorIVLamp = local_light(pos=(75*2,2,75*2), color=color.green)


# Ball
ball1 = sphere(pos=(0,0,0), color=color.cyan, radius=0.5, material=materials.rough)
ball2 = sphere(pos=(100,0,100), color=color.red, radius=0.5, material=materials.rough)
cylinderba2 = cylinder( pos=(100,0,100), axis=(0,100,0), radius=0.1, color=color.blue, material=materials.rough)
ball3 = sphere(pos=(100,0,100), color=color.red, radius=0.5, material=materials.rough)
cylinderba3 = cylinder( pos=(100,0,100), axis=(0,100,0), radius=0.1, color=color.blue, material=materials.rough)
anzahl = -2
ptext = 'Du hast bisher '+str(anzahl)+' Kugeln gefangen'
anzeige = label(text=ptext)

roam = False
lastPosition=1
while True:
    rate(300) 
    
    ball1.pos = scene.center-(0,1,0)
    cylinderba2.pos = ball2.pos
    cylinderba3.pos = ball3.pos
    anzeige.pos = scene.center
    ptext = 'Du hast bisher '+str(anzahl)+' Kugeln gefangen'
    anzeige.text = ptext
    
    roam = True

    # If in roaming mode, change center and forward according to mouse position
    if roam:
        ray = scene.mouse.ray
        newray = norm(vector(ray.x, 0, ray.z))
        angle = arcsin(dot(cross(scene.forward,newray),scene.up))
        newforward = rotate(scene.forward, axis=scene.up, angle=angle/150)
        scene.forward = newforward

    ball1.pos = scene.center-(0,1,0)

    # Falls Tastatur gedrückt...
    if scene.kb.keys:
        s = scene.kb.getkey()
        if ball1.x >= 100 and ball1.x<=200 and ball1.z>=0 and ball1.z<=100:
            b=0
            for i in sec1Radien:
                if mag(ball1.pos-i.pos)<=i.radius+0.5:
                    b=1
            
            if b==0:
                if ball1.color == color.red:
                    if   s == 'up' or s == 'w':     # vor gehen
                        lastPosition = vector(scene.center)
                        scene.center += scene.forward*mag(scene.center-scene.mouse.camera)/12

                    elif s == 'down' or s == 's':   # hinter gehen
                        lastPosition = vector(scene.center)
                        scene.center -= scene.forward*mag(scene.center-scene.mouse.camera)/12
    
                    elif s == 'left' or s == 'a':   # links gehen
                        lastPosition = vector(scene.center)
                        scene.center -= cross(scene.forward,scene.up)/6
    
                    elif s == 'right' or s == 'd':  # rechts gehen
                        lastPosition = vector(scene.center)
                        scene.center += cross(scene.forward,scene.up)/6
                
                    elif s == 'c':
                        ball1.color = color.cyan
                
                elif ball1.color==color.cyan:#catching ball
                    if s == 'up' or s == 'w':
                        lastPosition = vector(scene.center)
                        scene.center += scene.forward*mag(scene.center-scene.mouse.camera)/9
            
                    elif s == 'down' or s == 's':
                        lastPosition = vector(scene.center)
                        scene.center -= scene.forward*mag(scene.center-scene.mouse.camera)/9
                
                    elif s == 'left' or s == 'a':
                        lastPosition = vector(scene.center)
                        scene.center -= cross(scene.forward,scene.up)/4.5
    
                    elif s == 'right' or s == 'd':
                        lastPosition = vector(scene.center)
                        scene.center += cross(scene.forward,scene.up)/4.5
                
                    elif s == 'r':
                        ball1.color = color.red

            elif b==1:
                scene.center = lastPosition

        else:
            scene.center = lastPosition

    if mag(ball1.pos-ball2.pos)<1 and ball1.color == color.cyan:
        ball1.color = color.red
        ball2.color = color.cyan
        ball2.pos = (100,0,100)
        ball1.pos = (randint(1,200),0,randint(1,200))
        print str(ball1.pos)
        anzahl += 1
        
    if mag(ball1.pos-ball3.pos)<1 and ball1.color == color.cyan:
        ball1.color = color.red
        ball3.color = color.cyan
        ball3.pos = (100,0,100)
        ball1.pos = (randint(1,200),0,randint(1,200))
        print str(ball1.pos)
        anzahl += 1
        
    if mag(ball1.pos-ball2.pos)<1 and ball2.color == color.cyan:
        ball2.color = color.red
        ball1.color = color.cyan
        ball1.pos = (100,0,100)
        ball2.pos = ((randint(1,200),0,randint(1,200)))
        print str(ball2.pos)
        
    if mag(ball1.pos-ball3.pos)<1 and ball3.color == color.cyan:
        ball3.color = color.red
        ball1.color = color.cyan
        ball1.pos = (100,0,100)
        ball3.pos = ((randint(1,200),0,randint(1,200)))
        print str(ball3.pos)
    
    if ball1.color == color.red and ball2.color == color.cyan:
        ball2.pos = ball1.pos
        
    if ball1.color == color.red and ball3.color == color.cyan:
        ball3.pos = ball1.pos

    ball1.pos = scene.center-(0,1,0)

Funktionsleiste