Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.05 KB | None | 0 0
  1. import pygame
  2.  
  3. BLACK = (0, 0, 0)
  4. WHITE = (255, 255, 255)
  5. BLUE = (0, 0, 255)
  6. RED = (255, 0, 0)
  7.  
  8. WIDTH = 30
  9. HEIGHT = 30
  10. MARGIN = 10
  11. grid = []
  12. for row in range(10):
  13.     grid.append([])
  14.     for column in range(10):
  15.         grid[row].append(0)
  16. grid[1][5] = 1
  17. pygame.init()
  18.  
  19. # Set the HEIGHT and WIDTH of the screen
  20. WINDOW_SIZE = [800, 480]
  21. screen = pygame.display.set_mode(WINDOW_SIZE)
  22.  
  23. # Set title of screen
  24. pygame.display.set_caption("5 op een rij")
  25.  
  26. # Loop until the user clicks the close button.
  27. done = False
  28.  
  29. # Used to manage how fast the screen updates
  30. clock = pygame.time.Clock()
  31.  
  32. # -------- Main Program Loop -----------
  33. while not done:
  34.     for event in pygame.event.get():  # User did something
  35.         if event.type == pygame.QUIT:  # If user clicked close
  36.             done = True  # Flag that we are done so we exit this loop
  37.         elif event.type == pygame.MOUSEBUTTONDOWN:
  38.             # User clicks the mouse. Get the position
  39.             pos = pygame.mouse.get_pos()
  40.             # Change the x/y screen coordinates to grid coordinates
  41.             column = pos[0] // (WIDTH + MARGIN)
  42.             row = pos[1] // (HEIGHT + MARGIN)
  43.             # Set that location to one
  44.             grid[row][column] = 1
  45.             print("Click ", pos, "Grid coordinates: ", row, column)
  46.  
  47.     # Set the screen background
  48.     screen.fill(BLACK)
  49.  
  50.     # Draw the grid
  51.     for row in range(10):
  52.         for column in range(10):
  53.             color = WHITE
  54.             if grid[row][column] == 1:
  55.                 color = BLUE
  56.             pygame.draw.rect(screen,
  57.                              color,
  58.                              [(MARGIN + WIDTH) * column + MARGIN,
  59.                               (MARGIN + HEIGHT) * row + MARGIN,
  60.                               WIDTH,
  61.                               HEIGHT])
  62.  
  63.     # Limit to 60 frames per second
  64.     clock.tick(60)
  65.  
  66.     # Go ahead and update the screen with what we've drawn.
  67.     pygame.display.flip()
  68.  
  69. # Be IDLE friendly. If you forget this line, the program will 'hang'
  70. # on exit.
  71. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement