Давайте пограємо зі змією/Камера

Матеріал з Вікіпідручника
Перейти до навігації Перейти до пошуку
class Camera:
	def __init__(self,x,y,z,a,h=0):
		self.x = x
		self.y = y
		self.z = z
		self.ux = 0
		self.uy = 1
		self.uz = 0
		self.a = a
		self.h = h
		self.update_vel()
	def lookat(self):
		glMatrixMode(GL_MODELVIEW)
		glLoadIdentity()
		gluLookAt(self.x,self.y,self.z,self.x+self.dx,self.y+self.dy,self.z+self.dz,self.ux,self.uy,self.uz)

	def update_vel(self):
		ch = math.cos(self.h)
		self.dx = math.cos(self.a)*ch
		self.dz = math.sin(self.a)*ch
		self.dy = math.sin(self.h)

	def steer(self,da):
		self.a+=da
		if self.a>math.pi*2:
			self.a -= math.pi*2
		if self.a<0:
			self.a+= math.pi*2
		self.update_vel()
	
	def pitch(self,dh):
		self.h+=dh
		if self.h>math.pi/2.1:
			self.h = math.pi/2.1
		if self.h<-math.pi/2.1:
			self.h = - math.pi/2.1
		self.update_vel()
	
	def lift(self,dy):
		self.y += dy
	
	def forward(self,l):
		self.x+=self.dx*l
		self.y+=self.dy*l
		self.z+=self.dz*l
	
	def drawcursor(self):
		if self.dy>-0.1:
			return
		t = - self.y / self.dy
		self.curx = self.x + t* self.dx
		self.curz = self.z + t* self.dz

		glColor(0.5,0,0)
		cube(self.curx,0,self.curz,sc = 0.2)

	def addcube(self,what):
		xx = int(self.curx)
		yy = int(self.curz)
		if (0<= xx < SX) and (0<= yy < SY):
			field[xx,yy] = what
	
	def point_at_cube(self,x1,y1,z1,x2,y2,z2):
		if abs(self.dx)>= max(abs(self.dy),abs(self.dz)): # go throught x axis
			t = (x1 - self.x)/self.dx
			py = self.y + t*self.dy
			pz = self.z + t*self.dz

			if (y1 < py < y2) and (z1 < pz < z2):
				return True

			t = (x2 - self.x)/self.dx
			py = self.y + t*self.dy
			pz = self.z + t*self.dz

			if (y1 < py < y2) and (z1 < pz < z2):
				return True
			return False
		if abs(self.dy)> max(abs(self.dx),abs(self.dz)): # go throught y axis
			'''s/x/temp/g s/y/x/g s/temp/y/g'''
			t = (y1 - self.y)/self.dy
			px = self.x + t*self.dx
			pz = self.z + t*self.dz

			if (x1 < px < x2) and (z1 < pz < z2):
				return True

			t = (y2 - self.y)/self.dy
			px = self.x + t*self.dx
			pz = self.z + t*self.dz

			if (x1 < px < x2) and (z1 < pz < z2):
				return True
			return False
		if abs(self.dz)> max(abs(self.dy),abs(self.dx)): # go throught x axis
			'''s/x/temp/g s/z/x/g s/temp/z/g'''
			t = (z1 - self.z)/self.dz
			py = self.y + t*self.dy
			px = self.x + t*self.dx

			if (y1 < py < y2) and (x1 < px < x2):
				return True

			t = (z2 - self.z)/self.dz
			py = self.y + t*self.dy
			px = self.x + t*self.dx

			if (y1 < py < y2) and (x1 < px < x2):
				return True
			return False


camera = Camera(5,15,5,0)
camera.pitch(-1)
def draw ():
	glClear(GL_COLOR_BUFFER_BIT)

	camera.lookat()
	if event.type is KEYDOWN:
		if event.key == K_ESCAPE:
			sys.exit(0)
		if event.key == K_LEFT:
			camera.steer(-0.05)
		if event.key == K_RIGHT:
			camera.steer(0.05)
		if event.key == K_r:
			camera.lift(0.2)
		if event.key == K_f:
			camera.lift(-0.2)
		if event.key == K_w:
			camera.forward(0.5)
		if event.key == K_s:
			camera.forward(-0.5)
	if event.type is MOUSEMOTION:
		x,y = pygame.mouse.get_rel()
		camera.steer(x/300.0)
		camera.pitch(-y/300.0)
		if pygame.mouse.get_pressed()[0]:
			camera.addcube(1)
		if pygame.mouse.get_pressed()[2]:
			camera.addcube(0)