Shooter
shooter_step_2.py — Python Source, 10 KB (10499 bytes)
Dateiinhalt
# -*- coding: cp1252 -*- from visual import * #import time ''' basisklassen f�r 3d shooter ''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' class World: def __init__(self): self.player = None self.player = controller_player(self) self.room = None self.room = controller_room(self) pick = None scene.scale = vector((5,5,5)) scene.userzoom = False while True: self.player.on_update() sleep(0.02) '''if scene.mouse.events: currentevent = scene.mouse.getevent() if currentevent.drag and currentevent.pick == self.player.model.basemesh: drag_pos = currentevent.pickpos pick = currentevent.pick elif currentevent.drop: pick = None if pick: new_pos = scene.mouse.project(normal=(0,0,1)) if new_pos != drag_pos: pick.pos = new_pos drag_pos = new_pos''' def potential_collision(self,ctrl_1,ctrl_2,velocity_vec): #potential_collision(moving controller, stationary controller, moving controller velocity vector) for i in ctrl_1.bbox: for j in ctrl_2.bbox: if(self.potential_collision_exec(i,j,velocity_vec) == True): return True def potential_collision_exec(self,ctrl_1,ctrl_2,velocity_vec): pos_1 = ctrl_1.bbox_field[0] pos_2 = ctrl_1.bbox_field[1] potential_bbox = [] potential_bbox.append(pos_1 + velocity_vec) potential_bbox.append(pos_2 + velocity_vec) returnlist = [] self.field_bbox1 = potential_bbox self.field_bbox2 = ctrl_2.bbox_field x1bb1 = self.field_bbox1[1].x y1bb1 = self.field_bbox1[1].y z1bb1 = self.field_bbox1[1].z x0bb1 = self.field_bbox1[0].x y0bb1 = self.field_bbox1[0].y z0bb1 = self.field_bbox1[0].z x1bb2 = self.field_bbox2[1].x y1bb2 = self.field_bbox2[1].y z1bb2 = self.field_bbox2[1].z x0bb2 = self.field_bbox2[0].x y0bb2 = self.field_bbox2[0].y z0bb2 = self.field_bbox2[0].z x0bb2_in = (x0bb2 < x1bb1 and x0bb2 > x0bb1) x1bb2_in = (x1bb2 < x1bb1 and x1bb2 > x0bb1) x0bb1_in = (x0bb1 < x1bb2 and x0bb1 > x0bb2) x1bb1_in = (x1bb2 < x1bb2 and x1bb2 > x0bb2) y0bb2_in = (y0bb2 < y1bb1 and y0bb2 > y0bb1) y1bb2_in = (y1bb2 < y1bb1 and y1bb2 > y0bb1) y0bb1_in = (y0bb1 < y1bb2 and y0bb1 > y0bb2) y1bb1_in = (y1bb2 < y1bb2 and y1bb2 > y0bb2) z0bb2_in = (z0bb2 < z1bb1 and z0bb2 > z0bb1) z1bb2_in = (z1bb2 < z1bb1 and z1bb2 > z0bb1) z0bb1_in = (z0bb1 < z1bb2 and z0bb1 > z0bb2) z1bb1_in = (z1bb2 < z1bb2 and z1bb2 > z0bb2) if (((x0bb2_in) or (x1bb2_in) or (x0bb1_in) or (x1bb1_in))and ((y0bb2_in) or (y1bb2_in) or (y0bb1_in) or (y1bb1_in))and ((z0bb2_in) or (z1bb2_in) or (z0bb1_in) or (z1bb1_in))): #print "True" return True else: #print "False" return False '''def create_player(self): self.player.append(controller_player()) self.player.append(model_player()) self.player.append(self.player[1].generate_bbox()) def create_room(self): self.room.append(controller_room()) self.room.append(model_room()) self.room.append(self.room[1].generate_bbox())''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' class bbox: def __init__(self,pos_,h,l,w,visible_): #self.basemesh = box(pos = pos_, height = h, length = l, width = w,visible = visible_) #return self.basemesh self.bbox_field = [] self.bbox_field.append(pos_) self.bbox_field.append(vector((pos_[0] + l,pos_[1] + h, pos_[2] +w))) print self.bbox_field def on_update(self,pos_,h,l,w): self.bbox_field = [] self.bbox_field.append(pos_) self.bbox_field.append(vector((pos_[0] + l,pos_[1] + h, pos_[2] +w))) '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' class controller_player: def __init__(self,world): self.model = [] self.bbox = [] self.create_player() self.world = world self.velocity = vector((0,0,0)) self.gravity = vector((0,0.02,0)) self.ongoundvar = 0 def create_player(self): self.model.append(model_player()) self.bbox.append(bbox(vector((-0.5,0.5,-0.5)),1,1,1,False)) for i in self.model: i.basemesh.visible = False def on_update(self): for i in self.model: for j in self.bbox: j.on_update(i.basemesh.pos-(vector(0.5,0.5,0.5)),1,1,1) vel_x = vector((self.velocity.x,0,0)) if(self.world.potential_collision(self,self.world.room,vel_x) == True): self.velocity.x = 0 vel_y = vector((0,self.velocity.y,0)) if(self.world.potential_collision(self,self.world.room,vel_y) == True): if(vel_y.y < 0): self.ongroundvar = 0 self.velocity.y = 0 vel_z = vector((0,0,self.velocity.z)) if(self.world.potential_collision(self,self.world.room,vel_z) == True): self.velocity.z = 0 for i in self.model: i.basemesh.pos += self.velocity scene.center = i.basemesh.pos self.velocity -= self.gravity if scene.kb.keys: i = scene.kb.getkey() if(i == "w"): dir_vec = vector(scene.forward.x,0,scene.forward.z) y_component = self.velocity.y self.velocity = norm(dir_vec) *0.4 self.velocity.y = y_component elif(i == "a"): dir_vec = vector(scene.forward.x,0,scene.forward.z) y_component = self.velocity.y self.velocity = -(norm(dir_vec) *0.4) self.velocity.y = y_component self.velocity = rotate(self.velocity, -0.5*pi, (0,1,0) ) elif(i == "s"): dir_vec = vector(scene.forward.x,0,scene.forward.z) y_component = self.velocity.y self.velocity = -(norm(dir_vec) *0.4) self.velocity.y = y_component elif(i == "d"): dir_vec = vector(scene.forward.x,0,scene.forward.z) y_component = self.velocity.y self.velocity = -(norm(dir_vec) *0.4) self.velocity.y = y_component self.velocity = rotate(self.velocity, 0.5*pi, (0,1,0) ) elif(i == " "): if(self.ongroundvar == 0): self.velocity.y = 0.3 self.ongroundvar = 1 print "space" else: '''self.velocity.x /= 2 self.velocity.z /= 2''' pass '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' class model_player: def __init__(self): self.basemesh = sphere(pos = (0,0,0),material = materials.wood,color = (0.25,0.19,0),radius=0.5) #return self.basemesh def on_update(self): pass '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' class controller_room: def __init__(self,world): self.model = [] self.bbox = [] self.create_room() def create_room(self): self.model.append(model_room(vector((0,-6,-30)),4,60,1)) #wall 1 self.model.append(model_room(vector((0,-6, 30)),4,60,1)) #wall 2 self.model.append(model_room(vector((-30,-6,0)),4,1,60)) #wall 3 self.model.append(model_room(vector((30,-6,0)),4,1,60)) #wall 4 self.model.append(model_room(vector((0,-8.5,0)),1,60,60)) self.bbox.append(bbox(vector((-30,-8,-30.5)),4,60,1,False)) #wall 1 self.bbox.append(bbox(vector((-30,-8, 29.5)),4,60,1,False)) #wall 2 self.bbox.append(bbox(vector((-30.5,-8,-30)),4,1,60,False)) #wall 3 self.bbox.append(bbox(vector((29.5,-8,-30)),4,1,60,False)) #wall 4 self.bbox.append(bbox(vector((-30,-9,-30)),1,60,60,False)) def on_update(self): pass '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' class model_room: def __init__(self,pos_,h,l,w): self.basemesh = box(pos = pos_,material = materials.wood,color = (1,1,1),height = h, length = l, width = w) def on_update(self): pass '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' class controller_bullet: def __init__(self,world,direction,creator): self.model = [] self.bbox = [] self.create_bullet() self.direction = direction self.creator = creator def create_bullet(self): self.model.append(model_bullet(self.direction,self.creator.model[0].basemesh.pos)) self.bbox.append(bbox(self.creator.model[0].basemesh.pos - vector((0.05,0.05,0.05)),0.1,0.1,0.1,False)) def on_update(self): pass class model_bullet: def __init__(self,direction,pos_): self.basemesh = box(pos = pos_,color = (1,0,0), lenght = 0.1, width = 0.1, height = 0.1) def on_update(self): pass x = World()