Jump to content

Pygame/Python Text Display

aikoels

Sorry, I am loosing my mind. I haven't used pygame in a while and want to get back into it, so I am starting by making a giant tic tac toe board. But for whatever reason the 'X' and 'O' aren't displaying over the background. I was just wondering if anyone sees anything obvious I am missing...

 

part I am talking about:

for place in self.board.keys():            if self.board[place] != 'EMPTY':                move = largeFont.render(self.board[place], True, (255, 255, 255))                scrn.blit(move, placeToCords(int(brdNum), int(place)))                pygame.display.update()

 

full code:

import pygamefrom pygame.locals import * pygame.init() scrn = pygame.display.set_mode((600, 610)) smallFont = pygame.font.SysFont(None, 12)largeFont = pygame.font.SysFont(None, 50)background = pygame.image.load('background.jpg').convert()turn = 'X' class smBoard:    def __init__(self, index):        self.board = {'0':'EMPTY', '1':'EMPTY', '2':'EMPTY',                      '3':'EMPTY', '4':'EMPTY', '5':'EMPTY',                      '6':'EMPTY', '7':'EMPTY', '8':'EMPTY'}        self.own = None #nobody has this board yet        posPositions = [0, 213, 418]        self.pos = (index%3, int(index/3)) #the position of the board (not x, y)    def place(self, pos, chip):        self.board[str(pos)] = chip     def update(self, brdNum):        if self.own == None:            wins = [('0', '1', '2'), ('3', '4', '5'), ('6', '7', '8'),                    ('0', '3', '6'), ('1', '4', '7'), ('2', '5', '8'),                    ('0', '4', '8'), ('2', '4', '6')]            for winSet in wins:                pos1, pos2, pos3 = winSet[0], winSet[1], winSet[2]                chip1, chip2, chip3 = self.board[pos1], self.board[pos2], self.board[pos3]                if (chip1 == 'X' and chip2 == 'X' and chip3 == 'X') or (chip1 == 'O' and chip2 == 'O' and chip3 == 'O'):                    self.own = chip1                    print chip1 + ' claims a board!'        for place in self.board.keys():            if self.board[place] != 'EMPTY':                move = largeFont.render(self.board[place], True, (255, 255, 255))                scrn.blit(move, placeToCords(int(brdNum), int(place)))                pygame.display.update()                 def placeToCords(index, place): #index is the board number, 0-8, place is the place on the board 1-9    boardLocs = [0, 213, 418] #possible x, y locations    boardPos = (boardLocs[index%3], boardLocs[index/3])     placeLocs = [0, 63, 123]    place -= 1 #used for math    placePos = (placeLocs[place%3], placeLocs[place/3])    finalPos = (boardPos[0] + placePos[0], boardPos[1] + placePos[1])     return finalPos def updateTurn(turn):    if turn == 'X':        return 'O'    else:        return 'X'    def clickToPos(clickPos):    boardLocs = [0, 213, 418, 9999]    placeLocs = [0, 63, 123, 9999]    for x in xrange(len(boardLocs)):        num = boardLocs[x]        if clickPos[0] < num:            boardx = boardLocs[x - 1]            break    for x in xrange(len(boardLocs)):        num = boardLocs[x]        if clickPos[1] < num:            boardy = boardLocs[x - 1]            break            for x in xrange(len(placeLocs)):        num = placeLocs[x] + boardx        if clickPos[0] < num:            placex = placeLocs[x-1] + boardx            break    for x in xrange(len(placeLocs)):        num = placeLocs[x] + boardy        if clickPos[1] < num:            placey = placeLocs[x-1] + boardy            break     if placex >= 418:        placex -= 418    elif placex >= 213:        placex -= 213    if placey >= 418:        placey -= 418    elif placey >= 213:        placey -= 213      boardPos = (boardLocs.index(boardx), boardLocs.index(boardy))    boardNum = boardPostoNum(boardPos)    placeNum = placePostoNum((placex, placey))    return (boardNum, placeNum) def boardPostoNum(pos): #convert a tuple of board location to board index number (1, 0) -> 1    return pos[0] + (pos[1]*3) def placePostoNum(pos):    place = 0    if pos[0] == 63:        place += 1    elif pos[0] == 123:        place += 2            if pos[1] == 63:        place += 3    elif pos[1] == 123:        place += 6    return place boards = []for index in xrange(9):    boards.append(smBoard(index))keepGoing = Truescrn.blit(background, (0, 0))while keepGoing:    for event in pygame.event.get():        if event.type == QUIT:            keepGoing = False        if event.type == KEYDOWN:            if event.key == 27: #escape key                keepGoing = False        if event.type == MOUSEBUTTONDOWN and event.button == 1: #left click            tup = clickToPos(event.pos)            if boards[tup[0]].board[str(tup[1])] == 'EMPTY':                boards[tup[0]].place(tup[1], turn)                turn = updateTurn(turn)                print 'Next Turn: ' + turn            else:                print 'Invalid location!'                    for x in xrange(len(boards)):        board = boards[x]        board.update(x)    pygame.display.update() pygame.quit()

Intel 3570K - MSI GTX 660Ti 3GB OC Edition - 16GB Corsair LP RAM - ASRock Extreme4 Motherboard - Corsair HX850 - Adata Premier Pro SP900 120GB SSD with Windows 7 - Seagate Barracuda 1TD HDD - Seagate Barracuda 500GB HDD - Thermaltake Frio CPU Cooler - CM Storm Enforcer Case - Macbook Pro Early 2011 Laptop

Link to comment
Share on other sites

Link to post
Share on other sites

What is your background image? It seems to work fine for my on a plain black background.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

What is your background image? It seems to work fine for my on a plain black background.

I have tried it on plain black and also on a colored background, no luck, maybe I just need to reinstall pygame then?

Intel 3570K - MSI GTX 660Ti 3GB OC Edition - 16GB Corsair LP RAM - ASRock Extreme4 Motherboard - Corsair HX850 - Adata Premier Pro SP900 120GB SSD with Windows 7 - Seagate Barracuda 1TD HDD - Seagate Barracuda 500GB HDD - Thermaltake Frio CPU Cooler - CM Storm Enforcer Case - Macbook Pro Early 2011 Laptop

Link to comment
Share on other sites

Link to post
Share on other sites

I reinstalled it, and everything seems to be working now. I haven't installed pygame in a long time (since I last used it) and if I had to guess I would say i've reinstalled python since then, so it was probably just broke then. Thanks for the help! It would have taken me forever trying to fix that code before I thought to reinstall!

Intel 3570K - MSI GTX 660Ti 3GB OC Edition - 16GB Corsair LP RAM - ASRock Extreme4 Motherboard - Corsair HX850 - Adata Premier Pro SP900 120GB SSD with Windows 7 - Seagate Barracuda 1TD HDD - Seagate Barracuda 500GB HDD - Thermaltake Frio CPU Cooler - CM Storm Enforcer Case - Macbook Pro Early 2011 Laptop

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×