Links und Funktionen
Sprachumschaltung

Navigationspfad


Inhaltsbereich

Bitflight_version_2

Version_7(für_Python_2.6.5).py — text/python-source, 21 KB (22061 bytes)

Dateiinhalt

'''
Created on 16.04.2011

@author: 00cc00
'''

from visual import*
from math import*
from visual.controls import *
from random import*
import threading
from time import*

#########

class brick(object):                                            #Standard Brick used for the walls
    def __init__(self,pos,frame,color): 
        self.brickw = box(width = 1,\
                         height = 1,\
                         length = 1,\
                         pos = pos,\
                         material = materials.emissive,\
                         color = color,\
                         frame = frame,\
                         opacity = 1.,\
                         twosided = False)
        
    def set_opacity(self,i):
        self.brickw.opacity = i

#########

class wall_type_1(object):                                      #Wall segment used in the world()
    def __init__(self,i): 
        self.wallf = frame()
        self.tunnel = frame()
        self.wall = []
        self.speed = speed                
        self.dead_brick = 0
        self.segment = frame()
        self.offset = i
        self.color = color.hsv_to_rgb((random(),1,1))
        for i in range(L):                                                  #building a 4x4 Matrix
            self.wall.append(brick((i,0,0),self.wallf,self.color))
        for i in range(L):
            self.wall.append(brick((i,1,0),self.wallf,self.color))
        for i in range(L):
            self.wall.append(brick((i,2,0),self.wallf,self.color))
        for i in range(L):
            self.wall.append(brick((i,3,0),self.wallf,self.color))
        self.dead_brick = randrange(0,self.anzahlGeben())                   #random brick which will be invisible
        self.wall[self.dead_brick].set_opacity(0.)
        self.axis1 = curve(frame = self.tunnel, pos=[(0,0,3*L), (0,0,0)], color=color.white)    #painting the tunnel
        self.axis2 = curve(frame = self.tunnel, pos=[(L,0,3*L), (L,0,0)], color=color.white)
        self.axis3 = curve(frame = self.tunnel, pos=[(L,L,3*L), (L,L,0)], color=color.white)
        self.axis4 = curve(frame = self.tunnel, pos=[(0,L,3*L), (0,L,0)], color=color.white)
        self.tunnel.pos = self.tunnel.pos + (-2,-2,0)               #adjusting the frames and creating a total frame
        self.wallf.pos = self.wallf.pos + (-1.5,-1.5,0)
        self.tunnel.frame = self.segment
        self.wallf.frame = self.segment
        self.segment.z = self.segment.z + L*3*-self.offset
        
    def anzahlGeben(self):              #total count of bricks
        return len(self.wall) 
    
    def listeGeben(self):               #useless
        return self.wall
    
    def set_speed(self, speed):         #speed for the movement of the wall
        self.speed = speed
        
    def move(self, speed):              #moving the wall
        self.segment.z += speed

#########
    
class Me(threading.Thread,object):          #the object controlled by the player
    def __init__(self,world,control):
        threading.Thread.__init__(self)
        self.me = box(pos = (0,0,16), size = (.3,.3,.3))
        self.points = 0                     #counter for points
        self.misses = 0                     #counter for misses
        self.streak = 0                     #counter for current streak
        self.bstreak = 0                    #counter for best streak
        self.wallpass = 0                   #used for checking the wallpass
        self.world = world
        self.labels = frame()
        self.stage = 0                      #Speedstage
        self.virtualpoints = 0
        self.button = control
        #infolabels
        self.counterScore = label(frame = self.labels,\
                                  pos=self.me.pos,\
                                  text='Score: '+str(self.points),\
                                  xoffset=0,\
                                  yoffset=25,\
                                   line = False,\
                                   height = 10)
        self.counterMisses = label(frame = self.labels,\
                                   pos=self.me.pos,\
                                   text='Misses: '+str(self.misses),\
                                   xoffset=0,\
                                   yoffset=1,\
                                   line = False,\
                                   height = 10)
        self.counterStreak = label(frame = self.labels,\
                                   pos=self.me.pos,\
                                   text='Streak: '+str(self.streak),\
                                   xoffset=0,\
                                   yoffset=-1,\
                                   line = False,\
                                   height = 10)
        self.counterBStreak = label(frame = self.labels,\
                                   pos=self.me.pos,\
                                   text='Best Streak: '+str(self.bstreak),\
                                   xoffset=0,\
                                   yoffset=-25,\
                                   line = False,\
                                   height = 10)
        self.labels.height = 40
     
    def reset(self):                                    #counter for checking if you passed a wall is reseted
        self.wallpass = 0
         
    def run(self):                                      #observes the movement of the brick
        while true:
            sleep(0.02)                              
            self.newposy = scene1.mouse.pos.y           #checks if the new position is inside the tunnel
            if (L/2 > self.newposy > -L/2):             #L is the width/height of the tunnel so you have to check L/2 to the left/right/bottom/top
                self.me.y = self.newposy                #sets new pos
                scene1.forward = -self.me.pos           #sets new camera pos
                self.labels.y = self.newposy            #sets new label pos
            self.newposx = scene1.mouse.pos.x
            if (L/2 > self.newposx > -L/2):             #same as above but for the x-axis
                self.me.x = self.newposx
                scene1.forward = -self.me.pos
                self.labels.x = self.newposx
            if (self.me.z < self.world.stage[0].segment.pos.z-0.5)and self.wallpass == 0: #checks if you reached a wall
                self.wallpass += 1              #is needed for, that you do not count the wall multiple times
