It runs and works just fine, but it needs more program.
And what we have now
import pygame
import sys
import time
from pygame import *
pygame.init()
ttt = pygame.display.set_mode((300,325))
pygame.display.set_caption('Tic-Tac-Toe')
#space for functions
# Loop control variable
running = True
while (running == True):
for event in pygame.event.get():
if event.type is QUIT:
running = False
just isn't gonna cut it.
So we have to add more.
First we have to add some more variables under our ttt.
import pygame
import sys
import time
from pygame import *
pygame.init()
ttt = pygame.display.set_mode((300,325))
[B]# X will go first
XO = "X"
# Empty Grid
grid = [[ None, None, None],
[ None, None, None],
[ None, None, None,]][/B]
pygame.display.set_caption('Tic-Tac-Toe')
#space for functions
# Loop control variable
running = True
while (running == True):
for event in pygame.event.get():
if event.type is QUIT:
running = False
Now I'll set up the basic board.Put an initBoard function in that whitespace that we left for functions.
def initBoard(ttt): pass
It takes a pygame screen(or Surface) as its parameters.
Now we will initialize the board.
def initBoard(ttt): [INDENT]# Initialize the board and return it # As a variable # ttt : a properly initialized display variable background = pygame.Surface(ttt.get_size()) background = background.convert() background.fill((255,255,255))[/INDENT]We make a Surface to blit(bit-block image transfer or place) onto the main screen.
This Surface is our background.
We then convert it.
Then we fill it using background.fill((255,255,255)).
background.fill() requires a tuple of RGB values.
Next we need to draw our lines.
def initBoard(ttt): ... [INDENT]# Draw the grid lines # Vertical lines pygame.draw.line(background, (0,0,0), (100,0), (100, 300), 2) pygame.draw.line(background, (0,0,0), (200,0), (200, 300), 2) # Horizontal lines pygame.draw.line(background, (0,0,0), (0,100), (300, 100), 2) pygame.draw.line(background, (0,0,0), (0,200), (300, 200), 2) # Return the board return background[/INDENT]
pygame.draw.line requires uses 5 parameters, the Surface to blit it to, the color(in RGB values), the coordinates where the line starts, coordinates where the line ends, and the width in pixels.
We then return the background.
We still don't have anything if you run the program, so we have to show it.
Just before your main loop add this:
[B]board = initBoard(ttt)[/B] running = True while (running == True): for event in pygame.event.get(): if event.type is QUIT: running = False
Now we will make the function showBoard.
We will write:
def showBoard(ttt, board): # Redraw the board on the screen(ttt) ttt.blit(board, (0,0)) pygame.display.flip()We have just blitted the board to the screen starting at point 0,0(the top-right corner)
pygame.display.flip simply changes the screen to meet any changes you made to it.
Save your program, run it, and then go to the next tutorial.


Sign In
Create Account


Back to top









