Example #1
0
def convert_input_to_barrier(line):
    '''
    Parse the pig input to create pig object.
    '''
    m = line.strip().split('|')
    name = m[0]
    strength = float(m[1])
    xCent = float(m[2])
    yCent = float(m[3])
    radBa = float(m[4])
    return Barrier(name, strength, xCent, yCent, radBa)
Example #2
0
	def reinitVars(self):
		#reinitialize objects
		global C_GENINTERVALMAX, C_GENINTERVALMIN, C_BOMBTIMER
		self.mailMan = MailMan()
		self.board = Board()
		self.mailBox = MailBox()
		self.barrier = Barrier()
		self.explosions = []
		self.powerUps = []
		self.lastMultiplier = 0
		self.genRingTick = 1
		self.level += 1
		C_GENINTERVALMAX = copy.deepcopy(realConst_genIntervalMax)
		C_GENINTERVALMIN = copy.deepcopy(realConst_genIntervalMin)
		C_BOMBTIMER = copy.deepcopy(realConst_bombTimer)
		self.bgImage = pygame.image.load(ImageBase + lvlBGs[self.level])
		self.powerUpTick = r.randint(C_GENPUPMIN, C_GENPUPMAX)
Example #3
0
    def open(self):
        if not self._open:
            # Open cameras
            self._captures = []
            for id in self._deviceIds:
                capture = cv2.VideoCapture(id)
                capture.set(cv2.CAP_PROP_FRAME_WIDTH, self._width)
                capture.set(cv2.CAP_PROP_FRAME_HEIGHT, self._height)
                capture.set(cv2.CAP_PROP_FPS, self._framerate)
                fourcc = cv2.VideoWriter_fourcc(*self._pixel_format)
                capture.set(cv2.CAP_PROP_FOURCC, fourcc)
                self._captures.append(capture)
            self._open = True

            # Start thread
            self._barrier = Barrier.Barrier(len(self._deviceIds))
            self._threads = []
            for id in range(len(self._deviceIds)):
                self._threads.append(
                    threading.Thread(target=self._eventloop, args=(id, )))
                self._threads[id].daemon = True
                self._threads[id].start()
Example #4
0
    def setup_level(self):
        w = len(MAP[0])
        h = len(MAP) + 1  # We add a bit of space for the text at the bottom
        self.barriers = []
        self.ghosts = []
        self.foods = []
        self.stage_width, self._stage_height = w, h - 1
        self.size = (w * ICON_SIZE, h * ICON_SIZE)

        for i in range(len(MAP)):
            for j in range(len(MAP[i])):
                key = MAP[i][j]
                if key == 'P':
                    self.set_player(Pacman(i, j, 24, 24, {}))
                elif key == 'G':
                    self.add_ghost(Ghost(i, j, 24, 24, {}))
                elif key == 'O':
                    self.add_food(Food(i, j, 10, 10, {}))
                elif key == 'X':
                    self.add_barrier(Barrier(i, j, 24, 24, {}))

        self.goal_CGPA = 3.0
        self.current_CGPA = 0.0
        self.goal_message = "Objective: Collect good marks to meet your CGPA!"
Example #5
0
class MailDef:
	#CHANGED
	def __init__(self):
		pygame.init()
		pygame.mixer.music.set_endevent(C_MUSEVENT)
		self.music = pygame.mixer.music
		self.music.load(MusicBase+RussianMusicName)
		self.curVol = .5
		self.music.set_volume(self.curVol)
		self.music.play()
		self.playingMusic = True
		
		self.window = pygame.display.set_mode((C_WINSIZE, C_WINSIZE))
		pygame.display.set_caption("Mail Defense!")
		self.screen = pygame.display.get_surface()
		self.clock = pygame.time.Clock()
		self.initSounds()
		self.updateVol()
	
	def initSounds(self):
		self.explodeSound = pygame.mixer.Sound(SoundBase+ExplodeSound)
		self.victorySound = pygame.mixer.Sound(SoundBase+VictorySound)
		self.advanceSound = pygame.mixer.Sound(SoundBase+AdvanceSound)
		
	def updateVol(self):
		self.explodeSound.set_volume(self.curVol)
		self.victorySound.set_volume(self.curVol)
		self.advanceSound.set_volume(self.curVol)
		self.music.set_volume(self.curVol)
	
	def initVars(self):
		#initialize objects
		self.score = 0
		self.level = -1 # will be incremented soon
		return self.advanceLevel()
		
	def reinitVars(self):
		#reinitialize objects
		global C_GENINTERVALMAX, C_GENINTERVALMIN, C_BOMBTIMER
		self.mailMan = MailMan()
		self.board = Board()
		self.mailBox = MailBox()
		self.barrier = Barrier()
		self.explosions = []
		self.powerUps = []
		self.lastMultiplier = 0
		self.genRingTick = 1
		self.level += 1
		C_GENINTERVALMAX = copy.deepcopy(realConst_genIntervalMax)
		C_GENINTERVALMIN = copy.deepcopy(realConst_genIntervalMin)
		C_BOMBTIMER = copy.deepcopy(realConst_bombTimer)
		self.bgImage = pygame.image.load(ImageBase + lvlBGs[self.level])
		self.powerUpTick = r.randint(C_GENPUPMIN, C_GENPUPMAX)
	
	def advanceLevel(self):
		self.reinitVars()
		self.screen.blit(self.bgImage, self.screen.get_rect())
		
		if self.playingMusic:
			self.victorySound.play()
		
		if self.level != 5:
			font = pygame.font.Font(None, 32)
			text = font.render("SCORE: "+str(self.score)+"/"+str(levels[self.level]), 1, (255, 255, 255))
			textpos = text.get_rect(centerx=self.screen.get_width()*3/5)
			self.screen.blit(text, textpos)
			
			text = font.render("LEVEL "+str(self.level+1)+" / 5", 1, (255, 255, 255))
			textpos = text.get_rect(centerx=self.screen.get_width()/2, centery = 200)
			self.screen.blit(text, textpos)
			
			text = font.render("Hit ENTER or SPACE to continue", 1, (255, 255, 255))
			textpos = text.get_rect(centerx=self.screen.get_width()/2, centery = 240)
			self.screen.blit(text, textpos)
		else:
			font = pygame.font.Font(None, 32)
			text = font.render("GOOD WORK BRAVE MAILMAN!", 1, (10, 10, 10))
			textpos = text.get_rect(centerx=self.screen.get_width()/2, centery = 150)
			self.screen.blit(text, textpos)
			
			text = font.render("You've sent those Commies running -", 1, (10, 10, 10))
			textpos = text.get_rect(centerx=self.screen.get_width()/2, centery = 180)
			self.screen.blit(text, textpos)
			
			text = font.render("Our mail is safe once more!", 1, (10, 10, 10))
			textpos = text.get_rect(centerx=self.screen.get_width()/2, centery = 210)
			self.screen.blit(text, textpos)
			
			text = font.render("Game by:", 1, (10, 10, 10))
			textpos = text.get_rect(centerx=self.screen.get_width()/2, centery = 270)
			self.screen.blit(text, textpos)
			
			text = font.render("Eric Conlon", 1, (10, 10, 10))
			textpos = text.get_rect(centerx=self.screen.get_width()/2, centery = 300)
			self.screen.blit(text, textpos)
			
			text = font.render("Nick Farnan", 1, (10, 10, 10))
			textpos = text.get_rect(centerx=self.screen.get_width()/2, centery = 330)
			self.screen.blit(text, textpos)
			
			text = font.render("Joe Frambach", 1, (10, 10, 10))
			textpos = text.get_rect(centerx=self.screen.get_width()/2, centery = 360)
			self.screen.blit(text, textpos)
			
			text = font.render("Created for Pitt Geeks", 1, (10, 10, 10))
			textpos = text.get_rect(centerx=self.screen.get_width()/2, centery = 420)
			self.screen.blit(text, textpos)
			
			text = font.render("at the 1st OSGCC April 6 - April 7 2007!", 1, (10, 10, 10))
			textpos = text.get_rect(centerx=self.screen.get_width()/2, centery = 450)
			self.screen.blit(text, textpos)
			
			text = font.render("Hit ENTER or SPACE to continue", 1, (10, 10, 10))
			textpos = text.get_rect(centerx=self.screen.get_width()/2, centery = 480)
			self.screen.blit(text, textpos)
		
		pygame.display.flip()
		
		playKeys = set([K_RETURN, K_SPACE])
		
		keepGoing = True
		while (keepGoing):
			self.clock.tick(30)
			for event in pygame.event.get():
				if event.type == QUIT:
					return C_EXIT
				# handle keydown events	
				elif event.type == KEYDOWN:
					if event.key == K_ESCAPE:
						if (self.level == 5):
							return C_WIN
						else:
							return C_EXIT
					elif event.key in playKeys:
						if (self.level == 5):
							return C_WIN
						else:
							return C_PLAY
					elif event.key == K_m:
						if self.playingMusic:
							self.playingMusic = False
							self.music.stop()
						else:
							self.playingMusic = True
							self.music.play()
					elif event.key == K_PERIOD:
						self.curVol += .1
						if self.curVol >= 1:
							self.curVol = 1.0
						self.updateVol()
					elif event.key == K_COMMA:
						self.curVol -= .1
						if self.curVol <= 0:
							self.curVol = 0.0
						self.updateVol()
					elif event.key in playKeys:
						pygame.event.clear()
						return C_PLAY
				elif event.type == C_MUSEVENT:
					if self.playingMusic:
						self.music.play()
	
	def getHighScores(self):
		hsf = open(HighScoreFile, "r")
		highScores = [(line.strip()).split("\t") for line in hsf.readlines()]
		hsf.close()
		return highScores
			
	def setHighScore(self, newScore):
		highScores = self.getHighScores()
		newHigh = -1
		name = ""
		for i in range(len(highScores)):
			if ((newHigh == -1) and (newScore > (int)(highScores[i][1]))):
				newHigh = i
				name = self.getName()
				break
		if (newHigh != -1):
			# New high
			highScores = highScores[0:newHigh] + [[name,str(newScore)]] + highScores[newHigh:-1]
			hsf = open(HighScoreFile, "w")
			for hs in highScores:
				hsf.write(hs[0]+"\t"+hs[1]+"\n")
			hsf.close()
		return newHigh
	
	def getName(self):
		#name = raw_input()
		#if len(name) == 0:
		#	name = "Anon"
		#return name
		return "Brave Mailman!"
		
	#CHANGED
	def menu(self, hsNum):
		highScores = self.getHighScores()
		playKeys = set([K_RETURN, K_SPACE])
		self.updateMenu(hsNum)
		while (True):
			self.clock.tick(30)
			for event in pygame.event.get():
				if event.type == QUIT:
					return C_EXIT
				# handle keydown events	
				elif event.type == KEYDOWN:
					# quit on escape key
					if event.key == K_ESCAPE:
						return C_EXIT
					elif event.key == K_m:
						if self.playingMusic:
							self.playingMusic = False
							self.music.stop()
						else:
							self.playingMusic = True
							self.music.play()
					elif event.key == K_PERIOD:
						self.curVol += .1
						if self.curVol >= 1:
							self.curVol = 1.0
						self.updateVol()
					elif event.key == K_COMMA:
						self.curVol -= .1
						if self.curVol <= 0:
							self.curVol = 0.0
						self.updateVol()
					elif event.key in playKeys:
						pygame.event.clear()
						return C_PLAY
				elif event.type == C_MUSEVENT:
					if self.playingMusic:
						self.music.play()
	
	#draw the menu
	def updateMenu(self, hsNum):
		#set the US/USSR dueling background awesome image
		self.screen.blit(pygame.image.load(ImageBase+"menu.png"), self.screen.get_rect())
		
		### From here down diplays menutext ###
		font = pygame.font.Font(None, 32)
		text = font.render("Mail Defense!", 1, (10, 10, 10))
		textpos = text.get_rect(centerx=self.screen.get_width()/2, centery=150)
		#self.screen.blit(text, textpos)
		self.screen.blit(pygame.image.load(ImageBase+"logo.png"), textpos)
		
		font = pygame.font.Font(None, 20)
		text = font.render("Protect the US Postal Service", 1, (10, 10, 10))
		textpos = text.get_rect(centerx=self.screen.get_width()/2, centery = 190)
		self.screen.blit(text, textpos)
		
		text = font.render("by laying the right mail bombs!", 1, (10, 10, 10))
		textpos = text.get_rect(centerx=self.screen.get_width()/2, centery = 210)
		self.screen.blit(text, textpos)
		
		text = font.render("Exploit the dirty Communitsts' weaknesses:", 1, (10, 10, 10))
		textpos = text.get_rect(centerx=self.screen.get_width()/2, centery = 230)
		self.screen.blit(text, textpos)
		
		text = font.render("Lay bombs with 1, 2, 3, or 4 and move with the arrow keys.", 1, (10, 10, 10))
		textpos = text.get_rect(centerx=self.screen.get_width()/2, centery = 250)
		self.screen.blit(text, textpos)
		
		text = font.render("High scores!", 1, (10, 10, 10))
		textpos = text.get_rect(centerx=self.screen.get_width()/2, centery = 290)
		self.screen.blit(text, textpos)
		
		# read in high scores from file and print them
		hiscores = self.getHighScores()
		for i in range(len(hiscores)):
			mes = "%-1d. %-20s%16s" % (i, hiscores[i][0], hiscores[i][1])
			if (i == hsNum):
				mes = "!!! "+mes+" !!!"
			text = font.render(mes, 1, (10, 10, 10))
			textpos = text.get_rect(left=self.screen.get_width()/3, centery = 320+15*i)
			self.screen.blit(text, textpos)
			
		text = font.render("Press ENTER or SPACE to Start,", 1, (10, 10, 10))
		textpos = text.get_rect(centerx=self.screen.get_width()/2, centery = 500)
		self.screen.blit(text, textpos)
		
		text = font.render("ESC to Quit, or m to Toggle Sound.", 1, (10, 10, 10))
		textpos = text.get_rect(centerx=self.screen.get_width()/2, centery = 520)
		self.screen.blit(text, textpos)
		
		pygame.display.flip()
	#end updateMenu
	
	# redraws everything on the game screen
	def updateDisplay(self):
		self.screen.blit(self.bgImage, self.screen.get_rect())
		for pup in self.powerUps:
			pup.blit(self.screen)
		self.mailBox.blit(self.screen)
		self.barrier.blit(self.screen)
		self.board.blit(self.screen)
		self.mailMan.blit(self.screen)
		for exp in self.explosions:
			exp.blit(self.screen)
		
		font = pygame.font.Font(None, 32)
		text = font.render("SCORE: "+str(self.score)+"/"+str(levels[self.level]), 1, (255, 255, 255))
		textpos = text.get_rect(centerx=self.screen.get_width()*3/5)
		self.screen.blit(text, textpos)
		pygame.display.flip()
		
	# main game event loop
	def play(self):
		res = self.initVars()
		if res == C_EXIT:
			return self.score
		pygame.event.clear()
		keepPlaying = True
		while (keepPlaying):
			self.clock.tick(30)
			self.score += 1
			for event in pygame.event.get():
				if event.type == QUIT:
					keepPlaying = False
					break
				# handle keydown events	
				elif event.type == KEYDOWN:
					# quit on escape key
					if event.key == K_ESCAPE:
						keepPlaying = False
						break
					elif event.key == K_m:
						if self.playingMusic:
							self.playingMusic = False
							self.music.stop()
						else:
							self.playingMusic = True
							self.music.play()
					elif event.key == K_PERIOD:
						self.curVol += .1
						if self.curVol >= 1:
							self.curVol = 1.0
						self.music.set_volume(self.curVol)
					elif event.key == K_COMMA:
						self.curVol -= .1
						if self.curVol <= 0:
							self.curVol = 0.0
						self.music.set_volume(self.curVol)
					
					#!!! temp key binding for rings !!!
					#elif event.key == K_r:
					#	self.board.addRing()
						
					# key bindings for starting to move
					elif event.key == K_w or event.key == K_UP:
						self.mailMan.setSpeed([0, -1])
					elif event.key == K_a or event.key == K_LEFT:
						self.mailMan.setSpeed([-1, 0])
					elif event.key == K_s or event.key == K_DOWN:
						self.mailMan.setSpeed([0, 1])
					elif event.key == K_d or event.key == K_RIGHT:
						self.mailMan.setSpeed([1, 0])
						
					# dropping bombs
					elif event.key == K_u or event.key == K_1:
						mks = self.mailMan.drop(0)
						if mks and self.playingMusic:
							self.advanceSound.play()
					elif event.key == K_i or event.key == K_2:
						mks = self.mailMan.drop(1)
						if mks and self.playingMusic:
							self.advanceSound.play()
					elif event.key == K_o or event.key == K_3:
						mks = self.mailMan.drop(2)
						if mks and self.playingMusic:
							self.advanceSound.play()
					elif event.key == K_p or event.key == K_4:
						mks = self.mailMan.drop(3)
						if mks and self.playingMusic:
							self.advanceSound.play()
				
				# handler for keyup events
				elif event.type == KEYUP:
					# stopping moving
					if event.key == K_w or event.key == K_UP:
						self.mailMan.setSpeed([0, 1])
					elif event.key == K_a or event.key == K_LEFT:
						self.mailMan.setSpeed([1, 0])
					elif event.key == K_s or event.key == K_DOWN:
						self.mailMan.setSpeed([0, -1])
					elif event.key == K_d or event.key == K_RIGHT:
						self.mailMan.setSpeed([-1, 0])
						
				elif event.type == C_MUSEVENT:
					if self.playingMusic:
						self.music.play()			
			
			# check if powerUp should be added to the screen
			#	add if necessary
			self.powerUpTick -= 1
			if self.powerUpTick == 0:
				self.powerUps.append(PowerUp())
				self.powerUpTick = r.randint(C_GENPUPMIN, C_GENPUPMAX)
				
			# check if a new ring should be decorated
			#	add if necessary
			self.genRingTick -= 1
			if self.genRingTick == 0:
				self.board.addRing()
				self.genRingTick = r.randint(C_GENINTERVALMIN, C_GENINTERVALMAX)
				
			# check if any items have timed up and need to
			#	be removed from the screen
			exp = self.checkBombs()
			if exp and self.playingMusic:
				self.explodeSound.play()
			self.checkPowerUps()
			self.updateExplosions()
			
			self.mailMan.move()
			self.board.updateRings()

			if (self.checkCollisions()):
				keepPlaying = False
				break
				
			self.modifyDifficulty()
			self.updateDisplay()
			if (self.score >= levels[self.level]):
				res = self.advanceLevel()
				if ((res == C_EXIT) or (res == C_WIN)):
					return self.score
		# end game event loop
			
		# if the game loop was exited, game is over, return the score
		return self.score

	def modifyDifficulty(self):
		global C_GENINTERVALMAX, C_GENINTERVALMIN, C_BOMBTIMER
		newMultiplier = self.score/500
		if newMultiplier <= self.lastMultiplier:
			return
		self.lastMultiplier = newMultiplier
		C_GENINTERVALMIN -= newMultiplier
		C_GENINTERVALMAX -= newMultiplier
		C_BOMBTIMER -= newMultiplier
			
	def collectPowerUp(self, pupType):
		if self.playingMusic:
			self.advanceSound.play()
		self.score+=100
		if pupType == 0:
			exp = self.killAllBombs()
			if exp and self.playingMusic:
				self.explodeSound.play()
		elif pupType == 1:
			exp = self.killAllEnemies()
			if exp and self.playingMusic:
				self.explodeSound.play()
		elif pupType == 2:
			exp = self.killNearestEnemies()
			if exp and self.playingMusic:
				self.explodeSound.play()
				
	def checkCollisions(self):
		# CHECK FOR PERSON/POWERUP COLLISIONS!
		if len(self.powerUps)>0:
			collideList = self.mailMan.rect.collidelistall(self.powerUps)
			collideList.reverse()
			for index in collideList:
				self.collectPowerUp(self.powerUps.pop(index).pupType)
		# CHECK FOR BOMB/RING COLLISIONS
		for ring in self.board.rings:
			for side in range(4):
				collideList = ring.rects[side].collidelistall(self.mailMan.bombs)
				collideList.sort()
				collideList.reverse()
				if len(collideList) > 0:
					if self.playingMusic:
						self.explodeSound.play()
					for index in collideList:
						bomb = self.mailMan.detonate(index)
						self.explosions.append(Explosion(bomb.rect))
						ring.explode(side, bomb, self.explosions)
						if ring.isDead():
							#CHANGE
							self.board.rings.remove(ring)
							break
		# CHECK FOR RING/BARRIER COLLISIONS
		for ring in self.board.rings:
			for side in range(4):
				collideList = ring.rects[side].collidelistall(self.barrier.rects)
				if len(collideList) > 0:
					return True
		return False
					
	# if the explosions have exploded, remove them from the game
	def updateExplosions(self):
		delList = []
		for expIndex in range(len(self.explosions)):
			self.explosions[expIndex].life += 1
			if (self.explosions[expIndex].life >= C_LIFELIMIT):
				delList.append(expIndex)
		delList.reverse()
		for expIndex in delList:
			del self.explosions[expIndex]
			 
	# check the bomb timers, if any are up, detonate them
	def checkBombs(self):
		detonateList = []
		flag = False
		for index in range(len(self.mailMan.bombs)):
			self.mailMan.bombs[index].timer -= 1
			if self.mailMan.bombs[index].timer == 0:
				detonateList.append(index)
				flag = True
		detonateList.reverse()
		for index in detonateList:
			bomb = self.mailMan.detonate(index)
			self.explosions.append(Explosion(bomb.rect))
		return flag
			
	#similar deal for bombs
	def checkPowerUps(self):
		killList = []
		for index in range(len(self.powerUps)):
			self.powerUps[index].lifeTime -= 1
			if self.powerUps[index].lifeTime == 0:
				killList.append(index)
		killList.reverse()
		for index in killList:
			self.powerUps.pop(index)
			
	def killAllBombs(self):
		flag = False
		detList = [i for i in range(len(self.mailMan.bombs))]
		detList.reverse()
		if len(detList)>0:
			flag = True
		for index in detList:
			bomb = self.mailMan.detonate(index)
			self.explosions.append(Explosion(bomb.rect))
		return flag
	
	def killAllEnemies(self):
		flag = False
		if len(self.board.rings)>0:
			flag = True
		for ring in self.board.rings:
			ring.killAll(self.explosions)
			self.board.rings.remove(ring)
		return flag
	
	def killNearestEnemies(self):
		flag = False
		if len(self.board.rings)>0:
			flag = True
			self.board.rings[0].killAll(self.explosions)
			self.board.rings.remove(self.board.rings[0])
		return flag
Example #6
0
        pigs.append(Pig(pig['xc'], pig['radius'], pig['name'], pig['yc']))

    #PRINT INITIAL INFORMATION ABOUT PIGS
    print()
    print('There are {} pigs:'.format(len(pigs)))
    #FOR LOOP TO PRINT PIGS
    for pig in pigs:
        print('    {}: ({:.1f},{:.1f})'.format(pig.name, float(pig.x),
                                               float(pig.y)))

    #BARRIER LIST
    barriers = []
    for barrier in dictionary['barriers']:
        #ADD BARRIER OBJECTS TO A LIST
        barriers.append(
            Barrier(barrier['yc'], barrier['name'], barrier['xc'],
                    barrier['radius'], barrier['strength']))

    #PRINT INITIAL INFORMATION ABOUT BARRIERS
    print()
    print('There are {} barriers:'.format(len(barriers)))
    #FOR LOOP TO PRINT BARRIERS
    for barrier in barriers:
        print('    {}: ({:.1f},{:.1f})'.format(barrier.name, float(barrier.x),
                                               float(barrier.y)))

    #INITIALIZE TIME COUNTER
    time = 0

    #KEEP TRACK OF WHAT BIRD YOURE ON
    bird_num = 0
Example #7
0
 #Create pig list
 pigs = []
 
 #Parse Data    
 for line in open(pig_file):
     data = line.strip().split("|")
     pig = Pig(data[0],data[1],data[2],data[3])
     pigs.append(pig)        
 
 #Create barrier list
 barriers = []
 
 #Parse Data
 for line in open(barrier_file):
     data = line.strip().split("|")
     barrier = Barrier(data[0],data[1],data[2],data[3],data[4])
     barriers.append(barrier)
 
 #Output initial set-up
 
 #Birds first
 print("")    
 print("There are {} birds:".format(len(birds)))
 for bird in birds:
     print("    {}: ({:.1f},{:.1f})".format(bird.name,bird.x,bird.y))
     
 #Now pigs
 print("")
 print("There are {} pigs:".format(len(pigs)))
 for pig in pigs:
     print("    {}: ({:.1f},{:.1f})".format(pig.name,pig.x,pig.y))
Example #8
0
 def setup_method(self):
     self.barrier = Barrier.Barrier(400, 400)
Example #9
0
    def start_calibration(
            self, chessboard=(11, 8), duration=30, number_of_samples=100,
            stereo=True,
            calibration_file=(dirname(abspath(__file__)) +
                              "/../../../../../json/" +
                              "nico_vision_calibration_params.json"),
            overwrite=False,
            term_criteria=(cv2.TERM_CRITERIA_EPS +
                           cv2.TERM_CRITERIA_MAX_ITER, 30, 0.1),
            calibration_flags=(cv2.fisheye.CALIB_RECOMPUTE_EXTRINSIC +
                               cv2.fisheye.CALIB_CHECK_COND +
                               cv2.fisheye.CALIB_FIX_SKEW)):
        """
        Records images for given duration to calibrate cameras.

        Calibration related code was partially taken from:
        https://medium.com/@kennethjiang/calibrate-fisheye-lens-using-opencv-333b05afa0b0
        and
        https://github.com/sourishg/fisheye-stereo-calibration/blob/master/calibrate.cpp
        (stereo term_criteria and fov_scale)

        :param chessboard: Dimensions of the chessboard pattern (inner corners)
        :type chessboard: tuple(int)
        :param duration: Duration of the recording
        :type duration: int
        :param number_of_samples: Subset of images used for calibration
                                  (to reduce computing time)
        :type number_of_samples: int
        :param stereo: Whether cameras should be calibrated individually or as
                       stereo cameras
        :type overwrite: bool
        :param calibration_file: json file path to save calibration parameters
        :type calibration_file: str
        :param overwrite: Whether preexisting parameters in the file should be
                          overwritten
        :type overwrite: bool
        :param term_criteria: cv2 term_criteria for calibration
        :type term_criteria: list
        :param calibration_flags: cv2 calibration_flags
        :type calibration_flags: list
        """
        self._chessboard = chessboard
        self._criteria = term_criteria
        self._calibration_flags = calibration_flags
        # 2d points in image plane.
        self._imgpoints = [[]] * len(self._deviceIds)
        self._rvals = [False] * len(self._deviceIds)
        self._chess_detection_barrier = Barrier.Barrier(len(self._deviceIds))
        devicenames = MultiCamRecorder.get_devices()
        devicenames = map(lambda i: devicenames[i], self._deviceIds)
        zoom = self._device.get_zoom()
        # load preexisting calibrations from file
        if isfile(calibration_file):
            with open(calibration_file, 'r') as existing_file:
                existing_calibration = json.load(existing_file)
                if (stereo and str(devicenames) not in existing_calibration[
                        "stereo"]):
                    existing_calibration["stereo"][str(devicenames)] = {}
                elif (stereo and str(self._dim) in
                      existing_calibration["stereo"][str(devicenames)]):
                    existing_calibration["stereo"][str(
                        devicenames)][str(self._dim)] = {}
                elif not stereo:
                    for name in devicenames:
                        if name not in existing_calibration["mono"]:
                            existing_calibration["mono"][name] = {}
                        else if str(self._dim) not in existing_calibration[
                                "mono"][name]:
                            existing_calibration["mono"][name][
                                str(self._dim)] = {}
        else:
            existing_calibration = {
                "stereo": {str(devicenames): {str(self._dim): {}}},
                "mono": dict(zip(devicenames, [{str(self._dim): {}}
                                               for _ in devicenames]))}
        # abort if calibration for device and dim already exists and overwrite
        # not enabled
        if not overwrite:
            if (stereo and str(zoom) in existing_calibration["stereo"]
                    [str(devicenames)][str(self._dim)]):
                self._logger.warning(("Calibration aborted - Overwrite not " +
                                      "enabled and setting for devices {} " +
                                      "and dimension {} already exists in {}"
                                      ).format(devicenames, self._dim,
                                               calibration_file))
                return
            elif not stereo:
                for i in range(len(self._deviceIds)):
                    if (str(zoom[i]) in existing_calibration["mono"]
                            [str(devicenames[i])][str(self._dim)]):
                        self._logger.warning(
                            ("Calibration aborted - Overwrite " +
                             "not enabled and setting for " +
                             "device {} and dimension {} " +
                             "already exists in {}"
                             ).format(devicenames[i], self._dim,
                                      calibration_file))
                        return
        # start recording
        self._logger.info("Start recording images for calibration")
        self._recorder.add_callback(self._callback)
        self._recorder._open = True
        time.sleep(duration)
        self._chess_detection_barrier.abort()
        self._recorder.stop_recording()
        self._stop_event.set()
        self._display.join()
        time.sleep(1)
        self._logger.info("Recording finished - preparing for calibration")
        # reduce recorded image points to number of samples
        new_length = min(number_of_samples, len(
            self._imgpoints[0]), len(self._imgpoints[1]))
        self._imgpoints = map(lambda x: takespread(
            x, new_length), self._imgpoints)
        # start calibration
        if stereo:
            calib_params = self._calibrate_stereo()
            if calib_params:
                existing_calibration["stereo"][str(devicenames)][str(
                    self._dim)][str(zoom)] = calib_params
        else:
            for i in range(len(self._deviceIds)):
                calib_params = self._calibrate_mono(i)
                if calib_params:
                    existing_calibration["mono"][devicenames[i]][str(
                        self._dim)][str(zoom[i])] = calib_params
        # save results
        self._logger.info("Calibration finished - saving results")
        self._logger.debug(
            "Saving calibration {}".format(existing_calibration))
        with open(calibration_file, 'w') as outfile:
            json.dump(existing_calibration, outfile, cls=NumpyEncoder)
