Advertisement
Guest User

Untitled

a guest
May 28th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.77 KB | None | 0 0
  1. # MYCODE
  2. # Summary of game.
  3.  
  4. import pygame, sys, time
  5. from pygame.locals import *
  6.  
  7. # User-defined classes
  8. class Tile:
  9.    #Class Attributes
  10.    fgColor=pygame.Color('white')
  11.    bgColor=pygame.Color('black')
  12.    borderWidth=3
  13.    flashTime=0.05
  14.    
  15.    def __init__(self,x,y,surface):
  16.       self.surface=surface
  17.       width=self.surface.get_width()//3
  18.       height=self.surface.get_height()//3
  19.       self.rect=Rect(x,y,width,height)
  20.       self.content=''
  21.    
  22.    def draw(self):
  23.       pygame.draw.rect(self.surface,Tile.bgColor,self.rect)
  24.       pygame.draw.rect(self.surface,Tile.fgColor,self.rect,Tile.borderWidth)
  25.       font=pygame.font.SysFont(None,100,True)
  26.       textSurface=font.render(self.content,True,Tile.fgColor,Tile.bgColor)
  27.       textRect=textSurface.get_rect()
  28.       textRect.center=self.rect.center
  29.       surface.blit(textSurface,textRect)
  30.      
  31.    def handleMouseUp(self,position):
  32.       if self.rect.collidepoint(position):
  33.          if self.content == '':
  34.             self.content='X'
  35.          else:
  36.             self.flash()
  37.            
  38.    def flash(self):
  39.       pygame.draw.rect(self.surface,Tile.fgColor,self.rect)
  40.       pygame.display.update()
  41.       time.sleep(Tile.flashTime)
  42.       self.draw()
  43.      
  44.      
  45. # User-defined functions
  46.  
  47. def update(tile):
  48.    # Check if the game is over. If so, end the game and
  49.    # returnTrue. Otherwise, update the game objects, draw
  50.    # them, and return False.
  51.    # This is an example update function - replace it.
  52.    # - center is a list containing the x and y int coords
  53.    # of the center of a circle
  54.    # - surface is the pygame.Surface object for the window
  55.    if False: # check if the game is over
  56.       return True
  57.    else: # continue the game
  58.       tile.draw()
  59.       return True
  60.  
  61. # Main program    
  62.  
  63. # Initialize pygame
  64. pygame.init()
  65.  
  66. # Set window size and title, and frame delay
  67. surfaceSize = (500, 400) # example window size
  68. windowTitle = 'Tic Tac Toe' # example title
  69. #frameDelay = 0.02 # smaller is faster game
  70.  
  71. # Create the window
  72. surface = pygame.display.set_mode(surfaceSize, 0, 0)
  73. pygame.display.set_caption(windowTitle)
  74.  
  75. # create and initialize objects
  76. gameOver = False
  77. tile=Tile(0,0,surface)
  78.  
  79.  
  80. # Draw objects
  81. tile.draw()
  82.  
  83. # Refresh the display
  84. pygame.display.update()
  85.  
  86. # Loop forever
  87. while True:
  88.    # Handle events
  89.    for event in pygame.event.get():
  90.       if event.type == QUIT:
  91.          pygame.quit()
  92.          sys.exit()
  93.       # Handle additional events
  94.       if event.type == MOUSEBUTTONUP and not gameOver:
  95.          tile.handleMouseUp(event.pos)
  96.  
  97.    # Update and draw objects for the next frame
  98.    gameOver = update(tile)
  99.  
  100.    # Refresh the display
  101.    pygame.display.update()
  102.  
  103.    # Set the frame speed by pausing between frames
  104.    #time.sleep(frameDelay)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement