I’m making a tic-tac-toe game with pygame.
Everything is good except that I can’t make image2 and image 3, who correspond to X and O appears using blit even after an hour :(.
Here’s my code
They are in the main() function and I used is_clicked to verify some methods so it doesn’t take a part of the code.
import pygame, sys, random
from pygame.locals import *
import math
(width, height) = (600,600)
window = pygame.display.set_mode((width, height))
image = pygame.image.load("background.jpg").convert_alpha()
image2 = pygame.image.load("croix.png").convert_alpha()
image3 = pygame.image.load("rond.png").convert_alpha()
joueur1 = 0
joueur2 = 1
map = [['-', '-', '-'], ['-', '-', '-'], ['-', '-', '-']]
def pleine(map_game):
for ligne in map_game:
for symbole in ligne:
if symbole == '-':
return False
return True
def gagne_j1(map_game):
for ligne in map_game:
if ligne == [joueur1,joueur1,joueur1]:
return True
for indice_colonne in range(3):
if (map_game[0][indice_colonne] == joueur1 and map_game[1][indice_colonne] == joueur1\
and map_game[2][indice_colonne] == joueur1):
return True
def gagne_j2(map_game):
for ligne in map_game:
if ligne == [joueur2, joueur2, joueur2]:
return True
for indice_colonne in range(3):
if (map_game[0][indice_colonne] == joueur2 and map_game[1][indice_colonne] == joueur2 \
and map_game[2][indice_colonne] == joueur2):
return True
def turn(num):
if num%2 == 0:
num = 0
image_joueur = image2
else:
num = 1
image_joueur = image3
return num
def draw(image_j, i,j):
window.blit(image_j, (i,j))
pygame.display.update()
def main():
joueur_actif = 0
image_joueur = image2
is_clicked = False
while True:
window.blit(image, (0, 0))
if is_clicked:
window.blit(image_joueur, (0, 0))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if pygame.mouse.get_pressed() == (1,0,0):
x,y = pygame.mouse.get_pos()
i = math.trunc(y/200)
j = math.trunc(x/200)
if map[i][j] == '-':
map[i][j] = joueur_actif
draw(image_joueur, i,j)
is_clicked = True
joueur_actif+=1
joueur_actif = turn(joueur_actif)
if gagne_j1(map) or gagne_j2(map) or pleine(map):
pygame.quit()
sys.exit()
pygame.display.flip()
main()