I was running this on python 3.10.11 32-bit and the issue is caused by a duplicated block of code defining the Cell class and the resulting indentation errors.
Issue1
I have corrected it and made following changes for "Issue1"
-Removed the duplicated definition of the Cell class.
-Ensured all necessary parts of the script are included only once and organized properly.
-Adjusted the event handling to include the keypress logic within a single loop.
-Ensured the screen is cleared only once per loop.
Issue2
I have corrected it and made following changes for "Issue2"
-Fixed Text Positioning: The positions for player scores and game status messages are calculated and blitted correctly to avoid overlap.
-Game Over Screen: When the game is over, the screen is cleared, and the game over message, the winner, and the restart instructions are displayed prominently.
-Highlighting Current Turn: A visual indicator (a small rectangle) under the current player's score text highlights whose turn it is.
I was running this on python 3.10.11 32-bit and the issue is caused by a duplicated block of code defining the Cell class and the resulting indentation errors.
Issue1
I have corrected it and made following changes for "Issue1" -Removed the duplicated definition of the Cell class. -Ensured all necessary parts of the script are included only once and organized properly. -Adjusted the event handling to include the keypress logic within a single loop. -Ensured the screen is cleared only once per loop.
Issue2
I have corrected it and made following changes for "Issue2" -Fixed Text Positioning: The positions for player scores and game status messages are calculated and blitted correctly to avoid overlap. -Game Over Screen: When the game is over, the screen is cleared, and the game over message, the winner, and the restart instructions are displayed prominently. -Highlighting Current Turn: A visual indicator (a small rectangle) under the current player's score text highlights whose turn it is.
Corrected and updated code
import pygame
Constants
SCREEN_WIDTH, SCREEN_HEIGHT = 300, 300 CELL_SIZE = 40 PADDING = 20 ROWS = COLS = (SCREEN_WIDTH - 4 * PADDING) // CELL_SIZE
Colors
WHITE = (255, 255, 255) RED = (252, 91, 122) BLUE = (78, 193, 246) GREEN = (0, 255, 0) BLACK = (12, 12, 12) DARK_GRAY = (30, 30, 30) LIGHT_GRAY = (100, 100, 100)
Initialize Pygame
pygame.init()
win = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) font = pygame.font.SysFont('cursive', 25)
class Cell: def init(self, r, c): self.r = r self.c = c self.index = self.r ROWS + self.c self.rect = pygame.Rect((self.c CELL_SIZE + 2 PADDING, self.r CELL_SIZE + 3 * PADDING, CELL_SIZE, CELL_SIZE)) self.edges = [ [(self.rect.left, self.rect.top), (self.rect.right, self.rect.top)], [(self.rect.right, self.rect.top), (self.rect.right, self.rect.bottom)], [(self.rect.right, self.rect.bottom), (self.rect.left, self.rect.bottom)], [(self.rect.left, self.rect.bottom), (self.rect.left, self.rect.top)] ] self.sides = [False, False, False, False] self.winner = None
def create_cells(): cells = [] for r in range(ROWS): for c in range(COLS): cell = Cell(r, c) cells.append(cell) return cells
def reset_cells(): return None, None, False, False, False, False
def reset_score(): return 0, 0, 0
def reset_player(): return 0, ['X', 'O'], 'X', False
Game variables initialization
game_over = False cells = create_cells() pos, current_cell, up, right, bottom, left = reset_cells() fill_count, p1_score, p2_score = reset_score() turn, players, current_player, next_turn = reset_player()
Main game loop
running = True while running:
pygame.quit()