#the next line checks if you are inside the invisible block: self.world.stage[0].wall[self.world.stage[0].dead_brick].brickw.y returns the y pos of the invisible brick and so on
                if (self.world.stage[0].wall[self.world.stage[0].dead_brick].brickw.y-1.18 > self.me.y > self.world.stage[0].wall[self.world.stage[0].dead_brick].brickw.y-1.82) and (self.world.stage[0].wall[self.world.stage[0].dead_brick].brickw.x-1.18 > self.me.x > self.world.stage[0].wall[self.world.stage[0].dead_brick].brickw.x-1.82):
                    self.points += 1        #points + 1
                    self.virtualpoints += 1     #virtual points are needed for the speed up ever 20 points
                    self.counterScore.text = 'Score : '+str(self.points)  #label update
                    self.streak +=1         #streak + 1
                    self.counterStreak.text = 'Streak : '+str(self.streak)  #label update
                    if self.streak >= self.bstreak:     #checks if your current streak is better then the previous
                        self.bstreak = self.streak      #new best streak
                        self.counterBStreak.text = 'Best Streak : '+str(self.bstreak) #label update
                else:
                    self.misses += 1    #otherwise misses +1
                    self.counterMisses.text = 'Misses : '+str(self.misses)  #label update
                    self.streak =0      #resets current streak
                    self.counterStreak.text = 'Streak : '+str(self.streak)  #label update
            if self.virtualpoints == next_stage:        #checks if a speed up is required
                self.stage += 1                         #speed up + 1
                self.virtualpoints = 0                  #resets virtual points
                self.world.set_speed(speed+0.01*self.stage) #update of the global speed
            if self.misses == self.button.life_points:           #checks if you are dead    
                self.world.dead = True     
                #game over label                #sets the global dead to True 
                self.GameOver = label(frame = self.labels,\
                                      pos=self.labels.world_to_frame(self.me.pos),\
                                      text='Game Over',\
                                      xoffset=0,\
                                      yoffset=100,\
                                      line = False,\
                                      height = 40)
                break   #quits the while true: loop

#########
                      
class brick_effect(object):             #standard brick used for the screen effects
    def __init__(self,pos,frame,color):
        self.bricke = box(width = 1,\
                         height = 1,\
                         length = 1,\
                         pos = pos,\
                         material = materials.emissive,\
                         color = color,\
                         frame = frame,\
                         opacity = 1.,\
                         twosided = False)
        
    def set_o_s_p(self,i,size,pos):             #set size, opacity, pos function
        self.bricke.opacity = i
        self.bricke.size = size
        self.bricke.pos = pos
    
    def set_o(self,i):                           #set opacity
        self.bricke.opacity = i
        
    def move(self,vector):# move the brick
            self.bricke.pos = self.bricke.pos + vector/80
			
#########

class screen_effect(object):                    #single screen effect
    def __init__(self, xoffset, yoffset, world): 
        self.effect = []                        #list of bricks
        self.effect_frame = frame()             #frame for the bricks
        self.move = []                          #list of movement vectors for the bricks, can be implemented in the self.effect list too
        self.timer = 0                          #timer for the lifetime
        self.xoffset = xoffset                  #unused
        self.yoffset = yoffset                  #unused
        self.world = world                      #reference to the world
        
    def build(self):                            # first time buildup
        for i in range (particle_count):        #creates particle_count bricks and sets a movement vector for them
            self.effect.append(brick_effect(vector(0,0,-30), self.effect_frame, color = self.world.stage[0].color))
            self.effect[i].set_o_s_p(.1,(.5,.5,.5),vector(0,0,-2*L))
            self.move.append(vector(randrange(-4,4),randrange(-4,4),randrange(-4,4)))
          
#########

class effect_manager(threading.Thread,object):      #manages the effects
    def __init__(self, offset, timer, world, me):
        threading.Thread.__init__(self)
        self.effect_list = []
        self.offset = offset
        self.timer = timer
        self.world = world                  #references to world and me
        self.me = me
        self.set = 1
        self.counter = 0                    #internal couters
        self.wait = 0
        self.superstreak = 0
                
    def run(self):
        while True:
            sleep(0.02)   
            if self.world.dead == True:
                for i in range(self.counter):
                    self.effect_list[0].effect_frame.visible = False
                    self.effect_list.remove(self.effect_list[0])
                self.counter = 0
                break                            
            if (self.world.stage[0].segment.pos.z > self.me.me.z) and (self.set == 1) and (self.world.stage[0].wall[self.world.stage[0].dead_brick].brickw.y-1.18 > self.me.me.y > self.world.stage[0].wall[self.world.stage[0].dead_brick].brickw.y-1.82) and (self.world.stage[0].wall[self.world.stage[0].dead_brick].brickw.x-1.18 > self.me.me.x > self.world.stage[0].wall[self.world.stage[0].dead_brick].brickw.x-1.82):
                    self.set = 0
                    self.effect_list.append(screen_effect(self.offset, self.offset, self.world))  
                    self.effect_list[-1].build()     
                    self.counter +=1                
            if len(self.effect_list) > 0:
                for i in range(self.counter):
                    self.ii = i
                    if self.effect_list[self.ii-1].timer <= L*60:
                        for i in range(particle_count):
                            self.effect_list[self.ii-1].effect[i].move(self.effect_list[self.ii-1].move[i])
                            self.effect_list[self.ii-1].effect[i].set_o(0.1*self.me.streak/2)
                    self.effect_list[self.ii-1].timer +=1
                for i in range(self.counter):
                    if self.effect_list[0].timer > L*60:
                        self.effect_list[0].effect_frame.visible = False
                        self.effect_list.remove(self.effect_list[0])
                        self.counter -= 1
            self.wait += 1                
            if self.wait >= 10:
                self.set = 1
                self.wait = 0        

#########
               
class world(threading.Thread, object):
    def __init__(self):
        threading.Thread.__init__(self)            
        self.stage = []
        self.step = 3.364
        self.speed = speed
        self.dead = False
    
    def set_speed(self, speed):
        self.speed = speed
        
    def dead(self,input):
        if input == 1:
            self.dead = True
        
    def run(self):
        for i in range(5):
            self.stage.append(wall_type_1(i))
        while True:
            rate (50)
            for i in self.stage:
                i.move(self.speed)
                if self.stage[0].segment.z > 20:
                    i.tunnel.visible = False
                    i.wallf.visible = False        
                    self.stage.remove(self.stage[0])
                    del i
                    me.reset()
                    self.stage.append(wall_type_1(self.step))
            if self.dead == True:
                break
        for i in range(len(self.stage)):
            self.stage[0].tunnel.visible = False
            self.stage[0].wallf.visible = False        
            self.stage.remove(self.stage[0])
            
######

class brickr(object):
    def __init__(self,size,pos,frame,color): 
        self.brickr = box(size = size,\
                         pos = pos,\
                         material = materials.emissive,\
                         color = color,\
                         frame = frame,\
                         opacity = 1.,\
                         twosided = False)

######

class rotator(threading.Thread,object):
    def __init__(self, pos, me):
        threading.Thread.__init__(self)
        self.step_1 = frame()
        self.step_2 = frame()
        self.step_3 = frame()
        self.all = frame()
        self.color = color.hsv_to_rgb((random(),1,1))
        self.list = []
        self.rotation = []
        self.me = me
        self.step_1.pos = pos
        self.step_2.pos = pos
        self.step_3.pos = pos
        self.step_1.frame = self.all
        self.step_2.frame = self.all
        self.step_3.frame = self.all
        
    def run(self):
        self.list.append(brickr((1.,1.,4),(randrange(-3, 4),randrange(-3, 4),randrange(-3, 4)), self.step_1, self.color))
        self.list.append(brickr((1.,4,1.),(randrange(-3, 4),randrange(-3, 4),randrange(-3, 4)), self.step_1, self.color))
        self.list.append(brickr((1.,1.,4),(randrange(-3, 4),randrange(-3, 4),randrange(-3, 4)), self.step_1, self.color))
        self.list.append(brickr((4,1.,1.),(randrange(-3, 4),randrange(-3, 4),randrange(-3, 4)), self.step_1, self.color))
        self.color = color.hsv_to_rgb((random(),1,1))
        self.list.append(brickr((1.,1.,.4),(randrange(-2, 3),randrange(-2, 3),randrange(-2, 3)), self.step_2, self.color))
        self.list.append(brickr((1.,.4,1.),(randrange(-2, 3),randrange(-2, 3),randrange(-2, 3)), self.step_2, self.color))
        self.list.append(brickr((1.,1.,.4),(randrange(-2, 3),randrange(-2, 3),randrange(-2, 3)), self.step_2, self.color))
        self.list.append(brickr((.4,1.,1.),(randrange(-2, 3),randrange(-2, 3),randrange(-2, 3)), self.step_2, self.color))
        self.color = color.hsv_to_rgb((random(),1,1))
        self.list.append(brickr((1.,1.,.4),(randrange(-5, 6),randrange(-5, 6),randrange(-5, 6)), self.step_3, self.color))
        self.list.append(brickr((1.,.4,1.),(randrange(-5, 6),randrange(-5, 6),randrange(-5, 6)), self.step_3, self.color))
        self.list.append(brickr((1.,1.,.4),(randrange(-5, 6),randrange(-5, 6),randrange(-5, 6)), self.step_3, self.color))
        self.list.append(brickr((.4,1.,1.),(randrange(-5, 6),randrange(-5, 6),randrange(-5, 6)), self.step_3, self.color))
        while True:
            sleep(0.02)
            self.step_1.rotate(angle=radians(0.7*self.me.streak/5), axis = (0,1,0))
            self.step_2.rotate(angle=radians(-1*self.me.streak/5), axis = (1,1,0))
            self.step_3.rotate(angle=radians(-2*self.me.streak/5), axis = (-1,1,-1))
            self.all.rotate(angle=radians(0.1*self.me.streak/5), axis = (0,0,1), origin = (0,0,-10))
            for i in range(len(self.list)):
                self.rotation.append(vector(randrange(-1,2), randrange(-1,2), randrange(-1,2)))
                self.list[i].brickr.rotate(angle=radians(-1), axis = (self.rotation[i]))

######


 
class buttons(threading.Thread,object):
        def __init__(self):
            threading.Thread.__init__(self)
            self.speed = 1
            self.life_points = 1
            self.startg = 0
            self.intro = button(display = c.display, text = 'Bitflight', width=105, height=40,pos = (-40, 80))
            self.life_plus = button(display = c.display, pos=(-40,40), width=105, height=30, text='+', action=lambda: self.set_lifepoints(1))
            self.life_minus = button(display = c.display, pos=(-40,-20), width=105, height=30, text='-', action=lambda: self.set_lifepoints(-1))
            self.life_text = button(display = c.display, pos=(-40,10), width=105, height=30, text='Lifepoints: 1')
#            self.speed_plus = button(display = c.display, pos=(65,40), width=105, height=30, text='+', action=lambda: self.set_speed(1))
#            self.speed_minus = button(display = c.display, pos=(65,-20), width=105, height=30, text='-', action=lambda: self.set_speed(-1))
#            self.speed_text = button(display = c.display, pos=(65,10), width=105, height=30, text='Speed: 1')
            self.play_button = button(display = c.display, pos=(-40,-50), width=105, height=40, text='Play', action=lambda: self.play())
        
        def set_lifepoints(self, i):
            self.life_points += i
            if self.life_points < 1:
                self.life_points = 1
            if self.life_points > 30:
                self.life_points = 30
            self.life_text.text = 'Lifepoints: '+str(self.life_points)
            
#        def set_speed(self, i):
#            self.speed += i
#            if self.speed < 1:
#                self.speed = 1
#            if self.speed > 3:
#                self.speed = 3
#            self.speed_text.text = 'Speed: '+str(self.speed)
        
        def play(self):
            scene1.visible = True
            self.startg = 1
            run_game()
            
        
        def run(self):
            while self.startg == 0:
                sleep(0.01)
                c.interact()



#Initialisierung        
scene1 = display(title='Bitflight',\
                 width=500,\
                 height=500,\
                 center=(0,0,0),\
                 forward = (0, 0, -1),\
                 autoscale = False,\
                 userzoom = False,\
                 userspin = False,\
                 fullscreen = True)
c = controls()   
scene1.fullscreen = True
scene1.visible = False
L = 4                                  
particle_count = 30                     
start = 0                           
speed = 0.2                            
next_stage = 10
w = world()
b = buttons()
me = Me(w,b)
r1 = rotator((-10,-10,-10), me)
r2 = rotator((-10,10,-10), me)
r3 = rotator((10,-10,-10), me)
r4 = rotator((10,10,-10), me)
effects = effect_manager(L*2, 0, w, me)
startlabel = label(pos=me.me.pos,\
                    text='Press Space',\
                    xoffset=0,\
                    yoffset=100,\
                    line = False,\
                    height = 40)

b.start()

def run_game():
    global start
    while start == 0:
        sleep(0.02)
        if scene1.kb.keys:
            s = scene1.kb.getkey()
            if s == ' ':  
                startlabel.visible = False    
                sleep(0.03)
                w.start()
                sleep(0.03)
                me.start()
                sleep(0.03)
                effects.start()
                sleep(0.03)
                r2.start()
                sleep(0.03)
                r1.start()
                sleep(0.03)
                r3.start()
                sleep(0.03)
                r4.start()
                start = 1       



    

Funktionsleiste