[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] I'm trying to make my X's and O's show using a while statement but for some reason it aint updating.



The first self.cordnates value works, yet the others wont:

    def draw_game(self):   
        x = 1
        while x < len(self.cordnates) + 1:
            print self.cordnates[x]
            if self.cordnates [x][0] == 1 and self.cordnates[x][1] == 1:   
                self.XandO('o',x)
            elif self.cordnates[x][0] == 1 and self.cordnates[1][1] == 2:
                self.XandO('x',x)
            x += 1
import pygame,sys
from pygame.locals import *
class lines(object):
	def __init__(self):
		pygame.init()
		pygame.display.set_mode((300,300))
		self.window = pygame.display.get_surface()
		self.window.fill((0,0,0))
		self.columns = 100
		self.cordnates = {
		1 : [1,1] , 2 : [1,2] , 3 : [1,2],
		4 : [1,2] , 5 : [1,2] , 6 : [1,2],
		7 : [1,2] , 8 : [1,2] , 9 : [1,2]
		}
		self.gameover = False
	def check_game(self):
		if self.cordnates[1][0] == 1 and self.cordnates[2][0] == 1 and self.cordnates[3][0] == 1:
			if (self.cordnates[1][1] == 1 and self.cordnates[2][1] == 1 and self.cordnates[3][1] == 1) or (self.cordnates[1][1] == 2 and self.cordnates[2][1] == 2 and self.cordnates[3][1] == 2):
				self.winlines(1,1)
		elif self.cordnates[4][0] == 1 and self.cordnates[5][0] == 1 and self.cordnates[6][0] == 1:
			if (self.cordnates[4][1] == 1 and self.cordnates[5][1] == 1 and self.cordnates[6][1] == 1) or (self.cordnates[4][1] == 2 and self.cordnates[5][1] == 2 and self.cordnates[6][1] == 2):
				self.winlines(1,2)
		elif self.cordnates[7][0] == 1 and self.cordnates[8][0] == 1 and self.cordnates[9][0] == 1:
			if (self.cordnates[7][1] == 1 and self.cordnates[8][1] == 1 and self.cordnates[9][1] == 1) or (self.cordnates[7][1] == 2 and self.cordnates[8][1] == 2 and self.cordnates[9][1] == 2):
				self.winlines(1,3)
		elif self.cordnates[1][0] == 1 and self.cordnates[4][0] == 1 and self.cordnates[7][0] == 1:
			if (self.cordnates[1][1] == 1 and self.cordnates[4][1] == 1 and self.cordnates[7][1] == 1) or (self.cordnates[1][1] == 2 and self.cordnates[4][1] == 2 and self.cordnates[7][1] == 2):
				self.winlines(2,1)
		elif self.cordnates[2][0] == 1 and self.cordnates[5][0] == 1 and self.cordnates[8][0] == 1:
			if (self.cordnates[2][1] == 1 and self.cordnates[5][1] == 1 and self.cordnates[8][1] == 1) or (self.cordnates[2][1] == 2 and self.cordnates[5][1] == 2 and self.cordnates[8][1] == 2):
				self.winlines(2,2)
		elif self.cordnates[3][0] == 1 and self.cordnates[6][0] == 1 and self.cordnates[9][0] == 1:
			if (self.cordnates[3][1] == 1 and self.cordnates[6][1] == 1 and self.cordnates[9][1] == 1) or (self.cordnates[3][1] == 2 and self.cordnates[6][1] == 2 and self.cordnates[9][1] == 2):
				self.winlines(2,3)
		elif self.cordnates[3][0] == 1 and self.cordnates[5][0] == 1 and self.cordnates[7][0] == 1:
			if (self.cordnates[2][1] == 1 and self.cordnates[5][1] == 1 and self.cordnates[7][1] == 1) or (self.cordnates[3][1] == 2 and self.cordnates[5][1] == 2 and self.cordnates[7][1] == 2):
				self.winlines(3,2)
		elif self.cordnates[1][0] == 1 and self.cordnates[5][0] == 1 and self.cordnates[9][0] == 1:
			if (self.cordnates[1][1] == 1 and self.cordnates[5][1] == 1 and self.cordnates[9][1] == 1) or (self.cordnates[1][1] == 2 and self.cordnates[5][1] == 2 and self.cordnates[9][1] == 2):
				self.winlines(3,1)
	def draw_game(self):	
		x = 1
		while x < len(self.cordnates) + 1:
			print self.cordnates[x]
			if self.cordnates[x][0] == 1 and self.cordnates[x][1] == 1:	
				self.XandO('o',x)
			elif self.cordnates[x][0] == 1 and self.cordnates[1][1] == 2:
				self.XandO('x',x)
			x += 1
	def draw_x_and_o(self):
		if self.cordnates[1][0] == 0 and self.cordnates[1][1] == 1:	
			self.XandO('o',1)
		elif self.cordnates[1][0] == 0 and self.cordnates[1][1] == 1:	
			self.XandO('x',1)
	def drawline(self):
		#data = (144, 18), (199,166), (63,73), (221, 71), (79,159)
		#data = (100,300),(200,300)
		#pygame.draw.lines(self.window,(0,0,255,255),0,data)
		pygame.draw.line(self.window,(0,255,255),(100,0),(100,300))
		pygame.draw.line(self.window,(0,255,255),(200,0),(200,300))
		pygame.draw.line(self.window,(0,255,255),(0,100),(300,100))
		pygame.draw.line(self.window,(0,255,255),(0,200),(300,200))
	def drawX(self,X,Y):
		#pygame.draw.line(self.window,(255,255,255),y1,x1)
		pygame.draw.line(self.window,(255,255,255),X,Y)
	def drawO(self,o,r):
		inner1 = o[0]
		inner2 = o[1]
		inner = (inner1,inner2)
		pygame.draw.circle(self.window,(255,255,255),o,r)
		pygame.draw.circle(self.window,(0,0,0),inner,r-1)
	def winlines(self,type,num):
		if type == 1:
			if num == 1:
				pygame.draw.line(self.window,(255,255,255),(0,50),(300,50))
			elif num == 2:
				pygame.draw.line(self.window,(255,255,255),(0,150),(300,150))
			elif num == 3:
				pygame.draw.line(self.window,(255,255,255),(0,250),(300,250))
		elif type == 2:
			if num == 1:
				pygame.draw.line(self.window,(255,255,255),(50,0),(50,300))
			elif num == 2:
				pygame.draw.line(self.window,(255,255,255),(150,0),(150,300))
			elif num == 3:
				pygame.draw.line(self.window,(255,255,255),(250,0),(250,300))
		elif type == 3:
			if num ==  1:
				pygame.draw.line(self.window,(255,255,255),(0,0),(300,300))
			elif num == 2:
				pygame.draw.line(self.window,(255,255,255),(300,0),(0,300))
	def XandO(self,type,p):
		if type == 'x':
			b1 = 0
			b2 = 100
			if p == 1:
				self.drawX((25,25),(75,75))
				self.drawX((75,25),(25,75))
			elif p == 2:
				self.drawX((125,25),(175,75))
				self.drawX((175,25),(125,75))
			elif p == 3:
				self.drawX((225,25),(275,75))
				self.drawX((275,25),(225,75))
			elif p == 4:
				self.drawX((25,125),(75,175))
				self.drawX((75,125),(25,175))
			elif p == 5:
				self.drawX((125,125),(175,175))
				self.drawX((175,125),(125,175))
			elif p == 6:
				self.drawX((225,125),(275,175))
				self.drawX((275,125),(225,175))
			elif p == 7:
				self.drawX((25,225),(75,275))
				self.drawX((75,225),(25,275))
			elif p == 8:
				self.drawX((125,225),(175,275))
				self.drawX((175,225),(125,275))
			elif p == 9:
				self.drawX((225,225),(275,275))
				self.drawX((275,225),(225,275))
		elif type == 'o':
			if p == 1:
				self.drawO((50,50),25)
			elif p == 2:
				self.drawO((150,50),25)
			elif p == 3:
				self.drawO((250,50),25)
			elif p == 4:
				self.drawO((50,150),25)
			elif p == 5:
				self.drawO((150,150),25)
			elif p == 6:
				self.drawO((250,150),25)
			elif p == 7:
				self.drawO((50,250),25)
			elif p == 8:
				self.drawO((150,250),25)
			elif p == 9:
				self.drawO((250,250),25)
	def update(self):
		while True:
			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					sys.exit()
			self.draw_game()
			#self.check_game()
			pygame.display.update()
			pygame.display.flip()
line = lines()
line.drawline()
line.update()