Advertisement
Guest User

Untitled

a guest
Sep 4th, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. """
  2. Example program to show using an array to back a grid on-screen.
  3.  
  4. Sample Python/Pygame Programs
  5. Simpson College Computer Science
  6. http://programarcadegames.com/
  7. http://simpson.edu/computer-science/
  8.  
  9. Explanation video: http://youtu.be/mdTeqiWyFnc
  10. """
  11. import pygame
  12.  
  13. # Define some colors
  14. BLACK = (0, 0, 0)
  15. WHITE = (255, 255, 255)
  16. GREEN = (0, 255, 0)
  17. RED = (255, 0, 0)
  18.  
  19. # This sets the WIDTH and HEIGHT of each grid location
  20. WIDTH = 75
  21. HEIGHT = 75
  22.  
  23. # This sets the margin between each cell
  24. MARGIN = 5
  25.  
  26. # Create a 2 dimensional array. A two dimensional
  27. # array is simply a list of lists.
  28. grid = []
  29. for row in range(3):
  30. # Add an empty array that will hold each cell
  31. # in this row
  32. grid.append([])
  33. for column in range(3):
  34. grid[row].append(0) # Append a cell
  35. # Initialize pygame
  36. pygame.init()
  37.  
  38. # Set the HEIGHT and WIDTH of the screen
  39. WINDOW_SIZE = [245, 245]
  40. screen = pygame.display.set_mode(WINDOW_SIZE)
  41.  
  42. # Set title of screen
  43. pygame.display.set_caption("Array Backed Grid")
  44.  
  45. # Loop until the user clicks the close button.
  46. done = False
  47.  
  48. # Player counter
  49. player_counter = 0
  50.  
  51. # Used to manage how fast the screen updates
  52. clock = pygame.time.Clock()
  53.  
  54. # -------- Main Program Loop -----------
  55. while not done:
  56. for event in pygame.event.get(): # User did something
  57. if event.type == pygame.QUIT: # If user clicked close
  58. done = True # Flag that we are done so we exit this loop
  59. elif event.type == pygame.MOUSEBUTTONDOWN:
  60. # User clicks the mouse. Get the position
  61. pos = pygame.mouse.get_pos()
  62. # Change the x/y screen coordinates to grid coordinates
  63. column = pos[0] // (WIDTH + MARGIN)
  64. row = pos[1] // (HEIGHT + MARGIN)
  65. # Set that location to zero
  66. if grid[row][column] == 0:
  67. if player_counter % 2 == 0:
  68. grid[row][column] = 1
  69. else:
  70. grid[row][column] = 2
  71. player_counter += 1
  72. print("Click ", pos, "Grid coordinates: ", row, column)
  73.  
  74. # Set the screen background
  75. screen.fill(BLACK)
  76.  
  77. # Draw the grid
  78. for row in range(3):
  79. for column in range(3):
  80. color = WHITE
  81. if grid[row][column] == 1:
  82. color = GREEN
  83. elif grid[row][column] == 2:
  84. color = RED
  85. pygame.draw.rect(screen,
  86. color,
  87. [(MARGIN + WIDTH) * column + MARGIN,
  88. (MARGIN + HEIGHT) * row + MARGIN,
  89. WIDTH,
  90. HEIGHT])
  91.  
  92. # Limit to 60 frames per second
  93. clock.tick(60)
  94.  
  95. # Go ahead and update the screen with what we've drawn.
  96. pygame.display.flip()
  97.  
  98. # Be IDLE friendly. If you forget this line, the program will 'hang'
  99. # on exit.
  100. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement