Beispiel #1
0
	def __init__(self):
		pygame.init()
		self.prevState = {}
		
		# self.socket = UISocket.clientSocket()
		# UISocket.getAndSendName(self.socket)
		# self.seatno = UISocket.receiveSeat(self.socket)
		self.sockObj = UISocket()
		self.sockObj.getAndSendName()
		self.seatno = self.sockObj.receiveSeat()
		threading.Thread(target=self.sockObj.recvTableData).start()
		
		self.fps = pygame.time.Clock()
		self.initStateVariables()
		self.setDisplay()
		
		self.backgroundSurface = pygame.image.load("resources/images/TableBG.png")
		self.backgroundSurface = pygame.transform.scale(self.backgroundSurface, (800, 600))
		self.playArea.blit(self.backgroundSurface, (0,0))
		
		self.initPreviousDisplayRects()
		
		self.layout()
		pygame.display.set_caption('Biopoker')
Beispiel #2
0
class UI():
	wW, wH = 800, 600
	
	def __init__(self):
		pygame.init()
		self.prevState = {}
		
		# self.socket = UISocket.clientSocket()
		# UISocket.getAndSendName(self.socket)
		# self.seatno = UISocket.receiveSeat(self.socket)
		self.sockObj = UISocket()
		self.sockObj.getAndSendName()
		self.seatno = self.sockObj.receiveSeat()
		threading.Thread(target=self.sockObj.recvTableData).start()
		
		self.fps = pygame.time.Clock()
		self.initStateVariables()
		self.setDisplay()
		
		self.backgroundSurface = pygame.image.load("resources/images/TableBG.png")
		self.backgroundSurface = pygame.transform.scale(self.backgroundSurface, (800, 600))
		self.playArea.blit(self.backgroundSurface, (0,0))
		
		self.initPreviousDisplayRects()
		
		self.layout()
		pygame.display.set_caption('Biopoker')
		
		
	def setDisplay(self):
		"""
		Setting the display areas ready fro PyGame
		"""
		self.window = pygame.display.set_mode((UI.wW, UI.wH), 0, 32)
		self.playArea = pygame.display.get_surface()
		
	def initStateVariables(self):
		"""
		Initialise the variables used when unpacking state data from the server
		"""
		self.UIplayerList = []
		self.UIcommunityCards = []
		self.UIpots = []
		self.UIcurrentBet = []
		self.UIpreviousTurn = None
		self.UIturn = None
		self.UIisGameEnd = False
		
	def initPreviousDisplayRects(self):
		"""
		Initialise the PyGame rects that will be used to blit back part of the background
		"""
		# This variable used in self.displayCalInfo
		self.displayCallInfoPrevRect = None
		
		#This is used in self.displayPot
		self.displayPotPrevRect = None
		
	def applyState(self, update):
		"""
		Update the state variables from the dictionary ``update``
		"""
		self.prevState = update
		self.UIplayerList = update['playerlist']
		self.UIcommunityCards = update['comcards']
		self.UIpots = update['pots']
		self.UIcurrentBet = update['curbet']
		self.UIpreviousTurn = self.UIturn
		self.UIturn = update['turn']
		self.UIisGameEnd = update['isGameEnd']
		#test: delete this
		if self.UIisGameEnd:
			print 'Game End'
	
	def updateState(self):
		"""
		Update the visuals of the game through the instance state. Sets things such as the player's money 
		name and avatar and changes the turn marker for the current player's turn
		"""
		if not self.UIisGameEnd:
			for i, x in enumerate(self.UIplayerList):
				if x != None:
					#print "{0}: {1}".format(x.name, x.money)
					#print "Seat: {0}. Money: {1}. Hand: {2}".format(i, x.money, x.hand)
					self.seats[i].setName(x.name)
					self.seats[i].setMoney(x.money)
					self.seats[i].setAvatar(x.threshValue,x.peaksPerMin)
					#print "IN UI, avghighdata = {0}".format(x.biodataAvgHigh)
					if x.hand != []:
						self.seats[i].setCards(x.hand)
				else:
					self.seats[i].setDefault()
					
			if self.UIpreviousTurn != None:
				self.seats[self.UIpreviousTurn].removeTurnMarker()
			self.seats[self.UIturn].addTurnMarker()
				
			self.displayCommunityCards()
			self.displayCallInfo()
			self.displayPot()
			
		else:
			for i, x in enumerate(self.UIplayerList):
				if x != None:
					x.money = "Winner"
					self.seats[i].setMoney(x.money)
					print "{0} has won".format(x.name)
					break
		
	def displayState(self):
		"""
		Print out state information for debugging and testing
		"""
		print "Player List: {0}".format(self.UIplayerList)
		print "Community Cards: {0}".format(self.UIcommunityCards)
		print "Pots: {0}".format(self.UIpots)
		print "currentBet: {0}".format(self.UIcurrentBet)
		print "turn: {0}".format(self.UIturn)
		
	def getRaiseAmount(self):
		"""
		When the player hits the 'raise', this is called to determine what amount the player is raising by
		"""
		surfaceCopy = self.playArea.copy()
		textBoxRect = pygame.Rect((self.playArea.get_width() / 2) - 100,self.playArea.get_height() - 50,200,20)
		amount = inputbox.ask(self.playArea, "Amount", textBoxRect)
		clearRect = pygame.Rect((self.playArea.get_width() / 2) - 120,self.playArea.get_height() - 60,240,40)
		self.playArea.blit(surfaceCopy, clearRect, clearRect)
		if amount.isdigit():
			return int(amount)
		else:
			return None
			
	def determineAmountToCall(self, player):
		"""
		Determines the amount the players has to call for use in visualisation
		"""
		return sum(self.UIcurrentBet) - sum(player.betAmount)
		
	def loop(self):
		"""
		PyGame main event loop. Detects if certain buttons are pressed by the player and takes action
		accordingly
		"""
		while 1:
			for event in pygame.event.get():
			
				if event.type == QUIT:
					self.sockObj.socket.close()
					pygame.quit()
					sys.exit()
					
				elif event.type == pygame.MOUSEBUTTONUP:
					if self.seatno == self.UIturn:
						if event.button == 1:
							if self.buttonCall.clicked(event.pos) == 1:
								#UISocket.sendCommand(self.socket, 'call')
								self.sockObj.sendCommand('call')
							elif self.buttonRaise.clicked(event.pos) == 1:
								r = self.getRaiseAmount()
								if r != None:
									bettingAmount = self.determineAmountToCall(self.UIplayerList[self.UIturn]) + r
									if bettingAmount <= self.UIplayerList[self.UIturn].money:
										#UISocket.sendCommand(self.socket, "raise:{0}".format(r))
										self.sockObj.sendCommand("raise:{0}".format(r))
							elif self.buttonFold.clicked(event.pos) == 1:
								#UISocket.sendCommand(self.socket, 'fold')
								self.sockObj.sendCommand('fold')
							
				elif event.type == pygame.KEYDOWN:
					if event.key == K_SPACE:
						print "Space"
						#self.linker.printTableState()
					elif event.key == K_1:
						self.displayState()
			
			if self.prevState != self.sockObj.gameState:
				update = self.sockObj.gameState
				if update != {} and update != None:
					self.applyState(update)
					self.updateState()
				
			pygame.display.update()
			self.fps.tick(10)
	
	def displayCommunityCards(self):
		"""
		Displays the community cards to all the players to the screen
		"""
		self.cardyoffset = 50
		self.cardheight = UI.wH/2-self.cardyoffset
		
		for _ in range(5 - len(self.UIcommunityCards)):
			self.UIcommunityCards.append(52);
		
		self.card1 = UICard((UI.wW/2-50*2+3, self.cardheight), self.UIcommunityCards[0])
		self.card2 = UICard((UI.wW/2-50*1, self.cardheight), self.UIcommunityCards[1])
		self.card3 = UICard((UI.wW/2, self.cardheight), self.UIcommunityCards[2])
		self.card4 = UICard((UI.wW/2+50*1, self.cardheight), self.UIcommunityCards[3])
		self.card5 = UICard((UI.wW/2+50*2, self.cardheight), self.UIcommunityCards[4])
		self.playArea.blit(self.card1.image, self.card1.rect)
		self.playArea.blit(self.card2.image, self.card2.rect)
		self.playArea.blit(self.card3.image, self.card3.rect)
		self.playArea.blit(self.card4.image, self.card4.rect)
		self.playArea.blit(self.card5.image, self.card5.rect)
		
	def displayCallInfo(self):
		"""
		Displays the amount that the player has to call. This is different for each client
		"""
		if self.UIplayerList != []:
			
			if self.displayCallInfoPrevRect != None:
				self.playArea.blit(self.backgroundSurface, self.displayCallInfoPrevRect, self.displayCallInfoPrevRect)
			
			infoFont = pygame.font.Font("resources/fonts/arialbd.ttf", 30)
			fontRender = infoFont.render("To Call: {0}".format(self.determineAmountToCall(self.UIplayerList[self.seatno])), True, (200, 200, 200))
			x = self.playArea.get_width() - fontRender.get_width() - 10
			y = self.playArea.get_height() - fontRender.get_height() - 10
			self.playArea.blit(fontRender, (x, y))
			self.displayCallInfoPrevRect = (x, y, fontRender.get_width(), fontRender.get_height())
			
	def displayPot(self):
		"""
		Display the total pot amount that all players have contributed too
		"""
		if self.UIpots != []:
			
			if self.displayPotPrevRect != None:
				self.playArea.blit(self.backgroundSurface, self.displayPotPrevRect, self.displayPotPrevRect)
			
			infoFont = pygame.font.Font("resources/fonts/arialbd.ttf", 25)
			fontRender = infoFont.render(u'\xA3{0}'.format(sum(self.UIpots)), True, (30,30,30))
			x = self.playArea.get_width()/2 - fontRender.get_width()/2
			y = self.playArea.get_height()/2
			self.playArea.blit(fontRender, (x, y))
			self.displayPotPrevRect = (x, y, fontRender.get_width(), fontRender.get_height())
	
	def layout(self):
		"""
		Setup the various 'seats' around the table for each player and adding the card placeholders for each seat
		"""
		self.displayCommunityCards()
		self.displayCallInfo()
		
		self.seats = []
		self.seats.append( UISeat(self.playArea, (UI.wW/2-300, self.cardheight), self.backgroundSurface) )
		self.seats.append( UISeat(self.playArea, (UI.wW/2-100, self.cardheight-150), self.backgroundSurface) )
		self.seats.append( UISeat(self.playArea, (UI.wW/2+100, self.cardheight-150), self.backgroundSurface) )
		self.seats.append( UISeat(self.playArea, (UI.wW/2+300, self.cardheight), self.backgroundSurface) )
		self.seats.append( UISeat(self.playArea, (UI.wW/2+100, self.cardheight+170), self.backgroundSurface) )
		self.seats.append( UISeat(self.playArea, (UI.wW/2-100, self.cardheight+170), self.backgroundSurface) )
		
		# self.seat1 = UISeat(self.playArea, (UI.wW/2-300, self.cardheight))
		# self.seat2 = UISeat(self.playArea, (UI.wW/2-100, self.cardheight-150))
		# self.seat3 = UISeat(self.playArea, (UI.wW/2+100, self.cardheight-150))
		# self.seat4 = UISeat(self.playArea, (UI.wW/2+300, self.cardheight))
		# self.seat5 = UISeat(self.playArea, (UI.wW/2+100, self.cardheight+170))
		# self.seat6 = UISeat(self.playArea, (UI.wW/2-100, self.cardheight+170))
		
		# Buttons
		
		
		self.buttonCall = UIButton("resources/images/ButtonCall.jpg", (100, UI.wH - 150))
		self.buttonRaise = UIButton("resources/images/ButtonRaise.jpg", (100, UI.wH - 100 + 5))
		self.buttonFold = UIButton("resources/images/ButtonFold.jpg", (100, UI.wH - 50 + 5*2))
		self.playArea.blit(self.buttonCall.image, self.buttonCall.rect)
		self.playArea.blit(self.buttonRaise.image, self.buttonRaise.rect)
		self.playArea.blit(self.buttonFold.image, self.buttonFold.rect)