0

I have made a two player tic tac toe game using pygame which is completely basic.Everything in the game is working fine except the part where after one of the player wins by completing a match in a diagonal row.

The strike-through that connects the crosses or the noughts appear somehow appears after everything has been reset unlike the other cases when the strikethrough remains in the screen for 5s and then everything gets reset

#------Importing modules---------#

import pygame
import sys
import time
import pygame.mixer
from pygame.locals import*
import random


from gameover import*
from main1_pi import *
from main2 import *


#------Screen size & other parameters--------------#

pygame.init()


SCREEN_HEIGHT=768
SCREEN_WIDTH=1366

size=(SCREEN_WIDTH,SCREEN_HEIGHT)

ic=pygame.image.load('extras/ino.png')
pygame.display.set_icon(ic)
screen=pygame.display.set_mode((size),pygame.FULLSCREEN)

pygame.display.set_caption('PI-PY-PO')

#--------function to obtain text----------------#

def text_display(text,color,size,font):
   FONT=pygame.font.SysFont(font,size)
   FONT.set_bold(True)
   SURFACEFONT=FONT.render(text,True,color)
   return SURFACEFONT





#metro2(screen)                    #start screen

clr1=(22,122,211)                  #colors
clr2=(255,44,166)
clr3=(34,55,145)

red=(255,0,0)
white=(255,255,255)

p1,p2=0,0

text1=text_display('Player 2',white,30,'Arial')   #texts
text2=text_display('Player 1',white,30,'Arial')
text3=text_display('%s-%s'%(p1,p2),white,50,'Arial')




text1_a=text1.get_rect()
text2_b=text2.get_rect()
text3_b=text3.get_rect()

text1_a.center=(SCREEN_WIDTH-170,40)
text2_b.center=(180,40)
text3_b.center=(SCREEN_WIDTH/2,40)

clock=pygame.time.Clock()                    #
FPS=60
total_frames=0
width=260
height=195
xleft=300
ytop=100
turn=0
stat_of_turn=random.randint(1,2)
noft=0

list1=['0','0','0','0','0','0','0','0','0']
background1=pygame.image.load('back.jpg')

t=pygame.image.load('cross.gif')
p=pygame.image.load('circle.gif')
you=pygame.image.load('friend.gif')
friend=pygame.image.load('you.gif')



while True:




a=pygame.draw.rect(screen,white,(xleft,ytop,width,height))
b=pygame.draw.rect(screen,white,(xleft+width,ytop,width,height))
c=pygame.draw.rect(screen,white,(xleft+width*2,ytop,width,height))
d=pygame.draw.rect(screen,white,(xleft,ytop+height,width,height))
e=pygame.draw.rect(screen,white,(xleft+width,ytop+height,width,height))
f=pygame.draw.rect(screen,white,(xleft+width*2,ytop+height,width,height))
g=pygame.draw.rect(screen,white,(xleft,ytop+height*2,width,height))
h=pygame.draw.rect(screen,white,(xleft+width,ytop+height*2,width,height))
i=pygame.draw.rect(screen,white,(xleft+width*2,ytop+height*2,width,height))

screen.blit(background1,(0,0))

if stat_of_turn==1:
   screen.blit(you,(SCREEN_WIDTH-88,0))
   screen.blit(text1,text1_a)
if stat_of_turn==2:
    screen.blit(friend,(0,0))
    screen.blit(text2,text2_b)
screen.blit(text3,text3_b)

pygame.draw.line(screen, white, (xleft,ytop+height), (xleft+width*3,ytop+height),5)

pygame.draw.line(screen, white, (xleft,ytop+height*2), (xleft+width*3,ytop+height*2),5)

pygame.draw.line(screen, white, (xleft+width,ytop), (xleft+width,ytop+height*3),5)

pygame.draw.line(screen, white, (xleft+width*2,ytop), (xleft+width*2,ytop+height*3),5)

pos = pygame.mouse.get_pos()
pygame.mouse.set_visible(True)

if(list1[0]==list1[1]==list1[2]) and list1[0]!='0': 

   if stat_of_turn==1:
      p1=p1+1

   elif stat_of_turn==2:
      p2=p2+1
   x=pygame.draw.line(screen, white, (xleft,100+height/2), (xleft+width*3,100+height/2),5)

   pygame.display.update(x)
   time.sleep(5)
   list1=['0','0','0','0','0','0','0','0','0']
   stat_of_turn=random.randint(1,2)
   noft=0





elif(list1[0]==list1[3]==list1[6]) and list1[0]!='0':

   if stat_of_turn==1:
      p1=p1+1
   elif stat_of_turn==2:
      p2=p2+1

   x=pygame.draw.line(screen, white, (xleft+width/2,100), (xleft+width/2,100+3*height),5)

   pygame.display.update(x)
   time.sleep(5)

   list1=['0','0','0','0','0','0','0','0','0']
   stat_of_turn=random.randint(1,2)
   noft=0


elif(list1[0]==list1[4]==list1[8]) and list1[0]!='0':       

   if stat_of_turn==1:
      p1=p1+1
   elif stat_of_turn==2:
      p2=p2+1

   x=pygame.draw.line(screen, white, (xleft,100), (xleft+width*3,100+3*height),5)

   pygame.display.update(x)
   time.sleep(5)

   list1=['0','0','0','0','0','0','0','0','0']
   stat_of_turn=random.randint(1,2)
   noft=0


elif(list1[6]==list1[7]==list1[8]) and list1[8]!='0': 

   if stat_of_turn==1:
      p1=p1+1
   elif stat_of_turn==2:
      p2=p2+1

   x=pygame.draw.line(screen, white, (xleft,100+3*height-height/2), (xleft+width*3,100+3*height-height/2),5)

   pygame.display.update(x)
   time.sleep(5)

   list1=['0','0','0','0','0','0','0','0','0']
   stat_of_turn=random.randint(1,2)
   noft=0

   continue;
elif(list1[2]==list1[5]==list1[8]) and list1[8]!='0':

   if stat_of_turn==1:
      p1=p1+1
   elif stat_of_turn==2:
      p2=p2+1


   x=pygame.draw.line(screen, white, (xleft+width*3-width/2,100), (xleft+width*3-width/2,100+3*height),5)

   pygame.display.update(x)
   time.sleep(5)
   list1=['0','0','0','0','0','0','0','0','0']
   stat_of_turn=random.randint(1,2)
   noft=0

   continue;

elif(list1[6]==list1[4]==list1[2]) and list1[6]!='0':

   if stat_of_turn==1:
      p1=p1+1
   elif stat_of_turn==2:
      p2=p2+1



   x=pygame.draw.line(screen, white, (xleft+width*3,100), (xleft,100+3*height),1)

   pygame.display.update(x)
   time.sleep(5)
   list1=['0','0','0','0','0','0','0','0','0']
   stat_of_turn=random.randint(1,2)
   noft=0

   continue;

elif(list1[3]==list1[4]==list1[5]) and list1[3]!='0':

   if stat_of_turn==1:
      p1=p1+1
   elif stat_of_turn==2:
      p2=p2+1

   x=pygame.draw.line(screen, white, (xleft,100+height+height/2), (xleft+width*3,100+height+height/2),5)

   pygame.display.update(x)
   time.sleep(5)

   list1=['0','0','0','0','0','0','0','0','0']
   stat_of_turn=random.randint(1,2)
   noft=0

   continue;

elif(list1[1]==list1[4]==list1[7]) and list1[1]!='0':

   if stat_of_turn==1:
      p1=p1+1
   elif stat_of_turn==2:
      p2=p2+1


   x=pygame.draw.line(screen, white, (xleft+width*2-width/2,100), (xleft+width*2-width/2,100+3*height),5)

   pygame.display.update(x)
   time.sleep(5)
   list1=['0','0','0','0','0','0','0','0','0']
   stat_of_turn=random.randint(1,2)
   noft=0

   continue;

if noft==9:
   list1=['0','0','0','0','0','0','0','0','0']
   stat_of_turn=random.randint(1,2)
   noft=0

   continue;



text3=text_display('%s - %s'%(p1,p2),white,50,'Arial')





