Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # MYCODE
- # Summary of game.
- import pygame, sys, time
- from pygame.locals import *
- # User-defined classes
- class Tile:
- #Class Attributes
- fgColor=pygame.Color('white')
- bgColor=pygame.Color('black')
- borderWidth=3
- flashTime=0.05
- def __init__(self,x,y,surface):
- self.surface=surface
- width=self.surface.get_width()//3
- height=self.surface.get_height()//3
- self.rect=Rect(x,y,width,height)
- self.content=''
- def draw(self):
- pygame.draw.rect(self.surface,Tile.bgColor,self.rect)
- pygame.draw.rect(self.surface,Tile.fgColor,self.rect,Tile.borderWidth)
- font=pygame.font.SysFont(None,100,True)
- textSurface=font.render(self.content,True,Tile.fgColor,Tile.bgColor)
- textRect=textSurface.get_rect()
- textRect.center=self.rect.center
- surface.blit(textSurface,textRect)
- def handleMouseUp(self,position):
- if self.rect.collidepoint(position):
- if self.content == '':
- self.content='X'
- else:
- self.flash()
- def flash(self):
- pygame.draw.rect(self.surface,Tile.fgColor,self.rect)
- pygame.display.update()
- time.sleep(Tile.flashTime)
- self.draw()
- # User-defined functions
- def update(tile):
- # Check if the game is over. If so, end the game and
- # returnTrue. Otherwise, update the game objects, draw
- # them, and return False.
- # This is an example update function - replace it.
- # - center is a list containing the x and y int coords
- # of the center of a circle
- # - surface is the pygame.Surface object for the window
- if False: # check if the game is over
- return True
- else: # continue the game
- tile.draw()
- return True
- # Main program
- # Initialize pygame
- pygame.init()
- # Set window size and title, and frame delay
- surfaceSize = (500, 400) # example window size
- windowTitle = 'Tic Tac Toe' # example title
- #frameDelay = 0.02 # smaller is faster game
- # Create the window
- surface = pygame.display.set_mode(surfaceSize, 0, 0)
- pygame.display.set_caption(windowTitle)
- # create and initialize objects
- gameOver = False
- tile=Tile(0,0,surface)
- # Draw objects
- tile.draw()
- # Refresh the display
- pygame.display.update()
- # Loop forever
- while True:
- # Handle events
- for event in pygame.event.get():
- if event.type == QUIT:
- pygame.quit()
- sys.exit()
- # Handle additional events
- if event.type == MOUSEBUTTONUP and not gameOver:
- tile.handleMouseUp(event.pos)
- # Update and draw objects for the next frame
- gameOver = update(tile)
- # Refresh the display
- pygame.display.update()
- # Set the frame speed by pausing between frames
- #time.sleep(frameDelay)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement