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

[pygame] Anybody think they can help me with my small problem?



Well I was trying to get the current area of a Tic Tac Toe box: http://uni-code.com/MADIMG/1/afdeUntitled-1.gif

The first 3 boxes worked and printed 1,2,3 then when I got to all the other ones it just spazzed.

    def check_mouse(self,mouse):
        m1 = mouse[0]
        m2 = mouse[1]
        x = 1
        while x < len( self.area) + 1:
            if (m1 - self.area[x][0]) in range(0,100) and (m2 - self.area[x][1]) in range(0,100):
                print m1 - self.area[x][0],m2 - self.area[x][1]
                print x
                print
            elif (m1 - self.area[x][0]) in range(100,200) and (m2 - self.area[x][1]) in range(100,200):
                print m1 - self.area[x][0],m2 - self.area[x][1]
                print x
                print
            elif (m1 - self.area[x][0]) in range(200,300) and (m2 - self.area[x][1]) in range(200,300):
                print m1 - self.area[x][0],m2 - self.area[x][1]
                print x
                print
            elif (m1 - self.area[x][0]) in range(0,100) and (m2 - self.area[x][1]) in range(100,200):
                print m1 - self.area[x][0],m2 - self.area[x][1]
                print x
            elif (m1 - self.area[x][0]) in range(200,300) and (m2 - self.area[x][1]) in range(100,300):
                print x
            elif (m1 - self.area[x][0]) in range(300,400) and (m2 - self.area[x][1]) in range(100,200):
                print x
            elif (m1 - self.area[x][0]) in range(0,100) and (m2 - self.area[x][1]) in range(200,300):
                print x
            elif (m1 - self.area[x][0]) in range(100,200) and (m2 - self.area[x][1]) in range(200,300):
                print x
            elif (m1 - self.area[x][0]) in range(200,300) and (m2 - self.area[x][1]) in range(200,300):
                print x
            x += 1
It wasn't getting the correct box, it printed 2 nums out at the same time...
#CopyRighted Lamonte of CleanScript.com
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 : [0,0] , 2 : [0,0] , 3 : [0,0],
		4 : [0,0] , 5 : [0,0] , 6 : [0,0],
		7 : [0,0] , 8 : [0,0] , 9 : [0,0]
		}
		self.area = {
		1 : (0,0) , 2 : (100,0) , 3 : (200,0),
		4 : (0,100), 5 : (100,100), 6 : (200,100),
		5 : (0,200), 5 : (100,200), 7 : (200,200)
		}
		self.gameover = False
	def check_mouse(self,mouse):
		m1 = mouse[0]
		m2 = mouse[1]
		x = 1
		while x < len(self.area) + 1:
			if (m1 - self.area[x][0]) in range(0,100) and (m2 - self.area[x][1]) in range(0,100):
				print m1 - self.area[x][0],m2 - self.area[x][1]
				print x
				print 
			elif (m1 - self.area[x][0]) in range(100,200) and (m2 - self.area[x][1]) in range(100,200):
				print m1 - self.area[x][0],m2 - self.area[x][1]
				print x
				print
			elif (m1 - self.area[x][0]) in range(200,300) and (m2 - self.area[x][1]) in range(200,300):
				print m1 - self.area[x][0],m2 - self.area[x][1]
				print x
				print
			elif (m1 - self.area[x][0]) in range(0,100) and (m2 - self.area[x][1]) in range(100,200):
				print m1 - self.area[x][0],m2 - self.area[x][1]
				print x
			elif (m1 - self.area[x][0]) in range(200,300) and (m2 - self.area[x][1]) in range(100,300):
				print x
			elif (m1 - self.area[x][0]) in range(300,400) and (m2 - self.area[x][1]) in range(100,200):
				print x
			elif (m1 - self.area[x][0]) in range(0,100) and (m2 - self.area[x][1]) in range(200,300):
				print x
			elif (m1 - self.area[x][0]) in range(100,200) and (m2 - self.area[x][1]) in range(200,300):
				print x
			elif (m1 - self.area[x][0]) in range(200,300) and (m2 - self.area[x][1]) in range(200,300):
				print x
			x += 1
	def check_game(self):
		if (self.cordnates[1][0] == 1 and self.cordnates[2][0] == 1 and self.cordnates[3][0] == 1) and ((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) and ((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) and ((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) and ((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) and ((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) and ((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) and ((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) and ((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:
			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[x][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()
				elif event.type == pygame.MOUSEBUTTONDOWN:
					self.check_mouse(pygame.mouse.get_pos())
			self.draw_game()
			self.check_game()
			pygame.display.update()
			pygame.display.flip()
line = lines()
line.drawline()
line.update()