Python Forum

Full Version: Something weird happens with my game
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to make a simple Tic-Tac-Toe game with Pygame and I have a big problem. I've made it so that the SPACE key writes the symbol and the arrow keys are used to move around. I use a list ("list_") where I store the values for the squares. To know what value to change, I use a variable called "number" and changing it when the player moves.

The problem is, when I press SPACE it seems to change a random square, instead of the selected one. Something must be wrong with "number", but I can't find what it is. I'll mark where the variable is used or modified in the code. If someone can find the problem, I'll be really thankful.

Also, the arrow keys don't work too well. Sometimes it detects them, sometimes it doesn't, sometimes it moves two squares instead of one... I also need some help with that.

P.S.: I know it's probably really simple, but I'm a beginner to Python, so please understand this. Also, I'm Spanish, so please understand any grammar fails.

import pygame
import time
import os
import sys
from pygame.locals import*

list_=['','','','','','','','','']

WIDTH = 186
HEIGHT = 186

class Cursor(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = load_image("assets/cursor.png", True, 30, 30)
        self.xpos = 0
        self.ypos = 0
        self.realx = 59*self.xpos + 2
        self.realy = 59*self.ypos + 2
        self.number = 0         #################################################

    def update(self, keys):
        if self.xpos < 2:
            if keys[K_RIGHT]:
                self.xpos += 1
                self.number += 1      ##############################################
        if self.xpos > 0:
            if keys[K_LEFT]:
                self.xpos -= 1
                self.number -= 1      ##############################################
        if self.ypos < 2:
            if keys[K_DOWN]:
                self.ypos += 1
                self.number -= 3      ##############################################
        if self.ypos > 0:
            if keys[K_UP]:
                self.ypos -= 1
                self.number += 3      ##############################################

        self.realx = 59*self.xpos + 2
        self.realy = 59*self.ypos + 2

        return self.number            ##############################################

class cross(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = load_image("assets/cross.png", True, 30, 0)

class circle(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = load_image("assets/circle.png", True, 0, 0)

def updateSymbols(keys, cursorPos, turn):           #################################
    if keys[K_SPACE] and list_[cursorPos] == '':     ################################
        list_[cursorPos] = turn                                 #################################
        if turn == 'X':
            return 'O'
        elif turn == 'O':
            return 'X'
        elif turn == '' or turn == None:
            return 'X'
    else:
        return turn


def load_image(filename, transparent=False, x=0, y=0):
    try: image = pygame.image.load(filename)
    except pygame.error as message:
        raise SystemExit(message)
    image = image.convert()
    if transparent:
        color = image.get_at((x,y))
        image.set_colorkey(color, RLEACCEL)
    return image

def main():
    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    screen.fill([232, 244, 66])
    pygame.display.set_caption("TicTacToe")
    background = load_image("assets/background.png", True, 20,20)
    cursor = Cursor()
    o = circle()
    x = cross()

    cursorPos = 0                             #####################################
    turn = 'X'

    #symbol variables
    xpos = 0
    ypos = 0
    rx = 0
    ry = 0

    while True:
        xpos = 0
        ypos = 0
        keys = pygame.key.get_pressed()
        for eventos in pygame.event.get():
            if eventos.type == QUIT:
                sys.exit(0)

        cursorPos = cursor.update(keys)        ################################
        turn = updateSymbols(keys, cursorPos, turn)    ##########################
        print(turn)
        screen.blit(background, (0,0))
        for n in range (0,8):
            if xpos < 2:
                xpos += 1
            else:
                ypos += 1
                xpos = 0

            rx = 59*xpos + 7
            ry = 59*ypos + 7
           # print(list_)
            if list_[n] == 'X':
                screen.blit(x.image, (rx, ry))
            elif list_[n] == 'O':
                screen.blit(o.image, (rx, ry))
            elif list_[n] == None:
                print("Error!")
                quit()
        screen.blit(cursor.image,(cursor.realx, cursor.realy))
        pygame.display.flip()

        time.sleep(0.1)

    return 0

if __name__ == '__main__':
    pygame.init()
    main()