Example #10
0
import Barrier
import Background
import random
import os
import time
import sys
import Physics


#Preventing the recursion limit of the gameloop to close the game
sys.setrecursionlimit(10000)


Images = ["tankpic2.png", "tankpic.png"]

barrier = Barrier.Barrier(random.randint(450, 600), 470)

tank1 = Tank.Tank(Images[0], 800, 610, 1, 2)

tank2 = Tank.Tank(Images[1], 300, 610, 0, 1)

bg = Background.Background()



# The introduction screen of the game
def game_intro():

    intro = True

    while intro:
Example #11
0
#Reads the file and add them to the list as objects
for line in open(bd_f):
    line = line.strip()
    b = []
    b = line.split("|")
    birds.append(Bird(b[0],b[1],b[2],b[3],b[4],b[5],b[6]))
for line in open(pg_f):
    line = line.strip()
    p = []
    p = line.split("|")
    pigs.append(Pig(p[0],p[1],p[2],p[3]))
for line in open(br_f):
    line = line.strip()
    b = []
    b = line.split("|")
    barriers.append(Barrier(b[0],b[1],b[2],b[3],b[4]))

#Prints out the initial information
print("")
print("There are {} birds:".format(len(birds)))
for b in birds:
    print("    " + str(b))
print("")
print("There are {} pigs:".format(len(pigs)))
for p in pigs:
    print("    " + str(p))
print("")
print("There are {} barriers:".format(len(barriers)))
for b in barriers:
    print("    " + str(b))
print("")