I am trying to make a tic tak toe game with pygame and I was wondering how would I do the logic here is what I have so far. VIDEO < I only have it when I click on the middle button it will display the player 2 x on the screen and then the image that is hovering over my mouse will turn into O for player 1 turn to go BUT my question is how would I do the logic like if player 1 gets 3 in a row or player 2 gets 3 across in a row I am really confused on this and I need someone to walk me throw it
my tic tak toe game right now CODE
import pygame,random
pygame.init()
# draw our window
window = pygame.display.set_mode((500,540),pygame.NOFRAME)
pygame.display.set_caption("Tic Tac TOE")
MANUAL_CURSOR = pygame.image.load('nw.png').convert_alpha()
MANUAL_CURSOR2 = pygame.image.load('nOW.png').convert_alpha()
bg = pygame.image.load("ticO.png")
fps = 40
clock = pygame.time.Clock()
class button():
def __init__(self, color, x,y,width,height, text=''):
self.color = color
self.x = x
self.y = y
self.width = width
self.height = height
self.text = text
self.over = False
def draw(self,window,outline=None):
#Call this method to draw the button on the screen
if outline:
pygame.draw.rect(window, outline, (self.x-2,self.y-2,self.width+4,self.height+4),0)
pygame.draw.rect(window, self.color, (self.x,self.y,self.width,self.height),0)
if self.text != '':
font = pygame.font.SysFont('comicsans', 60)
text = font.render(self.text, 1, (0,0,0))
window.blit(text, (self.x + (self.width/2 - text.get_width()/2), self.y + (self.height/2 - text.get_height()/2)))
def isOver(self, pos):
#Pos is the mouse position or a tuple of (x,y) coordinates
if pos[0] > self.x and pos[0] < self.x + self.width:
if pos[1] > self.y and pos[1] < self.y + self.height:
return True
return False
def playSoundIfMouseIsOver(self, pos, sound):
if self.isOver(pos):
if not self.over:
beepsound.play()
self.over = True
else:
self.over = False
class particle:
def __init__(self,x):
self.x = x
self.partilight = pygame.image.load("noW.png")
def draw(self,window):
window.blit(self.partilight,(self.x))
white = (250,250,250)
greenbutton2 = button((0,255,0),190,215,100,100, '')
greenbutton3 = button((0,255,0),335,215,100,100, '')
greenbutton4 = button((0,255,0),70,215,100,100, '')
greenbutton5 = button((0,255,0),70,350,100,100, '')
greenbutton6 = button((0,255,0),190,350,100,100, '')
greenbutton7 = button((0,255,0),335,350,100,100, '')
greenbutton8 = button((0,255,0),70,90,100,100, '')
greenbutton9 = button((0,255,0),190,90,100,100, '')
greenbutton10 = button((0,255,0),335,90,100,100, '')
font = pygame.font.Font("fos.ttf", 60)
score = 1
cointext = font.render("" + str(score), True, (255,255,255))
coinrect = cointext.get_rect()
coinrect.center = ((100,50))
particles = []
pos = pygame.mouse.get_pos()
def redraw():
window.fill((174, 214, 241))
window.blit(bg,(0,0))
greenbutton2.draw(window)
greenbutton3.draw(window)
greenbutton4.draw(window)
greenbutton5.draw(window)
greenbutton6.draw(window)
greenbutton7.draw(window)
greenbutton8.draw(window)
greenbutton9.draw(window)
greenbutton10.draw(window)
for particle in particles:
particle.draw(window)
pos = pygame.mouse.get_pos()
if score >= 0:
window.blit(MANUAL_CURSOR, MANUAL_CURSOR.get_rect(center = pygame.mouse.get_pos()))
pygame.mouse.set_visible(False)
if score >= 1:
window.blit(MANUAL_CURSOR2, MANUAL_CURSOR2.get_rect(center = pygame.mouse.get_pos()))
if score >= 2:
window.blit(MANUAL_CURSOR, MANUAL_CURSOR.get_rect(center = pygame.mouse.get_pos()))
if score >= 3:
window.blit(MANUAL_CURSOR2, MANUAL_CURSOR2.get_rect(center = pygame.mouse.get_pos()))
if score >= 4:
window.blit(MANUAL_CURSOR, MANUAL_CURSOR.get_rect(center = pygame.mouse.get_pos()))
if score >= 5:
window.blit(MANUAL_CURSOR2, MANUAL_CURSOR2.get_rect(center = pygame.mouse.get_pos()))
if score >= 6:
window.blit(MANUAL_CURSOR, MANUAL_CURSOR.get_rect(center = pygame.mouse.get_pos()))
if score >= 7:
window.blit(MANUAL_CURSOR2, MANUAL_CURSOR2.get_rect(center = pygame.mouse.get_pos()))
if score >= 8:
window.blit(MANUAL_CURSOR, MANUAL_CURSOR.get_rect(center = pygame.mouse.get_pos()))
# our main loop
runninggame = True
while runninggame:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
runninggame = False
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
if greenbutton2.isOver(pos):
score += 1
cointext = font.render("" + str(score), True, (255,255,255))
coinrect.center = ((100,50))
for x in range(4):
particles.append(particle(MANUAL_CURSOR.get_rect(center = pygame.mouse.get_pos())))
redraw()
pygame.display.update()
pygame.quit()
the assets I am using Please! use so when your testing out the code
from Pygame Tic Tak Toe Logic? How Would I Do It
No comments:
Post a Comment