Links und Funktionen
Sprachumschaltung

Navigationspfad


Inhaltsbereich

tunnelrennen5a.py

Für das Spiel textur2.tga in das selbe Verzeichnis wie das Skript kopieren!

tunnelrennen5a.py — text/python-source, 12 KB (12499 bytes)

Dateiinhalt

#! /bin/bash /usr/bin/python2.6
# -*- coding: utf-8 -*-


'''
Created on 15.04.2011 - 20-04-2011, Probestudium Informatik LMU 2011

@author: Florian

Der Spieler faehrt auf einem sich drehenden Ring. Mit der Maus kann das Auto um den Ring rotiert werden. Es geht darum, moeglich
'''

### 

from visual import*
from visual.controls import *
from random import*
from math import *
import thread
import Image




class tunnelrennen():
    def __init__(self):
        self.scene = display(height = 1000,\
                        width = 1000,\
                        fullscreen=True,\
                        forward=(-1,-0.5,0)  ,\
                        up = (-1,0,0),\
                        fov = 2,\
                        center=(0,0,0),\
                        range = 1.5,\
                        userspin = False,\
                        userzoom = False,\
                        autoscale = False)          # Programmfenster
        
        ### Erstellung der Textur fuer den Tunnel
        
        #textur = "textur"                           
        #im = Image.open("textur.tga")    # Oeffnen des Bildes
        #materials.saveTGA(textur,im)      # Speichern des Bildes intern
        #self.pano = materials.texture(data=im, mapping="spherical")  # Darstellungsart
        
        textur2 = "textur2"                           
        im2 = Image.open("textur2.tga")    # Oeffnen des Bildes
        materials.saveTGA(textur2,im2)      # Speichern des Bildes intern
        self.pano2 = materials.texture(data=im2, mapping="spherical")  # Darstellungsart
        
        self.scene.title=("Weiche den Hindernissen aus!")
        self.welt = frame()
        self.system = frame(frame = self.welt)
        self.bahn = ring(frame = self.system, axis=(0,1,0), radius = 20, thickness = .8, material = materials.diffuse, twosided = false , opacity = 0.5)
        self.tunnel = ring(frame = self.system, axis = (0,1,0), radius = 20, thickness = 3, material = self.pano2)
        self.auto = box(frame = self.welt,color = color.red, length = 1, height = .3, width = .3, y = self.bahn.thickness+.15, z = self.bahn.radius)
        self.hitL = label(frame = self.welt,text="Rundenpunkte: \n Gesamtpunkte:", space=0.2, pos = self.auto.pos, opacity=0.25, yoffset = 10)
        self.StartL = label(line = false,frame = self.welt,space = 0.2, pos = self.auto.pos, opacity = 0.25, text = "Sammle moeglichst viel Hits!! \n Start mit Leertaste",yoffset = 70)
        self.origin = vector(0,0,self.bahn.radius)
        self.mode = 1
        self.start = 0
        self.ListHindernisse = []
        #lamp = local_light(pos=(0,0,0), color=color.yellow)
        self.scene.center=self.auto.pos
        self.drehwinkel = 0
        self.hits = 0
        self.hits2 = 0
        self.round = 1
        if self.scene.fullscreen==False:
            self.cwindow = controls(title = "Einstellungen", height = 400, width = 400)
            view = menu(text = "Sichtmodus", pos = (0,50), height = 20, width = 140)
            view.items.append(("Bewegung mit Auto", lambda: self.sichtModus(1)))
            view.items.append(("Unbewegter Beobachter", lambda: self.sichtModus(0)))
    
    
    def fahren(self):
        self.auto.rotateWinkel=0
        self.auto.rotateWinkel2 =0
        while true:
            rate(60)
            if self.start == 1:
                x=self.scene.center
                y = self.scene.mouse.pos.z
                rotateWinkel = radians(6*(self.scene.mouse.pos.z-x.z))
                if rotateWinkel >=0.08 or rotateWinkel <=-0.08:
                    rotateWinkel = abs(rotateWinkel)/rotateWinkel*0.07
                if rotateWinkel >=0.12 or rotateWinkel <=-0.12:
                    rotateWinkel = 0
                #print rotateWinkel
                self.auto.rotate(angle=rotateWinkel, axis = (1,0,0), origin = (0,0,self.bahn.radius))
                if self.mode == 1:
                    self.welt.rotate(angle=-rotateWinkel, axis =(1,0,0), origin = (0,0,self.bahn.radius))
                self.system.rotate(angle=radians(0.4*(sqrt(self.round)/1.5)), axis = (0,1,0))
                self.drehwinkel = self.drehwinkel  + 0.4*(sqrt(self.round)/1.5)
                self.scene.center=(self.welt.frame_to_world(self.auto.pos).x,self.welt.frame_to_world(self.auto.pos).y,self.welt.frame_to_world(self.auto.pos).z)
                self.scene.forward=(-1,-self.welt.frame_to_world(self.auto.pos).y/2, 0)
                self.scene.up=(self.auto.pos.y,self.auto.pos.z,self.auto.pos.x)
                self.hitL.pos = self.auto.pos
                self.auto.rotateWinkel = (self.auto.rotateWinkel + rotateWinkel)%(2*pi)
                self.auto.rotateWinkel2 =(self.auto.rotateWinkel2+rotateWinkel)%(2*pi)
            elif self.start == 2:
                break
    
    
    def hindernisse2(self,runde):
        "Setzen der Hindernisse fuer eine Runde"
        pos = vector(0,0,self.bahn.radius)           # Erstes Hinderniss wird bei Startposition des self.autos gesetzt
        winkel = 10                 # Berechnung des Winkelabstands zwischen zwei Hindernissen, abhaengig von der Runde
        self.ListHindernisse.append((ring(frame=self.system,pos = pos, radius = 2, thickness = 0.3, rotateWinkel = pi/2,\
                                     color = color.orange)))
        #lamp = local_light(frame = system,pos=pos, color=color.orange)
        pos=vector(pos.x*cos(radians(winkel))-pos.z*sin(radians(winkel)),pos.y,\
                      pos.z*cos(radians(winkel))+pos.x*sin(radians(winkel)))
        thread.start_new(self.Effekt2, (pos,))
        for i in range(36):
    
            self.ListHindernisse.append((box(frame=self.system, pos=pos, length=0.1,\
                                color=color.hsv_to_rgb((random(),1,1)),\
                                height=self.tunnel.thickness*2, width=0.1, axis=(pos.z,pos.y,-pos.x)))) # neues Hindernis an Liste anhaengen
            rotateWinkel = randrange(0,180)
            self.ListHindernisse[-1].rotate(angle=radians(rotateWinkel), axis=(pos.z,pos.y,-pos.x)) # Hindernis nach Zufall um Bahn drehen
            self.ListHindernisse[-1].rotateWinkel = rotateWinkel
            pos=vector(pos.x*cos(radians(winkel))-pos.z*sin(radians(winkel)),pos.y,\
                       pos.z*cos(radians(winkel))+pos.x*sin(radians(winkel)))   # Berechnung der neuen Hindernisposition mit Vektordrehung
        
    
    
     
    
                    
    def hits2m(self):
        alphaA = acos(vector(0,self.bahn.thickness,0).dot(vector(0,self.bahn.thickness,0.5*self.auto.width)) / \
                      (mag(vector(0,self.bahn.thickness,0))*mag(vector(-0.5*self.auto.width,self.bahn.thickness,0))))
        alphaB = acos(vector(0,self.bahn.thickness,0).dot(vector(0,self.bahn.thickness,0.05)) / \
                      (mag(vector(0,self.bahn.thickness,0))*mag(vector(-0.05,self.bahn.thickness,0))))
        alphaG = (alphaA + alphaB) / 2
        #print "aA =", alphaA
        #print "aB =", alphaB
        while true:
            time.sleep(0.02)
            for obj in self.ListHindernisse:
                #print system.frame_to_world(obj.pos).x
                
                if self.system.frame_to_world(obj.pos).x<=0.8 and self.system.frame_to_world(obj.pos).x>=-0.8 and self.system.frame_to_world(obj.pos).z>self.bahn.radius-0.5:
                    #objVector = vector(system.frame_to_world(obj.axis).x,system.frame_to_world(obj.axis).z,system.frame_to_world(obj.axis).y)
                    winkel1 = acos(((self.auto.pos-self.origin).dot(vector(0,self.bahn.thickness,0))) / (mag(self.auto.pos-self.origin)*mag ( vector(self.bahn.thickness,0))))
                    #winkel1 = self.auto.rotateWinkel2
                    winkel2a = radians(obj.rotateWinkel)
                    winkel2b = radians(180-obj.rotateWinkel)
                    winkela = abs(winkel2a-winkel1)
                    winkelb = abs(winkel2b-winkel1)
                    #print obj,system.frame_to_world(obj.pos), self.auto.pos-origin, degrees(winkela)
    
                    if winkela <= alphaG or winkelb <= alphaG or 2*pi-winkel2a <=alphaG or 2*pi-winkel2b <= alphaG:
                        self.hits=self.hits+1
                        self.hits2 = self.hits2 +1
                        self.hitL.text = "Rundenpunkte: "+str(self.hits)+"\n Gesamtpunkte: "+ str(self.hits2)
                        thread.start_new(self.Effekt, (obj.color,))
                        #print winkela,"   ",winkelb
                        while self.system.frame_to_world(obj.pos).x<=0.8:
                            time.sleep(0.02)
                            pass





    def neueHindernisse(self):
        while true:
            time.sleep(0.1)
            if self.drehwinkel > 360*(self.round-1) and self.hits < 10 and self.round > 1:
                self.start = 2
                self.StartL.pos = self.auto.pos
                self.StartL.visible = True
                self.StartL.text = "GAME OVER \n Du haettest mindestens 10 Punkte \n pro Runde erreichen muessen!! \n Press escape!"
                break                
            elif self.drehwinkel > 360*(self.round-1):# and (hits >=15 or round ==1) :
                self.hits = 0
                for obj in self.ListHindernisse:
                    obj.visible = False
                self.ListHindernisse=[]
                #lamp.visible = false
                self.hindernisse2(round)
                self.round=self.round+1
                time.sleep(5)
                                
    def Effekt(self,color):
        boxList = []
        for i in range(30):
            boxList.append(box(frame=self.welt,pos=self.auto.pos + vector(-1,0,0),width=0.1,height=0.1,length=0.1,\
                               color=color))
            boxList[-1].v=vector(-0.1,uniform(-0.5,0.5),uniform(-0.5,0.5))
        for i in range(50):
            for obj in boxList:
                obj.pos=obj.pos+obj.v
            time.sleep(0.02)
        for obj in boxList:
            obj.visible = false
        return None
    
    def Effekt2(self,pos):
        boxList = []
        for i in range(100):
            boxList.append(sphere(frame = self.welt, pos = pos,color=color.hsv_to_rgb((random(),1,1)),\
                                  radius = 0.5, v = vector(-0.1,uniform(-0.5,0.5),uniform(-0.5,0.5)) ))
        for i in range(25):
            for obj in boxList:
                obj.pos = obj.pos + obj.v
            time.sleep(0.05)
        for obj in boxList:
            obj.visible = false
    
    def sichtModus(self,modeN):
        global mode
        if modeN !=mode:
            if modeN == 1:
                self.welt.rotate(angle=-self.auto.rotateWinkel, axis =(1,0,0), origin = (0,0,self.bahn.radius))
            if modeN ==0:
                self.welt.rotate(angle=self.auto.rotateWinkel, axis =(1,0,0), origin = (0,0,self.bahn.radius))
            mode = modeN
    
    def sichtModusKey(self):
        while true:
            time.sleep(0.1)
            if self.scene.kb.keys !=0:
                s = self.scene.kb.getkey()
                if s=="v":
                    if self.mode == 0:
                        self.welt.rotate(angle=-self.auto.rotateWinkel, axis =(1,0,0), origin = (0,0,self.bahn.radius))
                    elif self.mode == 1:
                        self.welt.rotate(angle=self.auto.rotateWinkel, axis =(1,0,0), origin = (0,0,self.bahn.radius))
                    self.mode = (self.mode+1)%2
                    time.sleep(1)
    
    
    def startstop(self):

        while true:
            time.sleep(0.1)
            if self.scene.kb.keys !=0:
                s = self.scene.kb.getkey()
                if s==" ":
                    if self.start == 0:
                        self.StartL.visible=false
                    elif self.start == 1:
                        self.StartL.text = "Pause"
                        self.StartL.pos = self.auto.pos
                        self.StartL.visible = true
                    self.start = (self.start+1)%2
                    time.sleep(0.5)        
    
    def run(self):
        thread.start_new(self.startstop, ())           
        thread.start_new(self.fahren, ())
        thread.start_new(self.neueHindernisse, ())
        thread.start_new(self.hits2m, ())
        thread.start_new(self.sichtModusKey,())
        if self.scene.fullscreen==false:
            while true:
                rate(10)
                self.cwindow.interact()

x = tunnelrennen()
x.run()

Funktionsleiste