for event in pygame.event.get():
    if event.type==pygame.QUIT:
        pygame.quit()
        sys.exit()

    if event.type==pygame.KEYDOWN:

        if event.key==pygame.K_ESCAPE:
           metroxx(screen)

    if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:


            if a.collidepoint(pos) and list1[0]=='0':

               if stat_of_turn==2:
                  stat_of_turn=1
                  list1[0]='2'
               elif stat_of_turn==1:
                  stat_of_turn=2
                  list1[0]='1'
               noft=noft+1
            if b.collidepoint(pos)and list1[1]=='0':

               if stat_of_turn==2:
                  stat_of_turn=1
                  list1[1]='2'
               elif stat_of_turn==1:
                  stat_of_turn=2
                  list1[1]='1'
               noft=noft+1
            if c.collidepoint(pos)and list1[2]=='0':

               if stat_of_turn==2:
                  stat_of_turn=1
                  list1[2]='2'
               elif stat_of_turn==1:
                  stat_of_turn=2
                  list1[2]='1'
               noft=noft+1
            if d.collidepoint(pos)and list1[3]=='0':

               if stat_of_turn==2:
                  stat_of_turn=1
                  list1[3]='2'
               elif stat_of_turn==1:
                  stat_of_turn=2
                  list1[3]='1'
               noft=noft+1
            if e.collidepoint(pos)and list1[4]=='0':

               if stat_of_turn==2:
                  stat_of_turn=1
                  list1[4]='2'
               elif stat_of_turn==1:
                  stat_of_turn=2
                  list1[4]='1'
               noft=noft+1
            if f.collidepoint(pos)and list1[5]=='0':

               if stat_of_turn==2:
                  stat_of_turn=1
                  list1[5]='2'
               elif stat_of_turn==1:
                  stat_of_turn=2
                  list1[5]='1'
               noft=noft+1
            if g.collidepoint(pos)and list1[6]=='0':

               if stat_of_turn==2:
                  stat_of_turn=1
                  list1[6]='2'
               elif stat_of_turn==1:
                  stat_of_turn=2
                  list1[6]='1'
               noft=noft+1
            if h.collidepoint(pos)and list1[7]=='0':

               if stat_of_turn==2:
                  stat_of_turn=1
                  list1[7]='2'
               elif stat_of_turn==1:
                  stat_of_turn=2
                  list1[7]='1'
               noft=noft+1
            if i.collidepoint(pos)and list1[8]=='0':

               if stat_of_turn==2:
                  stat_of_turn=1
                  list1[8]='2'
               elif stat_of_turn==1:
                  stat_of_turn=2
                  list1[8]='1'
               noft=noft+1
if list1[0]=='1':
   screen.blit(t,a)
elif list1[0]=='2':
   screen.blit(p,a)
if list1[1]=='1':
   screen.blit(t,b)
elif list1[1]=='2':
   screen.blit(p,b)
if list1[2]=='1':
   screen.blit(t,c)
elif list1[2]=='2':
   screen.blit(p,c)
if list1[3]=='1':
   screen.blit(t,d)
elif list1[3]=='2':
   screen.blit(p,d)
if list1[4]=='1':
   screen.blit(t,e)
elif list1[4]=='2':
   screen.blit(p,e)
if list1[5]=='1':
   screen.blit(t,f)
elif list1[5]=='2':
   screen.blit(p,f)
if list1[6]=='1':
   screen.blit(t,g)
elif list1[6]=='2':
   screen.blit(p,g)
if list1[7]=='1':
   screen.blit(t,h)
elif list1[7]=='2':
   screen.blit(p,h)
if list1[8]=='1':
   screen.blit(t,i)
elif list1[8]=='2':
   screen.blit(p,i)

z=0
pygame.display.flip()
clock.tick(FPS)

enter image description here Horizontal and vertical ones working well as above

enter image description here Line appears after everything else has been rest. Awkward since the same technique as above is used.

enter image description here Same issue with the other diagonal too.

**the while loop has been properly indented in the original code.

Akshay
  • 463
  • 6
  • 15

0 Answers0