|
Expert Programmer
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 
|
Naughts and Crosses
Ok it may not seem so amazing to you, but this is my first ever proper pygame game. It took me 9 hours (with potty breaks and lunch break).
You can download the source code and files here: http://jackf.cogia.net/coldDeath-XandO.rar
UnRAR the file, then doubleclick game.py to run it.
It will require Python and pyGame to run.
Here is the source code so you can check it out:
#Name: Naughts and Crosses
#Author: ColdDeath
#Date: 11/09/2005
import os
import pygame
import random
import sys
from pygame.locals import *
pygame.init()
#Window Setup
gameWindow = pygame.display.set_mode((150, 150))
pygame.display.set_caption('X\'s and O\'s')
fileName_Icon = os.path.join('Data', 'x.png')
surface_Icon = pygame.image.load(fileName_Icon)
pygame.display.set_icon(surface_Icon)
gameScreen = pygame.display.get_surface()
#Make image paths into variables
fileName_Cross = os.path.join('Data', 'x.png')
fileName_Naught = os.path.join('Data', 'o.png')
fileName_Board = os.path.join('Data', 'board.png')
fileName_Win = os.path.join('Data', 'win.png')
fileName_Lose = os.path.join('Data', 'lose.png')
fileName_Draw = os.path.join('Data', 'draw.png')
#Make images into surfaces
surface_Cross = pygame.image.load(fileName_Cross)
surface_Naught = pygame.image.load(fileName_Naught)
surface_Board = pygame.image.load(fileName_Board)
surface_Win = pygame.image.load(fileName_Win)
surface_Lose = pygame.image.load(fileName_Lose)
surface_Draw = pygame.image.load(fileName_Draw)
#Draw the grid/board
gameScreen.blit(surface_Board, (0,0))
#Update Screen
pygame.display.flip()
class game:
Board = ['n','n','n','n','n','n','n','n','n']
availibleMoves = [0,1,2,3,4,5,6,7,8]
turn = 'x'
def checkQuit(self,events):
#When game is over check to see if Enter is press or if Exit button is clicked, then exits
for event in events:
if event.type == QUIT:
sys.exit(0)
if event.type == KEYDOWN:
if event.key == 13:
sys.exit(0)
def input(self,events):
#Checks for keyboard input and make a move if it finds it
for event in events:
if event.type == QUIT:
sys.exit(0)
if event.type == KEYUP:
if event.key == 49:
self.Board[0] = 'x'
gameScreen.blit(surface_Cross, (0,0))
self.availibleMoves[0] = 'n'
self.turn = 'o'
elif event.key == 50:
self.Board[1] = 'x'
gameScreen.blit(surface_Cross, (50,0))
self.availibleMoves[1] = 'n'
self.turn = 'o'
elif event.key == 51:
self.Board[2] = 'x'
gameScreen.blit(surface_Cross, (100,0))
self.availibleMoves[2] = 'n'
self.turn = 'o'
elif event.key == 52:
self.Board[3] = 'x'
gameScreen.blit(surface_Cross, (0,50))
self.availibleMoves[3] = 'n'
self.turn = 'o'
elif event.key == 53:
self.Board[4] = 'x'
gameScreen.blit(surface_Cross, (50,50))
self.availibleMoves[4] = 'n'
self.turn = 'o'
elif event.key == 54:
self.Board[5] = 'x'
gameScreen.blit(surface_Cross, (100,50))
self.availibleMoves[5] = 'n'
self.turn = 'o'
elif event.key == 55:
self.Board[6] = 'x'
gameScreen.blit(surface_Cross, (0,100))
self.availibleMoves[6] = 'n'
self.turn = 'o'
elif event.key == 56:
self.Board[7] = 'x'
gameScreen.blit(surface_Cross, (50,100))
self.availibleMoves[7] = 'n'
self.turn = 'o'
elif event.key == 57:
self.Board[8] = 'x'
gameScreen.blit(surface_Cross, (100,100))
self.availibleMoves[8] = 'n'
self.turn = 'o'
pygame.display.flip()
else:
#print event
pass
def aiMove(self):
new = random.choice(self.availibleMoves)
try:
if new == 'n':
self.aiMove()
else:
if self.Board[new] == 'n':
if new == 0:
self.Board[0] = 'o'
gameScreen.blit(surface_Naught, (0,0))
self.availibleMoves[0] = 'n'
elif new == 1:
self.Board[1] = 'o'
gameScreen.blit(surface_Naught, (50,0))
self.availibleMoves[1] = 'n'
elif new == 2:
self.Board[2] = 'o'
gameScreen.blit(surface_Naught, (100,0))
self.availibleMoves[2] = 'n'
elif new == 3:
self.Board[3] = 'o'
gameScreen.blit(surface_Naught, (0,50))
self.availibleMoves[3] = 'n'
elif new == 4:
self.Board[4] = 'o'
gameScreen.blit(surface_Naught, (50,50))
self.availibleMoves[4] = 'n'
elif new == 5:
self.Board[5] = 'o'
gameScreen.blit(surface_Naught, (100,50))
self.availibleMoves[5] = 'n'
elif new == 6:
self.Board[6] = 'o'
gameScreen.blit(surface_Naught, (0,100))
self.availibleMoves[6] = 'n'
elif new == 7:
self.Board[7] = 'o'
gameScreen.blit(surface_Naught, (50,100))
self.availibleMoves[7] = 'n'
elif new == 8:
self.Board[8] = 'o'
gameScreen.blit(surface_Naught, (100,100))
self.availibleMoves[8] = 'n'
except TypeError:
self.aiMove()
pygame.display.flip()
self.turn = 'x'
def checkStatus(self):
#Checks if anyone has won
#X win conditions
if self.Board[0:3] == ['x','x','x']: return 'x'
if self.Board[3:6] == ['x','x','x']: return 'x'
if self.Board[6:10] == ['x','x','x']: return 'x'
if (self.Board[0]+self.Board[3]+self.Board[6] == 'xxx'): return 'x'
if (self.Board[1]+self.Board[4]+self.Board[7] == 'xxx'): return 'x'
if (self.Board[2]+self.Board[5]+self.Board[8] == 'xxx'): return 'x'
if (self.Board[0]+self.Board[4]+self.Board[8] == 'xxx'): return 'x'
if (self.Board[2]+self.Board[4]+self.Board[6] == 'xxx'): return 'x'
#O win conditions
if self.Board[0:3] == ['o','o','o']: return 'o'
if self.Board[3:6] == ['o','o','o']: return 'o'
if self.Board[6:10] == ['o','o','o']: return 'o'
if (self.Board[0]+self.Board[3]+self.Board[6] == 'ooo'): return 'o'
if (self.Board[1]+self.Board[4]+self.Board[7] == 'ooo'): return 'o'
if (self.Board[2]+self.Board[5]+self.Board[8] == 'ooo'): return 'o'
if (self.Board[0]+self.Board[4]+self.Board[8] == 'ooo'): return 'o'
if (self.Board[2]+self.Board[4]+self.Board[6] == 'ooo'): return 'o'
#Draw conditions
if (self.availibleMoves == ['n','n','n','n','n','n','n','n','n']):
return 'd'
else:
return 'n'
NewGame = game()
#Main program loop
while 1:
#if x has won show the win image
if NewGame.checkStatus() == 'x':
gameScreen.blit(surface_Win, (0,0))
pygame.display.flip()
NewGame.checkQuit(pygame.event.get())
#if o has won show the lose image
elif NewGame.checkStatus() == 'o':
gameScreen.blit(surface_Lose, (0,0))
pygame.display.flip()
NewGame.checkQuit(pygame.event.get())
#if its a draw show draw image
elif NewGame.checkStatus() == 'd':
gameScreen.blit(surface_Draw, (0,0))
pygame.display.flip()
NewGame.checkQuit(pygame.event.get())
#otherwise make a move
else:
if NewGame.turn == 'x':
NewGame.input(pygame.event.get())
elif NewGame.turn == 'o':
NewGame.aiMove() Woot i'm really happy
Here is a screenshot of it in action:
__________________
Join us at #programmingforums @ irc.freenode.net!
My software never has bugs. It just develops random features.
|